def map_industry_blocks(): '''映射行业板块''' g = Graph(settings.NEO4J_URL, auth=(settings.NEO4J_USER, settings.NEO4J_PASSWD)) session = new_db_session() blocks = session.query(StockBlockInfo).filter( StockBlockInfo.parent_class == None).all() for block in blocks: matcher = NodeMatcher(g) industry = matcher.match(constants.NEO4J_LABEL_INDUSTRY, name=block.class_name).first() if industry: continue tx = g.begin() industry = Node(constants.NEO4J_LABEL_INDUSTRY, name=block.class_name) tx.create(industry) sub_blocks = session.query(StockBlockInfo).filter( StockBlockInfo.parent_class == block.class_id).all() for sub_block in sub_blocks: sub_industry = Node(constants.NEO4J_LABEL_INDUSTRY, name=sub_block.class_name) tx.create(sub_industry) tx.create( Relationship(industry, constants.NEO4J_RELTYPE_SUBINDUSTRY, sub_industry)) tx.commit() session.close()
def test_bound_node_equals_unbound_node_with_same_properties(self): alice_1 = Node(name="Alice") alice_1.graph = self.graph alice_1.identity = 999 alice_2 = Node(name="Alice") assert set(alice_1.labels) == set(alice_2.labels) assert dict(alice_1) == dict(alice_2)
def test_cannot_cast_relationship_from_generic_object_with_properties(self): class Foo(object): pass foo = Foo() foo.properties = {} with self.assertRaises(ValueError): Relationship.cast((Node(), foo, Node()))
def test_add_relation_not_exist(): start = Node("Person", name="Bob") end = Node("Person", name="Alice") new = nlmg.add_relationship(start, end, "LOVES", {"roles": "boyfriend"}) assert new.identity >= 0 assert dict(new) == {"roles": "boyfriend"} nlmg.graph.delete(new)
def test_pull_node_labels_if_stale(self): a = Node("Thing") self.graph.create(a) a.remove_label("Thing") a._stale.add("labels") labels = a.labels assert set(labels) == {"Thing"}
def createbinarynode(self, left, right, name): node = Node("Operator", name=name, wid=self.graph_id) self.graph.create(Relationship(left, "INPUT", node)) self.graph.create(Relationship(right, "INPUT", node)) outnode = Node('Data', wid=self.graph_id) self.graph.create(Relationship(node, "OUTPUT", outnode)) return outnode
def inst_constructor(): from py2neo.data import Node new_inst = Node() new_inst.graph = graph new_inst.identity = identity new_inst._stale.update({"labels", "properties"}) return new_inst
def create_relationship(graph, rel_tuple): """ create the new rel in the graph if the rel doesn't exist, create new nodes if necessary :param graph: :param rel_tuple: :return: """ label_1 = str(rel_tuple[0][0]) name_1 = str(rel_tuple[0][1]) label_2 = str(rel_tuple[2][0]) name_2 = str(rel_tuple[2][1]) rel_type = str(rel_tuple[1][0]) rel_name = str(rel_tuple[1][1]) # check dup matcher = NodeMatcher(graph) node_1 = matcher.match(label_1, name=name_1).first() node_2 = matcher.match(label_2, name=name_2).first() if node_1 and node_2: r_matcher = RelationshipMatcher(graph) first_rel = r_matcher.match([node_1, node_2], rel_type, name=rel_name).first() if first_rel: return elif node_1 and not node_2: node_2 = Node(label_2, name=name_2) elif node_2 and not node_1: node_1 = Node(label_1, name=name_1) else: node_1 = Node(label_1, name=name_1) node_2 = Node(label_2, name=name_2) # create rel rel = Relationship(node_1, rel_type, node_2, name=rel_name) graph.create(rel)
def test_relationship_does_not_exist(self): a = Node() b = Node() self.graph.create(a | b) r = Relationship(a, "TO", b) self.assertIsNot(r.graph, self.graph) self.assertFalse(self.graph.exists(r))
def _hydrate_relationship(self, identity, start_node_id, end_node_id, r_type, properties): start_node = Node.ref(self.graph, start_node_id) end_node = Node.ref(self.graph, end_node_id) rel = Relationship.ref(self.graph, identity, start_node, r_type, end_node) rel.clear() rel.update(properties) return rel
def test_relationship_creation(self): a = Node() b = Node() r = Relationship(a, "TO", b) self.graph.create(r) self.assertEqual(r.graph, self.graph) self.assertIsNotNone(r.identity)
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": path[0].identity, "bc": path[1].identity, "cd": path[2].identity } path[0]["amount"] = "lots" path[1]["amount"] = "some" path[2]["since"] = 1999 ab_amount, bc_amount, cd_since = next( self.graph.run(statement, parameters)) 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 = next( self.graph.run(statement, parameters)) assert ab_amount == "lots" assert bc_amount == "some" assert cd_since == 1999
def test_can_get_relationship_by_id_when_cached(self): a = Node() b = Node() r = Relationship(a, "TO", b) self.graph.create(r) got = self.graph.relationships.get(r.identity) assert got is r
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 = path[0].identity id_1 = path[1].identity id_2 = path[2].identity 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
def test_can_construct_simple_path(self): alice = Node(name="Alice") bob = Node(name="Bob") path = Path(alice, "KNOWS", bob) assert len(path.nodes) == 2 assert len(path.relationships) == 1 assert len(path) == 1
def test_should_push_one_additional_label(self): node = Node("A") self.graph.create(node) node_id = node.identity self.assert_has_labels(node_id, {"A"}) node.add_label("B") self.graph.push(node) self.assert_has_labels(node_id, {"A", "B"})
def test_relationship_equality_for_concrete(self): a = Node() b = Node() r1 = Relationship(a, "KNOWS", b) r2 = Relationship(a, "KNOWS", b) self.graph.create(r1) self.graph.create(r2) self.assertEqual(r1, r2)
def test_can_cast_relationship_with_integer_nodes(self): a = Node() b = Node() nodes = [a, b] r = Relationship.cast((0, "TO", 1), nodes) self.assertIs(r.start_node, a) assert r.end_node is b assert type(r).__name__ == "TO"
def test_should_push_one_label_onto_no_labels(self): node = Node() self.graph.create(node) node_id = node.identity self.assert_has_labels(node_id, {}) node.add_label("A") self.graph.push(node) self.assert_has_labels(node_id, {"A"})
def test_cannot_get_relationship_by_id_when_id_does_not_exist(self): a = Node() b = Node() r = Relationship(a, "TO", b) self.graph.create(r) rel_id = r.identity self.graph.delete(r) with self.assertRaises(KeyError): _ = self.graph.relationships[rel_id]
def __ogm__(self): if self.__ogm is None: if isinstance(self.__primarylabel__, tuple): node = Node(*self.__primarylabel__) else: node = Node(self.__primarylabel__) node.__model__ = self.__class__ self.__ogm = OGM(self, node) return self.__ogm
def test_can_merge_unsaved_changes_when_querying_node(self): a = Node("Person", name="Alice") b = Node() self.graph.create(a | b | Relationship(a, "KNOWS", b)) assert dict(a) == {"name": "Alice"} a["age"] = 33 assert dict(a) == {"name": "Alice", "age": 33} _ = list(self.graph.match((a, None), "KNOWS")) assert dict(a) == {"name": "Alice", "age": 33}
def setUp(self): TO = Relationship.type("TO") self.graph.delete_all() a = self.a = Node() b = self.b = Node() c = self.c = Node() d = self.d = Node() self.r = [TO(a, b), TO(b, a), TO(b, c), TO(b, b), TO(c, d), TO(a, d)] self.graph.create(reduce(or_, self.r))
def test_can_delete_relationship_by_separating(self): a = Node() b = Node() r = Relationship(a, "TO", b) self.graph.create(r) assert self.graph.exists(r) self.graph.separate(r) assert not self.graph.exists(r) assert self.graph.exists(a) assert self.graph.exists(b)
def test_can_merge_with_arguments(self): a = Node("A", a=1) b = Node("B", b=2) self.graph.create(a | b) a_id = a.identity b_id = b.identity node = Node("A", "B", a=1, b=2) self.graph.merge(node, "A", "a") assert node.identity == a_id assert node.identity != b_id
def test_can_cast_3_tuple(self): a = Node() b = Node() casted = Relationship.cast((a, "KNOWS", b)) self.assertIsInstance(casted, Relationship) self.assertIsNone(casted.graph) self.assertIsNone(casted.identity) assert casted.start_node == a self.assertIs(type(casted), KNOWS) assert casted.end_node == b
def test_blank_type_automatically_updates(self): a = Node() b = Node() r = Relationship(a, "TO", b) self.graph.create(r) r._type = None self.assertIsNotNone(r.graph) self.assertIsNotNone(r.identity) self.assertIsNone(r._type) self.assertEqual(type(r).__name__, "TO")
def test_path_equality(self): alice = Node(name="Alice") bob = Node(name="Bob") carol = Node(name="Carol") dave = Node(name="Dave") path_1 = Path(alice, "LOVES", bob, Relationship(carol, "HATES", bob), carol, "KNOWS", dave) path_2 = Path(alice, "LOVES", bob, Relationship(carol, "HATES", bob), carol, "KNOWS", dave) assert path_1 == path_2
def test_can_merge_node_that_does_exist(self): self.graph.create(Node("Person", name="Alice")) alice = Node("Person", name="Alice") old_order = len(self.graph.nodes) self.graph.merge(alice, "Person", "name") self.assertEqual(alice.graph, self.graph) self.assertIsNotNone(alice.identity) assert self.graph.exists(alice) new_order = len(self.graph.nodes) assert new_order == old_order
def test_can_cast_from_tuple_of_entities(self): a = Node() b = Node() r = Relationship(a, "TO", b) casted = Relationship.cast((a, r, b)) self.assertIsInstance(casted, Relationship) self.assertIsNone(casted.graph) self.assertIsNone(casted.identity) assert casted.start_node == a assert type(casted).__name__ == "TO" assert casted.end_node == b