"""Test handler.""" from unittest.mock import patch import xmltodict import index def php_to_dict(php): """Convert PHP object to dict.""" return xmltodict.parse(php) def test_handler_returns_400_when_upc_missing(): """Test handler returns 400 when pathParameters has no upc.""" response = index.handler({}, {}) assert response['statusCode'] == 400 assert response['body'] == 'UPC is required' def test_handler_returns_400_when_upc_empty(): """Test handler returns 400 when upc is an empty string.""" response = index.handler({'pathParameters': {'upc': ''}}, {}) assert response['statusCode'] == 400 assert response['body'] == 'UPC is required' @patch('index.graphql_gateway.execute') def test_handler_returns_api_gateway_response( mock_graphql_response, product_by_upc_video, marketing_info, vendor_sony_metadata_response, ): """Test handler returns a valid API Gateway response envelope.""" mock_graphql_response.side_effect = [ product_by_upc_video, marketing_info, vendor_sony_metadata_response, ] response = index.handler({'pathParameters': {'upc': '193483706238'}}, {}) assert response['statusCode'] == 200 assert response['headers']['Content-Type'] == 'application/xml; charset=utf-8' assert response['body'].startswith('This is rem testing marketing blurb
' expected_territory = 'Worldwide' expected_highlight = ( 'Global sync highlights \nTest how this is entered \n- ----\n-----\n-----' # noqa ) expected_store_highlight = 'APPLE MUSIC HIGHLIGHTS' expected_store = 'apple' expected_delivery_name = 'Spotify' expected_delivery_date = '2019-12-23 00:00:00' expected_mul_delivery_date = '2019-09-13 00:00:00' expected_video_associated_performer = 'MainArtist' expected_video_associated_producer = 'Producer' assert blurb == expected_blurb assert w_territory == expected_territory assert w_highlight == expected_highlight assert s_highlight == expected_store_highlight assert s_store == expected_store assert first_delivery_date == expected_delivery_date assert first_delivery_name == expected_delivery_name assert mul_delivery_date == expected_mul_delivery_date assert video_associated_performer == expected_video_associated_performer assert video_associated_producer == expected_video_associated_producer @patch('index.graphql_gateway.execute') def test_handler_returns_xml_audio_product( mock_graphql_response, product_by_upc_audio, marketing_info, vendor_no_sony_metadata_response, ): """Test main handler returns specific values in XML.""" mock_graphql_response.side_effect = [ product_by_upc_audio, marketing_info, vendor_no_sony_metadata_response, ] response = index.handler({'pathParameters': {'upc': '193483706238'}}, {}) ptd = php_to_dict(response['body'])['OrchardMessage'] blurb = ptd['Product']['MarketingBlurb'] first_delivery_name = ptd['Product']['Stores']['Store'][0]['Id'] # noqa first_delivery_date = ptd['Product']['Stores']['Store'][0]['DeliveryDate'] # noqa mul_delivery_date = ptd['Product']['Stores']['Store'][2]['DeliveryDate'] # noqa expected_blurb = 'This is rem testing marketing blurb
' expected_delivery_name = 'Spotify' expected_delivery_date = '2021-11-12 00:00:00' expected_mul_delivery_date = '2021-11-12 00:00:00' assert blurb == expected_blurb assert first_delivery_date == expected_delivery_date assert first_delivery_name == expected_delivery_name assert mul_delivery_date == expected_mul_delivery_date # Verify no Sony metadata elements when metadata is None assert 'RepertoireOwner' not in ptd['Product'] assert 'FinancialLabelOfRecord' not in ptd['Product'] assert 'ImprintLabel' not in ptd['Product'] assert 'SAPProfitCenter' not in ptd['Product'] @patch('index.graphql_gateway.execute') def test_handler_includes_sony_metadata_from_vendor( mock_graphql_response, product_by_upc_video, marketing_info, vendor_sony_metadata_response, ): """Test Sony financial metadata is included when fetched from vendor.""" mock_graphql_response.side_effect = [ product_by_upc_video, marketing_info, vendor_sony_metadata_response, ] response = index.handler({'pathParameters': {'upc': '193483706238'}}, {}) ptd = php_to_dict(response['body'])['OrchardMessage'] assert ptd['Product']['RepertoireOwner']['Code'] == 'ROC001' assert ptd['Product']['RepertoireOwner']['Name'] == 'Repertoire Owner XYZ' assert ptd['Product']['FinancialLabelOfRecord']['Code'] == 'FLC001' assert ptd['Product']['FinancialLabelOfRecord']['Name'] == 'Financial Label One' assert ptd['Product']['ImprintLabel']['Code'] == 'GIC001' assert ptd['Product']['ImprintLabel']['Name'] == 'Columbia' assert ptd['Product']['SAPProfitCenter']['Code'] == 'SAP001' assert ptd['Product']['SAPProfitCenter']['Name'] == 'SAP Profit Center One' @patch('index.graphql_gateway.execute') def test_handler_uses_subaccount_when_subaccount_id_present( mock_graphql_response, product_by_upc_with_subaccount, marketing_info, subaccount_sony_metadata_response, ): """Test subaccount query is used when subaccountId > 0.""" mock_graphql_response.side_effect = [ product_by_upc_with_subaccount, marketing_info, subaccount_sony_metadata_response, ] response = index.handler({'pathParameters': {'upc': '193483706238'}}, {}) ptd = php_to_dict(response['body'])['OrchardMessage'] # Verify subaccount metadata is used assert ptd['Product']['RepertoireOwner']['Code'] == 'ROC002' assert ptd['Product']['RepertoireOwner']['Name'] == 'Repertoire Owner ABC' assert ptd['Product']['FinancialLabelOfRecord']['Code'] == 'FLC002' assert ptd['Product']['ImprintLabel']['Name'] == 'Epic' @patch('index.graphql_gateway.execute') def test_handler_no_sony_metadata_elements_when_missing( mock_graphql_response, product_by_upc_audio, marketing_info, vendor_no_sony_metadata_response, ): """Test no Sony elements are added when metadata is missing.""" mock_graphql_response.side_effect = [ product_by_upc_audio, marketing_info, vendor_no_sony_metadata_response, ] response = index.handler({'pathParameters': {'upc': '193483706238'}}, {}) ptd = php_to_dict(response['body'])['OrchardMessage'] # These keys should not exist when metadata is None assert 'RepertoireOwner' not in ptd['Product'] assert 'FinancialLabelOfRecord' not in ptd['Product'] assert 'ImprintLabel' not in ptd['Product'] assert 'SAPProfitCenter' not in ptd['Product'] @patch('index.graphql_gateway.execute') def test_excludes_project_when_false( mock_graphql_response, product_by_upc_video, marketing_info, vendor_no_sony_metadata_response, ): """When `should_include_project` is False, no Project node should appear.""" mock_graphql_response.side_effect = [ product_by_upc_video, marketing_info, vendor_no_sony_metadata_response, ] with patch('index.should_include_project', return_value=False): response = index.handler({'pathParameters': {'upc': '193483706238'}}, {}) ptd = php_to_dict(response['body'])['OrchardMessage'] assert 'Project' not in ptd @patch('index.graphql_gateway.execute') def test_includes_project_when_true( mock_graphql_response, product_by_upc_video, marketing_info, vendor_sony_metadata_response, ): """When `should_include_project` is True, Project node should appear.""" mock_graphql_response.side_effect = [ product_by_upc_video, marketing_info, vendor_sony_metadata_response, ] with patch('index.should_include_project', return_value=True): response = index.handler({'pathParameters': {'upc': '193483706238'}}, {}) ptd = php_to_dict(response['body'])['OrchardMessage'] assert 'Project' in ptd