Add a minimal coverage unit test for runtime breakage detection
authorPlamen Dimitrov <plamen.dimitrov@intra2net.com>
Tue, 17 May 2022 03:03:10 +0000 (06:03 +0300)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 19 May 2022 09:13:27 +0000 (11:13 +0200)
Since the template fiels are mostly data loaded from templates, the
idea is to at least go through all of them and check for correct
class of the generated cnfvars and for defaults dictionaries making
sure the templates module is not populated with anything else.

test/cnfvar/test_templates.py [new file with mode: 0644]

diff --git a/test/cnfvar/test_templates.py b/test/cnfvar/test_templates.py
new file mode 100644 (file)
index 0000000..67d92fb
--- /dev/null
@@ -0,0 +1,59 @@
+# The software in this package is distributed under the GNU General
+# Public License version 2 (with a special exception described below).
+#
+# A copy of GNU General Public License (GPL) is included in this distribution,
+# in the file COPYING.GPL.
+#
+# As a special exception, if other files instantiate templates or use macros
+# or inline functions from this file, or you compile this file and link it
+# with other works to produce a work based on this file, this file
+# does not by itself cause the resulting work to be covered
+# by the GNU General Public License.
+#
+# However the source code for this file must still be made available
+# in accordance with section (3) of the GNU General Public License.
+#
+# This exception does not invalidate any other reasons why a work based
+# on this file might be covered by the GNU General Public License.
+#
+# Copyright (c) 2016-2022 Intra2net AG <info@intra2net.com>
+
+"""
+test_templates.py: unit tests for cnfvar/templates.py.
+
+Tests functions in cnfvar/templates.py
+
+For help see :py:mod:`unittest`
+"""
+
+import unittest
+from textwrap import dedent
+from types import SimpleNamespace
+from unittest.mock import patch, ANY
+
+from src.cnfvar import templates
+from src.cnfvar.model import Cnf
+
+
+class CnfTemplatesTest(unittest.TestCase):
+    """Test functions in cnfvar.templates."""
+
+    def test_template_calls(self):
+        """Tests calling each template to detect runtime breakage."""
+        template_cnf = templates.template("name", "value", instance=1, defaults={})
+        self.assertTrue(isinstance(template_cnf, Cnf))
+        attr_dict = templates.__dict__
+        for attr in attr_dict.keys():
+            if attr in ["Cnf", "CnfList"]:
+                continue
+            if attr == "template":
+                continue
+            if callable(attr_dict[attr]):
+                template_cnf = attr_dict[attr]("name", instance=1)
+                self.assertTrue(isinstance(template_cnf, Cnf))
+            elif attr.endswith("_defaults"):
+                self.assertTrue(isinstance(attr_dict[attr], dict))
+
+
+if __name__ == '__main__':
+    unittest.main()