Beispiel #1
0
 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))
Beispiel #2
0
 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)
Beispiel #3
0
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)
Beispiel #4
0
    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"
Beispiel #5
0
    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)
Beispiel #6
0
    def test_default_node_factory_bad_input(self):
        def bad_factory(x=None):
            pass

        with self.assertRaises(ValueError):
            ns = Namespace(default_node_factory=bad_factory)
        with self.assertRaises(ValueError):
            ns = Namespace(default_node_factory=['a', 'list', '?'])
Beispiel #7
0
    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"
Beispiel #8
0
 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)
Beispiel #9
0
    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)
Beispiel #10
0
    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)
Beispiel #11
0
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'
    ]
Beispiel #12
0
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'
    ]
Beispiel #13
0
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'
    ]
Beispiel #14
0
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))
Beispiel #15
0
    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)
Beispiel #16
0
 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)
Beispiel #17
0
 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)
Beispiel #18
0
 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)
Beispiel #19
0
 def test_basic_walk(self):
     ns = Namespace()
     self.assertEqual({".": {}}, ns.walk())
Beispiel #20
0
 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))
Beispiel #21
0
 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)
Beispiel #22
0
 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"
Beispiel #23
0
    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")
Beispiel #24
0
 def test_get_nonexisting_handle_fail(self):
     ns = Namespace()
     with self.assertRaises(NamespaceLookupError):
         handle = ns.get_handle(".something.totally.new")
Beispiel #25
0
def blank_namespace():
    return Namespace()
Beispiel #26
0
 def test_add_root_node(self):
     ns = Namespace()
     with self.assertRaises(NamespaceCollisionError):
         ns.add_exactly_one('.')
Beispiel #27
0
 def test_get_root_node(self):
     ns = Namespace()
     root = ns.get('.')
     self.assertEqual(ns.root, root)
Beispiel #28
0

@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')
Beispiel #29
0
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))