def test_add_then_get_ancestor(self): ns = Namespace() parent_nsid = '.a.b.c.d.e' child_nsid = make_child_nsid(parent_nsid, 'f') ns.add(child_nsid) node = ns.get(parent_nsid) self.assertEqual(parent_nsid, str(node.nsid))
def test_add_then_get_bad(self): ns = Namespace() new_nsid = '.a.b.c.d.e' bad_new_nsid = '.a.b.c.d.e.f' ns.add(new_nsid) with self.assertRaises(NamespaceLookupError): node = ns.get(bad_new_nsid)
def test_handle_add(self): ns = Namespace() ns.add(".more.stuff.here.too") handle = ns.get_handle(".more.stuff.here.too") new_node = handle.add(".subtree")[0] assert isinstance(handle.subtree, NamespaceNodeBase)
def test_handle_get(self): ns = Namespace() ns.add(".add.some.stuff.here") ns.add(".other.stuff.added.here.now") handle = ns.get_handle(".other.stuff") assert isinstance(handle.get('.added.here'), NamespaceNodeBase)
def test_root_delegate(self): ns = Namespace() ns.add('.a.few.nodes') #make sure it exists with this name ns.a.few.nodes.attr = "val" assert ns.a.few.nodes.attr == "val"
def test_delete_node(self): ns = Namespace() new_nsid = '.a.b.c.d' ns.add(new_nsid) node = ns.remove(new_nsid) self.assertEqual(str(node.nsid), new_nsid) with self.assertRaises(NamespaceLookupError): ns.get(new_nsid)
def test_handle_remove(self): ns = Namespace() ns.add(".more.stuff.here.too") handle = ns.get_handle(".more.stuff.here") node = handle.remove(".too") assert node.nsid.nsid == ".more.stuff.here.too"
def test_get_subnodes_from_handle(): ns = Namespace() ns.add(".a.few.nodes.here.and.there.and.everywhere") handle = ns.get_handle(".a.few") subnodes = handle.get_subnodes('.nodes.here.and.there') nsids = [str(x.nsid) for x in subnodes] assert nsids == [ '.a.few.nodes.here.and.there.and', '.a.few.nodes.here.and.there.and.everywhere' ]
def test_get_subnodes(): ns = Namespace() ns.add(".a.few.nodes.here.and.there.and.everywhere") subnodes = ns.get_subnodes('.a.few') nsids = [str(x.nsid) for x in subnodes] assert nsids == [ '.a.few.nodes', '.a.few.nodes.here', '.a.few.nodes.here.and', '.a.few.nodes.here.and.there', '.a.few.nodes.here.and.there.and', '.a.few.nodes.here.and.there.and.everywhere' ]
def test_get_subnodes_from_nested_handles(caplog): ns = Namespace() #caplog.set_level(logging.DEBUG) ns.add(".a.few.nodes.here.and.there.and.everywhere") handle1 = ns.get_handle(".a.few") handle2 = handle1.get_handle(".nodes") subnodes = handle2.get_subnodes('.here.and.there') nsids = [str(x.nsid) for x in subnodes] assert nsids == [ '.a.few.nodes.here.and.there.and', '.a.few.nodes.here.and.there.and.everywhere' ]
def test_add_node_return2(self): ns = Namespace() new_nsid = '.a.b' new_nodes = ns.add(new_nsid) #- add returns all the new nodes created in order of creation #- so the last one in the list will be the deepest child new_node = new_nodes[-1] self.assertEqual(str(new_nodes[-1].nsid), new_nsid)
def test_default_node_factory(self): class NewNamespaceNodeBase(NamespaceNodeBase): def __init__(self, nsid=None, namespace=None): super().__init__(nsid=nsid, namespace=namespace) ns = Namespace(default_node_factory=NewNamespaceNodeBase) new_nodes = ns.add('.this.is.all.new') for node in new_nodes: self.assertIsInstance(node, NewNamespaceNodeBase)
def test_walk(self): ns = Namespace() ns.add('.all.work.no_play.dull_boy') ns.add('.hackers.on.planet.earth') test_dict = { ".": { "all": { "work": { "no_play": { "dull_boy": {} } } }, "hackers": { "on": { "planet": { "earth": {} } } } } } self.assertEqual(ns.walk(), test_dict)
def test_add_then_get_new_node(self): ns = Namespace() new_nsid = '.a.b.c.d.e' ns.add(new_nsid) node = ns.get(new_nsid) self.assertEqual(new_nsid, str(node.nsid))
def test_add_bad_node2(self): ns = Namespace() # nsids must start with a dot new_nsid = 'a.b' with self.assertRaises(InvalidNsidError): new_nodes = ns.add(new_nsid)
def test_bad_get2(self): ns = Namespace() ns.add('.something.that') with self.assertRaises(NamespaceLookupError): ns.get(".something.that.does.not.exist.and.is.long")