| 1 |
"""Basic unit tests of TypedConfig.""" |
| 2 |
# pylint: disable=import-error |
| 3 |
|
| 4 |
import pathlib |
| 5 |
import unittest |
| 6 |
from dataclasses import dataclass, field |
| 7 |
|
| 8 |
from foomuuri import TypedConfig |
| 9 |
|
| 10 |
|
| 11 |
class TestTypedConfig(unittest.TestCase): |
| 12 |
"""Setters / getters / type conversion tests.""" |
| 13 |
|
| 14 |
def set(self, name, value): |
| 15 |
"""Set attribute and return value helper.""" |
| 16 |
self.config[name] = value |
| 17 |
return self.config[name] |
| 18 |
|
| 19 |
def set_from_str(self, name, value): |
| 20 |
"""Set attribute from string and return value helper.""" |
| 21 |
self.config.set_from_str(name, value) |
| 22 |
return self.config[name] |
| 23 |
|
| 24 |
def append_from_str(self, name, value): |
| 25 |
"""Append attribute value from string and return value helper.""" |
| 26 |
self.config.append_from_str(name, value) |
| 27 |
return self.config[name] |
| 28 |
|
| 29 |
def setUp(self): |
| 30 |
"""Setup tests.""" |
| 31 |
|
| 32 |
@dataclass |
| 33 |
class TestConfig(TypedConfig): |
| 34 |
"""Test class.""" |
| 35 |
|
| 36 |
# pylint: disable=too-many-instance-attributes |
| 37 |
initialized_str: str = 'init_value1' |
| 38 |
uninitialized_str: str = field(init=False) |
| 39 |
initialized_untyped = 'init_value2' # noqa: RUF100,RUF045 |
| 40 |
type_conversion_float: float = 0.0 |
| 41 |
type_conversion_int: int = 0 |
| 42 |
type_conversion_list: list = field(default_factory=list) |
| 43 |
type_conversion_posixpath: pathlib.PosixPath = field(init=False) |
| 44 |
type_conversion_set: set = field(default_factory=set) |
| 45 |
validation: str = field( |
| 46 |
init=False, metadata={'validate': lambda v: v} |
| 47 |
) |
| 48 |
conversion: list = field( |
| 49 |
init=False, metadata={'convert_str': lambda v: v.split()} |
| 50 |
) |
| 51 |
|
| 52 |
self.config = TestConfig() |
| 53 |
|
| 54 |
def test_accessor_style(self): |
| 55 |
"""Test dict/dot style accessor/setter.""" |
| 56 |
self.config.initialized_str = 'new_value1' |
| 57 |
self.assertEqual(self.config.initialized_str, 'new_value1') |
| 58 |
self.assertRaises( |
| 59 |
AttributeError, lambda: self.config.unknown_attribute |
| 60 |
) |
| 61 |
|
| 62 |
self.config['initialized_str'] = 'new_value2' |
| 63 |
self.assertEqual(self.config['initialized_str'], 'new_value2') |
| 64 |
self.assertRaises( |
| 65 |
AttributeError, lambda: self.config['unknown_attribute'] |
| 66 |
) |
| 67 |
|
| 68 |
self.assertRaises(AttributeError, self.set, 'unknown_attribute', '123') |
| 69 |
|
| 70 |
def test_initialized_typed(self): |
| 71 |
"""Test assigning values to initialized typed attributes.""" |
| 72 |
self.assertRaises(TypeError, self.set, 'initialized_str', 123) |
| 73 |
self.assertEqual(self.set('initialized_str', 'value'), 'value') |
| 74 |
|
| 75 |
def test_uninitialized_typed(self): |
| 76 |
"""Test assigning values to uninialized typed attributes.""" |
| 77 |
self.assertRaises( |
| 78 |
AttributeError, lambda: self.config.uninitialized_str |
| 79 |
) |
| 80 |
self.assertRaises(TypeError, self.set, 'uninitialized_str', 123) |
| 81 |
self.assertEqual(self.set('uninitialized_str', 'value'), 'value') |
| 82 |
|
| 83 |
def test_initialized_untyped(self): |
| 84 |
"""Test accessing listed untyped attributes.""" |
| 85 |
self.assertRaises( |
| 86 |
AttributeError, lambda: self.config.uninitialized_untyped |
| 87 |
) |
| 88 |
|
| 89 |
def test_set_from_str(self): |
| 90 |
"""Test attribute type conversion from str (set_from_str).""" |
| 91 |
# Invalid value type: only str supported |
| 92 |
self.assertRaises( |
| 93 |
TypeError, self.config.set_from_str, 'type_conversion_int', 123 |
| 94 |
) |
| 95 |
# Supported conversion, from str to int |
| 96 |
self.assertEqual(self.set_from_str('type_conversion_int', '123'), 123) |
| 97 |
# Supported conversion, from str to pathlib.PosixPath |
| 98 |
self.assertEqual( |
| 99 |
self.set_from_str('type_conversion_posixpath', '/path/'), |
| 100 |
pathlib.PosixPath('/path/'), |
| 101 |
) |
| 102 |
# Supported conversion, from str to list |
| 103 |
self.assertEqual( |
| 104 |
self.set_from_str('type_conversion_list', '1 2 2 3 4'), |
| 105 |
['1', '2', '2', '3', '4'], |
| 106 |
) |
| 107 |
# Supported conversion, from str to set |
| 108 |
self.assertEqual( |
| 109 |
self.set_from_str('type_conversion_set', '1 2 2 3 4'), |
| 110 |
{'1', '2', '3', '4'}, |
| 111 |
) |
| 112 |
# Assignment, str to str |
| 113 |
self.assertEqual(self.set_from_str('initialized_str', '123'), '123') |
| 114 |
# Assignment, list converted from str |
| 115 |
self.assertEqual(self.set_from_str('conversion', '1'), ['1']) |
| 116 |
# Unsupported conversion, from str to float |
| 117 |
self.assertRaises( |
| 118 |
TypeError, |
| 119 |
self.config.set_from_str, |
| 120 |
'type_conversion_float', |
| 121 |
'42.0', |
| 122 |
) |
| 123 |
# Assign non-existing attribute |
| 124 |
self.assertRaises( |
| 125 |
AttributeError, self.config.set_from_str, 'unknownattribute', '123' |
| 126 |
) |
| 127 |
|
| 128 |
def test_append_from_str(self): |
| 129 |
"""Test attribute value append from str (append_from_str).""" |
| 130 |
# Invalid value type: only str supported |
| 131 |
self.assertRaises( |
| 132 |
TypeError, self.config.append_from_str, 'initialized_str', 123 |
| 133 |
) |
| 134 |
# Appending, str to str |
| 135 |
self.assertEqual(self.set_from_str('initialized_str', '1'), '1') |
| 136 |
self.assertEqual(self.append_from_str('initialized_str', '2'), '1 2') |
| 137 |
# Appending, list converted from str to list |
| 138 |
self.assertEqual(self.set_from_str('conversion', '1'), ['1']) |
| 139 |
self.assertEqual(self.append_from_str('conversion', '2'), ['1', '2']) |
| 140 |
# Appending, str to list |
| 141 |
self.assertEqual( |
| 142 |
self.set_from_str('type_conversion_list', '1 2'), |
| 143 |
['1', '2'], |
| 144 |
) |
| 145 |
self.assertEqual( |
| 146 |
self.append_from_str('type_conversion_list', '2 3'), |
| 147 |
['1', '2', '2', '3'], |
| 148 |
) |
| 149 |
# Appending, str to set |
| 150 |
self.assertEqual( |
| 151 |
self.set_from_str('type_conversion_set', '1 2'), |
| 152 |
{'1', '2'}, |
| 153 |
) |
| 154 |
self.assertEqual( |
| 155 |
self.append_from_str('type_conversion_set', '2 3'), |
| 156 |
{'1', '2', '3'}, |
| 157 |
) |
| 158 |
# Unsupported append, from str to int |
| 159 |
self.assertRaises( |
| 160 |
TypeError, self.append_from_str, 'type_conversion_int', '1' |
| 161 |
) |
| 162 |
# Append non-existing attribute |
| 163 |
self.assertRaises( |
| 164 |
AttributeError, self.append_from_str, 'unknownattribute', '123' |
| 165 |
) |
| 166 |
|
| 167 |
def test_validation(self): |
| 168 |
"""Test attribute validation helper call.""" |
| 169 |
self.assertRaises(ValueError, self.set, 'validation', '') |
| 170 |
self.assertEqual(self.set('validation', 'test'), 'test') |
| 171 |
|
| 172 |
def test_iter(self): |
| 173 |
"""Test __iter__.""" |
| 174 |
self.assertEqual( |
| 175 |
sorted(self.config), |
| 176 |
[ |
| 177 |
'conversion', |
| 178 |
'initialized_str', |
| 179 |
'type_conversion_float', |
| 180 |
'type_conversion_int', |
| 181 |
'type_conversion_list', |
| 182 |
'type_conversion_posixpath', |
| 183 |
'type_conversion_set', |
| 184 |
'uninitialized_str', |
| 185 |
'validation', |
| 186 |
], |
| 187 |
) |
| 188 |
|
| 189 |
def test_get(self): |
| 190 |
"""Test get.""" |
| 191 |
self.assertEqual(self.config.get('initialized_str'), 'init_value1') |
| 192 |
self.assertEqual(self.config.get('unknown_attr'), None) |