Skip to content

Commit

Permalink
Improve docs (#56)
Browse files Browse the repository at this point in the history
* imporve docs

* tests

* review readme
  • Loading branch information
malmans2 authored Jan 9, 2023
1 parent dc0c345 commit 2b43bef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ False

```

## Configuration

Configuration settings can be accessed using `cacholote.config.get()` and modified using `cacholote.config.set(**kwargs)`. It is possible to use `cacholote.config.set` either as a context manager, or to configure global settings. See `help(cacholote.config.set)`.

Defaults are controlled by environment variables and dotenv files. See `help(cacholote.config.reset)`.

## Workflow for developers/contributors

For best experience create a new conda environment (e.g. DEVELOP) with Python 3.10:
Expand Down
5 changes: 5 additions & 0 deletions cacholote/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,9 @@ def reset(env_file: Optional[Union[str, Tuple[str]]] = None) -> None:
set()


def get() -> Settings:
"""Get cacholote settings."""
return SETTINGS.get()


reset()
24 changes: 8 additions & 16 deletions tests/test_01_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,24 @@


def test_change_engine(tmpdir: pathlib.Path) -> None:
old_db = config.SETTINGS.get().cache_db_urlpath
old_db = config.get().cache_db_urlpath
new_db = "sqlite:///" + str(tmpdir / "dummy.db")
old_engine = config.ENGINE.get()

with config.set(cache_db_urlpath=new_db):
assert config.ENGINE.get() is not old_engine
assert (
str(config.ENGINE.get().url)
== config.SETTINGS.get().cache_db_urlpath
== new_db
)
assert str(config.ENGINE.get().url) == config.get().cache_db_urlpath == new_db
assert config.ENGINE.get() is old_engine
assert (
str(config.ENGINE.get().url) == config.SETTINGS.get().cache_db_urlpath == old_db
)
assert str(config.ENGINE.get().url) == config.get().cache_db_urlpath == old_db

config.set(cache_db_urlpath=new_db)
assert config.ENGINE.get() is not old_engine
assert (
str(config.ENGINE.get().url) == config.SETTINGS.get().cache_db_urlpath == new_db
)
assert str(config.ENGINE.get().url) == config.get().cache_db_urlpath == new_db


def test_expiration() -> None:
with config.set(expiration="1492-10-12T00:00:00"):
assert config.SETTINGS.get().expiration == "1492-10-12T00:00:00"
assert config.get().expiration == "1492-10-12T00:00:00"


def test_env_variables(tmpdir: pathlib.Path) -> None:
Expand All @@ -46,9 +38,9 @@ def test_env_variables(tmpdir: pathlib.Path) -> None:

config.reset(str(dotenv_path))
try:
assert config.SETTINGS.get().cache_db_urlpath == "sqlite://"
assert config.get().cache_db_urlpath == "sqlite://"
assert str(config.ENGINE.get().url) == "sqlite://"
assert config.SETTINGS.get().io_delete_original is True
assert config.get().io_delete_original is True
assert str(config.ENGINE.get().url) == "sqlite://"
finally:
os.environ.clear()
Expand All @@ -62,5 +54,5 @@ def set_tag() -> None:
ctx = contextvars.copy_context()
ctx.run(set_tag)

assert config.SETTINGS.get().tag is None
assert config.get().tag is None
assert ctx[config.SETTINGS].tag == "foo"

0 comments on commit 2b43bef

Please sign in to comment.