"""Test service_carveout model.""" from unittest.mock import MagicMock, patch import pytest from sqlalchemy.exc import IntegrityError, SQLAlchemyError from carveouts.models.service_carveout import ( ServiceCarveout, add_account_carveouts, add_product_carveouts, create_account_new_service_carveouts, create_product_new_service_carveouts, dataload_vendor_contract_carveout, delete_product_carveouts, get_product_carveouts, get_subaccount_carveouts, get_vendor_carveouts, ) def test_get_vendor_carveouts(db_fixture: None) -> None: """Test get_vendor_carveouts.""" actual_response = get_vendor_carveouts(10) assert actual_response == { ServiceCarveout( service_id=1, service_name="iTunes/Apple", distribution_types=[1] ), } def test_get_subaccount_carveouts(db_fixture: None) -> None: """Test get_subaccount_carveouts.""" response = get_subaccount_carveouts(1) assert response == { ServiceCarveout( service_id=1, service_name="iTunes/Apple", distribution_types=[] ), } def test_get_product_carveouts(db_fixture: None) -> None: """Test get_product_carveouts.""" response = get_product_carveouts(123) assert response == { ServiceCarveout( service_id=1, service_name="iTunes/Apple", distribution_types=[1] ), } def test_delete_product_service_carveouts(db_fixture: None) -> None: """Test to delete product level service carveouts.""" delete_product_carveouts(123) assert True @patch( "carveouts.models.service_carveout.db_connector.db_session", side_effect=SQLAlchemyError("some sql error"), ) def test_delete_product_service_carveouts_error(mock_db_session: MagicMock) -> None: """Test delete_product_service_carveouts with exception.""" with pytest.raises(SQLAlchemyError) as exc: delete_product_carveouts(124) assert str(exc.value) == "some sql error" def test_dataload_vendor_contract_carveout(db_fixture: None) -> None: """Test dataload_vendor_contract_carveout.""" data = [ {"delivery_store_id": 1, "vendor_contract_id": 10}, {"delivery_store_id": 2, "vendor_contract_id": 20}, ] actual_response = dataload_vendor_contract_carveout(data) assert actual_response == [ {"vendor_contract_id": 10, "dms_id": 1, "distribution_type_list": "1"} ] def test_dataload_vendor_contract_carveout_not_found(db_fixture: None) -> None: """Test dataload_vendor_contract_carveout with no results.""" data = [ {"delivery_store_id": 2, "vendor_contract_id": 20}, {"delivery_store_id": 3, "vendor_contract_id": 30}, ] actual_response = dataload_vendor_contract_carveout(data) assert actual_response == [] @pytest.mark.parametrize( "carveouts, expected_count", [ ([], 0), ( [ ServiceCarveout( service_id=1, service_name="iTunes/Apple", distribution_types=[1], ), ServiceCarveout( service_id=187, service_name="Amazon Digital Services", distribution_types=[1], ), ], 2, ), ], ) def test_add_product_carveouts( carveouts: list[ServiceCarveout], expected_count: int, db_fixture: None, ) -> None: add_product_carveouts( product_id=123, upc=1234567890, carveouts=carveouts, updated_by=101, ) assert len(get_product_carveouts(123)) == expected_count def test_add_product_carveouts_integrity_error(db_fixture: None) -> None: upc = 1234568768889 service_id = 1 service_name = "iTunes/Apple" service_carveout = ServiceCarveout( service_id=service_id, service_name=service_name, distribution_types=[1], ) with pytest.raises(IntegrityError) as exc: add_product_carveouts( product_id=123, upc=upc, carveouts=[service_carveout, service_carveout], updated_by=101, ) assert f"Duplicate entry '{upc}-{service_id}" in str(exc.value) @pytest.mark.parametrize( "carveouts, expected_result", [ ([], 0), ( [ ServiceCarveout( service_id=286, service_name="Spotify", distribution_types=[1], ), ServiceCarveout( service_id=187, service_name="Amazon Digital Services", distribution_types=[1, 3], ), ], 2, ), ], ) def test_add_account_carveouts( carveouts: list[ServiceCarveout], expected_result: int, db_fixture: None, ) -> None: add_account_carveouts( vendor_contract_id=10, carveouts=carveouts, updated_by=101, ) assert len(get_vendor_carveouts(10)) == expected_result def test_add_account_product_carveouts_integrity_error(db_fixture: None) -> None: vendor_contract_id = 10 service_id = 1 distribution_id = 1 service_name = "iTunes/Apple" service_carveout = ServiceCarveout( service_id=service_id, service_name=service_name, distribution_types=[distribution_id], ) with pytest.raises(IntegrityError) as exc: add_account_carveouts( vendor_contract_id=vendor_contract_id, carveouts=[service_carveout, service_carveout], updated_by=101, ) assert ( f"Duplicate entry '{vendor_contract_id}-{service_id}-{distribution_id}'" in str(exc.value) ) def test_create_product_new_service_carveouts(db_fixture: None) -> None: create_product_new_service_carveouts(101, 101) carveouts = get_product_carveouts(123) assert ( ServiceCarveout( service_id=101, service_name="non-existent store", distribution_types=[1], ) in carveouts ) def test_create_account_new_service_carveouts(db_fixture: None) -> None: create_account_new_service_carveouts(101, 101) carveouts = get_vendor_carveouts(10) assert ( ServiceCarveout( service_id=101, service_name="non-existent store", distribution_types=[1], ) in carveouts )