1
Fork 0
mirror of https://github.com/Steffo99/cfig.git synced 2024-11-24 17:04:19 +00:00

🧪 Improve the test base, but it's not working yet

This commit is contained in:
Steffo 2022-04-15 19:01:20 +02:00
parent b894ecafb4
commit 9e12b0f7a3
Signed by: steffo
GPG key ID: 6965406171929D01

View file

@ -1,43 +1,85 @@
import pytest import pytest
import cfig import cfig
import os
import lazy_object_proxy
def test_config(monkeypatch): class TestConfig:
config = cfig.Configuration() def test_creation(self):
self.config = cfig.Configuration()
@config.required() assert isinstance(self.config, cfig.Configuration)
def FIRST_NUMBER(val: str) -> int: assert self.config.sources == cfig.Configuration.DEFAULT_SOURCES
"""The first number to sum."""
return int(val)
@config.optional() def test_required(self):
def SECOND_NUMBER(val: str) -> int: @self.config.required()
"""The second number to sum.""" def FIRST_NUMBER(val: str) -> int:
return int(val) """The first number to sum."""
return int(val)
# Assert the two configuration items have been registered assert isinstance(FIRST_NUMBER, lazy_object_proxy.Proxy)
assert "FIRST_NUMBER" in config.items assert callable(FIRST_NUMBER.__factory__)
assert "FIRST_NUMBER" in config.docs assert not FIRST_NUMBER.__resolved__
assert "SECOND_NUMBER" in config.items
assert "SECOND_NUMBER" in config.docs
# Assert docstrings are accessible even if the two items aren't assert self.config.items["FIRST_NUMBER"] is FIRST_NUMBER
assert config.docs["FIRST_NUMBER"] == """The first number to sum.""" assert self.config.docs["FIRST_NUMBER"] == """The first number to sum."""
assert config.docs["SECOND_NUMBER"] == """The second number to sum."""
# Assert that an error is raised if items are fetched without any value set self.FIRST_NUMBER = FIRST_NUMBER
with pytest.raises(cfig.MissingValueError):
config.fetch_all()
# Setup the environment def test_optional(self):
monkeypatch.setenv("FIRST_NUMBER", "1") @self.config.optional()
def SECOND_NUMBER(val: str) -> int:
"""The second number to sum."""
return int(val)
# Assert that no error is raised with all required values set assert isinstance(SECOND_NUMBER, lazy_object_proxy.Proxy)
config.fetch_all() assert callable(SECOND_NUMBER.__factory__)
assert not SECOND_NUMBER.__resolved__
# Assert the two variables have the correct values assert self.config.items["SECOND_NUMBER"] is SECOND_NUMBER
assert FIRST_NUMBER == 1 assert self.config.docs["SECOND_NUMBER"] == """The second number to sum."""
assert SECOND_NUMBER == None
# Please note that SECOND_NUMBER is not the same instance as None, as it is a lazy object proxy! self.SECOND_NUMBER = SECOND_NUMBER
assert SECOND_NUMBER is not None
def test_fetch_missing(self, monkeypatch):
monkeypatch.setenv("FIRST_NUMBER", "")
monkeypatch.setenv("SECOND_NUMBER", "")
assert not os.environ.get("FIRST_NUMBER")
assert not os.environ.get("SECOND_NUMBER")
with pytest.raises(cfig.MissingValueError):
self.config.fetch_all()
def test_fetch_required(self, monkeypatch):
monkeypatch.setenv("FIRST_NUMBER", "1")
monkeypatch.setenv("SECOND_NUMBER", "")
assert os.environ.get("FIRST_NUMBER") == "1"
assert not os.environ.get("SECOND_NUMBER")
self.config.fetch_all()
# noinspection PyUnresolvedReferences
assert self.FIRST_NUMBER.__resolved__
assert self.FIRST_NUMBER == 1
# noinspection PyUnresolvedReferences
assert self.SECOND_NUMBER.__resolved__
assert self.SECOND_NUMBER == None
assert self.SECOND_NUMBER is not None
def test_fetch_optional(self, monkeypatch):
monkeypatch.setenv("FIRST_NUMBER", "1")
monkeypatch.setenv("SECOND_NUMBER", "2")
assert os.environ.get("FIRST_NUMBER") == "1"
assert os.environ.get("FIRST_NUMBER") == "2"
self.config.fetch_all()
# noinspection PyUnresolvedReferences
assert self.FIRST_NUMBER.__resolved__
assert self.FIRST_NUMBER == 1
# noinspection PyUnresolvedReferences
assert self.SECOND_NUMBER.__resolved__
assert self.SECOND_NUMBER == 2