def test_nodeset_create_composite_index(self, graph, clear_graph): labels = ['TestNode'] properties = ['some_key', 'other_key'] ns = NodeSet(labels, merge_keys=properties) ns.create_index(graph) result = list(graph.run("CALL db.indexes()")) for row in result: # the result of the db.indexes() procedure is different for Neo4j 3.5 and 4 # this should also be synced with differences in py2neo versions if 'tokenNames' in row: assert row['tokenNames'] == labels and row['properties'] == properties \ or row['tokenNames'] == labels and row['properties'] == properties elif 'labelsOrTypes' in row: assert row['labelsOrTypes'] == labels and row['properties'] == properties \ or row['labelsOrTypes'] == labels and row['properties'] == properties
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 test_read_from_files(self, root_dir, clear_graph, graph): files_path = os.path.join(root_dir, "test", "files") json_file_path = os.path.join(files_path, 'nodes_csv_json.json') csv_file_path = os.path.join(files_path, 'nodes_csv_json.csv') assert os.path.exists(json_file_path) assert os.path.exists(csv_file_path) ns = NodeSet.from_csv_json_set(csv_file_path, json_file_path) assert ns.labels == ['Test'] assert ns.merge_keys == ['test_id'] ns.merge(graph) assert list(graph.run("MATCH (n:Test) RETURN count(n)"))[0][0] == 2
def test_nodeset_recreate_existing_single_index(self, graph, clear_graph): """ The output/error when you try to recreate an existing index is different in Neo4j 3.5 and 4. Create an index a few times to make sure this error is handled. """ labels = ['TestNode'] properties = ['some_key'] ns = NodeSet(labels, merge_keys=properties) ns.create_index(graph) ns.create_index(graph) ns.create_index(graph)
def test_nodeset_add_unique(): ns = NodeSet(['Test', 'Foo'], merge_keys=['name']) for i in range(10): ns.add_unique({'name': 'Peter'}) assert len(ns.nodes) == 1
def test_str(): ns = NodeSet(['Test', 'Foo'], merge_keys=['uuid']) assert str(ns) == "<NodeSet (['Test', 'Foo']; ['uuid'])>"
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 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 dataset(cls) -> NodeSet: """ :return: Return a :class:`~graphio.NodeSet` instance for this ModelNode. """ return NodeSet(cls.__labels__, merge_keys=cls.__merge_keys__)
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'}
def test_error_on_non_indexed(self): ns = NodeSet(['Test'], ['name']) with pytest.raises(TypeError): ns.update_node({'name': 'Peter', 'age': 60, 'city': 'Munich'})
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_nodeset_merge_key_id(): ns = NodeSet(['Test'], ['name', 'foo']) merge_key_id = ns._merge_key_id({'name': 'Peter', 'foo': 'bar'}) assert merge_key_id == ('Peter', 'bar')
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_create_instance_add_nodes(self, labels, merge_keys, data): ns = NodeSet(labels, merge_keys) for i in data: ns.add_node(i)
def test_relationshipset_csv_merge(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('MERGE') # run query a few times to check for duplications graph.run(query) graph.run(query) graph.run(query) result = graph.run( "MATCH (source:Test:Other)-[r:TEST]->(target:Foo:SomeLabel) RETURN r" ).data() assert len(result) == len(rs.relationships)