from typing import Any, Dict, Optional from uuid import UUID import pytest from pdp.fastapi.schemas.tenant import IdExchangeTenantHierarchy @pytest.mark.parametrize( "payload, expect_exception, expected", [ pytest.param({}, True, None, id="Missing company_brand"), pytest.param( { "company_brand": { "tenant_type": "company_brand", "tenant_uuid": "397513ac-da50-4921-aec2-913c08919580", } }, True, None, id="Missing id exchange data", ), pytest.param( { "company_brand": { "tenant_type": "company_brand", "tenant_uuid": "397513ac-da50-4921-aec2-913c08919580", }, "tenant_id": 1, "tenant_type": "account", }, True, None, id="Missing tenant uuid", ), pytest.param( { "company_brand": { "tenant_type": "company_brand", "tenant_uuid": "397513ac-da50-4921-aec2-913c08919580", }, "tenant_type": "account", "tenant_uuid": "fa168a0b-fbf4-411e-833c-5449ef111e99", }, True, None, id="Missing tenant id", ), pytest.param( { "company_brand": { "tenant_type": "company_brand", "tenant_uuid": "397513ac-da50-4921-aec2-913c08919580", }, "tenant_id": 1, "tenant_uuid": "fa168a0b-fbf4-411e-833c-5449ef111e99", }, True, None, id="Missing tenant type", ), pytest.param( { "company_brand": { "tenant_type": "company_brand", "tenant_uuid": "397513ac-da50-4921-aec2-913c08919580", }, "tenant_id": 1, "tenant_type": "account", "tenant_uuid": "fa168a0b-fbf4-411e-833c-5449ef111e99", }, False, { "company_brand": { "tenant_type": "company_brand", "tenant_uuid": "397513ac-da50-4921-aec2-913c08919580", }, "tenant_id": 1, "tenant_type": "account", "tenant_uuid": "fa168a0b-fbf4-411e-833c-5449ef111e99", }, id="The minimum set of required fields is present", ), pytest.param( { "company_brand": { "tenant_type": "company_brand", "tenant_uuid": "397513ac-da50-4921-aec2-913c08919580", }, "tenant_id": 1, "tenant_type": "not a type", "tenant_uuid": "fa168a0b-fbf4-411e-833c-5449ef111e99", }, True, None, id="Tenant type must be valid", ), pytest.param( { "company_brand": { "tenant_type": "company_brand", "tenant_uuid": "397513ac-da50-4921-aec2-913c08919580", }, "tenant_id": 1, "tenant_type": "account", "tenant_uuid": "not a uuid", }, True, None, id="Tenant uuid must be valid", ), pytest.param( { "company_brand": { "tenant_type": "company_brand", "tenant_uuid": "397513ac-da50-4921-aec2-913c08919580", }, "tenant_id": 1.23, "tenant_type": "account", "tenant_uuid": "fa168a0b-fbf4-411e-833c-5449ef111e99", }, True, None, id="Tenant id must be valid", ), ], ) def test_id_exchange_tenant_hierarchy( payload: Optional[Dict[str, Any]], expect_exception: bool, expected: Dict[str, Any], ) -> None: """Test the IdExchangeTenantHierarchy model.""" if expect_exception: with pytest.raises(Exception): IdExchangeTenantHierarchy.model_validate(payload) else: actual = IdExchangeTenantHierarchy.model_validate(payload) assert actual == IdExchangeTenantHierarchy(**expected) def test_id_exchange_tenant_hierarchy_to_key_vals_tuple() -> None: obj = IdExchangeTenantHierarchy.model_validate( { "company_brand": { "tenant_type": "company_brand", "tenant_uuid": "397513ac-da50-4921-aec2-913c08919580", }, "tenant_id": 1, "tenant_type": "account", "tenant_uuid": UUID("fa168a0b-fbf4-411e-833c-5449ef111e99"), } ) assert obj.to_key_vals_tuple() == [ ("tenant_id", "1"), ("tenant_type", "account"), ("tenant_uuid", "fa168a0b-fbf4-411e-833c-5449ef111e99"), ] def test_id_to_uuid_exchange_tenant_hierarchy_to_resource_attributes_dict() -> None: """Test IdExchangeTenantHierarchy.to_resource_attributes_dict.""" obj = IdExchangeTenantHierarchy.model_validate( { "company_brand": { "tenant_type": "company_brand", "tenant_uuid": "397513ac-da50-4921-aec2-913c08919580", }, "account": { "tenant_type": "account", "tenant_uuid": "ade0f151-f8f1-4043-87f4-c8792cc5d7d3", }, "tenant_id": 1, "tenant_type": "subaccount", "tenant_uuid": "fa168a0b-fbf4-411e-833c-5449ef111e99", } ) assert obj.to_resource_attributes_dict() == { "tenant_uuid": "fa168a0b-fbf4-411e-833c-5449ef111e99", "tenant_type": "subaccount", "tenant_hierarchy": [ "397513ac-da50-4921-aec2-913c08919580", "ade0f151-f8f1-4043-87f4-c8792cc5d7d3", ], } def test_id_exchange_tenant_hierarchy_to_cache_keys() -> None: """Test IdExchangeTenantHierarchy.to_cache_keys.""" obj = IdExchangeTenantHierarchy.model_validate( { "company_brand": { "tenant_type": "company_brand", "tenant_uuid": "397513ac-da50-4921-aec2-913c08919580", }, "account": { "tenant_type": "account", "tenant_uuid": "ade0f151-f8f1-4043-87f4-c8792cc5d7d3", }, "tenant_id": 1, "tenant_type": "subaccount", "tenant_uuid": "fa168a0b-fbf4-411e-833c-5449ef111e99", } ) assert obj.to_cache_keys() == [ "tenant_hierarchy@tenant_id#1|tenant_type#subaccount", "tenant_hierarchy@tenant_type#subaccount|tenant_uuid#fa168a0b-fbf4-411e-833c-5449ef111e99", # noqa: E501 ]