"""Test for country_carveout model.""" from unittest.mock import MagicMock, patch import pytest from sqlalchemy.exc import IntegrityError from carveouts.models.country_carveout import ( CountryCarveout, add_product_carveouts, delete_product_carveouts, get_product_carveouts, get_subaccount_carveouts, get_vendor_carveouts, update_account_carveouts, ) @pytest.mark.parametrize( "test_description, vendor_contract_id, expected", [ ( "happy path: territory_carve_out '1,3' → US, GB", 10, { CountryCarveout(country_id=1, country_code="US"), CountryCarveout(country_id=3, country_code="GB"), }, ), ("empty-string territory_carve_out", 11, set()), ("NULL territory_carve_out", 12, set()), ("missing vendor_contract row", 99999, set()), ], ) def test_get_vendor_carveouts( test_description: str, vendor_contract_id: int, expected: set[CountryCarveout], db_fixture: None, ) -> None: assert get_vendor_carveouts(vendor_contract_id) == expected def test_get_subaccount_carveouts(db_fixture: None) -> None: """Test get_subaccount_carveouts.""" response = get_subaccount_carveouts(1) assert response == { CountryCarveout(country_id=1, country_code="US"), } def test_get_product_carveouts(db_fixture: None) -> None: """Test get_product_carveouts.""" response = get_product_carveouts(123) assert response == { CountryCarveout(country_id=1, country_code="US"), } def test_delete_product_carveouts(db_fixture: None) -> None: """Test delete_product_carveouts.""" delete_product_carveouts(123) assert True @pytest.mark.parametrize( "country_ids, expected_result", [ ([], 0), ( [1, 2, 3], 3, ), ], ) def test_add_product_carveouts( country_ids: list[int], expected_result: int, db_fixture: None, ) -> None: add_product_carveouts( product_id=123, upc=1234568768889, country_ids=country_ids, ) result = get_product_carveouts(123) assert len(result) == expected_result def test_add_product_carveouts_integrity_error(db_fixture: None) -> None: upc = 1234568768889 with pytest.raises(IntegrityError) as exc: add_product_carveouts( product_id=123, upc=upc, country_ids=[1, 1, 3], ) assert f"Duplicate entry '{upc}-1'" in str(exc.value) @patch("carveouts.models.country_carveout.country", name="get_country_ids") def test_update_account_carveout(mock_country: MagicMock, db_fixture: None) -> None: vendor_contract_id = 123 country_codes = ["US", "CA", "FR"] mock_country.get_country_ids.return_value = [1, 2, 6] update_account_carveouts(vendor_contract_id, country_codes) assert True