def test_nodeset_merge_preserve_keeps_append_props(self, graph, clear_graph): """ Merge a nodeset 3 times and check number of nodes. """ ns = NodeSet(['Test'], merge_keys=['uuid'], append_props=['key'], preserve=['key']) for i in range(100): ns.add_node({'uuid': i, 'key': 'value'}) ns.merge(graph) assert list( graph.run("MATCH (n:Test) where 'value' IN n.key RETURN count(n)") )[0][0] == 100 append_ns = NodeSet(['Test'], merge_keys=['uuid'], append_props=['key'], preserve=['key']) for i in range(100): append_ns.add_node({'uuid': i, 'key': 'other_value'}) append_ns.merge(graph) assert list( graph.run("MATCH (n:Test) where 'value' IN n.key RETURN count(n)") )[0][0] == 100 assert list( graph.run( "MATCH (n:Test) where 'other_value' IN n.key RETURN count(n)") )[0][0] == 0
def test_node_set_from_dict(): people = NodeSet(["Person"], merge_keys=["name"]) people.add_node({"name": "Tom"}) people.add_node({"name": "Mary"}) people_dic = people.to_dict() people_copy = NodeSet.from_dict(people_dic) assert people_copy.to_dict() == people_dic
def test_nodeset_merge_preserve(self, graph, clear_graph): """ Merge a nodeset 3 times and check number of nodes. """ ns = NodeSet(['Test'], merge_keys=['uuid']) for i in range(100): ns.add_node({'uuid': i, 'key': 'value'}) ns.merge(graph) do_not_overwrite_ns = NodeSet(['Test'], merge_keys=['uuid'], preserve=['key']) for i in range(100): do_not_overwrite_ns.add_node({'uuid': i, 'key': 'other_value'}) do_not_overwrite_ns.merge(graph) assert list( graph.run("MATCH (n:Test) where n.key = 'value' RETURN count(n)") )[0][0] == 100 assert list( graph.run( "MATCH (n:Test) where n.key = 'other_value' RETURN count(n)") )[0][0] == 0
def test_relationshipset_csv_create(self, graph, clear_graph, neo4j_import_dir): # create the nodes required here ns1 = NodeSet(['Test', 'Other'], merge_keys=['uuid', 'numerical']) ns2 = NodeSet(['Foo', 'SomeLabel'], merge_keys=['uuid', 'value']) for i in range(20): ns1.add_node({'uuid': i, 'numerical': 1}) ns2.add_node({'uuid': i, 'value': 'foo'}) ns1.create_index(graph) ns1.create(graph) ns2.create_index(graph) ns2.create(graph) rs = RelationshipSet('TEST', ['Test', 'Other'], ['Foo', 'SomeLabel'], ['uuid', 'numerical'], ['uuid', 'value']) rs.uuid = 'peter' rs.create_index(graph) for i in range(10): rs.add_relationship({ 'uuid': i, 'numerical': 1 }, { 'uuid': i, 'value': 'foo' }, { 'value': i, 'other_value': 'peter' }) # add a few relationships with different props for i in range(10, 20): rs.add_relationship({ 'uuid': i, 'numerical': 1 }, { 'uuid': i, 'value': 'foo' }, { 'second_value': i, 'other_second_value': 'peter' }) path = rs.to_csv(neo4j_import_dir) # note: this is a hack to copy files into a running Docker container from Python # needed to run the tests without too many changes locally and in GitHub Actions copy_to_all_docker_containers(path, '/var/lib/neo4j/import') query = rs.csv_query('CREATE') graph.run(query) result = graph.run( "MATCH (source:Test:Other)-[r:TEST]->(target:Foo:SomeLabel) RETURN r" ).data() assert len(result) == len(rs.relationships)
def test_default_props_overwrite_from_node(self): ns = NodeSet(['Test', 'Foo', 'Bar'], merge_keys=['uuid'], default_props={'user': '******'}) for i in range(100): ns.add_node({'uuid': i, 'user': '******'}) for n in ns.nodes: assert n['user'] == 'bar'
def test_index_creation(self): ns = NodeSet(['Test'], ['name', 'id'], indexed=True) for i in range(1, 101): # add node twice to index ns.add_node({'name': str(i), 'id': i}) ns.add_node({'name': str(i), 'id': i}) # assert that the positions match as expected for i in range(1, 101): index = (str(i), i) assert index in ns.node_index assert ns.node_index[index] == [(i - 1) * 2, ((i - 1) * 2) + 1]
def test_nodeset__estimate_type_of_property_values(): ns = NodeSet(['Test'], merge_keys=['uuid']) for i in range(10): ns.add_node({'uuid': i, 'key': 'value', 'foo': 20.5, 'changing': 4}) # change type for one property to make sure that str is set for inconsistent types ns.add_node({'uuid': 1, 'key': 'value', 'foo': 20.5, 'changing': 30.4}) types = ns._estimate_type_of_property_values() assert types['uuid'] == int assert types['key'] == str assert types['foo'] == float assert types['changing'] == str
def create_nodes_test(graph, clear_graph): ns1 = NodeSet(['Test'], merge_keys=['uuid']) ns2 = NodeSet(['Foo'], merge_keys=['uuid']) for i in range(100): ns1.add_node({'uuid': i}) ns2.add_node({'uuid': i}) ns1.create(graph) ns2.create(graph) return ns1, ns2
def test_create_instance_add_nodes(self, labels, merge_keys, data): ns = NodeSet(labels, merge_keys) for i in data: ns.add_node(i)
def nodeset_multiple_labels_multiple_merge_keys(): ns = NodeSet(['Test', 'Foo', 'Bar'], merge_keys=['uuid', 'other']) for i in range(1000): ns.add_node({'uuid': i, 'other': i + 10}) return ns
def small_nodeset() -> NodeSet: ns = NodeSet(['Test'], merge_keys=['uuid']) for i in range(100): ns.add_node({'uuid': i, 'key': 'value'}) return ns
def test_update_nodes(self): ns = NodeSet(['Test'], ['name'], indexed=True) ns.add_node({'name': 'Peter', 'age': 50}) ns.update_node({'name': 'Peter', 'age': 60, 'city': 'Munich'}) assert len(ns.nodes) == 1 assert ns.nodes[0] == {'name': 'Peter', 'age': 60, 'city': 'Munich'}