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'")
80 "`milestones` table is missing")
90 "Config(bugzilla_url='', people={"
91 "'person1': Person(config=..., identifier='person1', "
92 "aliases={'a'}, email=None), "
93 "'person2': Person(config=..., identifier='person2', "
94 "aliases={'b'}, email=None)}, milestones={})")
101 "`email` field in person entry for 'person1' must be a string")
108 "Config(bugzilla_url='', people={}, milestones={})")
114 email = "email@example.com"
116 "Config(bugzilla_url='', people={"
117 "'person1': Person(config=..., identifier='person1', "
118 "aliases=set(), email='email@example.com')}, milestones={})")
125 "unknown field in person entry for 'person1': `blah`")
132 aliases = ["person1"]
134 "alias is not allowed to be the same as any person's identifier: "
135 "in person entry for 'person2': 'person1' is also the identifier "
136 "for person 'person1'")
146 "alias is not allowed to be the same as another person's alias: "
147 "in person entry for 'person2': 'a' is also an alias for person "
156 "milestones entry for 'abc' must be a table")
161 "abc" = { canonical_bug_id = "abc" }
164 "`canonical_bug_id` field in milestones entry for 'abc' must "
170 "abc" = { blah = "def" }
173 "unknown field in milestones entry for 'abc': `blah`")
181 "`canonical_bug_id` field is missing in milestones entry for 'abc'")
188 "`milestones` field must be a table")
193 "abc" = { canonical_bug_id = 1 }
194 "def" = { canonical_bug_id = 1 }
197 "canonical_bug_id is not allowed to be the same as another "
198 "milestone's canonical_bug_id: in milestone entry for 'def': "
199 "1 is also the canonical_bug_id for milestone 'abc'")
201 def test_all_names(self
):
202 config
= Config
.from_str(
207 aliases = ["person1_alias1", "alias1"]
209 aliases = ["person2_alias2", "alias2"]
211 person1
= config
.people
['person1']
212 person2
= config
.people
['person2']
213 self
.assertEqual(config
.all_names
,
216 'person1_alias1': person1
,
219 'person2_alias2': person2
,
223 def test_canonical_bug_ids(self
):
224 config
= Config
.from_str(
229 "Milestone 1" = { canonical_bug_id = 1 }
230 "Milestone 2" = { canonical_bug_id = 2 }
232 milestone1
= config
.milestones
['Milestone 1']
233 milestone2
= config
.milestones
['Milestone 2']
234 self
.assertEqual(config
.canonical_bug_ids
,
240 def test_from_file(self
):
242 with io
.StringIO(text
) as file:
243 return Config
.from_file(file)
245 with self
.assertRaisesRegex(TypeError,
246 "^list is not a valid file or path$"):
249 with self
.assertRaisesRegex(
251 "^TOML parse error: Empty value is invalid"):
252 load("""bad-toml=""")
254 self
.assertEqual(str(load(
256 bugzilla_url = "https://bugzilla.example.com/"
258 email = "person1@example.com"
261 "Milestone 1" = { canonical_bug_id = 123 }
263 "Config(bugzilla_url='https://bugzilla.example.com/', "
264 "people={'person1': Person(config=..., identifier='person1', "
265 "aliases={'alias1'}, email='person1@example.com')}, "
266 "milestones={'Milestone 1': Milestone(config=..., "
267 "identifier='Milestone 1', canonical_bug_id=123)})")
270 if __name__
== "__main__":