def test_can_push_path(graph): alice = Node(name="Alice") bob = Node(name="Bob") carol = Node(name="Carol") dave = Node(name="Dave") path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave) graph.create(path) if graph.neo4j_version >= (2, 0, 0): query = """\ START ab=rel({ab}), bc=rel({bc}), cd=rel({cd}) RETURN ab.amount, bc.amount, cd.since """ else: query = """\ START ab=rel({ab}), bc=rel({bc}), cd=rel({cd}) RETURN ab.amount?, bc.amount?, cd.since? """ params = {"ab": path[0]._id, "bc": path[1]._id, "cd": path[2]._id} path[0].properties["amount"] = "lots" path[1].properties["amount"] = "some" path[2].properties["since"] = 1999 results = graph.cypher.execute(query, params) ab_amount, bc_amount, cd_since = results[0].values assert ab_amount is None assert bc_amount is None assert cd_since is None path.push() results = graph.cypher.execute(query, params) ab_amount, bc_amount, cd_since = results[0].values assert ab_amount == "lots" assert bc_amount == "some" assert cd_since == 1999
def test_unbound_path_is_not_bound(): alice = Node(name="Alice") bob = Node(name="Bob") carol = Node(name="Carol") dave = Node(name="Dave") path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave) assert not path.bound
def test_bound_path_is_bound(graph): alice = Node(name="Alice") bob = Node(name="Bob") carol = Node(name="Carol") dave = Node(name="Dave") path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave) graph.create(path) assert path.bound
def test_can_unbind_unbound_path_without_error(): alice = Node(name="Alice") bob = Node(name="Bob") carol = Node(name="Carol") dave = Node(name="Dave") path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave) path.unbind() assert not path.bound
def test_can_pull_node_labels_only(graph): if not graph.supports_node_labels: return local = Node() remote = Node("Person") graph.create(remote) assert local.labels == set() local.bind(remote.uri) local.labels.pull() assert local.labels == remote.labels
def test_can_push_node(graph): local = Node("Person", name="Alice") remote = Node() graph.create(remote) assert remote.labels == set() assert remote.properties == {} local.bind(remote.uri) local.push() assert remote.labels == remote.labels assert remote.properties == remote.properties
def test_can_bind_node_to_resource(): uri = "http://localhost:7474/db/data/node/1" node = Node() node.bind(uri) assert node.bound assert isinstance(node.resource, Resource) assert node.resource.uri == uri node.unbind() assert not node.bound with pytest.raises(BindError): r = node.resource
def test_can_graph_pull_node(graph): if not graph.supports_node_labels: return local = Node() remote = Node("Person", name="Alice") graph.create(remote) assert local.labels == set() assert local.properties == {} local.bind(remote.uri) graph.pull(local) assert local.labels == remote.labels assert local.properties == remote.properties
def test_cannot_use_a_pointer_that_does_not_refer_to_a_node(graph): alice = Node(name="Alice") bob = Node(name="Bob") alice_knows_bob = Relationship(0, "KNOWS", 1) broken_relationship = Relationship(0, "KNOWS", 2) statement = CreateStatement(graph) statement.create(alice) statement.create(bob) statement.create(alice_knows_bob) try: statement.create(broken_relationship) except ValueError: assert True else: assert False
def test_can_create_two_nodes_and_a_relationship(graph): alice = Node(name="Alice") bob = Node(name="Bob") alice_knows_bob = Relationship(alice, "KNOWS", bob) statement = CreateStatement(graph) statement.create(alice) statement.create(bob) statement.create(alice_knows_bob) created = statement.execute() assert created == (alice, bob, alice_knows_bob) assert alice.bound assert bob.bound assert alice_knows_bob.bound assert alice_knows_bob.start_node is alice assert alice_knows_bob.end_node is bob
def test_can_create_naked_node(graph): node = Node() statement = CreateStatement(graph) statement.create(node) created = statement.execute() assert created == (node, ) assert node.bound
def test_can_create_node_with_properties(graph): node = Node(name="Alice") statement = CreateStatement(graph) statement.create(node) created = statement.execute() assert created == (node, ) assert node.bound
def test_can_output_correct_representation_with_no_rows(graph): alice = Node(name="Alice") graph.create(alice) results = graph.cypher.execute( "START a=node({A}) MATCH (a)-[:KNOWS]->(x) RETURN x", {"A": alice}) representation = repr(results) assert representation == (" x \n" "---\n" "(0 rows)\n")
def __init__(self, primary_label, primary_key=None, primary_value=None): CypherTask.__init__(self) self.__node = Node(primary_label) if primary_key is not None: self.__node.properties[primary_key] = CypherParameter("V", primary_value) self.__labels = LabelSet() self.__properties = PropertySet() self.__return = False
def test_cannot_create_unique_zero_length_path(graph): path = Path(Node()) statement = CreateStatement(graph) try: statement.create_unique(path) except ValueError: assert True else: assert False
def test_can_create_node_with_label(graph): if not graph.supports_node_labels: return node = Node("Person", name="Alice") statement = CreateStatement(graph) statement.create(node) created = statement.execute() assert created == (node, ) assert node.bound
def test_cannot_create_unique_node(graph): node = Node(name="Alice") statement = CreateStatement(graph) try: statement.create_unique(node) except TypeError: assert True else: assert False
def test_can_create_a_path_with_existing_nodes(graph): alice = graph.cypher.execute_one("CREATE (a {name:'Alice'}) RETURN a") bob = Node(name="Bob") carol = graph.cypher.execute_one("CREATE (c {name:'Carol'}) RETURN c") dave = Node(name="Dave") path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave) statement = CreateStatement(graph) statement.create(path) created = statement.execute() assert created == (path, ) assert bob.bound assert dave.bound ab, cb, cd = path.relationships assert ab.start_node is alice assert ab.end_node is bob assert cb.start_node is carol assert cb.end_node is bob assert cd.start_node is carol assert cd.end_node is dave
def test_can_create_an_entirely_new_path(graph): alice = Node(name="Alice") bob = Node(name="Bob") carol = Node(name="Carol") dave = Node(name="Dave") path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave) statement = CreateStatement(graph) statement.create(path) created = statement.execute() assert created == (path, ) assert alice.bound assert bob.bound assert carol.bound assert dave.bound ab, cb, cd = path.relationships assert ab.start_node is alice assert ab.end_node is bob assert cb.start_node is carol assert cb.end_node is bob assert cd.start_node is carol assert cd.end_node is dave
def test_can_create_one_node_and_a_relationship_to_an_existing_node(graph): alice = graph.cypher.execute_one("CREATE (a {name:'Alice'}) RETURN a") bob = Node(name="Bob") alice_knows_bob = Relationship(alice, "KNOWS", bob) statement = CreateStatement(graph) statement.create(bob) statement.create(alice_knows_bob) created = statement.execute() assert created == (bob, alice_knows_bob) assert bob.bound assert alice_knows_bob.bound assert alice_knows_bob.start_node is alice assert alice_knows_bob.end_node is bob
def test_can_pull_path(graph): alice = Node(name="Alice") bob = Node(name="Bob") carol = Node(name="Carol") dave = Node(name="Dave") path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave) graph.create(path) assert path[0].properties["amount"] is None assert path[1].properties["amount"] is None assert path[2].properties["since"] is None assert path[0].rel.properties["amount"] is None assert path[1].rel.properties["amount"] is None assert path[2].rel.properties["since"] is None graph.cypher.run("""\ START ab=rel({ab}), bc=rel({bc}), cd=rel({cd}) SET ab.amount = "lots", bc.amount = "some", cd.since = 1999 """, {"ab": path[0]._id, "bc": path[1]._id, "cd": path[2]._id}) path.pull() assert path[0].properties["amount"] == "lots" assert path[1].properties["amount"] == "some" assert path[2].properties["since"] == 1999 assert path[0].rel.properties["amount"] == "lots" assert path[1].rel.properties["amount"] == "some" assert path[2].rel.properties["since"] == 1999
def test_cypher_job_with_non_existent_node_id(graph): node = Node() graph.create(node) node_id = node._id graph.delete(node) batch = WriteBatch(graph) batch.append(CypherJob("START n=node({N}) RETURN n", {"N": node_id})) try: batch.submit() except BatchError as error: assert error.batch is batch assert error.job_id == 0 assert error.status_code == 400 assert error.uri == "cypher" else: assert False
def test_can_bind_node_to_resource(): uri = "http://localhost:7474/db/data/node/1" node = Node() node.bind(uri) assert node.bound assert isinstance(node.resource, Resource) assert node.resource.uri == uri node.unbind() assert not node.bound with pytest.raises(BindError): _ = node.resource
def _create_in_index(self, cls, index, key, value, abstract, query=None): uri = self._uri_for(self._index(cls, index), query=query) if cls is Node: node = Node.cast(abstract) return self.append_post(uri, { "key": key, "value": value, "properties": node.properties, }) elif cls is Relationship: relationship = Relationship.cast(abstract) return self.append_post(uri, { "key": key, "value": value, "start": self._uri_for(abstract.start_node), "type": str(abstract.type), "end": self._uri_for(abstract.end_node), "properties": relationship.properties, }) else: raise TypeError(cls)
def _create_in_index(self, cls, index, key, value, abstract, query=None): uri = self._uri_for(self._index(cls, index), query=query) if cls is Node: node = Node.cast(abstract) return self.append_post(uri, { "key": key, "value": value, "properties": node.properties, }) elif cls is Relationship: relationship = Relationship.cast(abstract) return self.append_post( uri, { "key": key, "value": value, "start": self._uri_for(abstract.start_node), "type": str(abstract.type), "end": self._uri_for(abstract.end_node), "properties": relationship.properties, }) else: raise TypeError(cls)
def test_can_write_node_with_labels(): r = Representation() r.write(Node("Dark Brown", "Chicken")) written = repr(r) assert written == '(:Chicken:`Dark Brown`)'
def test_can_write_simple_node(): r = Representation() r.write(Node()) written = repr(r) assert written == "()"
def test_can_write_node_with_properties(): r = Representation() r.write(Node(name="Gertrude", age=3)) written = repr(r) assert written == '({age:3,name:"Gertrude"})'
def test_can_write_node_with_labels_and_properties(): r = Representation() r.write(Node("Dark Brown", "Chicken", name="Gertrude", age=3)) written = repr(r) assert written == '(:Chicken:`Dark Brown` {age:3,name:"Gertrude"})'
def test_default_state_for_node_is_unbound(): node = Node() assert not node.bound with pytest.raises(BindError): _ = node.resource
def test_can_create_single_node_with_streaming(self): self.batch.create(Node(name="Alice")) for result in self.batch.stream(): assert isinstance(result, Node) assert result.properties == {"name": "Alice"}
entities = {} print "BEGIN" # create entities for line in lines: m = re.findall('<(.*?)>', line, re.DOTALL) if len(m) > 0: from_url = m[0][len(r_ent):] relation = m[1] toks = relation.split("/") relation = toks[len(toks) - 1] if from_url not in entities: fff = entities[from_url] = batch.create(Node("E", N=from_url)) else: fff = entities[from_url] name = re.findall(regexp, line, re.DOTALL) if len(name) > 0: name = name[0] name = name[1:len(name) - 1] ttt = batch.create(Node("P", N=name)) else: to_url = m[2][len(r_ent):] if to_url not in entities: ttt = entities[to_url] = batch.create(Node("E", N=to_url)) else: ttt = entities[to_url]
def __init__(self, *labels, **properties): CypherTask.__init__(self) self.__node = Node(*labels, **properties) self.__return = False