Mercurial > hgrepos > Python > libs > ConfigMix
annotate tests/test.py @ 104:d9f9a06e0c49
Make a better error message for "TypeError" exceptions when looking up variables.
Otherwise -- if a key exists but if it's not a dict as expected a TypeError is throws that nobody does understand.
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Fri, 23 Mar 2018 17:25:21 +0100 |
| parents | 1b4d95f60650 |
| children | 1c2f8a96dec2 |
| rev | line source |
|---|---|
| 93 | 1 # -*- coding: utf-8 -*- |
| 2 | |
| 3 import sys | |
| 4 import os | |
| 5 import unittest | |
| 6 | |
| 7 sys.path.insert( | |
| 8 0, | |
| 9 os.path.abspath( | |
| 10 os.path.normpath(os.path.join(os.path.dirname(__file__), "..")))) | |
| 11 | |
| 12 import configmix | |
| 13 import configmix.ini | |
| 14 import configmix.yaml | |
| 15 import configmix.py | |
| 16 from configmix.compat import u | |
| 17 | |
| 18 | |
| 19 TESTDATADIR = os.path.join( | |
| 20 os.path.abspath(os.path.dirname(__file__)), | |
| 21 "data") | |
| 22 | |
| 23 | |
| 24 class T01Basic(unittest.TestCase): | |
| 25 | |
| 26 """Check with low-level internal interfaces""" | |
| 27 | |
| 28 def __check_types(self, cfg): | |
| 29 self.assertEqual(u("the value"), | |
| 30 cfg.get("key1")) | |
| 31 self.assertTrue(isinstance(cfg.get("key1"), type(u('')))) | |
| 32 self.assertEqual(2, cfg.get("key2")) | |
| 33 self.assertEqual(5.7, cfg.get("key3")) | |
| 34 self.assertTrue(isinstance(cfg.get("key1"), type(u('')))) | |
| 35 self.assertTrue(cfg.get("key4")) | |
| 36 self.assertTrue(isinstance(cfg.get("key4"), bool)) | |
| 37 self.assertFalse(cfg.get("key5")) | |
| 38 self.assertTrue(isinstance(cfg.get("key5"), bool)) | |
| 95 | 39 self.assertEqual(255, cfg.get("key6")) |
| 93 | 40 |
|
97
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
41 def __check_tree(self, cfg): |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
42 self.assertEqual(u("in the root namespace"), |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
43 cfg.get("key1")) |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
44 self.assertEqual(u("in the root namespace -- too"), |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
45 cfg.get("key2")) |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
46 self.assertEqual(32, |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
47 cfg["tree1"]["key3"]) |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
48 self.assertEqual(u("get this as `tree1.tree2.key4'"), |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
49 cfg["tree1"]["tree2"]["key4"]) |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
50 self.assertTrue(cfg["tree1"]["tree2"]["key5"]) |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
51 |
| 93 | 52 def test01_ini_types(self): |
| 53 cfg = configmix.ini.load(os.path.join(TESTDATADIR, "conf1.ini")) | |
| 54 self.__check_types(cfg) | |
| 55 | |
| 56 def test02_py_types(self): | |
| 57 cfg = configmix.py.load(os.path.join(TESTDATADIR, "conf1.py")) | |
| 58 self.__check_types(cfg) | |
| 59 | |
| 60 def test03_yaml_types(self): | |
| 61 with open(os.path.join(TESTDATADIR, "conf1.yml"), "rt") as f: | |
| 62 cfg = configmix.yaml.safe_load(f) | |
| 63 self.__check_types(cfg) | |
| 64 | |
| 65 def test04_py_export_all(self): | |
| 66 # When __all__ is given only it's keys are exported | |
| 67 cfg = configmix.py.load(os.path.join(TESTDATADIR, "conf2.py")) | |
| 68 self.assertEqual(u("the next value"), cfg.get("key1")) | |
| 69 self.assertTrue(isinstance(cfg.get("key1"), type(u('')))) | |
| 70 self.assertTrue(cfg.get("key2") is None) | |
| 71 | |
| 72 def test05_py_hide_private(self): | |
| 73 # When no __all__ is given all symbols with leading "_" are hidden | |
| 74 cfg = configmix.py.load(os.path.join(TESTDATADIR, "conf3.py")) | |
| 75 self.assertEqual(u("the next value "), cfg.get("key1")) | |
| 76 self.assertTrue(isinstance(cfg.get("key1"), type(u('')))) | |
| 77 self.assertTrue(cfg.get("_key2") is None) | |
| 78 | |
|
97
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
79 def test06_ini_tree(self): |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
80 cfg = configmix.ini.load(os.path.join(TESTDATADIR, "conf10.ini")) |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
81 self.__check_tree(cfg) |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
82 |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
83 def test07_py_tree(self): |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
84 cfg = configmix.py.load(os.path.join(TESTDATADIR, "conf10.py")) |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
85 self.__check_tree(cfg) |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
86 |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
87 def test08_yaml_tree(self): |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
88 with open(os.path.join(TESTDATADIR, "conf10.yml"), "rt") as f: |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
89 cfg = configmix.yaml.safe_load(f) |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
90 self.__check_tree(cfg) |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
95
diff
changeset
|
91 |
| 93 | 92 |
| 93 if __name__ == "__main__": | |
| 94 unittest.main() |
