Mercurial > hgrepos > Python > libs > ConfigMix
changeset 93:84b9578cacce
Start with unit tests
| author | Franz Glasner <hg@dom66.de> |
|---|---|
| date | Sun, 18 Mar 2018 14:44:41 +0100 |
| parents | 75738d488e9d |
| children | 2b79ddc0f92b |
| files | CHANGES.txt MANIFEST.in doc/conf.py tests/data/conf1.ini tests/data/conf1.py tests/data/conf1.yml tests/data/conf2.py tests/data/conf3.py tests/test.py |
| diffstat | 9 files changed, 114 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/CHANGES.txt Sun Mar 18 14:21:48 2018 +0100 +++ b/CHANGES.txt Sun Mar 18 14:44:41 2018 +0100 @@ -1,7 +1,7 @@ .. -*- coding: utf-8; mode: rst; -*- .. -.. Valid tags: doc, feature, bugfix +.. Valid tags: doc, feature, bugfix, test .. _changelog: @@ -17,7 +17,12 @@ .. change:: :tags: doc - Begin the documentation with `Sphinx <http://www.sphinx-doc.org>`_. + Begin the documentation with `Sphinx <http://www.sphinx-doc.org>`_ + + .. change:: + :tags: test + + Begin formal unittests .. changelog:: :version: 0.5
--- a/MANIFEST.in Sun Mar 18 14:21:48 2018 +0100 +++ b/MANIFEST.in Sun Mar 18 14:44:41 2018 +0100 @@ -1,3 +1,5 @@ include .hg* *.txt requirement* -recursive-include doc *.* +graft doc +graft tests prune doc/_build +global-exclude *.pyc *.pyo
--- a/doc/conf.py Sun Mar 18 14:21:48 2018 +0100 +++ b/doc/conf.py Sun Mar 18 14:44:41 2018 +0100 @@ -209,4 +209,4 @@ # -- Options for changelog --------------------------------------------------- -changelog_inner_tag_sort = ['feature', 'bugfix', 'doc'] +changelog_inner_tag_sort = ['feature', 'bugfix', 'test', 'doc']
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/data/conf1.ini Sun Mar 18 14:44:41 2018 +0100 @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- + +[config] +key1 = the value +key2 = :int:2 +key3 = :float:5.7 +key4 = :bool:yes +key5 = :bool:off
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/data/conf1.py Sun Mar 18 14:44:41 2018 +0100 @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- + +key1 = u"the value" +key2 = 2 +key3 = 5.7 +key4 = True +key5 = False
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/data/conf1.yml Sun Mar 18 14:44:41 2018 +0100 @@ -0,0 +1,9 @@ +# -*- coding: utf-8; mode: yaml; indent-tabs-mode: nil; -*- +%YAML 1.1 +--- + +key1: the value +key2: 2 +key3: 5.7 +key4: true +key5: false
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/data/conf2.py Sun Mar 18 14:44:41 2018 +0100 @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- + +__all__ = ["key1", "nonexistingkey"] + +key1 = u"the next value" +key2 = "non-exported value"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/data/conf3.py Sun Mar 18 14:44:41 2018 +0100 @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- + +key1 = u"the next value " +_key2 = "non-exported value"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test.py Sun Mar 18 14:44:41 2018 +0100 @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- + +import sys +import os +import unittest + +sys.path.insert( + 0, + os.path.abspath( + os.path.normpath(os.path.join(os.path.dirname(__file__), "..")))) + +import configmix +import configmix.ini +import configmix.yaml +import configmix.py +from configmix.compat import u + + +TESTDATADIR = os.path.join( + os.path.abspath(os.path.dirname(__file__)), + "data") + + +class T01Basic(unittest.TestCase): + + """Check with low-level internal interfaces""" + + def __check_types(self, cfg): + self.assertEqual(u("the value"), + cfg.get("key1")) + self.assertTrue(isinstance(cfg.get("key1"), type(u('')))) + self.assertEqual(2, cfg.get("key2")) + self.assertEqual(5.7, cfg.get("key3")) + self.assertTrue(isinstance(cfg.get("key1"), type(u('')))) + self.assertTrue(cfg.get("key4")) + self.assertTrue(isinstance(cfg.get("key4"), bool)) + self.assertFalse(cfg.get("key5")) + self.assertTrue(isinstance(cfg.get("key5"), bool)) + + def test01_ini_types(self): + cfg = configmix.ini.load(os.path.join(TESTDATADIR, "conf1.ini")) + self.__check_types(cfg) + + def test02_py_types(self): + cfg = configmix.py.load(os.path.join(TESTDATADIR, "conf1.py")) + self.__check_types(cfg) + + def test03_yaml_types(self): + with open(os.path.join(TESTDATADIR, "conf1.yml"), "rt") as f: + cfg = configmix.yaml.safe_load(f) + self.__check_types(cfg) + + def test04_py_export_all(self): + # When __all__ is given only it's keys are exported + cfg = configmix.py.load(os.path.join(TESTDATADIR, "conf2.py")) + self.assertEqual(u("the next value"), cfg.get("key1")) + self.assertTrue(isinstance(cfg.get("key1"), type(u('')))) + self.assertTrue(cfg.get("key2") is None) + + def test05_py_hide_private(self): + # When no __all__ is given all symbols with leading "_" are hidden + cfg = configmix.py.load(os.path.join(TESTDATADIR, "conf3.py")) + self.assertEqual(u("the next value "), cfg.get("key1")) + self.assertTrue(isinstance(cfg.get("key1"), type(u('')))) + self.assertTrue(cfg.get("_key2") is None) + + +if __name__ == "__main__": + unittest.main()
