-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from karlicoss/more-documentation
More documentation & tests
- Loading branch information
Showing
6 changed files
with
174 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
''' | ||
Just a demo module for testing and documentation purposes | ||
''' | ||
|
||
from .core.common import Paths | ||
|
||
from datetime import tzinfo | ||
import pytz | ||
|
||
from my.config import demo as user_config | ||
from dataclasses import dataclass | ||
|
||
@dataclass | ||
class demo(user_config): | ||
data_path: Paths | ||
username: str | ||
timezone: tzinfo = pytz.utc | ||
|
||
|
||
def config() -> demo: | ||
from .core.cfg import make_config | ||
config = make_config(demo) | ||
return config | ||
|
||
|
||
|
||
from pathlib import Path | ||
from typing import Sequence, Iterable | ||
from datetime import datetime | ||
from .core.common import Json, get_files | ||
|
||
@dataclass | ||
class Item: | ||
''' | ||
Some completely arbirary artificial stuff, just for testing | ||
''' | ||
username: str | ||
raw: Json | ||
dt: datetime | ||
|
||
|
||
def inputs() -> Sequence[Path]: | ||
return get_files(config().data_path) | ||
|
||
|
||
import json | ||
def items() -> Iterable[Item]: | ||
for f in inputs(): | ||
dt = datetime.fromtimestamp(f.stat().st_mtime, tz=config().timezone) | ||
j = json.loads(f.read_text()) | ||
for raw in j: | ||
yield Item( | ||
username=config().username, | ||
raw=raw, | ||
dt=dt, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import sys | ||
from pathlib import Path | ||
from more_itertools import ilen | ||
|
||
# TODO NOTE: this wouldn't work because of an early my.config.demo import | ||
# from my.demo import items | ||
|
||
def test_dynamic_config_1(tmp_path: Path) -> None: | ||
import my.config | ||
|
||
class user_config: | ||
username = 'user' | ||
data_path = f'{tmp_path}/*.json' | ||
my.config.demo = user_config # type: ignore[misc, assignment] | ||
|
||
from my.demo import items | ||
[item1, item2] = items() | ||
assert item1.username == 'user' | ||
|
||
|
||
# exactly the same test, but using a different config, to test out the behavious w.r.t. import order | ||
def test_dynamic_config_2(tmp_path: Path) -> None: | ||
# doesn't work without it! | ||
# because the config from test_dybamic_config_1 is cached in my.demo.demo | ||
del sys.modules['my.demo'] | ||
|
||
import my.config | ||
|
||
class user_config: | ||
username = 'user2' | ||
data_path = f'{tmp_path}/*.json' | ||
my.config.demo = user_config # type: ignore[misc, assignment] | ||
|
||
from my.demo import items | ||
[item1, item2] = items() | ||
assert item1.username == 'user2' | ||
|
||
|
||
import pytest # type: ignore | ||
|
||
@pytest.mark.skip(reason="won't work at the moment because of inheritance") | ||
def test_dynamic_config_simplenamespace(tmp_path: Path) -> None: | ||
# doesn't work without it! | ||
# because the config from test_dybamic_config_1 is cached in my.demo.demo | ||
del sys.modules['my.demo'] | ||
|
||
import my.config | ||
from types import SimpleNamespace | ||
|
||
user_config = SimpleNamespace( | ||
username='user3', | ||
data_path=f'{tmp_path}/*.json', | ||
) | ||
my.config.demo = user_config # type: ignore[misc, assignment] | ||
|
||
from my.demo import config | ||
assert config().username == 'user3' | ||
|
||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def prepare(tmp_path: Path): | ||
(tmp_path / 'data.json').write_text(''' | ||
[ | ||
{"key1": 1}, | ||
{"key2": 2} | ||
] | ||
''') | ||
yield |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters