"""Config for tests.""" import application from flexmock import flexmock from oto import response import pytest from label_copy_export.connectors import sqs from tests import dbutils TEST_CORRELATION_ID = 'c305d169-455b-4bdd-ab69-0c49c49ae4fc' sqs_attributes_correlation_id = { sqs.CORRELATION_ID_ATTRIBUTE: { 'string_value': TEST_CORRELATION_ID } } @pytest.fixture def model_function(): """Fixture for overriding the model level function. Usage: @pytest.mark.parametrize( 'model_function', [MOCK_MODEL_FUNCTION]) def test_my_fucntion(model_function): pass """ return 'override with @pytest.mark.parametrize' @pytest.fixture def model_module(): """Fixture for overriding the model level module. Usage: @pytest.mark.parametrize( 'model_module', [IMPORTED_MODULE_NAME)]) def test_my_fucntion(model_module): pass """ return 'override with @pytest.mark.parametrize' @pytest.fixture def valid_response(model_function, model_module): """Mock model_module.model_function and return oto Response. Returns: response.Response: fake valid response. """ valid_json = {'project_name': 'NewOne'} oto_resp = response.Response(message=valid_json) (flexmock(model_module) .should_receive(model_function) .and_return(oto_resp)) return oto_resp @pytest.fixture def invalid_response(model_function, model_module): """Mock model_module.model_function and return oto Response. Returns: response.Response: fake invalid response. """ valid_json = {'project_name': 'NewOne'} oto_resp = response.Response(errors=valid_json, status=400) (flexmock(model_module) .should_receive(model_function) .and_return(oto_resp)) return oto_resp @pytest.fixture def valid_response_with_message(model_function, model_module, valid_json): """Mock model_module.model_function and return oto Response. Returns: response.Response: fake valid response. """ oto_resp = response.Response(message=valid_json) (flexmock(model_module) .should_receive(model_function) .and_return(oto_resp)) return oto_resp @pytest.fixture def invalid_response_with_params(model_function, model_module, errors, status): """Mock model_module.model_function and return oto Response. Returns: response.Response: fake invalid response. """ oto_resp = response.Response(errors=errors, status=status) (flexmock(model_module) .should_receive(model_function) .and_return(oto_resp)) return oto_resp @pytest.fixture def product_physical_from_ows(): """Fixture for product physical data retrieved from ows.""" test_product_physical_response = { 'release_status': 'label_processing', 'project_name': 'Test Project', 'project_code': '123456', 'artist_id': 33344, 'description': 'test description', 'project_highlights': 'test highlights', 'product_name': 'Test Product', 'product_code': 'ABCD1234', 'upc': '2222888899', 'product_artist_name': 'Best Artist', 'artist_is_individual': 'N', 'label': 'The Hit Factory', 'genre_id': 4, 'subgenre_id': 3, 'version': 'Mexican Version', 'explicit': 'Y', 'product_type': 'product_type', 'sale_start_date': '2016-12-12', 'release_date': '2016-12-12', 'embargo_date': '2016-12-12', 'distribution_format_name': 'Cassette', 'format': 'Single', 'units_per_set': 5, 'value_adds': '2 x CD', 'packaging_id': 3, 'initial_stock': 500, 'box_lot': '30', 'wholesale_price': 9.99, 'pricing': 'Priced to move', 'discount': 'discount', 'pline': '1986 Record Label', 'cline': '1986 Record Label', 'exclusive_for': 'Somebody', 'exportable': 'Y', 'manufacturing_obligation': 'Test obligation', 'production_notes': 'Test notes', 'product_highlights': 'Test highlights', 'disc': 1, 'track_number': 2, 'track_name': 'Test Name', 'performer': 'Test Artist', 'isrc': 1234567, 'primary_artist': 'Test Artist', } return test_product_physical_response @pytest.fixture def project_products_from_ows(): """Fixture for project pproducts data retrieved from ows.""" products_for_project_test_response_message = { 'items': [{ 'artist_names': ['Mike Jones'], 'product_type': 'Music', 'product_type_id': 1, 'product_id': 1001, 'release_name': 'Something Strange Here', 'release_status': 'error_correction', 'display_upc': '888831283041', 'identifier_upc': 888831283041, 'version': 'Deluxe', 'distribution_format': { 'id': 1, 'name': 'DIGITAL', 'context': 'digital' }, 'format': 'EP' }, { 'artist_names': ['Mike Jones'], 'product_type': 'Music', 'product_type_id': 1, 'product_id': 1002, 'release_name': 'Something Strange Here', 'release_status': 'action_required', 'display_upc': '889845667810', 'identifier_upc': 889845667810, 'version': 'Unremarkable', 'distribution_format': { 'id': 2, 'name': 'CD', 'context': 'physical' }, 'format': 'Single' }] } return products_for_project_test_response_message @pytest.fixture(scope='function') def valid_csv_generation_triggering_message(): """Emulate valid sqs message for triggering csv generation.""" return { 'project_id': 11223344, 'job_id': 333 } @pytest.fixture(scope='function') def invalid_csv_generation_triggering_message(): """Emulate invalid sqs message for triggering csv generation.""" return { 'project_id': '777' } @pytest.fixture(scope='function') def no_job_id_csv_generation_triggering_message(): """Emulate invalid sqs csv generation message without job_id.""" return { 'project_id': 777 } @pytest.fixture(scope='function') def no_project_id_csv_generation_triggering_message(): """Emulate invalid sqs csv generation message without project_id.""" return { 'product_id': '777' } @pytest.fixture(scope='function') def create_sqs_message(): """Automate mock message creation.""" def create_message(msg): json_message = sqs.JSONMessageExt() (flexmock(json_message) .should_receive('message_attributes') .and_return(sqs_attributes_correlation_id)) (flexmock(json_message) .should_receive('get_body') .and_return(msg)) return json_message return create_message @pytest.yield_fixture def test_database(): """Initialize in-memory database for testing purposes.""" dbutils.db_setup_function() yield dbutils.db_teardown_function() @pytest.fixture def context(): """Return flask app context. Returns: AppContext: flask app context. """ context = application.app.app_context() return context