1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
import configparser
from dataclasses import dataclass
from datetime import datetime
from decimal import Decimal
from pathlib import Path
import pytest
from mako.testing._config import ConfigValueTypeError
from mako.testing._config import MissingConfig
from mako.testing._config import MissingConfigItem
from mako.testing._config import MissingConfigSection
from mako.testing._config import ReadsCfg
from mako.testing.assertions import assert_raises_message_with_given_cause
from mako.testing.assertions import assert_raises_with_given_cause
PATH_TO_TEST_CONFIG = Path(__file__).parent / "dummy.cfg"
@dataclass
class BasicConfig(ReadsCfg):
int_value: int
bool_value: bool
float_value: float
str_value: str
section_header = "basic_values"
@dataclass
class BooleanConfig(ReadsCfg):
yes: bool
one: bool
true: bool
on: bool
no: bool
zero: bool
false: bool
off: bool
section_header = "boolean_values"
@dataclass
class UnsupportedTypesConfig(ReadsCfg):
decimal_value: Decimal
datetime_value: datetime
section_header = "additional_types"
@dataclass
class SupportedTypesConfig(ReadsCfg):
decimal_value: Decimal
datetime_value: datetime
section_header = "additional_types"
converters = {
Decimal: lambda v: Decimal(str(v)),
datetime: lambda v: datetime.fromisoformat(v),
}
@dataclass
class NonexistentSectionConfig(ReadsCfg):
some_value: str
another_value: str
section_header = "i_dont_exist"
@dataclass
class TypeMismatchConfig(ReadsCfg):
int_value: int
section_header = "type_mismatch"
@dataclass
class MissingItemConfig(ReadsCfg):
present_item: str
missing_item: str
section_header = "missing_item"
class BasicConfigTest:
@pytest.fixture(scope="class")
def config(self):
return BasicConfig.from_cfg_file(PATH_TO_TEST_CONFIG)
def test_coercions(self, config):
assert isinstance(config.int_value, int)
assert isinstance(config.bool_value, bool)
assert isinstance(config.float_value, float)
assert isinstance(config.str_value, str)
def test_values(self, config):
assert config.int_value == 15421
assert config.bool_value == True
assert config.float_value == 14.01
assert config.str_value == "Ceci n'est pas une chaƮne"
def test_error_on_loading_from_nonexistent_file(self):
assert_raises_with_given_cause(
MissingConfig,
FileNotFoundError,
BasicConfig.from_cfg_file,
"./n/o/f/i/l/e/h.ere",
)
def test_error_on_loading_from_nonexistent_section(self):
assert_raises_with_given_cause(
MissingConfigSection,
configparser.NoSectionError,
NonexistentSectionConfig.from_cfg_file,
PATH_TO_TEST_CONFIG,
)
class BooleanConfigTest:
@pytest.fixture(scope="class")
def config(self):
return BooleanConfig.from_cfg_file(PATH_TO_TEST_CONFIG)
def test_values(self, config):
assert config.yes is True
assert config.one is True
assert config.true is True
assert config.on is True
assert config.no is False
assert config.zero is False
assert config.false is False
assert config.off is False
class UnsupportedTypesConfigTest:
@pytest.fixture(scope="class")
def config(self):
return UnsupportedTypesConfig.from_cfg_file(PATH_TO_TEST_CONFIG)
def test_values(self, config):
assert config.decimal_value == "100001.01"
assert config.datetime_value == "2021-12-04 00:05:23.283"
class SupportedTypesConfigTest:
@pytest.fixture(scope="class")
def config(self):
return SupportedTypesConfig.from_cfg_file(PATH_TO_TEST_CONFIG)
def test_values(self, config):
assert config.decimal_value == Decimal("100001.01")
assert config.datetime_value == datetime(2021, 12, 4, 0, 5, 23, 283000)
class TypeMismatchConfigTest:
def test_error_on_load(self):
assert_raises_message_with_given_cause(
ConfigValueTypeError,
"Wrong value type for int_value",
ValueError,
TypeMismatchConfig.from_cfg_file,
PATH_TO_TEST_CONFIG,
)
class MissingItemConfigTest:
def test_error_on_load(self):
assert_raises_message_with_given_cause(
MissingConfigItem,
"No config item for missing_item",
configparser.NoOptionError,
MissingItemConfig.from_cfg_file,
PATH_TO_TEST_CONFIG,
)
|