Esempio n. 1
0
 def test_can_pull_path(self):
     alice = Node(name="Alice")
     bob = Node(name="Bob")
     carol = Node(name="Carol")
     dave = Node(name="Dave")
     path = Path(alice, "LOVES", bob, Relationship(carol, "HATES", bob),
                 carol, "KNOWS", dave)
     self.graph.create(path)
     assert path[0]["amount"] is None
     assert path[1]["amount"] is None
     assert path[2]["since"] is None
     statement = (
         "MATCH ()-[ab]->() WHERE id(ab)={ab} "
         "MATCH ()-[bc]->() WHERE id(bc)={bc} "
         "MATCH ()-[cd]->() WHERE id(cd)={cd} "
         "SET ab.amount = 'lots', bc.amount = 'some', cd.since = 1999")
     id_0 = remote(path[0])._id
     id_1 = remote(path[1])._id
     id_2 = remote(path[2])._id
     parameters = {"ab": id_0, "bc": id_1, "cd": id_2}
     self.graph.run(statement, parameters)
     self.graph.pull(path)
     assert path[0]["amount"] == "lots"
     assert path[1]["amount"] == "some"
     assert path[2]["since"] == 1999
Esempio n. 2
0
 def test_can_write_with_wrapper_function(self):
     alice, bob, carol, dave = Node(name="Alice"), Node(name="Bob"), \
                               Node(name="Carol"), Node(name="Dave")
     path = Path(alice, "LOVES", bob, Relationship(carol, "HATES", bob),
                 carol, "KNOWS", dave)
     written = cypher_repr(path)
     assert written == "(alice)-[:LOVES]->(bob)<-[:HATES]-(carol)-[:KNOWS]->(dave)"
Esempio n. 3
0
 def test_can_push_path(self):
     alice = Node(name="Alice")
     bob = Node(name="Bob")
     carol = Node(name="Carol")
     dave = Node(name="Dave")
     path = Path(alice, "LOVES", bob, Relationship(carol, "HATES", bob),
                 carol, "KNOWS", dave)
     self.graph.create(path)
     statement = ("MATCH ()-[ab]->() WHERE id(ab)={ab} "
                  "MATCH ()-[bc]->() WHERE id(bc)={bc} "
                  "MATCH ()-[cd]->() WHERE id(cd)={cd} "
                  "RETURN ab.amount, bc.amount, cd.since")
     parameters = {
         "ab": remote(path[0])._id,
         "bc": remote(path[1])._id,
         "cd": remote(path[2])._id
     }
     path[0]["amount"] = "lots"
     path[1]["amount"] = "some"
     path[2]["since"] = 1999
     ab_amount, bc_amount, cd_since = self.graph.run(statement,
                                                     parameters).next()
     assert ab_amount is None
     assert bc_amount is None
     assert cd_since is None
     self.graph.push(path)
     ab_amount, bc_amount, cd_since = self.graph.run(statement,
                                                     parameters).next()
     assert ab_amount == "lots"
     assert bc_amount == "some"
     assert cd_since == 1999
Esempio n. 4
0
 def test_can_write_relationship_with_name(self):
     r = Relationship(Node(name="Fred"), "LIVES WITH", Node(name="Wilma"))
     string = StringIO()
     writer = CypherWriter(string)
     writer.write_relationship(r, name="fred_wilma")
     written = string.getvalue()
     assert written == '(fred)-[fred_wilma:`LIVES WITH`]->(wilma)'
Esempio n. 5
0
 def test_can_delete_nodes_and_relationship_nodes_first(self):
     alice = Node("Person", name="Alice")
     bob = Node("Person", name="Bob")
     ab = Relationship(alice, "KNOWS", bob)
     self.graph.create(alice | bob | ab)
     assert self.graph.exists(alice | bob | ab)
     self.graph.delete(alice | bob | ab)
     assert not self.graph.exists(alice | bob | ab)
Esempio n. 6
0
 def test_can_delete_path(self):
     alice, bob, carol, dave = Node(), Node(), Node(), Node()
     path = Path(alice, "LOVES", bob, Relationship(carol, "HATES", bob),
                 carol, "KNOWS", dave)
     self.graph.create(path)
     assert self.graph.exists(path)
     self.graph.delete(path)
     assert not self.graph.exists(path)
Esempio n. 7
0
 def test_can_write_relationship_with_properties(self):
     r = Relationship(Node(name="Fred"), ("LIVES WITH", {
         "place": "Bedrock"
     }), Node(name="Wilma"))
     string = StringIO()
     writer = CypherWriter(string)
     writer.write(r)
     written = string.getvalue()
     assert written == '(fred)-[:`LIVES WITH` {place:"Bedrock"}]->(wilma)'
Esempio n. 8
0
 def test_can_graph_pull_node(self):
     alice_1 = Node()
     alice_2 = Node("Person", name="Alice")
     self.graph.create(alice_2)
     assert set(alice_1.labels()) == set()
     assert dict(alice_1) == {}
     alice_1.__remote__ = RemoteEntity(remote(alice_2).uri)
     self.graph.pull(alice_1)
     assert set(alice_1.labels()) == set(alice_2.labels())
     assert dict(alice_1) == dict(alice_2)
Esempio n. 9
0
 def test_can_create_relationship(self):
     a = Node("Person", name="Alice")
     b = Node("Person", name="Bob")
     r = Relationship(a, "KNOWS", b, since=1999)
     self.graph.create(r)
     assert remote(a)
     assert remote(b)
     assert remote(r)
     assert r.start_node() == a
     assert r.end_node() == b
Esempio n. 10
0
 def test_can_write_simple_path(self):
     alice, bob, carol, dave = Node(name="Alice"), Node(name="Bob"), \
                               Node(name="Carol"), Node(name="Dave")
     path = Path(alice, "LOVES", bob, Relationship(carol, "HATES", bob),
                 carol, "KNOWS", dave)
     string = StringIO()
     writer = CypherWriter(string)
     writer.write(path)
     written = string.getvalue()
     assert written == "(alice)-[:LOVES]->(bob)<-[:HATES]-(carol)-[:KNOWS]->(dave)"
Esempio n. 11
0
 def test_can_write_simple_relationship(self):
     a = Node()
     b = Node()
     r = Relationship(a, "KNOWS", b)
     a.__name__ = "a"
     b.__name__ = "b"
     string = StringIO()
     writer = CypherWriter(string)
     writer.write(r)
     written = string.getvalue()
     assert written == "(a)-[:KNOWS]->(b)"
Esempio n. 12
0
 def test_walkable_repr(self):
     a = Node("Person", name="Alice")
     b = Node("Person", name="Bob")
     c = Node("Person", name="Carol")
     d = Node("Person", name="Dave")
     ab = Relationship(a, "LOVES", b)
     cb = Relationship(c, "HATES", b)
     cd = Relationship(c, "KNOWS", d)
     t = Walkable([a, ab, b, cb, c, cd, d])
     r = repr(t)
     expected = "(alice)-[:LOVES]->(bob)<-[:HATES]-(carol)-[:KNOWS]->(dave)"
     assert r == expected
Esempio n. 13
0
 def test_can_add_single_relationship(self):
     alice = Node(name="Alice Smith")
     bob = Node(name="Bob Smith")
     ab = Relationship(alice, "KNOWS", bob)
     self.graph.create(alice | bob | ab)
     self.batch.add_to_index(Relationship, self.friendships, "friends",
                             "alice_&_bob", ab)
     self.batch.run()
     # check entries
     rels = self.friendships.get("friends", "alice_&_bob")
     assert len(rels) == 1
     assert ab in rels
     # done
     self.recycling = [ab, alice, bob]
Esempio n. 14
0
 def test_can_add_two_similar_nodes(self):
     alice = Node(name="Alice Smith")
     bob = Node(name="Bob Smith")
     self.graph.create(alice | bob)
     self.batch.add_to_index(Node, self.people, "surname", "Smith", alice)
     self.batch.add_to_index(Node, self.people, "surname", "Smith", bob)
     nodes = self.batch.run()
     assert nodes[0] != nodes[1]
     # check entries
     smiths = self.people.get("surname", "Smith")
     assert len(smiths) == 2
     assert alice in smiths
     assert bob in smiths
     # done
     self.graph.delete(alice | bob)
Esempio n. 15
0
 def test_can_add_nodes_only_if_none_exist(self):
     alice = Node(name="Alice Smith")
     bob = Node(name="Bob Smith")
     self.graph.create(alice | bob)
     self.batch.get_or_add_to_index(Node, self.people, "surname", "Smith",
                                    alice)
     self.batch.get_or_add_to_index(Node, self.people, "surname", "Smith",
                                    bob)
     nodes = self.batch.run()
     assert nodes[0] == nodes[1]
     # check entries
     smiths = self.people.get("surname", "Smith")
     assert len(smiths) == 1
     assert alice in smiths
     # done
     self.graph.delete(alice | bob)
Esempio n. 16
0
 def test_can_write_node_with_properties(self):
     node = Node(name="Gertrude", age=3)
     string = StringIO()
     writer = CypherWriter(string)
     writer.write(node)
     written = string.getvalue()
     assert written == '(gertrude {age:3,name:"Gertrude"})'
Esempio n. 17
0
 def test_can_write_node_with_labels_and_properties(self):
     node = Node("Dark Brown", "Chicken", name="Gertrude", age=3)
     string = StringIO()
     writer = CypherWriter(string)
     writer.write(node)
     written = string.getvalue()
     assert written == '(gertrude:Chicken:`Dark Brown` {age:3,name:"Gertrude"})'
Esempio n. 18
0
 def setUp(self):
     try:
         self.index_manager.delete_index(Node, "node_removal_test_index")
     except LookupError:
         pass
     self.index = self.index_manager.get_or_create_index(
         Node, "node_removal_test_index")
     self.fred = Node(name="Fred Flintstone")
     self.wilma = Node(name="Wilma Flintstone")
     self.graph.create(self.fred | self.wilma)
     self.index.add("name", "Fred", self.fred)
     self.index.add("name", "Wilma", self.wilma)
     self.index.add("name", "Flintstone", self.fred)
     self.index.add("name", "Flintstone", self.wilma)
     self.index.add("flintstones", "%", self.fred)
     self.index.add("flintstones", "%", self.wilma)
     self.batch = ManualIndexWriteBatch(self.graph)
Esempio n. 19
0
 def test_query_can_return_collection(self):
     node = Node()
     self.graph.create(node)
     statement = "MATCH (a) WHERE id(a)={N} RETURN collect(a) AS a_collection"
     params = {"N": remote(node)._id}
     cursor = self.graph.run(statement, params)
     record = cursor.next()
     assert record["a_collection"] == [node]
Esempio n. 20
0
 def test_param_used_once(self):
     node = Node()
     self.graph.create(node)
     statement = "MATCH (a) WHERE id(a)={X} RETURN a"
     params = {"X": remote(node)._id}
     cursor = self.graph.run(statement, params)
     record = cursor.next()
     assert record["a"] == node
Esempio n. 21
0
 def test_param_used_twice(self):
     node = Node()
     self.graph.create(node)
     statement = "MATCH (a) WHERE id(a)={X} MATCH (b) WHERE id(b)={X} RETURN a, b"
     params = {"X": remote(node)._id}
     record = self.graph.run(statement, params).next()
     assert record["a"] == node
     assert record["b"] == node
Esempio n. 22
0
 def test_can_write_simple_node(self):
     node = Node()
     node.__name__ = "a"
     string = StringIO()
     writer = CypherWriter(string)
     writer.write(node)
     written = string.getvalue()
     assert written == "(a)"
Esempio n. 23
0
 def test_can_write_node_with_labels(self):
     node = Node("Dark Brown", "Chicken")
     node.__name__ = "a"
     string = StringIO()
     writer = CypherWriter(string)
     writer.write(node)
     written = string.getvalue()
     assert written == '(a:Chicken:`Dark Brown`)'
Esempio n. 24
0
 def test_can_push_relationship(self):
     a = Node()
     b = Node()
     ab = Relationship(a, "KNOWS", b)
     self.graph.create(ab)
     value = self.graph.evaluate(
         "MATCH ()-[ab:KNOWS]->() WHERE id(ab)={i} "
         "RETURN ab.since",
         i=remote(ab)._id)
     assert value is None
     ab["since"] = 1999
     self.graph.push(ab)
     value = self.graph.evaluate(
         "MATCH ()-[ab:KNOWS]->() WHERE id(ab)={i} "
         "RETURN ab.since",
         i=remote(ab)._id)
     assert value == 1999
Esempio n. 25
0
 def __ogm__(self):
     if self.__ogm is None:
         self.__ogm = OGM(Node(self.__primarylabel__))
     node = self.__ogm.node
     if not hasattr(node, "__primarylabel__"):
         setattr(node, "__primarylabel__", self.__primarylabel__)
     if not hasattr(node, "__primarykey__"):
         setattr(node, "__primarykey__", self.__primarykey__)
     return self.__ogm
Esempio n. 26
0
 def test_subgraph_repr(self):
     a = Node("Person", name="Alice")
     b = Node("Person", name="Bob")
     ab = Relationship(a, "TO", b)
     ba = Relationship(b, "FROM", a)
     s = ab | ba
     assert isinstance(s, Subgraph)
     r = repr(s)
     assert r.startswith("({")
     assert r.endswith("})")
     nodes, _, relationships = r[2:-2].partition("}, {")
     items = [item.strip() for item in nodes.split(",")]
     assert len(items) == 2
     for i, item in enumerate(items):
         assert re.match(
             r'\(_?[0-9A-Za-z]+:Person \{name:"(Alice|Bob)"\}\)', item)
     items = [item.strip() for item in relationships.split(",")]
     assert len(items) == 2
     for _ in items:
         assert re.match(r'\(.*\)-\[:(TO|FROM)\]->\(.*\)', repr(ab))
Esempio n. 27
0
 def test_can_add_single_node(self):
     alice = Node(name="Alice Smith")
     self.graph.create(alice)
     self.batch.add_to_index(Node, self.people, "surname", "Smith", alice)
     self.batch.run()
     # check entries
     smiths = self.people.get("surname", "Smith")
     assert len(smiths) == 1
     assert alice in smiths
     # done
     self.graph.delete(alice)
Esempio n. 28
0
 def test_param_used_thrice(self):
     node = Node()
     self.graph.create(node)
     statement = "MATCH (a) WHERE id(a)={X} " \
                 "MATCH (b) WHERE id(b)={X} " \
                 "MATCH (c) WHERE id(c)={X} " \
                 "RETURN a, b, c"
     params = {"X": remote(node)._id}
     cursor = self.graph.run(statement, params)
     record = cursor.next()
     assert record["a"] == node
     assert record["b"] == node
     assert record["c"] == node
Esempio n. 29
0
 def test_can_create_nodes_and_relationship(self):
     self.graph.delete_all()
     a = Node()
     b = Node()
     c = Node()
     ab = Relationship(a, "TO", b)
     bc = Relationship(b, "TO", c)
     ca = Relationship(c, "TO", a)
     self.graph.create(ab | bc | ca)
     assert remote(a)
     assert remote(b)
     assert remote(c)
     assert remote(ab)
     assert ab.start_node() == a
     assert ab.end_node() == b
     assert remote(bc)
     assert bc.start_node() == b
     assert bc.end_node() == c
     assert remote(ca)
     assert ca.start_node() == c
     assert ca.end_node() == a
     assert order(self.graph) == 3
     assert size(self.graph) == 3
Esempio n. 30
0
 def test_relationship_property_pull_scenarios(self):
     property_sets = [{}, {
         "name": "Alice"
     }, {
         "name": "Alice",
         "age": 33
     }, {
         "name": "Bob"
     }]
     for old_props in property_sets:
         for new_props in property_sets:
             a = Node()
             b = Node()
             relationship = Relationship(a, "TO", b, **old_props)
             self.graph.create(relationship)
             relationship_id = remote(relationship)._id
             assert dict(relationship) == old_props
             self.graph.run("MATCH ()-[r]->() WHERE id(r)={x} SET r={y}",
                            x=relationship_id,
                            y=new_props)
             self.graph.pull(relationship)
             assert dict(relationship) == new_props, \
                 "Failed to pull new properties %r over old properties %r" % \
                 (new_props, old_props)