3 from budget_sync
.config
import Config
, ConfigParseError
6 class TestConfig(unittest
.TestCase
):
9 def test_config_parsing(self
):
10 def check_error(text
: str, expected_error_text
: str):
11 with self
.assertRaises(ConfigParseError
) as e
:
13 self
.assertEqual(str(e
.exception
), expected_error_text
)
15 def check(text
: str, expected_repr_text
: str):
16 self
.assertEqual(repr(Config
.from_str(text
)), expected_repr_text
)
20 "TOML parse error: Empty value is invalid "
21 "(line 1 column 1 char 0)")
25 "`bugzilla_url` field is missing")
30 "`bugzilla_url` must be a string")
35 "`people` table is missing")
40 "unknown config entry: `blah`")
46 "`people` field must be a table")
53 "person entry for 'person1' must be a table")
60 "`aliases` field in person entry for 'person1' must be a list "
68 "`aliases` field in person entry for 'person1' must be a list "
76 "duplicate alias in person entry for 'person1': 'a'")
82 "`milestones` table is missing")
88 "`full_name` field is missing in person entry for "
96 "`full_name` field in person entry for 'person1' must "
104 full_name = "Person One"
107 full_name = "Person Two"
109 "Config(bugzilla_url='', people={"
110 "'person1': Person(config=..., identifier='person1', "
111 "full_name='Person One', "
112 "aliases=OrderedSet(['a']), email=None), "
113 "'person2': Person(config=..., identifier='person2', "
114 "full_name='Person Two', "
115 "aliases=OrderedSet(['b']), email=None)}, milestones={})")
121 full_name = "Person One"
123 "`email` field in person entry for 'person1' must be a string")
130 "Config(bugzilla_url='', people={}, milestones={})")
136 email = "email@example.com"
137 full_name = "Person One"
139 "Config(bugzilla_url='', people={"
140 "'person1': Person(config=..., identifier='person1', "
141 "full_name='Person One', "
142 "aliases=OrderedSet(), email='email@example.com')}, "
149 full_name = "Person One"
151 "unknown field in person entry for 'person1': `blah`")
157 full_name = "Person One"
159 aliases = ["person1"]
160 full_name = "Person Two"
162 "alias is not allowed to be the same as any person's identifier: "
163 "in person entry for 'person2': 'person1' is also the identifier "
164 "for person 'person1'")
170 full_name = "Person One"
174 full_name = "Person Two"
176 "alias is not allowed to be the same as another person's alias, "
177 "email, or full_name: in person entry for 'person2': 'a' is also an alias, "
178 "email, or full_name for person 'person1'")
184 full_name = "Person One"
185 aliases = ["abc@example.com"]
187 email = "abc@example.com"
188 full_name = "Person Two"
190 "email is not allowed to be the same as another person's alias, "
191 "email, or full_name: in person entry for 'person2': 'abc@example.com' is also "
192 "an alias, email, or full_name for person 'person1'")
198 full_name = "Person One"
199 aliases = ["Person Two"]
201 email = "abc@example.com"
202 full_name = "Person Two"
204 "full_name is not allowed to be the same as another person's alias, "
205 "email, or full_name: in person entry for 'person2': 'Person Two' is also "
206 "an alias, email, or full_name for person 'person1'")
212 email = "abc@example.com"
213 full_name = "Person Two"
215 full_name = "Person One"
216 aliases = ["abc@example.com"]
218 "alias is not allowed to be the same as another person's alias, "
219 "email, or full_name: in person entry for 'person1': 'abc@example.com' is also "
220 "an alias, email, or full_name for person 'person2'")
228 "milestones entry for 'abc' must be a table")
233 "abc" = { canonical_bug_id = "abc" }
236 "`canonical_bug_id` field in milestones entry for 'abc' must "
242 "abc" = { blah = "def" }
245 "unknown field in milestones entry for 'abc': `blah`")
253 "`canonical_bug_id` field is missing in milestones entry for 'abc'")
260 "`milestones` field must be a table")
265 "abc" = { canonical_bug_id = 1 }
266 "def" = { canonical_bug_id = 1 }
269 "canonical_bug_id is not allowed to be the same as another "
270 "milestone's canonical_bug_id: in milestone entry for 'def': "
271 "1 is also the canonical_bug_id for milestone 'abc'")
273 def test_all_names(self
):
274 config
= Config
.from_str(
279 aliases = ["person1_alias1", "alias1"]
280 full_name = "Person One"
282 aliases = ["person2_alias2", "alias2"]
283 full_name = "Person Two"
285 person1
= config
.people
['person1']
286 person2
= config
.people
['person2']
287 self
.assertEqual(config
.all_names
,
290 'Person One': person1
,
291 'person1_alias1': person1
,
294 'Person Two': person2
,
295 'person2_alias2': person2
,
299 def test_canonical_bug_ids(self
):
300 config
= Config
.from_str(
305 "Milestone 1" = { canonical_bug_id = 1 }
306 "Milestone 2" = { canonical_bug_id = 2 }
308 milestone1
= config
.milestones
['Milestone 1']
309 milestone2
= config
.milestones
['Milestone 2']
310 self
.assertEqual(config
.canonical_bug_ids
,
316 def test_bugzilla_url_stripped(self
):
319 bugzilla_url = "https://bugzilla.example.com/prefix"
324 self
.assertEqual(c
.bugzilla_url_stripped
,
325 "https://bugzilla.example.com/prefix")
328 bugzilla_url = "https://bugzilla.example.com/prefix/"
333 self
.assertEqual(c
.bugzilla_url_stripped
,
334 "https://bugzilla.example.com/prefix")
337 bugzilla_url = "https://bugzilla.example.com/"
342 self
.assertEqual(c
.bugzilla_url_stripped
,
343 "https://bugzilla.example.com")
345 def test_from_file(self
):
347 with io
.StringIO(text
) as file:
348 return Config
.from_file(file)
350 with self
.assertRaisesRegex(TypeError,
351 "^list is not a valid file or path$"):
354 with self
.assertRaisesRegex(
356 "^TOML parse error: Empty value is invalid"):
357 load("""bad-toml=""")
359 self
.assertEqual(str(load(
361 bugzilla_url = "https://bugzilla.example.com/"
363 email = "person1@example.com"
365 full_name = "Person One"
367 "Milestone 1" = { canonical_bug_id = 123 }
369 "Config(bugzilla_url='https://bugzilla.example.com/', "
370 "people={'person1': Person(config=..., identifier='person1', "
371 "full_name='Person One', "
372 "aliases=OrderedSet(['alias1']), email='person1@example.com')}, "
373 "milestones={'Milestone 1': Milestone(config=..., "
374 "identifier='Milestone 1', canonical_bug_id=123)})")
377 if __name__
== "__main__":