1
Fork 0
mirror of https://github.com/Steffo99/cfig.git synced 2024-11-21 23:44:21 +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)
assert self.config.sources == cfig.Configuration.DEFAULT_SOURCES
def test_required(self):
@self.config.required()
def FIRST_NUMBER(val: str) -> int: def FIRST_NUMBER(val: str) -> int:
"""The first number to sum.""" """The first number to sum."""
return int(val) return int(val)
@config.optional() assert isinstance(FIRST_NUMBER, lazy_object_proxy.Proxy)
assert callable(FIRST_NUMBER.__factory__)
assert not FIRST_NUMBER.__resolved__
assert self.config.items["FIRST_NUMBER"] is FIRST_NUMBER
assert self.config.docs["FIRST_NUMBER"] == """The first number to sum."""
self.FIRST_NUMBER = FIRST_NUMBER
def test_optional(self):
@self.config.optional()
def SECOND_NUMBER(val: str) -> int: def SECOND_NUMBER(val: str) -> int:
"""The second number to sum.""" """The second number to sum."""
return int(val) return int(val)
# Assert the two configuration items have been registered assert isinstance(SECOND_NUMBER, lazy_object_proxy.Proxy)
assert "FIRST_NUMBER" in config.items assert callable(SECOND_NUMBER.__factory__)
assert "FIRST_NUMBER" in config.docs assert not SECOND_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["SECOND_NUMBER"] is SECOND_NUMBER
assert config.docs["FIRST_NUMBER"] == """The first number to sum.""" assert self.config.docs["SECOND_NUMBER"] == """The second number to sum."""
assert config.docs["SECOND_NUMBER"] == """The second number to sum."""
self.SECOND_NUMBER = SECOND_NUMBER
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")
# Assert that an error is raised if items are fetched without any value set
with pytest.raises(cfig.MissingValueError): with pytest.raises(cfig.MissingValueError):
config.fetch_all() self.config.fetch_all()
# Setup the environment def test_fetch_required(self, monkeypatch):
monkeypatch.setenv("FIRST_NUMBER", "1") monkeypatch.setenv("FIRST_NUMBER", "1")
monkeypatch.setenv("SECOND_NUMBER", "")
# Assert that no error is raised with all required values set assert os.environ.get("FIRST_NUMBER") == "1"
config.fetch_all() assert not os.environ.get("SECOND_NUMBER")
# Assert the two variables have the correct values self.config.fetch_all()
assert FIRST_NUMBER == 1
assert SECOND_NUMBER == None
# Please note that SECOND_NUMBER is not the same instance as None, as it is a lazy object proxy! # noinspection PyUnresolvedReferences
assert SECOND_NUMBER is not None 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