Exemple #1
0
    def test_create_csv_query(self):
        rs = RelationshipSet('TEST', ['Test', 'Other'], ['Foo', 'SomeLabel'],
                             ['uuid', 'numerical'], ['uuid', 'value'])
        rs.uuid = 'peter'

        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'
            })

        query = rs.csv_query('CREATE')
        print(query)
        assert query == """USING PERIODIC COMMIT 1000 
Exemple #2
0
 def dataset(cls) -> RelationshipSet:
     """
     :return: Return a :class:`~graphio.RelationshipSet` instance for this ModelRelationship.
     """
     return RelationshipSet(cls.type, cls.source.__labels__,
                            cls.target.__labels__,
                            cls.source.__merge_keys__,
                            cls.target.__merge_keys__)
Exemple #3
0
    def test_to_csv(self, tmp_path):
        rs = RelationshipSet('TEST', ['Test', 'Other'], ['Foo', 'SomeLabel'],
                             ['uuid', 'numerical'], ['uuid', 'value'])

        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'
            })

        csv_file = rs.to_csv(tmp_path)
        print(csv_file)

        expected_num_of_fields = len(rs.all_property_keys()) + len(
            rs.fixed_order_start_node_properties) + len(
                rs.fixed_order_end_node_properties)

        with open(csv_file) as f:
            lines = f.readlines()
            assert len(lines) - 1 == len(rs.relationships)

            for l in lines:
                l = l.strip()
                assert len(l.split(',')) == expected_num_of_fields
Exemple #4
0
    def map_to_1(self, graph, target_labels, target_properties, rel_type=None):
        """
        Create relationships from all nodes in this NodeSet to 1 target node.

        :param graph: The py2neo Graph
        :param other_node: The target node.
        :param rel_type: Relationship Type
        """

        if not rel_type:
            rel_type = 'FROM_SET'

        rels = RelationshipSet(rel_type, self.labels, target_labels, self.merge_keys, target_properties)

        for node in self.nodes:
            # get properties for merge_keys
            node_properties = {}
            for k in self.merge_keys:
                node_properties[k] = node[k]

            rels.add_relationship(node_properties, target_properties, {})

        rels.create(graph)
Exemple #5
0
    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)