from datetime import datetime from flexmock import flexmock from freezegun import freeze_time from masters_registry.connectors import mysql from masters_registry.constant import bulk_tasks_const from masters_registry.models import bulk_tasks from tests.helpers import patches def test_get_task_statuses_count( setup_bulk_status_db, feature_engine): """Test getting number of bulk processing tasks.""" bulk_tasks.create_task('test id', 123, 'BULK_LOCK', 1, ['QA123']) bulk_tasks.create_task('test id', 123, 'BULK_LOCK', 1, ['QA456']) result = bulk_tasks.get_task_statuses_count() assert result == 2 def test_get_task_statuses_count_ignore_bulk_remove_territories( setup_bulk_status_db): """Test getting the correct number of bulk processing tasks.""" bulk_tasks.create_task('test id', 123, 'BULK_LOCK', 1, ['QA123']) bulk_tasks.create_task('test id', 123, 'BULK_LOCK', 1, ['QA456']) bulk_tasks.create_task( 'test id', 123, 'BULK_REMOVE_TERRITORIES', 1, ['QA456']) result = bulk_tasks.get_task_statuses_count() assert result == 2 def test_get_task_report(feature_engine): """Test getting list of bulk processing tasks.""" num_records = 4 page_offset = 3 tasks = [] for correlation_id in range(1, 10): task = bulk_tasks.TaskStatus( correlation_id=correlation_id, user_id=1, type=bulk_tasks_const.BULK_TYPES[0], create_datetime=datetime.now(), finish_datetime=datetime.now() if correlation_id // 2 else None, count=2 ) tasks.append((task, task.finish_datetime is None)) mocks = [tasks[:num_records]] for function_name, args in [ ('all', []), ('limit', (num_records, )), ('offset', (page_offset,)), ('order_by', (object, object)), ('filter', (object,)), ('query', (bulk_tasks.TaskStatus, object)) ]: mock = flexmock() mock.should_receive(function_name).with_args(*args)\ .and_return(mocks[-1]).once() mocks.append(mock) (flexmock(mysql) .should_receive('bulk_statuses_session_scope') .and_return(mocks[-1])) result = bulk_tasks.get_task_report( num_records, 'finish_datetime', 'desc', page_offset) assert result == [t.as_dict( include_result=False) for t, _ in tasks[:num_records]] def test_get_task_report_auto_update_claims( setup_bulk_status_db, seed_bulk_status_db): """Test getting list of bulk processing tasks.""" test_account_type = 'vendor' test_account_id = 333 test_task = bulk_tasks.TaskStatus( correlation_id='correlation_id', user_id=1, type=bulk_tasks_const.BULK_IMPORT, create_datetime=datetime.now(), count=2) test_task_auto = bulk_tasks.TaskStatus( correlation_id='correlation_id', user_id=1, type=bulk_tasks_const.BULK_REMOVE_TERRITORIES, create_datetime=datetime.now(), count=2, account_type=test_account_type, account_id=test_account_id ) seed_bulk_status_db([test_task, test_task_auto]) report = bulk_tasks.get_task_report(2, 'finish_datetime', 'desc') assert len(report) == 1 @freeze_time(patches.TEST_TIME_AS_STR) def test_get_task(setup_bulk_status_db, seed_bulk_status_db, feature_engine): """Test getting single task.""" # TODO replace with factory test_task = bulk_tasks.TaskStatus( correlation_id='correlation_id', user_id=1, type=bulk_tasks_const.BULK_IMPORT, create_datetime=datetime.now(), count=2) seed_bulk_status_db([test_task]) result = bulk_tasks.get_task(1) expected_task_dict = { 'id': 1, 'correlation_id': 'correlation_id', 'user_id': '1', 'type': bulk_tasks_const.BULK_IMPORT, 'create_datetime': patches.TEST_TIME_AS_STR, 'count': 2, 'user_name': '', 'finish_datetime': None, 'result': None, 'status': bulk_tasks_const.PROCESSING_STATUS, 'account_type': None, 'account_id': None } assert result.status == 200 assert result.message.as_dict() == expected_task_dict def test_task_not_found(setup_bulk_status_db): """Test for 404 error of get_task function.""" result = bulk_tasks.get_task(777) assert result.status == 404 def test_create_task(feature_engine): """Test inserting single task into table """ correlation_id = '1' user_id = 1 task_type = bulk_tasks_const.BULK_TYPES[1] count = 1 context = ['ISRC1', 'ISRC2'] session_scope = flexmock(add=lambda item: None, flush=lambda: None) (flexmock(mysql) .should_receive('bulk_statuses_session_scope') .and_return(session_scope) ) session_scope.should_call('add').once() bulk_tasks.create_task(correlation_id, user_id, task_type, count, context) def test_create_task_auto_update_claims_enabled( setup_bulk_status_db): """Test inserting single task into table with ff enabled.""" correlation_id = '1' user_id = 1 task_type = bulk_tasks_const.BULK_TYPES[1] count = 1 test_account_type = 'vendor' test_account_id = 333 context = [] id = bulk_tasks.create_task( correlation_id=correlation_id, user_id=user_id, task_type=task_type, count=count, context=context, user_name='', account_id=test_account_id, account_type=test_account_type) task = bulk_tasks.get_task(id) assert task assert task.message.account_type == test_account_type assert task.message.account_id == test_account_id def test_update_task(): """Test update task """ task = flexmock(bulk_tasks.TaskStatus( correlation_id='1', user_id=1, type=bulk_tasks_const.BULK_TYPES[1], create_datetime=datetime.utcnow(), count=2 )) task.update = flexmock() task_id = 1 status = bulk_tasks_const.BULK_STATUSES[1] result = {'TERRITORIES': ['US', 'CA']} query = flexmock(filter_by=lambda id: task) session_scope = flexmock(query=lambda data: query) (flexmock(mysql) .should_receive('bulk_statuses_session_scope') .and_return(session_scope) ) query.should_call('filter_by').once().with_args(id=task_id) bulk_tasks.update_task(task_id, status, result)