"""Test ows-account connector and types.""" import json from typing import Any, Dict, List, Optional from uuid import UUID import httpx import pytest from owsclient import AsyncOwsClient from owsclient.test import OwsClientMock from pdp.connectors import ows_account @pytest.mark.parametrize( "payload, expect_exception, expected, description", [ ({}, True, None, "Empty object not allowed"), ( {"vendor_id": "ec1fd7e2-9c95-4e09-a037-e924e0244283"}, True, None, "Missing uuid", ), ( {"uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283"}, True, None, "Missing vendor_id", ), ( { "vendor_id": 21989, "uuid": "not-a-valid-uuid", }, True, None, "Invalid vendor uuid format", ), ( { "vendor_id": 21989, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", "company_brand_uuid": "not-a-valid-uuid", }, True, None, "Invalid company uuid format", ), ( { "vendor_id": 21989, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", }, False, { "vendor_id": 21989, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", "company_brand_uuid": None, "parent_company_uuid": None, }, "Should be a valid vendor", ), ( { "vendor_id": 21989, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", }, False, { "vendor_id": 21989, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": None, }, "Should be a valid vendor with company brand", ), ( { "vendor_id": 21989, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, False, { "vendor_id": 21989, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", "company_brand_uuid": None, "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, "Should be a valid vendor with parent company", ), ( { "vendor_id": 21989, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, False, { "vendor_id": 21989, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, "Should be a valid vendor with company brand and parent company", ), ], ) def test_ows_account_lookup_vendor_model_validate( payload: Any, expect_exception: bool, expected: Any, description: str, ) -> None: """Test the LookupVendor model.""" if expect_exception: with pytest.raises(Exception): ows_account.LookupVendor.model_validate(payload) else: payload_obj = ows_account.LookupVendor.model_validate(payload) expected_obj = ows_account.LookupVendor.model_validate(expected) assert payload_obj == expected_obj, description assert json.loads(payload_obj.model_dump_json()) == expected @pytest.mark.parametrize( "payload, expect_exception, expected, description", [ ({}, True, None, "Missing vendors"), ({"vendors": [{}]}, True, None, "Objects must be valid vendor."), ( {"vendors": []}, False, ows_account.LookupVendorsResponse.model_validate({"vendors": []}), "Valid payload with empty vendors list", ), ( { "vendors": [ {"vendor_id": 6971, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02"}, None, ] }, False, ows_account.LookupVendorsResponse.model_validate( { "vendors": [ { "vendor_id": 6971, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", }, None, ] } ), "Valid payload with non-empty vendors list", ), ], ) def test_ows_account_lookup_vendor_dataloader_response_model_validate( payload: Any, expect_exception: bool, expected: Optional[ows_account.LookupVendorsResponse], description: str, ) -> None: """Test the LookupVendorsResponse model.""" if expect_exception: with pytest.raises(Exception): ows_account.LookupVendorsResponse.model_validate(payload) else: actual = ows_account.LookupVendorsResponse.model_validate(payload) assert actual == expected, description @pytest.mark.parametrize( "payload, expect_exception, expected, description", [ ({}, True, None, "Missing uuids"), ( {"uuids": ["not.a.valid.uuid"]}, True, None, "Invalid payload with invalid UUIDs.", ), ( {"uuids": []}, False, ows_account.LookupVendorsRequest.model_validate({"uuids": []}), "Valid payload with empty uuids list", ), ( { "uuids": [ "fff741c2-6def-4493-bfdf-c2bcb1128e02", "ec1fd7e2-9c95-4e09-a037-e924e0244283", ] }, False, ows_account.LookupVendorsRequest.model_validate( { "uuids": [ "fff741c2-6def-4493-bfdf-c2bcb1128e02", "ec1fd7e2-9c95-4e09-a037-e924e0244283", ] } ), "Valid payload with non-empty vendors list", ), ], ) def test_ows_account_lookup_vendor_request_model_validate( payload: Any, expect_exception: bool, expected: Optional[ows_account.LookupVendorsRequest], description: str, ) -> None: """Test the LookupVendorsResponse model.""" if expect_exception: with pytest.raises(Exception): ows_account.LookupVendorsRequest.model_validate(payload) else: actual = ows_account.LookupVendorsRequest.model_validate(payload) assert actual == expected, description @pytest.mark.parametrize( "payload, expect_exception, expected, description", [ ({}, True, None, "Empty object not allowed"), ( {"subaccount_id": 7834}, True, None, "Missing uuid", ), ( {"uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02"}, True, None, "Missing subaccount_id", ), ( { "subaccount_id": 7834, "uuid": "not-a-valid-uuid", }, True, None, "Invalid subaccount uuid format", ), ( { "subaccount_id": 7834, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "company_brand_uuid": "not-a-valid-uuid", }, True, None, "Invalid company uuid format", ), ( { "subaccount_id": 7834, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", }, False, { "subaccount_id": 7834, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "vendor_uuid": None, "company_brand_uuid": None, "parent_company_uuid": None, }, "Should be a valid subaccount", ), ( { "subaccount_id": 7834, "vendor_uuid": "4e51137e-acbb-44c9-afc0-99bed4c474de", "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, False, { "subaccount_id": 7834, "vendor_uuid": "4e51137e-acbb-44c9-afc0-99bed4c474de", "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, "Should be a valid vendor with company brand and parent company", ), ( { "subaccount_id": 7834, "vendor_uuid": "4e51137e-acbb-44c9-afc0-99bed4c474de", "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "additional_fields": True, "vendor_id": 2222, }, False, { "subaccount_id": 7834, "vendor_uuid": "4e51137e-acbb-44c9-afc0-99bed4c474de", "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": None, }, "Should be a valid vendor even with additional fields are provided", ), ], ) def test_ows_account_lookup_subaccount_model_validate( payload: Any, expect_exception: bool, expected: Optional[ows_account.LookupSubaccount], description: str, ) -> None: """Test the LookupSubaccount model.""" if expect_exception: with pytest.raises(Exception): ows_account.LookupSubaccount.model_validate(payload) else: payload_obj = ows_account.LookupSubaccount.model_validate(payload) expected_obj = ows_account.LookupSubaccount.model_validate(expected) assert payload_obj == expected_obj, description assert json.loads(payload_obj.model_dump_json()) == expected @pytest.mark.parametrize( "payload, expect_exception, expected, description", [ ({}, True, None, "Empty object not allowed"), ( {"company_brand_id": 1}, True, None, "Missing uuid", ), ( {"uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02"}, True, None, "Missing company_brand_id", ), ( { "company_brand_id": 1, "uuid": "not-a-valid-uuid", }, True, None, "Invalid company brand uuid format", ), ( { "company_brand_id": 1, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "parent_company_uuid": "not-a-valid-uuid", }, True, None, "Invalid parent company uuid format", ), ( { "company_brand_id": 1, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "parent_company_uuid": None, }, False, { "company_brand_id": 1, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "parent_company_uuid": None, }, "Valid LookupCompanyBrand object with None parent company uuid", ), ( { "company_brand_id": 1, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "parent_company_uuid": "ee967d1f-68e3-4253-9474-1b44ba4bf2a7", }, False, { "company_brand_id": 1, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "parent_company_uuid": "ee967d1f-68e3-4253-9474-1b44ba4bf2a7", }, "Valid LookupCompanyBrand object with parent company uuid", ), ( { "company_brand_id": 1, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "parent_company_uuid": "ee967d1f-68e3-4253-9474-1b44ba4bf2a7", "some other field": "not needed", }, False, { "company_brand_id": 1, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "parent_company_uuid": "ee967d1f-68e3-4253-9474-1b44ba4bf2a7", }, "Valid LookupCompanyBrand object even when additional fields are provided", ), ], ) def test_ows_account_lookup_company_brand_model_validate( payload: Any, expect_exception: bool, expected: Optional[ows_account.LookupCompanyBrand], description: str, ) -> None: """Test the LookupCompanyBrand model.""" if expect_exception: with pytest.raises(Exception): ows_account.LookupCompanyBrand.model_validate(payload) else: payload_obj = ows_account.LookupCompanyBrand.model_validate(payload) expected_obj = ows_account.LookupCompanyBrand.model_validate(expected) assert payload_obj == expected_obj assert json.loads(payload_obj.model_dump_json()) == expected @pytest.mark.parametrize( "payload, expect_exception, expected, description", [ ({}, True, None, "Missing company_brands"), ({"company_brands": [{}]}, True, None, "Objects must be valid company brand."), ( { "company_brands": [ { "company_brand_id": 1, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, { "company_brand_id": 2, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", "parent_company_uuid": None, }, ] }, False, ows_account.LookupCompanyBrandsResponse.model_validate( { "company_brands": [ { "company_brand_id": 1, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", # noqa: E501 }, { "company_brand_id": 2, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", "parent_company_uuid": None, }, ] } ), "Valid payload with non-empty company_brands list", ), ], ) def test_ows_account_lookup_company_brand_response_model_validate( payload: Any, expect_exception: bool, expected: Optional[ows_account.LookupCompanyBrandsResponse], description: str, ) -> None: """Test the LookupCompanyBrandsResponse model.""" if expect_exception: with pytest.raises(Exception): ows_account.LookupCompanyBrandsResponse.model_validate(payload) else: actual = ows_account.LookupCompanyBrandsResponse.model_validate(payload) assert actual == expected, description @pytest.mark.parametrize( "payload, expect_exception, expected, description", [ ({}, True, None, "Empty object not allowed"), ( {"parent_company_id": 1}, True, None, "Missing uuid", ), ( {"uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02"}, True, None, "Missing parent_company_id", ), ( { "parent_company_id": 1, "uuid": "not-a-valid-uuid", }, True, None, "Invalid parent company uuid format", ), ( { "parent_company_id": 1, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", }, False, { "parent_company_id": 1, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", }, "Valid LookupParentCompany object with None parent company uuid", ), ( { "parent_company_id": 1, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "some other field": "not needed", }, False, { "parent_company_id": 1, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", }, "Valid LookupParentCompany object even when additional fields are provided", ), ], ) def test_ows_account_lookup_parent_company_model_validate( payload: Any, expect_exception: bool, expected: Optional[ows_account.LookupParentCompany], description: str, ) -> None: """Test the LookupParentCompany model.""" if expect_exception: with pytest.raises(Exception): ows_account.LookupParentCompany.model_validate(payload) else: payload_obj = ows_account.LookupParentCompany.model_validate(payload) expected_obj = ows_account.LookupParentCompany.model_validate(expected) assert payload_obj == expected_obj assert json.loads(payload_obj.model_dump_json()) == expected @pytest.mark.parametrize( "payload, expect_exception, expected, description", [ ({}, True, None, "Missing parent_companies"), ( {"parent_companies": [{}]}, True, None, "Objects must be valid parent company.", ), ( {"parent_companies": []}, False, ows_account.LookupParentCompaniesResponse.model_validate( {"parent_companies": []} ), "Parent company list can be empty.", ), ( { "parent_companies": [ { "parent_company_id": 1, "uuid": "f1594122-7f99-4916-b103-08b0444c7b46", }, { "parent_company_id": 2, "uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, ] }, False, ows_account.LookupParentCompaniesResponse.model_validate( { "parent_companies": [ { "parent_company_id": 1, "uuid": "f1594122-7f99-4916-b103-08b0444c7b46", }, { "parent_company_id": 2, "uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, ] } ), "Valid payload with non-empty parent_companies list", ), ], ) def test_ows_account_lookup_parent_company_brands_response_model_validate( payload: Any, expect_exception: bool, expected: Optional[ows_account.LookupParentCompaniesResponse], description: str, ) -> None: """Test the LookupParentCompaniesResponse model.""" if expect_exception: with pytest.raises(Exception): ows_account.LookupParentCompaniesResponse.model_validate(payload) else: actual = ows_account.LookupParentCompaniesResponse.model_validate(payload) assert actual == expected, description @pytest.fixture def ows_account_client( mock_async_ows_client: AsyncOwsClient, ) -> ows_account.OwsAccountClient: """Return an ows-account mock client.""" return ows_account.OwsAccountClient(async_ows_client=mock_async_ows_client) ######## Lookup Vendors By UUIDs tests ######### async def test_lookup_vendors_by_uuids_empty_list( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, ) -> None: """Test lookup_vendors_by_uuids returns immediately when uuids list is empty.""" result = await ows_account_client.lookup_vendors_by_uuids( uuids=[], fetch_flags=ows_account.DEFAULT_FETCH_FLAGS ) assert result == ows_account.LookupVendorsResponse(vendors=[]) @pytest.mark.parametrize( "description, fetch_flags, response, expected", [ ( "Valid response that includes fetch flags", ows_account.DEFAULT_FETCH_FLAGS, { "vendors": [ { "vendor_id": 6971, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, { "vendor_id": 21989, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", }, None, ] }, ows_account.LookupVendorsResponse( vendors=[ ows_account.LookupVendor( vendor_id=6971, uuid="fff741c2-6def-4493-bfdf-c2bcb1128e02", company_brand_uuid="d25a4cd1-e820-45f2-be5c-56edcfeb8298", parent_company_uuid="955a1bbd-b623-4ea1-ab5f-8d6620c442fb", ), ows_account.LookupVendor( vendor_id=21989, uuid="ec1fd7e2-9c95-4e09-a037-e924e0244283", company_brand_uuid="d25a4cd1-e820-45f2-be5c-56edcfeb8298", parent_company_uuid=None, ), None, ] ), ), ( "Valid response that has no fetch flags", [], { "vendors": [ { "vendor_id": 6971, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", }, { "vendor_id": 21989, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", }, None, ] }, ows_account.LookupVendorsResponse( vendors=[ ows_account.LookupVendor( vendor_id=6971, uuid="fff741c2-6def-4493-bfdf-c2bcb1128e02", ), ows_account.LookupVendor( vendor_id=21989, uuid="ec1fd7e2-9c95-4e09-a037-e924e0244283", ), None, ] ), ), ], ) async def test_lookup_vendors_by_uuids( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, description: str, fetch_flags: List[ows_account.LookupVendorFetchFlags], response: Dict[str, Any], expected: ows_account.LookupVendorsResponse, ) -> None: """Test lookup_vendors_by_uuids call.""" uuids = [ UUID("fff741c2-6def-4493-bfdf-c2bcb1128e02"), UUID("ec1fd7e2-9c95-4e09-a037-e924e0244283"), ] ows_client_mock.post( "ows-account", path="/lookup/vendors/uuids/", json={ "uuids": [str(_uuid) for _uuid in uuids], "fetch_flags": fetch_flags, }, ).mock( return_value=httpx.Response( 200, json=response, ) ) result = await ows_account_client.lookup_vendors_by_uuids( uuids=uuids, fetch_flags=fetch_flags ) assert result == expected, description async def test_lookup_vendors_by_uuids_failure( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, tenant_1_uuid: UUID, tenant_1_uuid_as_string: str, ) -> None: """Test lookup_vendors_by_uuids call with 400 response raises exception.""" ows_client_mock.post( "ows-account", path="/lookup/vendors/uuids/", json={"uuids": [tenant_1_uuid_as_string], "fetch_flags": []}, ).mock( return_value=httpx.Response( 400, json={"code": "400", "message": "bad"}, ) ) with pytest.raises(Exception): await ows_account_client.lookup_vendors_by_uuids(uuids=[tenant_1_uuid]) async def test_lookup_vendors_by_uuids_timeout_failure( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, tenant_1_uuid: UUID, tenant_1_uuid_as_string: str, ) -> None: """Test lookup_vendors_by_uuids with a timeout error.""" ows_client_mock.post( "ows-account", path="/lookup/vendors/uuids/", json={"uuids": [tenant_1_uuid_as_string], "fetch_flags": []}, ).mock(side_effect=httpx.ConnectTimeout(message="mock error")) with pytest.raises(Exception): await ows_account_client.lookup_vendors_by_uuids(uuids=[tenant_1_uuid]) ######## Lookup Subaccounts By UUIDs tests ######### @pytest.mark.parametrize( "description, fetch_flags, response, expected", [ ( "Valid response that includes fetch flags", ows_account.DEFAULT_FETCH_FLAGS, { "subaccounts": [ { "subaccount_id": 7834, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "vendor_uuid": "4e51137e-acbb-44c9-afc0-99bed4c474de", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, { "subaccount_id": 67295, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", "vendor_uuid": "6a8b1152-831d-4bc1-afe8-d013d66d84f7", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", }, None, ] }, ows_account.LookupSubaccountsResponse( subaccounts=[ ows_account.LookupSubaccount( subaccount_id=7834, uuid="fff741c2-6def-4493-bfdf-c2bcb1128e02", vendor_uuid="4e51137e-acbb-44c9-afc0-99bed4c474de", company_brand_uuid="d25a4cd1-e820-45f2-be5c-56edcfeb8298", parent_company_uuid="955a1bbd-b623-4ea1-ab5f-8d6620c442fb", ), ows_account.LookupSubaccount( subaccount_id=67295, uuid="ec1fd7e2-9c95-4e09-a037-e924e0244283", vendor_uuid="6a8b1152-831d-4bc1-afe8-d013d66d84f7", company_brand_uuid="d25a4cd1-e820-45f2-be5c-56edcfeb8298", parent_company_uuid=None, ), None, ] ), ), ( "Valid response that does not include fetch flags", None, { "subaccounts": [ { "subaccount_id": 7834, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", }, { "subaccount_id": 67295, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", }, None, ] }, ows_account.LookupSubaccountsResponse( subaccounts=[ ows_account.LookupSubaccount( subaccount_id=7834, uuid="fff741c2-6def-4493-bfdf-c2bcb1128e02", ), ows_account.LookupSubaccount( subaccount_id=67295, uuid="ec1fd7e2-9c95-4e09-a037-e924e0244283", ), None, ] ), ), ( "Empty list of subaccounts returns empty list", None, {"subaccounts": []}, ows_account.LookupSubaccountsResponse(subaccounts=[]), ), ], ) async def test_lookup_subaccounts_by_uuids( description: str, fetch_flags: Optional[List[ows_account.LookupVendorFetchFlags]], response: Dict[str, Any], expected: ows_account.LookupSubaccountsResponse, ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, ) -> None: """Test lookup_subaccounts_by_uuids call.""" uuids = [ UUID("fff741c2-6def-4493-bfdf-c2bcb1128e02"), UUID("ec1fd7e2-9c95-4e09-a037-e924e0244283"), ] ows_client_mock.post( "ows-account", path="/lookup/subaccounts/uuids/", json={ "uuids": [str(_uuid) for _uuid in uuids], "fetch_flags": fetch_flags or [], }, ).mock( return_value=httpx.Response( 200, json=response, ) ) if fetch_flags: result = await ows_account_client.lookup_subaccounts_by_uuids( uuids=uuids, fetch_flags=fetch_flags ) else: result = await ows_account_client.lookup_subaccounts_by_uuids(uuids=uuids) assert result == expected, description async def test_lookup_subaccounts_by_uuids_failure( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, tenant_1_uuid: UUID, tenant_1_uuid_as_string: str, ) -> None: """Test lookup_subaccounts_by_uuids call with 400 response raises exception.""" ows_client_mock.post( "ows-account", path="/lookup/subaccounts/uuids/", json={"uuids": [tenant_1_uuid_as_string], "fetch_flags": []}, ).mock( return_value=httpx.Response( 400, json={"code": "400", "message": "bad"}, ) ) with pytest.raises(Exception): await ows_account_client.lookup_subaccounts_by_uuids(uuids=[tenant_1_uuid]) async def test_lookup_subaccounts_by_uuids_timeout_failure( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, tenant_1_uuid: UUID, tenant_1_uuid_as_string: str, ) -> None: """Test lookup_subaccounts_by_uuids with a timeout error.""" ows_client_mock.post( "ows-account", path="/lookup/subaccounts/uuids/", json={"uuids": [tenant_1_uuid_as_string], "fetch_flags": []}, ).mock(side_effect=httpx.ConnectTimeout(message="mock error")) with pytest.raises(Exception): await ows_account_client.lookup_subaccounts_by_uuids(uuids=[tenant_1_uuid]) ######## Lookup Company Brands By UUIDs tests ######### async def test_lookup_company_brands_by_uuids_empty_list( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, ) -> None: """Test lookup_company_brands_by_uuids returns immediately when uuids list is empty.""" # noqa: E501 result = await ows_account_client.lookup_company_brands_by_uuids(uuids=[]) assert result == ows_account.LookupCompanyBrandsResponse(company_brands=[]) async def test_lookup_company_brands_by_uuids( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, ) -> None: """Test lookup_company_brands_by_uuids call.""" uuids = [ UUID("fff741c2-6def-4493-bfdf-c2bcb1128e02"), UUID("ec1fd7e2-9c95-4e09-a037-e924e0244283"), UUID("246c5db5-682a-42a7-84b5-db54c03af495"), ] ows_client_mock.post( "ows-account", path="/lookup/company-brands/uuids/", json={"uuids": [str(_uuid) for _uuid in uuids]}, ).mock( return_value=httpx.Response( 200, json={ "company_brands": [ { "company_brand_id": 1, "uuid": "fff741c2-6def-4493-bfdf-c2bcb1128e02", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, { "company_brand_id": 2, "uuid": "ec1fd7e2-9c95-4e09-a037-e924e0244283", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, None, ] }, ) ) result = await ows_account_client.lookup_company_brands_by_uuids(uuids=uuids) assert result == ows_account.LookupCompanyBrandsResponse( company_brands=[ ows_account.LookupCompanyBrand( company_brand_id=1, uuid="fff741c2-6def-4493-bfdf-c2bcb1128e02", parent_company_uuid="955a1bbd-b623-4ea1-ab5f-8d6620c442fb", ), ows_account.LookupCompanyBrand( company_brand_id=2, uuid="ec1fd7e2-9c95-4e09-a037-e924e0244283", parent_company_uuid="955a1bbd-b623-4ea1-ab5f-8d6620c442fb", ), None, ] ) async def test_lookup_company_brands_by_uuids_failure( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, ) -> None: """Test lookup_company_brands_by_uuids call with 400 response raises exception.""" uuids = [UUID("ec1fd7e2-9c95-4e09-a037-e924e0244283")] ows_client_mock.post( "ows-account", path="/lookup/company-brands/uuids/", json={"uuids": [str(_uuid) for _uuid in uuids]}, ).mock( return_value=httpx.Response( 400, json={"code": "400", "message": "bad"}, ) ) with pytest.raises(Exception): await ows_account_client.lookup_company_brands_by_uuids(uuids=uuids) ######## Lookup Vendors By IDs tests ######### async def test_lookup_vendors_by_ids_empty_list( ows_account_client: ows_account.OwsAccountClient, ) -> None: """Test lookup_vendors_by_ids returns immediately when vendor_ids list is empty.""" result = await ows_account_client.lookup_vendors_by_ids( vendor_ids=[], fetch_flags=ows_account.DEFAULT_FETCH_FLAGS ) assert result == ows_account.LookupVendorsResponse(vendors=[]) async def test_lookup_vendors_by_ids( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, tenant_1_id: int, tenant_2_id: int, ) -> None: """Test lookup_vendors_by_ids call.""" vendor_ids = [tenant_1_id, tenant_2_id] ows_client_mock.post( "ows-account", path="/lookup/vendors/vendor-ids/", json={ "vendor_ids": vendor_ids, "fetch_flags": ows_account.DEFAULT_FETCH_FLAGS, }, ).mock( return_value=httpx.Response( 200, json={ "vendors": [ { "vendor_id": tenant_1_id, "uuid": "481c406e-3ad0-49ca-afa8-4c27e21a14d8", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", }, { "vendor_id": tenant_2_id, "uuid": "f4371743-b00d-4404-8bfb-103896cd5e2b", "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, None, ] }, ) ) result = await ows_account_client.lookup_vendors_by_ids( vendor_ids, fetch_flags=ows_account.DEFAULT_FETCH_FLAGS ) assert result == ows_account.LookupVendorsResponse( vendors=[ ows_account.LookupVendor( vendor_id=tenant_1_id, uuid="481c406e-3ad0-49ca-afa8-4c27e21a14d8", company_brand_uuid="d25a4cd1-e820-45f2-be5c-56edcfeb8298", parent_company_uuid=None, ), ows_account.LookupVendor( vendor_id=tenant_2_id, uuid="f4371743-b00d-4404-8bfb-103896cd5e2b", company_brand_uuid="d25a4cd1-e820-45f2-be5c-56edcfeb8298", parent_company_uuid="955a1bbd-b623-4ea1-ab5f-8d6620c442fb", ), None, ] ) async def test_lookup_vendors_by_ids_failure( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, ) -> None: """Test lookup_vendors_by_ids call with 400 response raises exception.""" vendor_ids = [999] ows_client_mock.post( "ows-account", path="/lookup/vendors/vendor-ids/", json={ "vendor_ids": vendor_ids, "fetch_flags": [], }, ).mock( return_value=httpx.Response( 400, json={"code": "400", "message": "bad"}, ) ) with pytest.raises(Exception): await ows_account_client.lookup_vendors_by_ids(vendor_ids=vendor_ids) async def test_lookup_vendors_by_ids_timeout_failure( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock ) -> None: """Test lookup_vendors_by_ids with a timeout error.""" ows_client_mock.post( "ows-account", path="/lookup/vendors/vendor-ids/", json={"vendor_ids": [1], "fetch_flags": []}, ).mock(side_effect=httpx.ConnectTimeout(message="mock error")) with pytest.raises(Exception): await ows_account_client.lookup_vendors_by_ids(vendor_ids=[1]) ######## Lookup Subaccounts By IDs tests ######### async def test_lookup_subaccounts_by_ids( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, tenant_1_uuid_as_string: str, tenant_2_uuid_as_string: str, ) -> None: """Test lookup_vendors_by_ids call.""" subaccount_ids = [1, 2] ows_client_mock.post( "ows-account", path="/lookup/subaccounts/subaccount-ids/", json={ "subaccount_ids": subaccount_ids, "fetch_flags": ows_account.DEFAULT_FETCH_FLAGS, }, ).mock( return_value=httpx.Response( 200, json={ "subaccounts": [ { "subaccount_id": 1, "uuid": "481c406e-3ad0-49ca-afa8-4c27e21a14d8", "vendor_uuid": tenant_1_uuid_as_string, "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, { "subaccount_id": 2, "uuid": "f4371743-b00d-4404-8bfb-103896cd5e2b", "vendor_uuid": tenant_2_uuid_as_string, "company_brand_uuid": "d25a4cd1-e820-45f2-be5c-56edcfeb8298", "parent_company_uuid": "955a1bbd-b623-4ea1-ab5f-8d6620c442fb", }, None, ] }, ) ) result = await ows_account_client.lookup_subaccounts_by_ids( subaccount_ids, fetch_flags=ows_account.DEFAULT_FETCH_FLAGS ) assert result == ows_account.LookupSubaccountsResponse( subaccounts=[ ows_account.LookupSubaccount( subaccount_id=1, uuid="481c406e-3ad0-49ca-afa8-4c27e21a14d8", vendor_uuid=tenant_1_uuid_as_string, company_brand_uuid="d25a4cd1-e820-45f2-be5c-56edcfeb8298", parent_company_uuid="955a1bbd-b623-4ea1-ab5f-8d6620c442fb", ), ows_account.LookupSubaccount( subaccount_id=2, uuid="f4371743-b00d-4404-8bfb-103896cd5e2b", vendor_uuid=tenant_2_uuid_as_string, company_brand_uuid="d25a4cd1-e820-45f2-be5c-56edcfeb8298", parent_company_uuid="955a1bbd-b623-4ea1-ab5f-8d6620c442fb", ), None, ] ) async def test_lookup_subaccounts_by_ids_failure( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, ) -> None: """Test lookup_vendors_by_ids call with 400 response raises exception.""" subaccount_ids = [1] ows_client_mock.post( "ows-account", path="/lookup/subaccounts/subaccount-ids/", json={"subaccount_ids": subaccount_ids, "fetch_flags": []}, ).mock( return_value=httpx.Response( 400, json={"code": "400", "message": "bad"}, ) ) with pytest.raises(Exception): await ows_account_client.lookup_subaccounts_by_ids(subaccount_ids) async def test_lookup_subaccounts_by_ids_timeout_failure( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock ) -> None: """Test lookup_vendors_by_ids with a timeout error.""" subaccount_ids = [1] ows_client_mock.post( "ows-account", path="/lookup/subaccounts/subaccount-ids/", json={"subaccount_ids": subaccount_ids, "fetch_flags": []}, ).mock(side_effect=httpx.ConnectTimeout(message="mock error")) with pytest.raises(Exception): await ows_account_client.lookup_subaccounts_by_ids(subaccount_ids) ######## Lookup Company Brands By IDs tests ######### async def test_lookup_company_brands_by_ids_empty_list( ows_account_client: ows_account.OwsAccountClient, ) -> None: """Test lookup_company_brands_by_ids returns immediately when company_brand_ids list is empty.""" # noqa: E501 result = await ows_account_client.lookup_company_brands_by_ids(company_brand_ids=[]) assert result == ows_account.LookupCompanyBrandsResponse(company_brands=[]) async def test_lookup_company_brands_by_ids( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, ) -> None: """Test lookup_company_brands_by_ids call.""" company_brand_ids = [1, 2, 67] ows_client_mock.post( "ows-account", path="/lookup/company-brands/company-brand-ids/", json={"company_brand_ids": company_brand_ids}, ).mock( return_value=httpx.Response( 200, json={ "company_brands": [ { "company_brand_id": 1, "uuid": "481c406e-3ad0-49ca-afa8-4c27e21a14d8", }, { "company_brand_id": 2, "uuid": "f4371743-b00d-4404-8bfb-103896cd5e2b", }, None, ] }, ) ) result = await ows_account_client.lookup_company_brands_by_ids(company_brand_ids) assert result == ows_account.LookupCompanyBrandsResponse( company_brands=[ ows_account.LookupCompanyBrand( company_brand_id=1, uuid="481c406e-3ad0-49ca-afa8-4c27e21a14d8", ), ows_account.LookupCompanyBrand( company_brand_id=2, uuid="f4371743-b00d-4404-8bfb-103896cd5e2b", ), None, ] ) async def test_lookup_company_brands_by_ids_failure( ows_account_client: ows_account.OwsAccountClient, ows_client_mock: OwsClientMock, ) -> None: """Test lookup_company_brands_by_ids call with 400 response raises exception.""" company_brand_ids = [1] ows_client_mock.post( "ows-account", path="/lookup/company-brands/company-brand-ids/", json={"company_brand_ids": company_brand_ids}, ).mock( return_value=httpx.Response( 400, json={"code": "400", "message": "bad"}, ) ) with pytest.raises(Exception): await ows_account_client.lookup_company_brands_by_ids(company_brand_ids) ######## Lookup Parent Companies By UUIDs tests ######### def test_lookup_parent_companies_by_uuids_empty_list( ows_account_client: ows_account.OwsAccountClient, ) -> None: """Test lookup_parent_companies_by_uuids returns immediately when uuids list is empty.""" # noqa: E501 result = ows_account_client.lookup_parent_companies_by_uuids(uuids=[]) assert result == ows_account.LookupParentCompaniesResponse(parent_companies=[]) def test_lookup_parent_companies_by_uuids( ows_account_client: ows_account.OwsAccountClient, ) -> None: """Test lookup_parent_companies_by_uuids call.""" uuids = [ UUID("f1594122-7f99-4916-b103-08b0444c7b46"), # SME UUID("ec1fd7e2-9c95-4e09-a037-e924e0244283"), # Not a Parent Company uuid ] result = ows_account_client.lookup_parent_companies_by_uuids(uuids=uuids) assert result == ows_account.LookupParentCompaniesResponse( parent_companies=[ ows_account.LookupParentCompany( parent_company_id=1, uuid="f1594122-7f99-4916-b103-08b0444c7b46", ), None, ] )