"""Test handler.""" from unittest.mock import patch from ddex_ingester_common.schemas.s3_schema import S3Schema from ddex_ingester_common.schemas.state_machine_schema import \ StateMachineSchema import index from constants import queries @patch('index.graphql_gateway.execute') @patch('index.load_ddex_json') def test_handler( mock_load_ddex_json, mock_graphql_execute, context, s3_ddex): """Test the main handler.""" mock_load_ddex_json.return_value = s3_ddex index.handler(context, None) assert mock_graphql_execute.call_count == 2 @patch('index.graphql_gateway.execute') @patch('index.load_ddex_json') def test_handler_unsupported_video_workflow( mock_load_ddex_json, mock_graphql_execute, context, s3_ddex): """Test the main handler.""" mock_load_ddex_json.return_value = s3_ddex context.get('product')['status'] = 'in_content' context.get('product')['release_type'] = 'VideoSingle' index.handler(context, None) assert mock_graphql_execute.call_count == 0 @patch('index.graphql_gateway.execute') def test_update_music_album_product_pricing_tier( mock_graphql_execute, context, s3_ddex): """Test that update pricing tier calls graphql with correct payload.""" mock_graphql_execute.reset_mock() context = StateMachineSchema().load(context) s3_ddex_data = S3Schema().load(s3_ddex) expected_payload = { 'data': { 'productId': context.product.product_id, 'orchardPricingTier': 11, 'pricingFamily': 'MUSIC_ALBUM', } } index.update_music_album_product_pricing_tier(context, s3_ddex_data) mock_graphql_execute.assert_called_with( queries.UPDATE_PRODUCT_PRICING_TIER, expected_payload) @patch('index.graphql_gateway.execute') def test_update_music_track_pricing_tier( mock_graphql_execute, context, s3_ddex): """Test that update track pricing calls graphql with correct payload.""" mock_graphql_execute.reset_mock() context = StateMachineSchema().load(context) s3_ddex_data = S3Schema().load(s3_ddex) expected_payload = { 'data': { 'productId': context.product.product_id, 'orchardPricingTier': 13, 'pricingFamily': 'MUSIC_TRACK', 'trackOverrides': [{ 'trackId': 1234, 'orchardPricingTier': 12, }], } } index.update_music_track_pricing_tier(context, s3_ddex_data) mock_graphql_execute.assert_called_with( queries.UPDATE_PRODUCT_PRICING_TIER, expected_payload) def test_get_track_price_tier(s3_ddex): """Test that get_track_price_tier works correctly.""" s3_ddex_data = S3Schema().load(s3_ddex) track = s3_ddex_data.tracks[0] deals = s3_ddex_data.deals result = index.get_track_price_tier(track, deals) assert result == '12' def test_get_track_price_tier_2(s3_ddex): """Test that get_track_price_tier works correctly.""" s3_ddex_data = S3Schema().load(s3_ddex) track = s3_ddex_data.tracks[0] track.release_reference = 'R2' deals = s3_ddex_data.deals result = index.get_track_price_tier(track, deals) assert result == '13' def test_get_track_price_tier_null(s3_ddex): """Test that get_track_price_tier works with null values.""" s3_ddex_data = S3Schema().load(s3_ddex) s3_ddex_data.deals[1].deal_terms[0].price_type = None s3_ddex_data.deals[2].deal_terms[0].price_type = None track = s3_ddex_data.tracks[0] deals = s3_ddex_data.deals result = index.get_track_price_tier(track, deals) assert result is None def test_get_most_common_track_price_tier(s3_ddex): """Test that get_most_common_track_price_tier works correctly.""" s3_ddex_data = S3Schema().load(s3_ddex) result = index.get_most_common_track_price_tier(s3_ddex_data) assert result == '13' def test_get_most_common_track_price_tier_2(s3_ddex): """Test that get_most_common_track_price_tier works correctly.""" s3_ddex_data = S3Schema().load(s3_ddex) s3_ddex_data.deals[1].release_references = ['R1', 'R2'] s3_ddex_data.deals[2].release_references = ['R3'] result = index.get_most_common_track_price_tier(s3_ddex_data) assert result == '12' def test_get_most_common_track_price_tier_null(s3_ddex): """Test that get_most_common_track_price_tier works with null values.""" s3_ddex_data = S3Schema().load(s3_ddex) s3_ddex_data.deals[1].deal_terms[0].price_type = None s3_ddex_data.deals[2].deal_terms[0].price_type = None result = index.get_most_common_track_price_tier(s3_ddex_data) assert result is None def test_get_pricing_tier_track_overrides(context, s3_ddex): """Test that get_pricing_tier_track_overrides works correctly.""" context = StateMachineSchema().load(context) s3_ddex_data = S3Schema().load(s3_ddex) expected_result = [{'orchardPricingTier': 12, 'trackId': 1234}] result = index.get_pricing_tier_track_overrides( context, s3_ddex_data, '13' ) assert result == expected_result def test_get_pricing_tier_track_overrides_2(context, s3_ddex): """Test that get_pricing_tier_track_overrides works correctly.""" context = StateMachineSchema().load(context) s3_ddex_data = S3Schema().load(s3_ddex) expected_result = [ {'orchardPricingTier': 13, 'trackId': 12345}, {'orchardPricingTier': 13, 'trackId': 123456}, ] result = index.get_pricing_tier_track_overrides( context, s3_ddex_data, '12' ) assert result == expected_result def test_get_pricing_tier_track_overrides_null(context, s3_ddex): """Test that get_pricing_tier_track_overrides works with null values.""" context = StateMachineSchema().load(context) s3_ddex_data = S3Schema().load(s3_ddex) s3_ddex_data.deals[1].deal_terms[0].price_type = None s3_ddex_data.deals[2].deal_terms[0].price_type = None result = index.get_pricing_tier_track_overrides( context, s3_ddex_data, None ) assert result == []