示例#1
0
    def test_union_filter_for_labels_with_no_props(self):
        id_generator = IDGenerator()
        marko = Vertex(
            "person",
            name="Marko",
        )
        josh = Vertex(
            "person",
            name="Marko",
        )
        edge = Edge(marko, "father", josh)
        edge2 = Edge(josh, "son", marko)

        # attach ID's to the vertices and edges.
        marko.ident = id_generator.get_vertex_id()
        josh.ident = id_generator.get_vertex_id()
        edge.ident = id_generator.get_edge_id()
        edge2.ident = id_generator.get_edge_id()

        container1 = EntitySet()
        container2 = EntitySet()

        container1.add(edge)
        container2.add(edge2)
        container3 = container1 | container2

        self.assertEqual(
            container3.filter("father").sorted(),
            sorted([edge])
        )
        self.assertEqual(
            container3.filter("son").sorted(),
            sorted([edge2])
        )
示例#2
0
    def setUp(self):
        id_generator = IDGenerator()
        self.marko = Vertex(
            "Father",
            name="Marko",
            surname="Jones",
            age=30,
        )
        self.john = Vertex(
            "Brother",
            name="John",
            surname="Jones",
            age=30
        )
        self.peter = Vertex(
            "Uncle",
            name="Peter",
            surname="Doe",
            age=20
        )

        # add id's to the unbound vertices
        self.marko.ident = id_generator.get_vertex_id()
        self.john.ident = id_generator.get_vertex_id()
        self.peter.ident = id_generator.get_vertex_id()
        self.container = EntitySet([self.marko, self.john, self.peter])
示例#3
0
 def test_append_edge_with_ident_set(self):
     node1 = Vertex(label="NODE")
     node2 = Vertex(label="NODE")
     edge = Edge(node1, "knows", node2)
     edge.ident = 0
     self.assertRaises(interfaces.DatabaseException, self.graph.append_edge,
                       edge)
示例#4
0
 def test_add_dup_id(self):
     sue = Vertex("person", name="dup_vertex_id")
     sue.ident = 0
     self.assertRaises(
         KeyError,
         self.container.add,
         sue
     )
示例#5
0
 def test_add_dup_id(self):
     sue = Vertex("person", name="dup_vertex_id")
     sue.ident = 0
     self.assertRaises(
         KeyError,
         self.container.add,
         sue
     )
示例#6
0
 def test_append_vertex_with_ident_set(self):
     node = Vertex(label="NODE")
     node.ident = 0
     self.assertRaises(
         interfaces.EntityIDError,
         self.graph.append_vertex,
         node
     )
示例#7
0
    def test_append_edge(self):
        node1 = Vertex(label="NODE")
        node2 = Vertex(label="NODE")
        edge = Edge(node1, "knows", node2)
        self.graph.append_edge(edge)

        self.assertEqual(edge.ident, self.graph._id_tracker.eid - 1)
        self.assertIn(edge, self.graph)
        self.assertEqual(edge.is_bound(), True)
示例#8
0
 def test_filter_for_labels_with_no_props(self):
     marko = Vertex(
         "person",
         name="Marko",
     )
     josh = Vertex(
         "person",
         name="Marko",
     )
     edge = Edge(marko, "father", josh)
     container = EntitySet()
     container.add(edge)
     self.assertEqual(container.filter("father").sorted(), sorted([edge]))
示例#9
0
 def test_bind_to_graph(self):
     sue = Vertex(100, name="Sue")
     self.graph.bind_to_graph(sue)
     self.assertIs(
         sue.graph,
         self.graph
     )
示例#10
0
    def test_add_out_edge_unbound(self):
        marko = Vertex("person", name="marko")
        josh = Vertex("person", name="josh")
        new_edge = marko.add_out_edge(josh, "knows")

        self.assertIsInstance(new_edge, IEdge)

        self.assertIn(
            new_edge,
            marko.out_edges,
        )

        self.assertEqual(
            marko.get_both_edges().sorted(),
            [new_edge],
        )
示例#11
0
 def test_remove_unknown_entity_id(self):
     sue = Vertex(100, name="sue")
     self.assertRaises(
         KeyError,
         self.container.remove,
         sue,
     )
示例#12
0
 def test_set_property_on_an_unknown_entity(self):
     dog = Vertex("dog", name="socks")
     self.assertRaises(
         interfaces.UnknownEntityError,
         self.graph.set_property,
         dog,
         name="spot"
     )
示例#13
0
 def test_intersection(self):
     sue = Vertex("Sister", name="Sue")
     intersection = self.container & EntitySet([self.marko, sue])
     self.assertEqual(
         intersection.sorted(),  # pylint: disable=no-member
         sorted([
             self.marko,
         ]))
示例#14
0
 def test_difference(self):
     sue = Vertex("Sister", name="Sue")
     diff = self.container - EntitySet([self.marko, sue])
     self.assertEqual(
         diff.sorted(),  # pylint: disable=no-member
         sorted([
             self.john,
             self.peter,
         ]))
示例#15
0
    def test_add_out_edge_unbound(self):
        marko = Vertex("person", name="marko")
        josh = Vertex("person", name="josh")
        new_edge = marko.add_out_edge(josh, "knows")

        self.assertIsInstance(
            new_edge,
            IEdge
        )

        self.assertIn(
            new_edge,
            marko.out_edges,
        )

        self.assertEqual(
            marko.get_both_edges().sorted(),
            [new_edge],
        )
示例#16
0
 def test_filter_for_vertex_with_prop_None(self):
     sue = Vertex(
         "person",
         name="sue",
         age=None,
     )
     self.container.add(sue)
     self.assertEqual(
         self.container.filter(name="Marko", age__gt=0).all(),
         [self.marko],
     )
示例#17
0
    def test_union_filter_for_labels_with_no_props(self):
        id_generator = IDGenerator()
        marko = Vertex(
            "person",
            name="Marko",
        )
        josh = Vertex(
            "person",
            name="Marko",
        )
        edge = Edge(marko, "father", josh)
        edge2 = Edge(josh, "son", marko)

        # attach ID's to the vertices and edges.
        marko.ident = id_generator.get_vertex_id()
        josh.ident = id_generator.get_vertex_id()
        edge.ident = id_generator.get_edge_id()
        edge2.ident = id_generator.get_edge_id()

        container1 = EntitySet()
        container2 = EntitySet()

        container1.add(edge)
        container2.add(edge2)
        container3 = container1 | container2

        self.assertEqual(container3.filter("father").sorted(), sorted([edge]))
        self.assertEqual(container3.filter("son").sorted(), sorted([edge2]))
示例#18
0
 def test_union(self):
     sue = Vertex(
         "Sister",
         name="Sue"
     )
     union = self.container | EntitySet([sue, self.marko])
     self.assertEqual(
         union.sorted(),  # pylint: disable=no-member
         sorted(
             [
                 self.marko,
                 self.john,
                 self.peter,
                 sue
             ]
         )
     )
示例#19
0
 def setUp(self):
     self.marko = Vertex(label="person", name="marko", age=29)
     super(TestEdge, self).setUp()
示例#20
0
 def setUp(self):
     self.marko = Vertex(label="person", name="marko", age=29)
     super(TestVertex, self).setUp()
示例#21
0
class TestVertex(base.TestBase, TestEntityBase):
    def setUp(self):
        self.marko = Vertex(label="person", name="marko", age=29)
        super(TestVertex, self).setUp()

    def test_is_bound_not_bound(self):
        self.marko.graph = None
        self.assertEqual(
            self.marko.is_bound(),
            False
        )

    def test_add_in_edge_bound_to_graph(self):
        sue = self.graph.get_or_create_vertex("person", name="sue")
        new_edge = sue.add_in_edge(self.josh, "brother")

        self.assertIsInstance(
            new_edge,
            IEdge
        )

        self.assertIn(
            new_edge,
            sue.in_edges,
        )

        self.assertEqual(
            sue.get_both_edges().sorted(),
            sorted(
                [
                    new_edge
                ]
            )
        )

    def test_add_in_edge_unbound(self):
        marko = Vertex("person", name="marko")
        josh = Vertex("person", name="josh")
        new_edge = marko.add_in_edge(josh, "brother")

        self.assertIsInstance(
            new_edge,
            IEdge
        )

        self.assertIn(
            new_edge,
            marko.in_edges,
        )

        self.assertEqual(
            marko.get_both_edges().sorted(),
            [new_edge],
        )

    def test_add_out_edge_unbound(self):
        marko = Vertex("person", name="marko")
        josh = Vertex("person", name="josh")
        new_edge = marko.add_out_edge(josh, "knows")

        self.assertIsInstance(
            new_edge,
            IEdge
        )

        self.assertIn(
            new_edge,
            marko.out_edges,
        )

        self.assertEqual(
            marko.get_both_edges().sorted(),
            [new_edge],
        )

    def test_add_out_edge(self):
        sue = self.graph.get_or_create_vertex("person", name="sue")
        new_edge = sue.add_out_edge(self.josh, "sister")

        self.assertIsInstance(
            new_edge,
            IEdge
        )

        self.assertIn(
            new_edge,
            sue.out_edges,
        )

        self.assertEqual(
            sue.get_both_edges().sorted(),
            sorted([new_edge]),
        )

    def test_in_edge_count(self):
        self.marko.add_in_edge(self.josh, "brother")
        self.assertEqual(
            self.marko.in_edge_count(),
            len(self.marko.get_in_edges()),
        )

    def test_out_edge_count(self):
        self.assertEqual(
            self.marko.out_edge_count(),
            len(self.marko.get_out_edges()),
        )

    def test_out_edge_count_remove_edge(self):
        edge = self.marko.get_out_edges().all()[0]
        self.marko.remove_edge(edge)
        self.assertEqual(
            self.marko.out_edge_count(),
            len(self.marko.get_out_edges()),
        )

    def test_in_edge_count_remove_edge(self):
        edge = self.josh.get_in_edges().all()[0]
        self.josh.remove_edge(edge)
        self.assertEqual(
            self.marko.in_edge_count(),
            len(self.marko.get_in_edges()),
        )

    def test_remove_edge(self):
        self.marko.remove_edge(self.marko_created_lop)
        self.assertEqual(
            self.marko.get_both_edges().sorted(),
            sorted([self.marko_knows_josh, self.marko_knows_vadas])
        )

    def test_remove_edge_with_unknown_head(self):
        self.assertRaises(
            VertexError,
            self.marko.remove_edge,
            self.josh_created_lop
        )

    def test_get_in_edges(self):
        self.assertEqual(
            self.josh.get_in_edges().sorted(),
            sorted([self.marko_knows_josh])
        )

    def test_get_in_edges_with_property(self):
        self.assertEqual(
            self.josh.get_in_edges(weight=1).sorted(),
            sorted([self.marko_knows_josh])
        )

    def test_get_in_edges_with_property_that_contains(self):
        self.marko_knows_josh.set_property(since="work")
        self.assertEqual(
            self.josh.get_in_edges(since__contains="r").sorted(),
            sorted([self.marko_knows_josh])
        )

    def test_get_in_edges_with_property_that_startswith(self):
        self.marko_knows_josh.set_property(since="work")
        self.assertEqual(
            self.josh.get_in_edges(since__startswith="w").sorted(),
            sorted([self.marko_knows_josh])
        )

    def test_get_in_edges_with_property_that_endswith(self):
        self.marko_knows_josh.set_property(since="work")
        self.assertEqual(
            self.josh.get_in_edges(since__endswith="k").sorted(),
            sorted([self.marko_knows_josh])
        )

    def test_get_out_edges(self):
        self.assertEqual(
            self.josh.get_out_edges().sorted(),
            sorted([self.josh_created_lop, self.josh_created_ripple])
        )

    def test_get_out_edges_with_property(self):
        self.assertEqual(
            self.josh.get_out_edges(weight=1).sorted(),
            sorted([self.josh_created_ripple])
        )

    def test_get_out_edges_with_property_that_contains(self):
        self.josh_created_lop.set_property(since="work")
        self.josh_created_ripple.set_property(since="class")
        self.assertEqual(
            self.josh.get_out_edges(since__contains="o").sorted(),
            sorted([self.josh_created_lop])
        )

    def test_get_out_edges_with_property_that_startswith(self):
        self.josh_created_lop.set_property(since="work")
        self.josh_created_ripple.set_property(since="class")
        self.assertEqual(
            self.josh.get_out_edges(since__startswith="w").sorted(),
            sorted([self.josh_created_lop])
        )

    def test_get_out_edges_with_property_that_endswith(self):
        self.josh_created_lop.set_property(since="work")
        self.josh_created_ripple.set_property(since="class")
        self.assertEqual(
            self.josh.get_out_edges(since__endswith="s").sorted(),
            sorted([self.josh_created_ripple])
        )

    def test_get_both_edges(self):
        self.assertEqual(
            self.josh.get_both_edges().sorted(),
            sorted(
                [
                    self.josh_created_lop,
                    self.josh_created_ripple,
                    self.marko_knows_josh,
                ]
            )
        )

    def test_get_both_edges_with_property(self):
        self.assertEqual(
            self.josh.get_out_edges().sorted(),
            sorted(
                [
                    self.josh_created_lop,
                    self.josh_created_ripple,
                ]
            )
        )

    def test_get_both_edges_with_property_that_contains(self):
        e = self.graph.get_or_create_edge(
            self.marko,
            "friend",
            self.josh,
            since="school",
        )
        x = self.marko.get_in_edges() | self.marko.get_out_edges()
        x.update_index(e, **e.properties)  # pylint: disable=no-member
        self.assertEqual(
            self.marko.get_both_edges(since__contains="o").all(),
            [e],
        )

    def test_get_both_edges_with_property_that_startswith(self):
        e = self.graph.get_or_create_edge(
            self.marko,
            "friend",
            self.josh,
            since="school",
        )
        self.assertEqual(
            self.marko.get_both_edges(since__startswith="s").all(),
            [e],
        )

    def test_get_both_edges_with_property_that_endswith(self):
        e = self.graph.get_or_create_edge(
            self.marko,
            "friend",
            self.josh,
            since="school",
        )
        self.assertEqual(
            self.marko.get_both_edges(since__endswith="l").all(),
            [e],
        )

    def test_get_in_vertices(self):
        self.assertEqual(
            self.josh.get_in_vertices().sorted(),
            sorted([self.marko])
        )

    def test_get_in_vertices_by_label(self):
        self.assertEqual(
            self.josh.get_in_vertices(label="person").sorted(),
            sorted([self.marko])
        )

    def test_get_in_vertices_by_property(self):
        self.assertEqual(
            self.josh.get_in_vertices(name="marko").sorted(),
            sorted([self.marko])
        )

    def test_get_in_vertices_by_property_that_contains(self):
        self.assertEqual(
            self.josh.get_in_vertices(name__contains="r").sorted(),
            sorted([self.marko])
        )

    def test_get_in_vertices_by_property_that_startswith(self):
        self.assertEqual(
            self.josh.get_in_vertices(name__startswith="m").sorted(),
            sorted([self.marko])
        )

    def test_get_in_vertices_by_property_that_endswith(self):
        self.assertEqual(
            self.josh.get_in_vertices(name__endswith="o").sorted(),
            sorted([self.marko])
        )

    def test_get_out_vertices(self):
        self.assertEqual(
            self.josh.get_out_vertices().sorted(),
            sorted([self.lop, self.ripple])
        )

    def test_get_out_vertices_by_label(self):
        self.assertEqual(
            self.josh.get_out_vertices(label="app").sorted(),
            sorted([self.lop, self.ripple])
        )

    def test_get_out_vertices_by_property(self):
        self.assertEqual(
            self.josh.get_out_vertices(name="ripple").sorted(),
            sorted([self.ripple])
        )

    def test_get_out_vertices_by_property_that_contains(self):
        self.assertEqual(
            self.josh.get_out_vertices(name__contains="i").sorted(),
            sorted([self.ripple])
        )

    def test_get_out_vertices_by_property_that_startswith(self):
        self.assertEqual(
            self.josh.get_out_vertices(name__startswith="l").sorted(),
            sorted([self.lop])
        )

    def test_get_out_vertices_by_property_that_endswith(self):
        self.assertEqual(
            self.josh.get_out_vertices(name__endswith="p").sorted(),
            sorted([self.lop])
        )

    def test_get_both_vertices(self):
        self.assertEqual(
            self.josh.get_both_vertices().sorted(),
            sorted([self.marko, self.lop, self.ripple])
        )

    def test_get_both_vertices_by_label(self):
        self.assertEqual(
            self.josh.get_both_vertices(label="person").sorted(),
            sorted([self.marko])
        )

    def test_get_both_vertices_by_property(self):
        self.assertEqual(
            self.josh.get_both_vertices(age=29).sorted(),
            sorted([self.marko])
        )

    def test_get_both_vertices_by_property_that_contains(self):
        self.assertEqual(
            self.josh.get_both_vertices(name__contains="r").sorted(),
            sorted([self.marko, self.ripple])
        )

    def test_get_both_vertices_by_property_that_startswith(self):
        self.assertEqual(
            self.josh.get_both_vertices(name__startswith="m").sorted(),
            sorted([self.marko])
        )

    def test_get_both_vertices_by_property_that_endsswith(self):
        self.assertEqual(
            self.josh.get_both_vertices(name__endswith="o").sorted(),
            sorted([self.marko])
        )

    def test_as_dict(self):
        self.assertDictEqual(
            self.marko.as_dict(),
            {
                "id": 0,
                "label": "person",
                "metadata": {
                    "in_edge_count": 0,
                    "out_edge_count": 3,
                },
                "properties": {
                    "name": "marko",
                    "age": 29,
                },
            }
        )

    def test_as_dict_skip_private_keys(self):
        self.marko.set_property(_private_name="Sasquatch")
        self.assertDictEqual(
            self.marko.as_dict(),
            {
                "id": 0,
                "label": "person",
                "metadata": {
                    "in_edge_count": 0,
                    "out_edge_count": 3,
                },
                "properties": {
                    "name": "marko",
                    "age": 29,
                },
            }
        )

    def test_as_dict_include_private_keys(self):
        self.marko.set_property(_private_name="Sasquatch")
        self.assertDictEqual(
            self.marko.as_dict(include_privates=True),
            {
                "id": 0,
                "label": "person",
                "metadata": {
                    "in_edge_count": 0,
                    "out_edge_count": 3,
                },
                "properties": {
                    "name": "marko",
                    "age": 29,
                    "_private_name": "Sasquatch",
                },
            }
        )
示例#22
0
 def test_contains_vertex_not_found(self):
     v = Vertex("person", name="not_in_graph")
     self.assertEqual(self.graph.__contains__(v), False)
示例#23
0
 def test_append_vertex(self):
     node = Vertex(label="NODE")
     self.graph.append_vertex(node)
     self.assertEqual(node.ident, self.graph._id_tracker.vid - 1)
     self.assertIn(node, self.graph)
     self.assertEqual(node.is_bound(), True)