diff tests/test.py @ 647:df58983f28a2

Allow to disable the internal caching in configmix. Also allow to re-enable internal caching.
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 11 Mar 2022 01:53:08 +0100
parents db3ff4fbb4ce
children fe1299825a9a
line wrap: on
line diff
--- a/tests/test.py	Mon Mar 07 09:11:22 2022 +0100
+++ b/tests/test.py	Fri Mar 11 01:53:08 2022 +0100
@@ -439,10 +439,31 @@
                          os.path.join(TESTDATADIR, "conf24.toml"))
         x = cfg.interpolate_variables("{{intl.cache.items|Empty}}")
         self.assertEqual(10, x)
+        x = cfg.interpolate_variables("{{intl.cache.items|Empty}}")
+        self.assertEqual(10, x)
+
+    def test12a_Empty_filter_pass_through_without_cache(self):
+        cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml"),
+                         os.path.join(TESTDATADIR, "conf21.yml"),
+                         os.path.join(TESTDATADIR, "conf22.ini"),
+                         os.path.join(TESTDATADIR, "conf23.json"),
+                         os.path.join(TESTDATADIR, "conf24.toml"))
+        cfg.disable_cache()
+        x = cfg.interpolate_variables("{{intl.cache.items|Empty}}")
+        self.assertEqual(10, x)
+        x = cfg.interpolate_variables("{{intl.cache.items|Empty}}")
+        self.assertEqual(10, x)
 
     def test13_keyerror(self):
         cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml"))
         self.assertRaises(KeyError, cfg.getvar_s, "non.existing.key")
+        self.assertRaises(KeyError, cfg.getvar_s, "non.existing.key")        
+
+    def test13a_keyerror_without_cache(self):
+        cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml"))
+        cfg.disable_cache()
+        self.assertRaises(KeyError, cfg.getvar_s, "non.existing.key")
+        self.assertRaises(KeyError, cfg.getvar_s, "non.existing.key")
 
     def test14_getvar_with_default(self):
         cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml"))
@@ -458,6 +479,16 @@
         cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml"))
         self.assertEqual("999", cfg.getvar_s("non.existing.key",
                                              default="999"))
+        self.assertEqual("999", cfg.getvar_s("non.existing.key",
+                                             default="999"))
+
+    def test15a_getvar_s_with_default_without_cache(self):
+        cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml"))
+        cfg.disable_cache()
+        self.assertEqual("999", cfg.getvar_s("non.existing.key",
+                                             default="999"))
+        self.assertEqual("999", cfg.getvar_s("non.existing.key",
+                                             default="999"))
 
     def test15_getvar_s_with_original_default(self):
         # The default must be the original and not a copy
@@ -534,6 +565,20 @@
                                                "intl.fallback",
                                                default=None))
 
+    def test20a_getfirstvar_existing_without_cache(self):
+        cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml"))
+        cfg.disable_cache()
+        self.assertEqual("test-configmix", cfg.getfirstvar("intl.domain"))
+        self.assertEqual("test-configmix", cfg.getfirstvar("intl.domain",
+                                                           "intl.fallback"))
+        self.assertEqual("de", cfg.getfirstvar("intl.fallback",
+                                               "intl.domain",
+                                               default=None))
+
+        self.assertEqual("de", cfg.getfirstvar("intl.non.existing",
+                                               "intl.fallback",
+                                               default=None))
+
     def test21_getfirstvar_s_existing(self):
         cfg = self._load(
             os.path.join(TESTDATADIR, "conf20.yml"),
@@ -558,6 +603,20 @@
             "intl.non.existing",
             "intl.non.existing2")
 
+    def test22a_getfirstvar_s_non_existing_without_cache(self):
+        cfg = self._load(
+            os.path.join(TESTDATADIR, "conf20.yml"),
+            os.path.join(TESTDATADIR, "conf21.yml"))
+        cfg.disable_cache()
+        self.assertIsNone(
+            cfg.getfirstvar_s("intl.non.existing", "intl.non.existing2",
+                              default=None))
+        self.assertRaises(
+            KeyError,
+            cfg.getfirstvar_s,
+            "intl.non.existing",
+            "intl.non.existing2")
+
     def test23_getfirstintvar_s_nonexisting(self):
         cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml"))
         self.assertIsNone(cfg.getfirstintvar_s("db.non.existing.key",
@@ -868,6 +927,16 @@
         cfg = self._load(os.path.join(TESTDATADIR, "conf10.py"))
         cfg.clear_cache()
 
+    def test46_reenable_cache(self):
+        cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml"))
+        self.assertTrue(cfg.getvarl() is cfg)        
+        cfg.disable_cache()
+        self.assertTrue(cfg.getvarl() is cfg)        
+        cfg.clear_cache()
+        self.assertTrue(cfg.getvarl() is cfg)        
+        cfg.enable_cache()
+        self.assertTrue(cfg.getvarl() is cfg)
+
 
 class T02LoadAndMerge(_T02MixinLoadAndMerge, unittest.TestCase):