add email to Person.all_names and Config.all_names
[utils.git] / src / budget_sync / test / test_config.py
index 6c27899f55ebb1302c8ec89643ef554b0702b922..156aa392641d18021514b1ba2facc1b6b61264bf 100644 (file)
@@ -1,4 +1,5 @@
 import unittest
+import io
 from budget_sync.config import Config, ConfigParseError
 
 
@@ -12,7 +13,346 @@ class TestConfig(unittest.TestCase):
         def check(text: str, expected_repr_text: str):
             self.assertEqual(repr(Config.from_str(text)), expected_repr_text)
 
-        raise NotImplementedError("finish adding test cases")
+        check_error(
+            "bad-toml=",
+            "TOML parse error: Empty value is invalid "
+            "(line 1 column 1 char 0)")
+        check_error(
+            """
+            """,
+            "`bugzilla_url` field is missing")
+        check_error(
+            """
+            [bugzilla_url]
+            """,
+            "`bugzilla_url` must be a string")
+        check_error(
+            """
+            bugzilla_url = ""
+            """,
+            "`people` table is missing")
+        check_error(
+            """
+            blah = ""
+            """,
+            "unknown config entry: `blah`")
+        check_error(
+            """
+            bugzilla_url = ""
+            people = []
+            """,
+            "`people` field must be a table")
+        check_error(
+            """
+            bugzilla_url = ""
+            [people]
+            person1 = 1
+            """,
+            "person entry for 'person1' must be a table")
+        check_error(
+            """
+            bugzilla_url = ""
+            [people."person1"]
+            aliases = ""
+            """,
+            "`aliases` field in person entry for 'person1' must be a list "
+            "of strings")
+        check_error(
+            """
+            bugzilla_url = ""
+            [people."person1"]
+            aliases = [1]
+            """,
+            "`aliases` field in person entry for 'person1' must be a list "
+            "of strings")
+        check_error(
+            """
+            bugzilla_url = ""
+            [people."person1"]
+            aliases = ["a", "a"]
+            """,
+            "duplicate alias in person entry for 'person1': 'a'")
+        check_error(
+            """
+            bugzilla_url = ""
+            [people]
+            """,
+            "`milestones` table is missing")
+        check_error(
+            """
+            bugzilla_url = ""
+            [people."person1"]
+            """,
+            "`output_markdown_file` field is missing in person entry for "
+            "'person1'")
+        check_error(
+            """
+            bugzilla_url = ""
+            [people."person1"]
+            output_markdown_file = 1
+            """,
+            "`output_markdown_file` field in person entry for 'person1' must "
+            "be a string")
+        check(
+            """
+            bugzilla_url = ""
+            [milestones]
+            [people."person1"]
+            aliases = ["a"]
+            output_markdown_file = "person1.mdwn"
+            [people."person2"]
+            aliases = ["b"]
+            output_markdown_file = "person2.mdwn"
+            """,
+            "Config(bugzilla_url='', people={"
+            "'person1': Person(config=..., identifier='person1', "
+            "output_markdown_file='person1.mdwn', "
+            "aliases={'a'}, email=None), "
+            "'person2': Person(config=..., identifier='person2', "
+            "output_markdown_file='person2.mdwn', "
+            "aliases={'b'}, email=None)}, milestones={})")
+        check_error(
+            """
+            bugzilla_url = ""
+            [people."person1"]
+            email = 123
+            output_markdown_file = "person1.mdwn"
+            """,
+            "`email` field in person entry for 'person1' must be a string")
+        check(
+            """
+            bugzilla_url = ""
+            [people]
+            [milestones]
+            """,
+            "Config(bugzilla_url='', people={}, milestones={})")
+        check(
+            """
+            bugzilla_url = ""
+            [milestones]
+            [people."person1"]
+            email = "email@example.com"
+            output_markdown_file = "person1.mdwn"
+            """,
+            "Config(bugzilla_url='', people={"
+            "'person1': Person(config=..., identifier='person1', "
+            "output_markdown_file='person1.mdwn', "
+            "aliases=set(), email='email@example.com')}, milestones={})")
+        check_error(
+            """
+            bugzilla_url = ""
+            [people."person1"]
+            blah = 123
+            output_markdown_file = "person1.mdwn"
+            """,
+            "unknown field in person entry for 'person1': `blah`")
+        check_error(
+            """
+            bugzilla_url = ""
+            [milestones]
+            [people."person1"]
+            output_markdown_file = "person1.mdwn"
+            [people."person2"]
+            aliases = ["person1"]
+            output_markdown_file = "person2.mdwn"
+            """,
+            "alias is not allowed to be the same as any person's identifier: "
+            "in person entry for 'person2': 'person1' is also the identifier "
+            "for person 'person1'")
+        check_error(
+            """
+            bugzilla_url = ""
+            [milestones]
+            [people."person1"]
+            output_markdown_file = "person1.mdwn"
+            aliases = ["a"]
+            [people."person2"]
+            aliases = ["a"]
+            output_markdown_file = "person2.mdwn"
+            """,
+            "alias is not allowed to be the same as another person's alias or "
+            "email: in person entry for 'person2': 'a' is also an alias or "
+            "email for person 'person1'")
+        check_error(
+            """
+            bugzilla_url = ""
+            [milestones]
+            [people."person1"]
+            output_markdown_file = "person1.mdwn"
+            aliases = ["abc@example.com"]
+            [people."person2"]
+            email = "abc@example.com"
+            output_markdown_file = "person2.mdwn"
+            """,
+            "email is not allowed to be the same as another person's alias or "
+            "email: in person entry for 'person2': 'abc@example.com' is also "
+            "an alias or email for person 'person1'")
+        check_error(
+            """
+            bugzilla_url = ""
+            [milestones]
+            [people."person2"]
+            email = "abc@example.com"
+            output_markdown_file = "person2.mdwn"
+            [people."person1"]
+            output_markdown_file = "person1.mdwn"
+            aliases = ["abc@example.com"]
+            """,
+            "alias is not allowed to be the same as another person's alias or "
+            "email: in person entry for 'person1': 'abc@example.com' is also "
+            "an alias or email for person 'person2'")
+        check_error(
+            """
+            bugzilla_url = ""
+            [milestones]
+            "abc" = 1
+            [people]
+            """,
+            "milestones entry for 'abc' must be a table")
+        check_error(
+            """
+            bugzilla_url = ""
+            [milestones]
+            "abc" = { canonical_bug_id = "abc" }
+            [people]
+            """,
+            "`canonical_bug_id` field in milestones entry for 'abc' must "
+            "be an integer")
+        check_error(
+            """
+            bugzilla_url = ""
+            [milestones]
+            "abc" = { blah = "def" }
+            [people]
+            """,
+            "unknown field in milestones entry for 'abc': `blah`")
+        check_error(
+            """
+            bugzilla_url = ""
+            [milestones]
+            "abc" = {}
+            [people]
+            """,
+            "`canonical_bug_id` field is missing in milestones entry for 'abc'")
+        check_error(
+            """
+            bugzilla_url = ""
+            milestones = 1
+            [people]
+            """,
+            "`milestones` field must be a table")
+        check_error(
+            """
+            bugzilla_url = ""
+            [milestones]
+            "abc" = { canonical_bug_id = 1 }
+            "def" = { canonical_bug_id = 1 }
+            [people]
+            """,
+            "canonical_bug_id is not allowed to be the same as another "
+            "milestone's canonical_bug_id: in milestone entry for 'def': "
+            "1 is also the canonical_bug_id for milestone 'abc'")
+
+    def test_all_names(self):
+        config = Config.from_str(
+            """
+            bugzilla_url = ""
+            [milestones]
+            [people."person1"]
+            aliases = ["person1_alias1", "alias1"]
+            output_markdown_file = "person1.mdwn"
+            [people."person2"]
+            aliases = ["person2_alias2", "alias2"]
+            output_markdown_file = "person2.mdwn"
+            """)
+        person1 = config.people['person1']
+        person2 = config.people['person2']
+        self.assertEqual(config.all_names,
+                         {
+                             'person1': person1,
+                             'person1_alias1': person1,
+                             'alias1': person1,
+                             'person2': person2,
+                             'person2_alias2': person2,
+                             'alias2': person2,
+                         })
+
+    def test_canonical_bug_ids(self):
+        config = Config.from_str(
+            """
+            bugzilla_url = ""
+            [people]
+            [milestones]
+            "Milestone 1" = { canonical_bug_id = 1 }
+            "Milestone 2" = { canonical_bug_id = 2 }
+            """)
+        milestone1 = config.milestones['Milestone 1']
+        milestone2 = config.milestones['Milestone 2']
+        self.assertEqual(config.canonical_bug_ids,
+                         {
+                             1: milestone1,
+                             2: milestone2,
+                         })
+
+    def test_bugzilla_url_stripped(self):
+        c = Config.from_str(
+            """
+            bugzilla_url = "https://bugzilla.example.com/prefix"
+            [people]
+            [milestones]
+            """
+        )
+        self.assertEqual(c.bugzilla_url_stripped,
+                         "https://bugzilla.example.com/prefix")
+        c = Config.from_str(
+            """
+            bugzilla_url = "https://bugzilla.example.com/prefix/"
+            [people]
+            [milestones]
+            """
+        )
+        self.assertEqual(c.bugzilla_url_stripped,
+                         "https://bugzilla.example.com/prefix")
+        c = Config.from_str(
+            """
+            bugzilla_url = "https://bugzilla.example.com/"
+            [people]
+            [milestones]
+            """
+        )
+        self.assertEqual(c.bugzilla_url_stripped,
+                         "https://bugzilla.example.com")
+
+    def test_from_file(self):
+        def load(text):
+            with io.StringIO(text) as file:
+                return Config.from_file(file)
+
+        with self.assertRaisesRegex(TypeError,
+                                    "^list is not a valid file or path$"):
+            Config.from_file([])
+
+        with self.assertRaisesRegex(
+                ConfigParseError,
+                "^TOML parse error: Empty value is invalid"):
+            load("""bad-toml=""")
+
+        self.assertEqual(str(load(
+            """
+            bugzilla_url = "https://bugzilla.example.com/"
+            [people."person1"]
+            email = "person1@example.com"
+            aliases = ["alias1"]
+            output_markdown_file = "person1.mdwn"
+            [milestones]
+            "Milestone 1" = { canonical_bug_id = 123 }
+            """)),
+            "Config(bugzilla_url='https://bugzilla.example.com/', "
+            "people={'person1': Person(config=..., identifier='person1', "
+            "output_markdown_file='person1.mdwn', "
+            "aliases={'alias1'}, email='person1@example.com')}, "
+            "milestones={'Milestone 1': Milestone(config=..., "
+            "identifier='Milestone 1', canonical_bug_id=123)})")
 
 
 if __name__ == "__main__":