diff --git a/pccommon/pccommon/config/core.py b/pccommon/pccommon/config/core.py index be7518f2..7e9f8d6f 100644 --- a/pccommon/pccommon/config/core.py +++ b/pccommon/pccommon/config/core.py @@ -4,7 +4,7 @@ from cachetools import Cache, LRUCache, cachedmethod from cachetools.func import lru_cache from cachetools.keys import hashkey -from pydantic import BaseModel, BaseSettings, Field, PrivateAttr +from pydantic import BaseModel, BaseSettings, Field, PrivateAttr, validator from pccommon.config.collections import CollectionConfigTable from pccommon.config.containers import ContainerConfigTable @@ -23,6 +23,16 @@ class TableConfig(BaseModel): table_name: str account_url: Optional[str] = None + @validator("account_url") + def validate_url(cls, value: str) -> str: + if value and not value.startswith("http://azurite:"): + raise ValueError( + "Non-azurite account url provided. " + "Account keys can only be used with Azurite emulator." + ) + + return value + class PCAPIsConfig(BaseSettings): _cache: Cache = PrivateAttr(default_factory=lambda: LRUCache(maxsize=10)) diff --git a/pccommon/tests/config/test_table_service.py b/pccommon/tests/config/test_table_service.py deleted file mode 100644 index 8504c0c1..00000000 --- a/pccommon/tests/config/test_table_service.py +++ /dev/null @@ -1,29 +0,0 @@ -import pytest - -from pccommon.tables import TableService - - -def test_table_service_azurite() -> None: - with TableService.from_environment( - account_name="devstoreaccount1", - table_name="testtable", - account_url="http://azurite:10002", - ) as table: - assert table - assert table.table_name == "testtable" - assert table.account_name == "devstoreaccount1" - - -def test_table_service_fails_without_azurite() -> None: - with pytest.raises(ValueError) as excinfo: - with TableService.from_environment( - account_name="devstoreaccount1", - table_name="testtable", - account_url="https://devstoreaccount1.table.core.windows.net", - ) as _: - pass - - assert str(excinfo.value) == ( - "Non-azurite account url provided. " - "Account keys can only be used with Azurite emulator." - ) diff --git a/pccommon/tests/config/test_table_settings.py b/pccommon/tests/config/test_table_settings.py new file mode 100644 index 00000000..bdb765c9 --- /dev/null +++ b/pccommon/tests/config/test_table_settings.py @@ -0,0 +1,22 @@ +import pytest + +from pccommon.config.core import TableConfig + + +def test_raises_on_non_azurite_account_url() -> None: + + invalid_url = "https://example.com" + with pytest.raises(ValueError) as exc_info: + TableConfig(account_url=invalid_url, table_name="test", account_name="test") + + assert ( + "Non-azurite account url provided. " + "Account keys can only be used with Azurite emulator." + ) in str(exc_info.value) + + +def test_settings_accepts_azurite_url() -> None: + valid_url = "http://azurite:12345" + + config = TableConfig(account_url=valid_url, table_name="test", account_name="test") + assert config.account_url == valid_url