# HG changeset patch # User Franz Glasner # Date 1521823416 -3600 # Node ID 1c2f8a96dec208c2a5d342bf3433e0be428f6f35 # Parent d9f9a06e0c49fd700cf7254d123d3abd180e0d17 Unittests with some real-worl-examples of .yml and .ini configuration files diff -r d9f9a06e0c49 -r 1c2f8a96dec2 tests/data/conf20.yml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/data/conf20.yml Fri Mar 23 17:43:36 2018 +0100 @@ -0,0 +1,90 @@ +# -*- coding: utf-8; mode: yaml; indent-tabs-mode: nil; -*- +# +# For testing: merge with conf21.yml +# +%YAML 1.1 +--- + +process: + umask: 0o0027 + +intl: + domain: test-configmix + localedir: '{{appdir}}/locale' + fallback: de + cache: + items: 10 + + +db: + user: + name: + pwd: + + catalog: + + locinfo: + rw: + hostname: localhost + port: 5432 + + ro: + hostname: localhost + port: 5432 + + engines: + rw: + url: 'postgresql+psycopg2://{{db.user.name|urlquote}}:{{db.user.pwd|urlquote}}@{{db.locinfo.rw.hostname}}:{{db.locinfo.rw.port}}/{{db.catalog|urlquote}}' + encoding: utf-8 + isolation_level: 'SERIALIZABLE' + echo: false + echo_pool: false + + ro: + url: 'postgresql+psycopg2://{{db.user.name|urlquote}}:{{db.user.pwd|urlquote}}@{{db.locinfo.ro.hostname}}:{{db.locinfo.ro.port}}/{{db.catalog|urlquote}}' + encoding: utf-8 + isolation_level: 'SERIALIZABLE' + echo: false + echo_pool: false + is_readonly: true + + msg: + url: 'postgresql+psycopg2://{{db.user.name|urlquote}}:{{db.user.pwd|urlquote}}@{{db.locinfo.rw.hostname}}:{{db.locinfo.rw.port}}/{{db.catalog|urlquote}}' + encoding: utf-8 + isolation_level: 'SERIALIZABLE' + echo: false + echo_pool: false + + mandant: + url: 'postgresql+psycopg2://{{db.user.name|urlquote}}:{{db.user.pwd|urlquote}}@{{db.locinfo.ro.hostname}}:{{db.locinfo.ro.port}}/{{db.catalog|urlquote}}' + encoding: utf-8 + isolation_level: 'REPEATABLE_READ' + echo: false + echo_pool: false + is_readonly: true + + session: + url: 'postgresql+psycopg2://{{db.user.name|urlquote}}:{{db.user.pwd|urlquote}}@{{db.locinfo.rw.hostname}}:{{db.locinfo.rw.port}}/{{db.catalog|urlquote}}' + encoding: utf-8 + isolation_level: 'REPEATABLE_READ' + echo: false + echo_pool: false + + +wsgi: + debug: false + + debugger: + type: werkzeug + params: + evalex: true + + profile: false + + profiler: + type: werkzeug + params: + #stream: sys:stderr + profile_dir: '{{tmpdir}}/profiler' + #sort_by: ['time', 'calls'] + #restrictions=(), diff -r d9f9a06e0c49 -r 1c2f8a96dec2 tests/data/conf21.yml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/data/conf21.yml Fri Mar 23 17:43:36 2018 +0100 @@ -0,0 +1,20 @@ +# -*- coding: utf-8; mode: yaml; indent-tabs-mode: nil; -*- +# +# To be merged into conf20.yml +# +%YAML 1.1 +--- + +appdir: '{{OS:cwd}}' +_tmpdir: '{{appdir}}/tmp' +tmpdir: '{{_tmpdir|abspath|normpath}}' + +db: + user: + name: 'the_database_user' + pwd: 'the-database-password' + catalog: 'my_database_catalog' + + locinfo: + ro: + hostname: anotherhost diff -r d9f9a06e0c49 -r 1c2f8a96dec2 tests/data/conf22.ini --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/data/conf22.ini Fri Mar 23 17:43:36 2018 +0100 @@ -0,0 +1,9 @@ +[config] +tmpdir = {{_tmpdir}}\2 + +[config.db.user] +name = the_database_user_2 +pwd=the-database-password-2 + +[config.db.locinfo.ro] +hostname = 3rd-host diff -r d9f9a06e0c49 -r 1c2f8a96dec2 tests/test.py --- a/tests/test.py Fri Mar 23 17:25:21 2018 +0100 +++ b/tests/test.py Fri Mar 23 17:43:36 2018 +0100 @@ -90,5 +90,63 @@ self.__check_tree(cfg) +class T02LoadAndMerge(unittest.TestCase): + + def test01_load(self): + cfg = configmix.load( + os.path.join(TESTDATADIR, "conf20.yml"), + os.path.join(TESTDATADIR, "conf21.yml")) + + self.assertEqual(u("the_database_user"), + cfg.getvar_s("db.user.name")) + self.assertEqual(u("the-database-password"), + cfg.getvar_s("db.user.pwd")) + + tmpdir = cfg.getvar_s("tmpdir") + if os.name == 'nt': + self.assertFalse(u('/') in tmpdir) + self.assertEqual(os.path.normpath( + os.path.abspath(os.path.join(os.getcwd(), "tmp"))), + tmpdir) + + self.assertEqual(u("anotherhost"), + cfg.getvar_s("db.locinfo.ro.hostname")) + self.assertEqual(u("localhost"), + cfg.getvar_s("db.locinfo.rw.hostname")) + + self.assertEqual(5432, cfg.getvar_s("db.locinfo.ro.port")) + + url = cfg.getvar_s("db.engines.ro.url") + self.assertEqual( + u("postgresql+psycopg2://the_database_user:the-database-password@anotherhost:5432/my_database_catalog"), + url) + + def test02_load_with_ini(self): + cfg = configmix.load( + os.path.join(TESTDATADIR, "conf20.yml"), + os.path.join(TESTDATADIR, "conf21.yml"), + os.path.join(TESTDATADIR, "conf22.ini")) + + self.assertEqual(u("the_database_user_2"), + cfg.getvar_s("db.user.name")) + self.assertEqual(u("the-database-password-2"), + cfg.getvar_s("db.user.pwd")) + + tmpdir = cfg.getvar_s("tmpdir") + self.assertEqual(u(os.getcwd()) + u("/tmp\\2"), tmpdir) + + self.assertEqual(u("3rd-host"), + cfg.getvar_s("db.locinfo.ro.hostname")) + self.assertEqual(u("localhost"), + cfg.getvar_s("db.locinfo.rw.hostname")) + + self.assertEqual(5432, cfg.getvar_s("db.locinfo.ro.port")) + + url = cfg.getvar_s("db.engines.ro.url") + self.assertEqual( + u("postgresql+psycopg2://the_database_user_2:the-database-password-2@3rd-host:5432/my_database_catalog"), + url) + + if __name__ == "__main__": unittest.main()