def test_basic_namespace(self): a = reusables.Namespace(one=1, two=2, three=3) b = reusables.Namespace({'one': 1, 'two': 2, 'three': 3}) c = reusables.Namespace((zip(['one', 'two', 'three'], [1, 2, 3]))) d = reusables.Namespace(([('two', 2), ('one', 1), ('three', 3)])) e = reusables.Namespace(({'three': 3, 'one': 1, 'two': 2})) assert a == b == c == d == e
def test_namespace_from_bad_dict(self): try: ns = reusables.Namespace('{"k1": "v1", ' '"k2": {"k3": "v2"}}') except ValueError: assert True else: assert False, "Should have raised type error"
def test_namespace_modifiy_at_depth(self): test_dict = { 'key1': 'value1', "Key 2": { "Key 3": "Value 3", "Key4": { "Key5": "Value5" } } } namespace = reusables.Namespace(**test_dict) assert 'key1' in namespace assert 'key2' not in namespace namespace['Key 2'].new_thing = "test" assert namespace['Key 2'].new_thing == "test" namespace['Key 2'].new_thing += "2" assert namespace['Key 2'].new_thing == "test2" assert namespace['Key 2'].to_dict( )['new_thing'] == "test2", namespace['Key 2'].to_dict() assert namespace.to_dict()['Key 2']['new_thing'] == "test2" namespace.__setattr__('key1', 1) assert namespace['key1'] == 1 namespace.__delattr__('key1') assert 'key1' not in namespace
def test_namespace_tree(self): test_dict = { 'key1': 'value1', "Key 2": { "Key 3": "Value 3", "Key4": { "Key5": "Value5" } } } namespace = reusables.Namespace(**test_dict) result = namespace.tree_view(sep=" ") assert result.startswith("key1\n") or result.startswith("Key 2\n") assert "Key4" in result and " Value5\n" not in result
def test_error_namespace(self): test_dict = { 'key1': 'value1', "Key 2": { "Key 3": "Value 3", "Key4": { "Key5": "Value5" } } } namespace = reusables.Namespace(**test_dict) try: getattr(namespace, 'hello') except AttributeError: pass else: raise AssertionError("Should not find 'hello' in the test dict")
def get_config(config_file="config.yaml"): config = default_config.copy() if os.path.exists(config_file): with open(config_file) as f: config.update(yaml.load(f)) logger.info("Loaded config") logger.debug("Config - {0}".format(config)) config['video_dir'] = ( config['storage_directory'] if not config.get('video_sub_dir') else os.path.join(config['storage_directory'], config['video_sub_dir'])) config['image_dir'] = ( config['storage_directory'] if not config.get('image_sub_dir') else os.path.join(config['storage_directory'], config['image_sub_dir'])) return reusables.Namespace(**config)
def test_namespace(self): test_dict = { 'key1': 'value1', "Key 2": { "Key 3": "Value 3", "Key4": { "Key5": "Value5" } } } namespace = reusables.Namespace(**test_dict) assert namespace.key1 == test_dict['key1'] assert dict(getattr(namespace, 'Key 2')) == test_dict['Key 2'] setattr(namespace, 'TEST_KEY', 'VALUE') assert namespace.TEST_KEY == 'VALUE' delattr(namespace, 'TEST_KEY') assert 'TEST_KEY' not in namespace.to_dict(), namespace.to_dict() assert isinstance(namespace['Key 2'].Key4, reusables.Namespace) assert "'key1': 'value1'" in str(namespace) assert repr(namespace).startswith("<Namespace:")
def test_namespace_from_dict(self): ns = reusables.Namespace({"k1": "v1", "k2": {"k3": "v2"}}) assert ns.k2.k3 == "v2"