"""Test for service_country model.""" from unittest.mock import MagicMock, patch import pytest from sqlalchemy.exc import SQLAlchemyError from carveouts.models.service_country_carveout import ( ServiceCountryCarveout, ServiceCountryPayload, add_product_carveouts, delete_product_carveouts, get_product_carveouts, get_subaccount_carveouts, get_vendor_carveouts, ) @pytest.mark.parametrize( "test_description, vendor_contract_id, expected", [ ( "happy path: customer_id 1 missing from customer_master; 2/3 → US/GB", 10, { ServiceCountryCarveout(service_id=1, country_id=1, country_code="US"), ServiceCountryCarveout(service_id=1, country_id=3, country_code="GB"), }, ), ("empty-string dms_carve_out", 11, set()), ("NULL dms_carve_out", 12, set()), ("missing vendor_contract row", 99999, set()), ], ) def test_get_vendor_carveouts( test_description: str, vendor_contract_id: int, expected: set[ServiceCountryCarveout], db_fixture: None, ) -> None: assert get_vendor_carveouts(vendor_contract_id) == expected def test_get_subaccount_carveouts(db_fixture: None) -> None: """Test get subaccount service country carveouts.""" response = get_subaccount_carveouts(1) assert response == { ServiceCountryCarveout(service_id=1, country_id=1, country_code="US"), } def test_get_product_carveouts(db_fixture: None) -> None: """Test get product service country carveouts.""" response = get_product_carveouts(123) assert response == { ServiceCountryCarveout(service_id=1, country_id=1, country_code="US"), } def test_delete_all_release_dms_carveouts(db_fixture: None) -> None: """Test to delete release level dms carveouts.""" delete_product_carveouts(123) assert True @patch( "carveouts.models.service_country_carveout.db_connector.db_session", side_effect=SQLAlchemyError("some sql error"), ) def test_delete_release_dms_carveouts_error(mock_db_session: MagicMock) -> None: """Test delete_release_dms_carveouts with exception.""" with pytest.raises(SQLAlchemyError) as exc: delete_product_carveouts(124) assert str(exc.value) == "some sql error" @pytest.mark.parametrize( "test_description, carveouts, expected_carveouts", [ ("empty payload", [], set()), ( "successful inserts", [ ServiceCountryPayload( service_id=1, country_codes=["US", "GB"], ), ServiceCountryPayload( service_id=187, country_codes=["US", "GB"], ), ], { ServiceCountryCarveout( country_id=1, country_code="US", service_id=1, ), ServiceCountryCarveout( country_id=3, country_code="GB", service_id=1, ), ServiceCountryCarveout( country_id=1, country_code="US", service_id=187, ), ServiceCountryCarveout( country_id=3, country_code="GB", service_id=187, ), }, ), ( "incorrect sub-store payload data", [ ServiceCountryPayload( service_id=187, country_codes=["CA"], ), ], { ServiceCountryCarveout( country_id=1, country_code="US", service_id=1, ), }, ), ], ) def test_add_product_carveouts( test_description: str, carveouts: list[ServiceCountryPayload], expected_carveouts: set[ServiceCountryCarveout], db_fixture: None, ) -> None: add_product_carveouts( product_id=123, upc=1234567890, carveouts=carveouts, ) assert get_product_carveouts(123) == expected_carveouts