def test_merge(self): newsettings = StringDict() newsettings['my_data'] = {} newsettings['my_data']['elf'] = '10' newsettings['mychild:second'] = '30' self.parent1['morf_child'] = 'none' newsettings.merge(self.parent1) newsettings['parent_key'] = 'newpar' newsettings['parent3_key'] = 'par3' self.assertEqual({ 'my_data': { 'elf': '10', }, 'morf_child': 'none', 'mychild': { 'child_key': 'child1', 'morf2_parent': 'parent2 morf', 'morf3_parent': 'par3 morf', 'morf_parent': 'parent1 morf'}, 'mychild:second': '30', 'parent_key': 'newpar', 'parent3_key': 'par3', }, newsettings.to_dict())
def test_morfing(self): def simple_morf(obj, value): return value + '***' data = StringDict({'key': 'value', 'key2': 'v2'}) self.assertEqual('value', data['key']) self.assertEqual('v2', data['key2']) data.set_morf('key', simple_morf) self.assertEqual('value***', data['key']) self.assertEqual('v2', data['key2'])
def _populte_default_settings(self): def morf_sql_url(obj, value): if obj["type"] == "sqlite": value = "sqlite:///%(paths:sqlite_db)s" else: value = "%(type)s://%(login)s:%(password)s@%(host)s:%(port)s/" "%(name)s" return value % obj super()._populte_default_settings() dbsettings = StringDict() dbsettings["url"] = "" dbsettings.set_morf("url", morf_sql_url) self.settings["db"] = dbsettings
def setUp(self): super().setUp() self.parent1 = StringDict({'parent_key': 'parent1'}) self.parent2 = StringDict({'parent2_key': 'parent2'}) self.child = StringDict({'child_key': 'child1'}) self.child['morf_parent'] = '%(parent_key)s morf' self.child['morf2_parent'] = '%(parent2_key)s morf' self.child['morf3_parent'] = '%(parent3_key)s morf' self.child.append_parent(self.parent1) self.parent2['child'] = self.child self.parent1['mychild'] = self.child self.parent1['morf_child'] = '%(mychild:morf_parent)s morf child'
def _populte_default_settings(self): def morf_sql_url(obj, value): if obj['type'] == 'sqlite': value = 'sqlite:///%(paths:sqlite_db)s' else: value = ('%(type)s://%(login)s:%(password)s@%(host)s:%(port)s/' '%(name)s') return value % obj super()._populte_default_settings() dbsettings = StringDict() dbsettings['url'] = '' dbsettings.set_morf('url', morf_sql_url) self.settings['db'] = dbsettings
def _populte_default_settings(self): def morf_sql_url(obj, value): if obj['type'] == 'sqlite': value = 'sqlite:///%(paths:sqlite_db)s' else: value = ( '%(type)s://%(login)s:%(password)s@%(host)s:%(port)s/' '%(name)s' ) return value % obj super()._populte_default_settings() dbsettings = StringDict() dbsettings['url'] = '' dbsettings.set_morf('url', morf_sql_url) self.settings['db'] = dbsettings
class StringDictEnvTest(TestCase): def setUp(self): self.environ_patcher = patch('morfdict.models.environ', {}) self.menviron = self.environ_patcher.start() self.settings = StringDict() def tearDown(self): self.environ_patcher.stop() def test_simple_assign(self): self.menviron['NAME'] = 'value1' self.assertEqual( self.settings.get_from_env('NAME'), 'value1') def test_with_default(self): self.assertEqual( self.settings.get_from_env('NAME', 'value2'), 'value2') def test_with_false_default(self): self.assertEqual( self.settings.get_from_env('NAME', None), None) def test_with_error(self): try: self.settings.get_from_env('NAME', error='message3') assert False except EnvirontmentValueMissing as error: assert error.message == 'message3' def test_with_default_error(self): try: self.settings.get_from_env('NAME') assert False except EnvirontmentValueMissing as error: assert error.message == 'Environtment "NAME" value missing'
class ParrentsTest(TestCase): def setUp(self): super().setUp() self.parent1 = StringDict({'parent_key': 'parent1'}) self.parent2 = StringDict({'parent2_key': 'parent2'}) self.child = StringDict({'child_key': 'child1'}) self.child['morf_parent'] = '%(parent_key)s morf' self.child['morf2_parent'] = '%(parent2_key)s morf' self.child['morf3_parent'] = '%(parent3_key)s morf' self.child.append_parent(self.parent1) self.parent2['child'] = self.child self.parent1['mychild'] = self.child self.parent1['morf_child'] = '%(mychild:morf_parent)s morf child' def test_morf_parent(self): self.assertEqual('parent1 morf', self.child['morf_parent']) self.assertEqual('parent2 morf', self.child['morf2_parent']) def test_morf_error(self): self.assertRaises(KeyError, self.child.get, 'morf3_parent') def test_morf_child(self): self.assertRaises(KeyError, lambda: self.parent1['morf_child']) def test_get_default_error(self): self.assertRaises(KeyError, self.child.get, 'morf3_parent', 'default') def test_get_default(self): self.assertEqual([], self.child.get('jinja2.extensions', [])) def test_setter(self): self.assertEqual(True, self.parent2 in self.child._parents) def test_items(self): data = list(self.child.items()) data.sort() expected_data = [ ('child_key', 'child1'), ('morf2_parent', 'parent2 morf'), ('morf_parent', 'parent1 morf')] self.assertEqual(expected_data, data) def test_merge(self): newsettings = StringDict() newsettings['my_data'] = {} newsettings['my_data']['elf'] = '10' newsettings['mychild:second'] = '30' self.parent1['morf_child'] = 'none' newsettings.merge(self.parent1) newsettings['parent_key'] = 'newpar' newsettings['parent3_key'] = 'par3' self.assertEqual({ 'my_data': { 'elf': '10', }, 'morf_child': 'none', 'mychild': { 'child_key': 'child1', 'morf2_parent': 'parent2 morf', 'morf3_parent': 'par3 morf', 'morf_parent': 'parent1 morf'}, 'mychild:second': '30', 'parent_key': 'newpar', 'parent3_key': 'par3', }, newsettings.to_dict())
def test_error(self): settings = StringDict({'name': 'value'}) self.assertEqual([], settings.get_errors()) settings['error'] = '%(errorkey)s' self.assertEqual(KeyError, type(settings.get_errors()[0]))
def setUp(self): self.environ_patcher = patch('morfdict.models.environ', {}) self.menviron = self.environ_patcher.start() self.settings = StringDict()