def test_pickle_rick_dict_decon(self): test_conf_yaml = Rickle('./tests/placebos/test_config.yaml', deep=True, load_lambda=True) d = test_conf_yaml.dict() s = test_conf_yaml.to_yaml_string()
def test_class_definition(self): files = [ './tests/placebos/test_config.yaml', './tests/placebos/test_second.yaml' ] test_conf_yaml = Rickle(files, deep=True, load_lambda=True) obj = test_conf_yaml.TesterClass() obj.datenow() obj.math_e(99, 99) obj.math_e() self.assertTrue(True)
def test_extended_config_add_lambda(self): from datetime import datetime as dd test_conf = Rickle() load = "lambda: dd.utcnow().strftime('%Y-%m-%d')" imports = ['from datetime import datetime as dd'] test_conf.add_lambda('date_str', load, imports) y = test_conf.date_str() y_true = dd.utcnow().strftime('%Y-%m-%d') self.assertEquals(y, y_true)
def test_config_iterator(self): test_dict = {'user': {'hello': 'world'}, 'func': {'hello': 'world'}} test_conf = Rickle(test_dict) for k in test_conf: self.assertEquals(k.hello, 'world')
def test_config_keys(self): test_dict = { 'user': { 'type': 'env', 'load': 'USERNAME' }, 'func': { 'type': 'lambda', 'load': 'lambda x: x+1' } } test_conf = Rickle(test_dict) for k in test_conf.keys(): self.assertIn(k, ['user', 'func'])
def test_multi_file_load(self): files = [ './tests/placebos/test_config.yaml', './tests/placebos/test_second.yaml' ] test_conf_yaml = Rickle(files, deep=True, load_lambda=True) self.assertTrue(True)
def test_extended_config(self): # Test normal dict test_dict = {'A': 1, 'l': [1, {'deep': 'hole'}], 'B': {'k': 'v'}} test_conf = Rickle(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 = Rickle(test_dict, deep=True) self.assertEquals(test_conf.l[-1].deep, 'hole') # Test extended operations (OS ENV, functions) test_dict = { 'user': { 'type': 'env', 'load': 'USERNAME' }, 'func': { 'type': 'lambda', 'load': 'lambda x: x+1' } } test_conf = Rickle(test_dict, load_lambda=True) expected_username = os.getenv('USERNAME') self.assertEquals(test_conf.user, expected_username) self.assertEquals(test_conf.func(41), 42)
def test_extended_config_add_function(self): import math test_conf = Rickle() load = """ def tester(x, c): y = x * 2 + c return math.cos(y) """ args = {'x': 0.42, 'c': 1.7} imports = ['math'] test_conf.add_function('tester', load, args, imports) y = test_conf.tester(x=0.66, c=1.6) y_true = math.cos(0.66 * 2 + 1.6) self.assertEquals(y, y_true)
def test_config_has_key(self): test_dict = {'user': {'hello': 'world'}, 'func': {'hello': 'world'}} test_conf = Rickle(test_dict) self.assertTrue(test_conf.has('user')) self.assertFalse(test_conf.has('hello')) self.assertTrue(test_conf.has('hello', deep=True)) self.assertFalse(test_conf.has('BYE', deep=True)) self.assertFalse(test_conf.has('BYE', deep=False))
def test_pickle_rick_load_param(self): test_conf_yaml = Rickle('./tests/placebos/test_config.yaml', deep=True, arg_name='hallo_wereld', load_lambda=True) d = test_conf_yaml.dict() s = test_conf_yaml.to_yaml_string() s = test_conf_yaml.to_yaml_string(serialised=False) test_conf_yaml = Rickle('./tests/placebos/test_config.yaml', deep=True, load_lambda=True) d = test_conf_yaml.dict() s = test_conf_yaml.to_yaml_string()
def test_pickle_rick_dict_decon_deserialised_vs_serialised(self): test_conf_yaml = Rickle('./tests/placebos/test_config.yaml', deep=True, load_lambda=True) d = test_conf_yaml.dict() s = test_conf_yaml.to_yaml_string() d_ = test_conf_yaml.dict(serialised=True) s_ = test_conf_yaml.to_yaml_string(serialised=False) self.assertTrue(True)
def test_rickler(self): t = Testing() rickler = ObjectRickler() d = rickler.deconstruct(t) d['testing_function_new'] = { 'name': 'testing_function_new', 'type': 'function', # 'includes_self_reference' : True, 'args': { 'a': None, 'b': None, 'c': 1 }, 'load': """def testing_function_new(a, b, c=1): print(f\'{self.__class__.__name__} Howdy {a}, {b}, and {c}\') """ } del d['testing_function'] y = rickler.to_yaml_string(t) r = Rickle(d, deep=True, load_lambda=True) # r = rickler.to_rickle(t, deep=True, load_lambda=True) # r.testing_function_new('Milly', 'Willy') r.x = 99 r.y = 99 r.z = 99 r.b = 99 obj = rickler.from_rickle(r, Testing) obj.testing_function('Jack', 'Sally', 'Justin?') obj.testing_function_new('Jack', 'Sally') self.assertTrue(True)
def test_load_dump_load(self): files = [ './tests/placebos/test_config.yaml', './tests/placebos/test_second.yaml' ] test_conf_yaml = Rickle(files, deep=True, load_lambda=True) test_conf_yaml.to_yaml_file('./test_out.yaml') test_conf_yaml_reload = Rickle('./test_out.yaml', deep=True, load_lambda=True) test_conf_yaml_reload.BASICS.outer_math_e() obj = test_conf_yaml_reload.TesterClass() obj.datenow() obj.math_e(99, "^99>") os.remove('./test_out.yaml') self.assertTrue(True)