"""Test the tenant_cache utils.""" from typing import Any, List, Optional from uuid import UUID import pytest from pdp.utils.tenant_cache import ( TENANT_HIERARCHY_KEY_PREFIX, get_cache_key_from_tenant_uuid, get_cache_keys_from_tenant_uuid_list, make_simple_cache_key, ) @pytest.mark.parametrize( "args, delimiter, expect_exception, expected, description", [ ( ["SERIOUS_KEY_PREFIX", "some-uuid-here"], None, False, "SERIOUS_KEY_PREFIX#some-uuid-here", "Use default delimiter", ), ( [], None, True, None, "Zero key values does not make a cache key", ), ( ["SERIOUS_KEY_PREFIX"], None, True, None, "One key value does not make a cache key", ), ( ["SERIOUS_KEY_PREFIX"], "!", True, None, "One key value does not make a cache key, even with custom delimiter", ), ( [], "!", True, None, "Zero key values does not make a cache key, even with custom delimiter", ), ( ["SERIOUS_KEY_PREFIX", "some-uuid-here"], "??", False, "SERIOUS_KEY_PREFIX??some-uuid-here", "Use custom delimiter can consist of multiple characters", ), ( ["SERIOUS_KEY_PREFIX", "some-uuid-here", "another"], "@", False, "SERIOUS_KEY_PREFIX@some-uuid-here@another", "More than two args can be passed", ), ], ) def test_make_simple_cache_key( args: List[str], delimiter: str, expect_exception: bool, expected: Optional[str], description: str, ) -> None: """Test make_simple_cache_key.""" if expect_exception: with pytest.raises(AssertionError): make_simple_cache_key(*args) make_simple_cache_key(*args, delimiter=delimiter) else: if delimiter: assert make_simple_cache_key(*args, delimiter=delimiter) == expected, ( description ) else: assert make_simple_cache_key(*args) == expected, description assert make_simple_cache_key(*args, delimiter=delimiter) == expected, ( description ) @pytest.mark.parametrize( "tenant_uuid, prefix, expect_exception, expected, description", [ ( UUID("4378cd91-7852-49eb-b39f-653dec222db8"), "some prefix", False, "some prefix_4378cd91-7852-49eb-b39f-653dec222db8", "valid prefix can be used for a tenant cache key", ), ( UUID("4378cd91-7852-49eb-b39f-653dec222db8"), "", True, "some prefix_4378cd91-7852-49eb-b39f-653dec222db8", "empty prefix cannot be used for a tenant cache key", ), ( UUID("4378cd91-7852-49eb-b39f-653dec222db8"), " ", True, "some prefix_4378cd91-7852-49eb-b39f-653dec222db8", "whitespace-only prefix cannot be used for a tenant cache key", ), ], ) def test_get_cache_key_from_tenant_uuid( tenant_uuid: UUID, prefix: str, expect_exception: bool, expected: Any, description: str, ) -> None: """Test get_cache_key_from_tenant_uuid.""" if expect_exception: with pytest.raises(Exception): get_cache_key_from_tenant_uuid(tenant_uuid, prefix) else: assert get_cache_key_from_tenant_uuid(tenant_uuid, prefix) == expected, ( description ) @pytest.mark.parametrize( "tenant_uuids, cache_prefix, expected, description", [ ( [UUID("f34d9680-0bdd-11ef-a234-4a2888760682")], TENANT_HIERARCHY_KEY_PREFIX, ["tenant_hierarchy_f34d9680-0bdd-11ef-a234-4a2888760682"], "IS OK for a valid uuid list and key prefix", ), ( [], TENANT_HIERARCHY_KEY_PREFIX, [], "Returns an empty list for an empty UUID list.", ), ], ) def test_get_cache_keys_from_tenant_uuid_list( tenant_uuids: List[UUID], cache_prefix: str, expected: List[str], description: str, ) -> None: """Test get_cache_keys_from_tenant_uuid_list.""" assert ( get_cache_keys_from_tenant_uuid_list(tenant_uuids, cache_prefix=cache_prefix) == expected ), description def test_get_cache_keys_from_tenant_uuid_list_error() -> None: """Test get_cache_keys_from_tenant_uuid_list raises errors.""" with pytest.raises(ValueError): get_cache_keys_from_tenant_uuid_list( tenant_uuids=[UUID("f34d9680-0bdd-11ef-a234-4a2888760682")], cache_prefix=" ", )