"""Unit tests for release correction detail logic.""" from flexmock import flexmock from oto import response import pytest from product_workflow.logic import release_correction_detail from product_workflow.models \ import release_correction_detail as release_correction_detail_model from tests.factories import release_correction as release_correction_factory @pytest.fixture def release_correction_id(): """Create a release correction id for testing.""" return 123 @pytest.fixture def correction_details(release_correction_id): """Fixture data for a fake response from the model layer.""" correction_details = release_correction_factory \ .ReleaseCorrectionFactory \ .build_batch(3, release_correction_id=release_correction_id) return [detail.to_dict() for detail in correction_details] @pytest.fixture def test_model_response(correction_details): """Fake response for mocking the model layer.""" payload = {'items': correction_details} return response.Response(message=payload) @pytest.fixture def correction_details_properties(release_correction_id): """Return a list of the correction details properties for testing.""" return [ { 'table_name': 'randy', 'field_name': 'butternubs', 'key_id': 123, 'key_value': 'track_1.wav', 'last_updated_by': 1234, 'last_updated_type': 'vendor' }, { 'table_name': 'lumpy', 'field_name': 'space', 'key_id': 124, 'key_value': 'track_2.wav', 'last_updated_by': 1234, 'last_updated_type': 'vendor' }, { 'table_name': 'jake', 'field_name': 'the_doge', 'key_id': 125, 'key_value': 808, 'last_updated_by': 1234, 'last_updated_type': 'vendor' }, ] @pytest.fixture def release_correction_details_create_message(): """Return the correction details create message properties for testing.""" return [ { 'release_correction_id': release_correction_id, 'table_name': 'randy', 'field_name': 'butternubs', 'key_id': 123, 'key_value': 'track_1.wav', }, { 'release_correction_id': release_correction_id, 'table_name': 'lumpy', 'field_name': 'space', 'key_id': 124, 'key_value': 'track_2.wav', }, { 'release_correction_id': release_correction_id, 'table_name': 'jake', 'field_name': 'the_doge', 'key_id': 125, 'key_value': 808, }, ] @pytest.fixture def release_correction_details_payload(): """Return the correction details payload for testing.""" return [ { 'table_name': 'randy', 'field_name': 'butternubs', 'key_id': 123, 'key_value': 'track_1.wav', 'last_updated_by': 1234, 'last_updated_type': 'vendor' }, { 'table_name': 'lumpy', 'field_name': 'space', 'key_id': 124, 'key_value': 'track_2.wav', 'last_updated_by': 1234, 'last_updated_type': 'vendor' }, { 'table_name': 'jake', 'field_name': 'the_doge', 'key_id': 125, 'key_value': 808, 'last_updated_by': 1234, 'last_updated_type': 'vendor' }, ] @pytest.fixture def release_correction_details_create_response( release_correction_details_create_message): """Return a release correction details create message for testing.""" return response.Response( release_correction_details_create_message, status=201) @pytest.fixture def db_error_response(): """Return a generic 400 level error response.""" return response.create_error_response( status=500, code='internal_error', message='something went wrong') def test_create_release_correction_details_success( correction_details_properties, release_correction_details_payload, release_correction_id, release_correction_details_create_response, release_correction_details_create_message): """Test that create_many function is called and returns a 201 response.""" flexmock(release_correction_detail_model) \ .should_receive('upsert_many') \ .with_args(release_correction_id, correction_details_properties) \ .and_return(release_correction_details_create_response) \ .once() result = release_correction_detail \ .upsert_correction_details_records( release_correction_id, release_correction_details_payload) assert result.status == 201 assert result.message == release_correction_details_create_message def test_create_release_correction_details_failure( correction_details_properties, generic_error_response, release_correction_id, release_correction_details_payload): """Test that model layer create function is called and 400 response.""" flexmock(release_correction_detail_model) \ .should_receive('upsert_many') \ .with_args(release_correction_id, correction_details_properties) \ .and_return(generic_error_response) \ .once() result = release_correction_detail \ .upsert_correction_details_records( release_correction_id, release_correction_details_payload) assert result.status == 400 assert result.errors == generic_error_response.errors def test_create_release_correction_details_db_failure( release_correction_id, correction_details_properties, release_correction_details_payload, db_error_response): """Test create or update details function is called and 500 response.""" flexmock(release_correction_detail_model) \ .should_receive('upsert_many') \ .with_args(release_correction_id, correction_details_properties) \ .and_return(db_error_response) \ .once() result = release_correction_detail \ .upsert_correction_details_records( release_correction_id, release_correction_details_payload) assert result.status == 500