3 from budget_sync
.config
import Config
, ConfigParseError
6 class TestConfig(unittest
.TestCase
):
7 def test_config_parsing(self
):
8 def check_error(text
: str, expected_error_text
: str):
9 with self
.assertRaises(ConfigParseError
) as e
:
11 self
.assertEqual(str(e
.exception
), expected_error_text
)
13 def check(text
: str, expected_repr_text
: str):
14 self
.assertEqual(repr(Config
.from_str(text
)), expected_repr_text
)
18 "TOML parse error: Empty value is invalid "
19 "(line 1 column 1 char 0)")
23 "`bugzilla_url` field is missing")
28 "`bugzilla_url` must be a string")
33 "`people` table is missing")
38 "unknown config entry: `blah`")
44 "`people` field must be a table")
51 "person entry for 'person1' must be a table")
58 "`aliases` field in person entry for 'person1' must be a list "
66 "`aliases` field in person entry for 'person1' must be a list "
74 "duplicate alias in person entry for 'person1': 'a'")
83 "Config(bugzilla_url='', people={"
84 "'person1': Person(config=..., identifier='person1', "
85 "aliases={'a'}, email=None), "
86 "'person2': Person(config=..., identifier='person2', "
87 "aliases={'b'}, email=None)})")
94 "`email` field in person entry for 'person1' must be a string")
100 "Config(bugzilla_url='', people={})")
105 email = "email@example.com"
107 "Config(bugzilla_url='', people={"
108 "'person1': Person(config=..., identifier='person1', "
109 "aliases=set(), email='email@example.com')})")
116 "unknown field in person entry for 'person1': `blah`")
122 aliases = ["person1"]
124 "alias is not allowed to be the same as any person's identifier: "
125 "in person entry for 'person2': 'person1' is also the identifier "
126 "for person 'person1'")
135 "alias is not allowed to be the same as another person's alias: "
136 "in person entry for 'person2': 'a' is also an alias for person "
139 def test_all_names(self
):
140 config
= Config
.from_str(
144 aliases = ["person1_alias1", "alias1"]
146 aliases = ["person2_alias2", "alias2"]
148 person1
= config
.people
['person1']
149 person2
= config
.people
['person2']
150 self
.assertEqual(config
.all_names
,
153 'person1_alias1': person1
,
156 'person2_alias2': person2
,
160 def test_from_file(self
):
162 with io
.StringIO(text
) as file:
163 return Config
.from_file(file)
165 with self
.assertRaisesRegex(TypeError,
166 "^list is not a valid file or path$"):
169 with self
.assertRaisesRegex(
171 "^TOML parse error: Empty value is invalid"):
172 load("""bad-toml=""")
175 if __name__
== "__main__":