Skip to content

Commit

Permalink
Move settings validation to startup check
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcfarland committed Jun 21, 2024
1 parent fca13de commit 02479ae
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
12 changes: 11 additions & 1 deletion pccommon/pccommon/config/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
29 changes: 0 additions & 29 deletions pccommon/tests/config/test_table_service.py

This file was deleted.

22 changes: 22 additions & 0 deletions pccommon/tests/config/test_table_settings.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 02479ae

Please sign in to comment.