def test_dump_json(self): test_dict = { 'user': { 'type': 'env', 'load': 'USERNAME' }, 'func': { 'type': 'lambda', 'load': 'lambda x: x+1' } } conf = BaseRickle(test_dict) dumped_string = conf.to_json_string() expected = '{"user": {"type": "env", "load": "USERNAME"}, "func": {"type": "lambda", "load": "lambda x: x+1"}}' self.assertEquals(dumped_string, expected) filename = './unit_test_file_dump.json' conf.to_json_file(filename) self.assertTrue(os.path.isfile(filename)) os.remove(filename)
def test_dump_yaml(self): import os test_dict = { 'user': { 'type': 'env', 'load': 'USERNAME' }, 'func': { 'type': 'lambda', 'load': 'lambda x: x+1' } } conf = BaseRickle(test_dict) dumped_string = conf.to_yaml_string() self.assertTrue('env' in dumped_string) self.assertTrue('USERNAME' in dumped_string) self.assertTrue('lambda x: x+1' in dumped_string) filename = './unit_test_file_dump.yaml' conf.to_yaml_file(filename) self.assertTrue(os.path.isfile(filename)) os.remove(filename)
def test_config_items(self): test_dict = {'user': '******', 'func': 'hello'} test_conf = BaseRickle(test_dict) for k, v in test_conf.items(): self.assertEquals(v, test_dict[k])
def test_base_config_add_attr(self): test_conf = BaseRickle() test_conf.add_attr('new_var', 42) self.assertEquals(test_conf.new_var, 42) test_conf.new_var += 1 self.assertEquals(test_conf.new_var, 43)
def test_config_values(self): test_dict = {'user': '******', 'func': 'hello'} expected = ['BYE', 'hello'] test_conf = BaseRickle(test_dict) l = test_conf.values() self.assertListEqual(l, expected)
def test_config_get_search(self): test_conf_yaml = BaseRickle('./tests/placebos/test_config.yaml', deep=True) value = test_conf_yaml.get('one') self.assertIsNone(value) value = test_conf_yaml.get('one', 'VALUE') self.assertEquals(value, 'VALUE') value = test_conf_yaml.get('one', do_recursive=True) self.assertEquals(value, 'value') value = test_conf_yaml.get('ONE') self.assertIsNone(value) value = test_conf_yaml.get('ONE', 'default_one', do_recursive=True) self.assertEquals(value, 'default_one') value = test_conf_yaml.get('USERNAME', do_recursive=True) self.assertIsInstance(value, BaseRickle) self.assertEquals(value.type, 'env')
def test_config_to_dict(self): test_dict = { 'user': { 'type': 'env', 'load': 'USERNAME' }, 'func': { 'type': 'lambda', 'load': 'lambda x: x+1' } } test_conf = BaseRickle(test_dict) self.assertDictEqual(test_conf.dict(), test_dict)
def test_base_config(self): # Test normal dict test_dict = {'A': 1, 'l': [1, {'deep': 'hole'}], 'B': {'k': 'v'}} test_conf = BaseRickle(test_dict) self.assertEquals(test_conf.A, 1) self.assertListEqual(test_conf.l, [1, {'deep': 'hole'}]) self.assertEquals(test_conf.B.k, 'v') # Test deep dict test_conf = BaseRickle(test_dict, deep=True) self.assertEquals(test_conf.l[-1].deep, 'hole') # Test YAML and JSON doc loading with file path test_conf_yaml = BaseRickle('./tests/placebos/test_config.yaml', deep=True) test_conf_json = BaseRickle('./tests/placebos/test_config.json', deep=True) self.assertGreater(len(test_conf_yaml), 0) self.assertGreater(len(test_conf_json), 0) # Test YAML and JSON doc loading with file stream with open('./tests/placebos/test_config.yaml', 'r') as fs_y, open('./tests/placebos/test_config.json', 'r') as fs_j: test_conf_yaml = BaseRickle(fs_y, deep=True) test_conf_json = BaseRickle(fs_j, deep=True) self.assertGreater(len(test_conf_yaml), 0) self.assertGreater(len(test_conf_json), 0) # Test string loading yaml_string = """ ONE: "value" """ json_string = """ { "ONE" : "value"} """ test_conf_yaml = BaseRickle(yaml_string, deep=True) test_conf_json = BaseRickle(json_string, deep=True) self.assertTrue(test_conf_yaml == test_conf_json) self.assertEquals(test_conf_yaml.ONE, "value") self.assertEquals(test_conf_json.ONE, "value")