"""Test for the Product logic.""" from unittest.mock import patch from owsresponse import response from product_store_mapping.logic import product from product_store_mapping.models import productstoremapping TEST_MAPPING_DATA = [ productstoremapping.ProductStoreMapping( 123456, 1, 1234654, None, '{"provider": "orchard", "custom_id": "807978099098790"}', ), productstoremapping.ProductStoreMapping( 123456, 1, 12346545234, 1, '{"custom_id": "34523452345_ASDF3412341"}' ), productstoremapping.ProductStoreMapping( 123456, 1, 12346542345, 2, '{"custom_id": "34523452345_ASDF3414534"}' ), productstoremapping.ProductStoreMapping( 123461, 1, 12346542345, 2, '{"provider": "orchard", "video_order_id":"abc"}' ), productstoremapping.ProductStoreMapping( 112233, 463, 0, 0, '{"provider": "orchard", "video_order_id":"abc"}' ), ] @patch("product_store_mapping.models.productstoremapping.get_product_with_tracks") def test_get_product_with_tracks(mock_model): """Test logic.Product.get_product_with_tracks.""" mock_model.return_value = response.Response(TEST_MAPPING_DATA[0:3]) result = product.get_product_with_tracks(123456, 1) assert productstoremapping.get_product_with_tracks.called productstoremapping.get_product_with_tracks.assert_called_with(123456, 1) assert result.message == { "product": { "store_unique_id": 1234654, "provider": "orchard", "custom_id": "807978099098790", }, "tracks": [ { 1: { "store_unique_id": 12346545234, "custom_id": "34523452345_ASDF3412341", } }, { 2: { "store_unique_id": 12346542345, "custom_id": "34523452345_ASDF3414534", } }, ], } @patch("product_store_mapping.models.productstoremapping.get_product_with_tracks") def test_get_product_with_tracks_not_found(mock_model): """Test logic.Product.get_product_with_tracks.""" mock_model.return_value = response.create_not_found_response() result = product.get_product_with_tracks(123456, 1) assert productstoremapping.get_product_with_tracks.called productstoremapping.get_product_with_tracks.assert_called_with(123456, 1) assert result.status == 404 @patch("product_store_mapping.models.productstoremapping.get_product_with_tracks") def test_get_product_with_tracks_for_googleplay(mock_model): """Test get_product_with_tracks for googleplay video_order_id.""" mock_model.return_value = response.Response(TEST_MAPPING_DATA[4:]) result = product.get_product_with_tracks(112233, 463) assert productstoremapping.get_product_with_tracks.called productstoremapping.get_product_with_tracks.assert_called_with(112233, 463) assert result.message == { "product": { "custom_id": None, "provider": "orchard", "store_unique_id": 0, "video_order_id": "abc", }, "tracks": [], } @patch("product_store_mapping.models.productstoremapping.get_product") def test_get_product(db_fixture): """Test getting the product.""" db_fixture.return_value = response.Response(TEST_MAPPING_DATA[0]) result = product.get_product(123456, 1) assert productstoremapping.get_product.called productstoremapping.get_product.assert_called_with(123456, 1) assert result.message == { "store_unique_id": 1234654, "provider": "orchard", "custom_id": "807978099098790", } @patch("product_store_mapping.models.productstoremapping.get_product") def test_get_product_not_found_error(db_fixture): """Test getting the product not found error.""" db_fixture.return_value = response.create_not_found_response() result = product.get_product(123456, 1) assert productstoremapping.get_product.called productstoremapping.get_product.assert_called_with(123456, 1) assert result.status == 404 @patch("product_store_mapping.models.productstoremapping.get_product") def test_get_product_with_video_order_id(db_fixture): """Test getting the product.""" db_fixture.return_value = response.Response(TEST_MAPPING_DATA[3]) result = product.get_product(123461, 1) assert productstoremapping.get_product.called productstoremapping.get_product.assert_called_with(123461, 1) expected_result = { "store_unique_id": 12346542345, "provider": "orchard", "video_order_id": "abc", } assert result.message == expected_result @patch("product_store_mapping.models.productstoremapping.delete_product") def test_delete_product(db_fixture): """Test delete_product success.""" db_fixture.return_value = response.Response([]) result = product.delete_product(123461, 1) assert productstoremapping.delete_product.called productstoremapping.delete_product.assert_called_with(123461, 1) expected_result = [] assert result.message == expected_result @patch("product_store_mapping.models.productstoremapping.save_product") def test_save_product_data(mock_model): """Test save product data.""" expected_result = { "product_id": 1234, "store_id": 463, "provider": "Orchard", "video_order_id": "USALL0300140", "store_unique_id": 1, "track_unique_id": 0, } mock_model.return_value = response.Response(expected_result) result = product.save_product_data(1234, 463, "Orchard", "USALL0300140", 1) assert result.message == expected_result assert result.status == 200 @patch("product_store_mapping.models.productstoremapping.save_product") def test_save_product_data_with_error(mock_model): """Test save product data with error 400.""" expected_result = { "product_id": 1234, "store_id": 463, "provider": "Orchard", "video_order_id": "USALL0300140", "store_unique_id": 1, "track_unique_id": 0, } mock_model.return_value = response.Response(expected_result) result = product.save_product_data(0, 463, "Orchard", "USALL0300140", 1) assert result.status == 400 assert result.errors["code"] == "InvalidInput" assert ( result.errors["message"] == "product_id, store_id, provider & video_order_id all are required." ) @patch("product_store_mapping.models.productstoremapping.save_product") def test_save_product_data_without_optional_param(mock_model): """Test save product data without optional params.""" expected_result = { "product_id": 1234, "store_id": 463, "provider": "Orchard", "video_order_id": "USALL0300140", "store_unique_id": 0, "track_unique_id": 0, } mock_model.return_value = response.Response(expected_result) mapping_data = { "provider": "Orchard", "video_order_id": "USALL0300140", "store_unique_id": 0, } result = product.save_product_data(1234, 463, "Orchard", "USALL0300140") mock_model.assert_called_once_with(1234, 463, mapping_data) assert result.message == expected_result assert result.status == 200 @patch("product_store_mapping.models.productstoremapping.save_product") def test_save_product_data_without_required_param(mock_model): """Test save product data without required params.""" expected_result = { "product_id": 1234, "store_id": 463, "provider": "Orchard", "video_order_id": "USALL0300140", "store_unique_id": 0, "track_unique_id": 0, } mock_model.return_value = response.Response(expected_result) result = product.save_product_data(1234, 463, "", "") assert result.status == 400 assert result.errors["code"] == "InvalidInput" assert ( result.errors["message"] == "product_id, store_id, provider & video_order_id all are required." ) mock_model.assert_not_called()