feat: create libretime_shared package (#1349)

* feat: create libretime_shared package

- We don't use pydantic.BaseSettings because of some
  incompatble loading behavior.

* fix: whitelist pydantic in pylintrc

* docs: update to new BaseConfig behavior

* docs: change confusing config filepath
This commit is contained in:
Jonas L 2022-01-06 14:40:52 +01:00 committed by GitHub
parent ba8b51af76
commit 3a615cafa0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 568 additions and 0 deletions

View file

@ -0,0 +1,72 @@
from os import environ
from unittest import mock
from pytest import mark, raises
from libretime_shared.config import BaseConfig, Database
# pylint: disable=too-few-public-methods
class FixtureConfig(BaseConfig):
api_key: str
database: Database
FIXTURE_CONFIG_RAW = """
api_key: "f3bf04fc"
# Comment !
database:
host: "localhost"
port: 5672
ignored: "ignored"
"""
def test_base_config(tmpdir):
config_filepath = tmpdir.join("config.yml")
config_filepath.write(FIXTURE_CONFIG_RAW)
with mock.patch.dict(
environ,
dict(
LIBRETIME_API="invalid",
LIBRETIME_API_KEY="f3bf04fc",
LIBRETIME_DATABASE="invalid",
LIBRETIME_DATABASE_PORT="8888",
WRONGPREFIX_API_KEY="invalid",
),
):
config = FixtureConfig(filepath=config_filepath)
assert config.api_key == "f3bf04fc"
assert config.database.host == "localhost"
assert config.database.port == 8888
FIXTURE_CONFIG_RAW_MISSING = """
database:
host: "localhost"
"""
FIXTURE_CONFIG_RAW_INVALID = """
database
host: "localhost"
"""
@mark.parametrize(
"raw,exception",
[
(FIXTURE_CONFIG_RAW_INVALID, SystemExit),
(FIXTURE_CONFIG_RAW_MISSING, SystemExit),
],
)
def test_load_config_error(tmpdir, raw, exception):
config_filepath = tmpdir.join("config.yml")
config_filepath.write(raw)
with raises(exception):
with mock.patch.dict(environ, {}):
FixtureConfig(filepath=config_filepath)

View file

@ -0,0 +1,47 @@
from pathlib import Path
import pytest
from loguru import logger
from libretime_shared.logging import (
create_task_logger,
level_from_verbosity,
setup_logger,
)
@pytest.mark.parametrize(
"verbosity,level_name,level_no",
[
(-100, "error", 40),
(-1, "error", 40),
(0, "warning", 30),
(1, "info", 20),
(2, "debug", 10),
(3, "trace", 5),
(100, "trace", 5),
],
)
def test_level_from_verbosity(verbosity, level_name, level_no):
level = level_from_verbosity(verbosity)
assert level.name == level_name
assert level.no == level_no
def test_setup_logger(tmp_path: Path):
log_filepath = tmp_path / "test.log"
extra_log_filepath = tmp_path / "extra.log"
setup_logger(1, log_filepath)
extra_logger = create_task_logger(2, extra_log_filepath, True)
logger.info("test info")
extra_logger.info("extra info")
logger.debug("test debug")
extra_logger.complete()
logger.complete()
assert len(log_filepath.read_text().splitlines()) == 1
assert len(extra_log_filepath.read_text().splitlines()) == 1