def test_NamespaceConfigParser_instantiation_with_node_factory_override_3(): ns = Namespace(default_node_factory=NamespaceNodeBaseSubclass) nfactory = partial(ns.add_exactly_one, node_factory=NamespaceNodeBaseSubclass) nscp = NamespaceConfigParser(node_factory=nfactory) new_node = nscp.new_node('.test') assert (isinstance(new_node, NamespaceNodeBaseSubclass))
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_parse_existing_namespace(): test_dict = { "all": { "work": { "no": { "play": { "dull_boy": {} } } } }, "hackers": { "on": { "planet": { "earth": {} } } } } ns = Namespace() nscp = NamespaceConfigParser2(namespace=ns) nscp.parse(dictConfig=test_dict) for nsid in get_nsid_ancestry('.all.work.no.play.dull_boy'): assert isinstance(ns.get(nsid), NamespaceNodeBase) for nsid in get_nsid_ancestry('.hackers.on.planet.earth'): assert isinstance(ns.get(nsid), NamespaceNodeBase)
def test_get_handle(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.added.here, NamespaceNodeBase)
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_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_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_add_exactly_one_bad_input(self): ns = Namespace() nsid = '.two.nodes' with self.assertRaises(ValueError): new_node = ns.add_exactly_one(nsid) nsid = '.now.three.nodes' with self.assertRaises(ValueError): new_node = ns.add_exactly_one(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_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_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 blank_namespace(): return Namespace()
def test_basic_walk(self): ns = Namespace() self.assertEqual({".": {}}, ns.walk())
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")
def test_get_root_node(self): ns = Namespace() root = ns.get('.') self.assertEqual(ns.root, root)
def test_add_exactly_one_happy_path(self): ns = Namespace() nsid = '.one_new_node' new_node = ns.add_exactly_one(nsid) self.assertEqual(str(new_node.nsid), nsid)
def test_get_nonexisting_handle(self): ns = Namespace() handle = ns.get_handle(".something.totally.new", create_nodes=True) assert handle.get('.').nsid.nsid == ".something.totally.new"
def test_add_root_node(self): ns = Namespace() with self.assertRaises(NamespaceCollisionError): ns.add_exactly_one('.')
def test_get_nonexisting_handle_fail(self): ns = Namespace() with self.assertRaises(NamespaceLookupError): handle = ns.get_handle(".something.totally.new")
@pytest.fixture def mock_attribute_map(): def mock_provider_callable(): print("mocked provider called!") return "just a value to return" return { "attribute_1": "a value for attribute_1", "attribute_2": "a.mock.provider.namespace.nsid", "attribute_3": mock_provider_callable } NS = Namespace() def test_SecondLife_instantation(mock_attribute_map): MapNode = SecondLifeNode(nsid=".test.nsid.string", namespace=NS, secondlife=mock_attribute_map) assert str(MapNode.nsid) == '.test.nsid.string' assert MapNode._secondlife.keys() == mock_attribute_map.keys() def test_SecondLife_map1(mock_attribute_map): MapNode = SecondLifeNode(nsid=".test.nsid.string", namespace=NS, secondlife=mock_attribute_map) assert MapNode.attribute_1 == mock_attribute_map.get('attribute_1')
def test_NamespaceConfigParser_instantiation_with_node_factory_override_2(): ns = Namespace(default_node_factory=NamespaceNodeBaseSubclass) nscp = NamespaceConfigParser(node_factory=ns.add) new_node = nscp.new_node(nsid='.test')[0] assert (isinstance(new_node, NamespaceNodeBaseSubclass))