Skip to content

Commit

Permalink
Add initial cache configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
purefunctor committed Jun 9, 2021
1 parent 0466e41 commit 2d01077
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions stlt/assets/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ client_id = "CLIENT_ID"
client_secret = "CLIENT_SECRET"
redirect_uri = "REDIRECT_URI"
scope = "SCOPE"

[cache]
auth_cache = "CACHE"
24 changes: 24 additions & 0 deletions stlt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ def from_dict(cls, data: t.Mapping) -> OAuthConfig:
return cls(**fields)


@attr.s
class CacheConfig:
"""Configuration data class for the cache."""

auth_cache: Path = attr.ib(converter=Path)

@classmethod
def from_dict(cls, data: t.Mapping) -> CacheConfig:
"""Create a `CacheConfig` from a `toml`-based mapping."""
fields = {}
for field in attr.fields(cls):
name = field.name
try:
fields[name] = data[name]
except KeyError:
err = f"Missing required key '{name}'"
raise ConfigError(err) from None
return cls(**fields)


@attr.s(slots=True)
class Config:
"""Configuration data class for the project."""
Expand All @@ -46,6 +66,10 @@ class Config:
metadata={"section": ["oauth"], "builder": OAuthConfig.from_dict}
)

cache: CacheConfig = attr.ib(
metadata={"section": ["cache"], "builder": CacheConfig.from_dict}
)

@classmethod
def from_dict(cls, data: t.Mapping) -> Config:
"""Create a `Config` from a `toml`-based mapping."""
Expand Down
21 changes: 20 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import toml

from stlt.config import (
CacheConfig,
Config,
DEFAULT_CONFIG_FILE,
ensure_config,
Expand Down Expand Up @@ -72,12 +73,30 @@ def test_from_dict_fails_on_missing_keys(self, path: Path) -> None:
OAuthConfig.from_dict({})


class TestCacheConfig:
"""Test for the `CacheConfig` class."""

def test_from_dict_parses_a_valid_mapping(self, path: Path) -> None:
"""Test valid serialization with `CacheConfig.from_dict`."""
expected = CacheConfig(**DEFAULT_CONFIG_FILE["cache"])

assert CacheConfig.from_dict(DEFAULT_CONFIG_FILE["cache"]) == expected

def test_from_dict_fails_on_missing_keys(self, path: Path) -> None:
"""Test invalid serialization with `CacheConfig.from_dict`."""
with pytest.raises(ConfigError, match="Missing required key"):
CacheConfig.from_dict({})


class TestConfig:
"""Tests for the `Config` class."""

def test_from_dict_parses_a_valid_mapping(self, path: Path) -> None:
"""Test valid serialization with `Config.from_dict`."""
expected = Config(oauth=OAuthConfig.from_dict(DEFAULT_CONFIG_FILE["oauth"]))
expected = Config(
oauth=OAuthConfig(**DEFAULT_CONFIG_FILE["oauth"]),
cache=CacheConfig(**DEFAULT_CONFIG_FILE["cache"]),
)

assert Config.from_dict(DEFAULT_CONFIG_FILE) == expected

Expand Down

0 comments on commit 2d01077

Please sign in to comment.