Beispiel #1
0
    def test_can_build_single_create_relationship_with_end_node_with_unique_properties_create_query_with_ensure_unique_relationship(
            self):
        n = 'mark {}'.format(random())
        start = OpenNode(properties={'name': n})
        n2 = 'kram {}'.format(random())
        end = UniquePropertiesNode(properties={'name': n2})
        since = 'yeserday'
        rel = OpenRelationship(start=start,
                               end=end,
                               properties={'since': since})
        q = Query(rel)
        query, params = q.save(ensure_unique=True)
        label = rel.type

        exp = (
            "MERGE ({end_var}:`{end_label}` {{`name`: ${end_name}}})"
            " ON CREATE SET {end_var}.`location` = ${end_loc}, {end_var}.`name` = ${end_name}"
            " ON MATCH SET {end_var}.`location` = ${end_loc}, {end_var}.`name` = ${end_name}"
            " MERGE ({start_var}:`{start_label}` {{`name`: ${start_name}}})-[{rel_var}:`{rel_label}` {{`since`: ${rel_since}}}]"
            "->({end_var})"
            " RETURN {start_var}, {end_var}, {rel_var}").format(
                start_var=start.query_variable,
                start_label=start.labels[0],
                start_name=get_dict_key(params, n),
                start_loc=get_dict_key(params, ''),
                end_var=end.query_variable,
                end_label=end.labels[0],
                end_name=get_dict_key(params, n2),
                end_loc=get_dict_key(params, ''),
                rel_var=rel.query_variable,
                rel_since=get_dict_key(params, since),
                rel_label=rel.labels)

        self.assertEqual(exp, query)
        self.assertEqual(4, len(params))
Beispiel #2
0
    def test_can_build_single_node_with_multiple_unique_properties_create_query(
            self):
        class UniqueMultiplePropertiesNode(Node):
            pass

        class UniqueMultiplePropertiesNodeMapper(EntityMapper):
            entity = UniqueMultiplePropertiesNode
            __PROPERTIES__ = {
                'name': String(ensure_unique=True),
                'location': String(ensure_unique=True),
            }

        name = 'mark {}'.format(random())
        loc = 'loc {}'.format(random())
        n = UniqueMultiplePropertiesNode(properties={
            'name': name,
            'location': loc
        })
        q = Query(n)
        query, params = q.save()
        exp = (
            'MERGE ({var}:`{label}` {{`location`: ${loc_val}, `name`: ${name_val}}})'
            ' ON CREATE SET {var}.`location` = ${loc_val}, {var}.`name` = ${name_val}'
            ' ON MATCH SET {var}.`location` = ${loc_val}, {var}.`name` = ${name_val}'
            ' RETURN {var}').format(var=n.query_variable,
                                    name_val=get_dict_key(params, name),
                                    loc_val=get_dict_key(params, loc),
                                    label=n.labels[0])

        self.assertEqual(exp, query)
        self.assertEqual(2, len(params))
Beispiel #3
0
    def test_can_build_single_update_query(self):
        sid = 99
        name = 'mark {}'.format(random())
        start = OpenNode(id=sid, properties={'name': name})
        eid = 88
        name2 = 'kram {}'.format(random())
        end = OpenNode(id=eid, properties={'name': name2})
        rid = 447788
        since = 'since {}'.format(random())
        rel = OpenRelationship(id=rid,
                               start=start,
                               end=end,
                               properties={'since': since})
        q = Query(rel)
        query, params = q.save()

        exp = (
            "MATCH ({start}) WHERE id({start}) = ${sid}"
            " MATCH ({end}) WHERE id({end}) = ${eid}"
            " MATCH ({start})-[{rel}:`{label}`]->({end}) WHERE id({rel}) = ${rid}"
            " SET {start}.`name` = ${name}, {end}.`name` = ${name2}, {rel}.`since` = ${since}"
            " RETURN {start}, {end}, {rel}".format(
                start=start.query_variable,
                sid=get_dict_key(params, sid),
                end=end.query_variable,
                eid=get_dict_key(params, eid),
                rel=rel.query_variable,
                label=rel.type,
                name=get_dict_key(params, name),
                name2=get_dict_key(params, name2),
                rid=get_dict_key(params, rid),
                since=get_dict_key(params, since)))

        self.assertEqual(exp, query)
        self.assertEqual(6, len(params))
Beispiel #4
0
    def test_can_build_single_create_relationship_with_one_existing_one_new_node_create_query(
            self):
        sid = 99
        n = 'mark {}'.format(random())
        start = OpenNode(id=sid, properties={'name': n})
        n2 = 'kram {}'.format(random())
        end = OpenNode(properties={'name': n2})
        since = 'yeserday'
        rel = OpenRelationship(start=start,
                               end=end,
                               properties={'since': since})
        q = Query(rel)
        query, params = q.save()
        label = rel.type
        exp = (
            "MATCH ({var}) WHERE id({var}) = ${id}"
            " CREATE ({var})-[{rel}:`{label}` {{`since`: ${since}}}]->({var2}:`{end_label}` {{`name`: ${name2}}})"
            " SET {var}.`name` = ${name}"
            " RETURN {var}, {var2}, {rel}".format(
                var=start.query_variable,
                id=get_dict_key(params, sid),
                rel=rel.query_variable,
                label=rel.labels,
                since=get_dict_key(params, since),
                var2=end.query_variable,
                name=get_dict_key(params, n),
                name2=get_dict_key(params, n2),
                end_label=end.labels[0]))

        self.assertEqual(exp, query)
        self.assertEqual(4, len(params))
Beispiel #5
0
    def test_can_build_single_create_relationship_with_existing_nodes_create_query_with_ensure_unique(
            self):
        sid = 99
        n = 'mark {}'.format(random())
        start = OpenNode(id=sid, properties={'name': n})
        eid = 88
        n2 = 'kram {}'.format(random())
        end = OpenNode(id=eid, properties={'name': n2})
        since = 'yeserday'
        rel = OpenRelationship(start=start,
                               end=end,
                               properties={'since': since})
        q = Query(rel)
        query, params = q.save(ensure_unique=True)
        label = rel.type
        exp = (
            "MATCH ({var}) WHERE id({var}) = ${id}"
            " MATCH ({var2}) WHERE id({var2}) = ${id2}"
            " MERGE ({var})-[{var3}:`{rel_label}` {{`since`: ${since}}}]->({var2})"
            " SET {var}.`name` = ${val1}, {var2}.`name` = ${val2}"
            " RETURN {var}, {var2}, {var3}").format(
                var=start.query_variable,
                var2=end.query_variable,
                var3=rel.query_variable,
                label=label,
                id=get_dict_key(params, sid),
                id2=get_dict_key(params, eid),
                val1=get_dict_key(params, n),
                val2=get_dict_key(params, n2),
                since=get_dict_key(params, since),
                rel_label=rel.labels)

        self.assertEqual(exp, query)
        self.assertEqual(5, len(params))
Beispiel #6
0
    def test_can_build_single_create_multiple_relationship_with_different_existing_nodes_create_query(
            self):
        sid = 99
        name = 'mark {}'.format(random())
        start = OpenNode(id=sid, properties={'name': name})
        eid = 88
        name2 = 'kram {}'.format(random())
        end = OpenNode(id=eid, properties={'name': name2})
        sid2 = 999
        name3 = 'mark {}'.format(random())
        start2 = OpenNode(id=sid2, properties={'name': name3})
        eid2 = 888
        name4 = 'kram {}'.format(random())
        end2 = OpenNode(id=eid2, properties={'name': name4})
        since = 'yeserday'
        since2 = 'some time ago'
        label2 = 'knows_two'
        rel = OpenRelationship(start=start,
                               end=end,
                               properties={'since': since})
        rel2 = OpenRelationship(start=start2,
                                end=end2,
                                labels=label2,
                                properties={'since': since2})
        q = Query([rel, rel2])
        query, params = q.save()
        label = rel.type
        exp = (
            "MATCH ({var}) WHERE id({var}) = ${id}"
            " MATCH ({var2}) WHERE id({var2}) = ${id2}"
            " MATCH ({var3}) WHERE id({var3}) = ${id3}"
            " MATCH ({var4}) WHERE id({var4}) = ${id4}"
            " CREATE ({var})-[{rel}:`{rel_label}` {{`since`: ${since}}}]->({var2}),"
            " ({var3})-[{rel2}:`{rel2_label}` {{`since`: ${since2}}}]->({var4})"
            " SET {var}.`name` = ${name}, {var2}.`name` = ${name2}, {var3}.`name` = ${name3}, {var4}.`name` = ${name4}"
            " RETURN {var}, {var2}, {rel}, {var3}, {var4}, {rel2}").format(
                var=start.query_variable,
                id=get_dict_key(params, sid),
                var2=end.query_variable,
                id2=get_dict_key(params, eid),
                var3=start2.query_variable,
                id3=get_dict_key(params, sid2),
                var4=end2.query_variable,
                id4=get_dict_key(params, eid2),
                rel=rel.query_variable,
                since=get_dict_key(params, since),
                rel2=rel2.query_variable,
                since2=get_dict_key(params, since2),
                label=label2,
                name=get_dict_key(params, name),
                name2=get_dict_key(params, name2),
                name3=get_dict_key(params, name3),
                name4=get_dict_key(params, name4),
                rel_label=rel.type,
                rel2_label=rel2.type)

        self.assertEqual(exp, query)
        self.assertEqual(10, len(params))
Beispiel #7
0
    def test_can_build_mixed_update_and_insert_query(self):
        sid = 99
        name = 'mark {}'.format(random())
        start = OpenNode(id=sid, properties={'name': name})
        eid = 88
        name2 = 'kram {}'.format(random())
        end = OpenNode(id=eid, properties={'name': name2})
        rid = 447788
        since = 'since {}'.format(random())
        rel = OpenRelationship(id=rid,
                               start=start,
                               end=end,
                               properties={'since': since})
        sid2 = 887
        name3 = 'name {}'.format(random())
        start2 = OpenNode(id=sid2, properties={'name': name3})
        name4 = 'name {}'.format(random())
        end2 = OpenNode(properties={'name': name4})
        rel2 = OpenRelationship(start=start2,
                                end=end2,
                                properties={'since': since})
        q = Query([rel, rel2])
        query, params = q.save()

        exp = (
            "MATCH ({start}) WHERE id({start}) = ${sid}"
            " MATCH ({end}) WHERE id({end}) = ${eid}"
            " MATCH ({start})-[{rel}:`{rel_label}`]->({end}) WHERE id({rel}) = ${rid}"
            " MATCH ({start2}) WHERE id({start2}) = ${sid2}"
            " CREATE ({start2})-[{rel2}:`{rel2_label}` {{`since`: ${since}}}]->({end2}:`{end2_label}` {{`name`: ${name4}}})"
            " SET {start}.`name` = ${name}, {end}.`name` = ${name2}, {rel}.`since` = ${since}, {start2}.`name` = ${name3}"
            " RETURN {start}, {end}, {rel}, {start2}, {end2}, {rel2}".format(
                start=start.query_variable,
                sid=get_dict_key(params, sid),
                end=end.query_variable,
                eid=get_dict_key(params, eid),
                rel=rel.query_variable,
                label='Relationship',
                name=get_dict_key(params, name),
                name2=get_dict_key(params, name2),
                rid=get_dict_key(params, rid),
                since=get_dict_key(params, since),
                rel2=rel2.query_variable,
                name3=get_dict_key(params, name3),
                start2=start2.query_variable,
                sid2=get_dict_key(params, sid2),
                end2=end2.query_variable,
                name4=get_dict_key(params, name4),
                rel_label=rel.type,
                rel2_label=rel2.type,
                end2_label=end2.labels[0]))

        self.assertEqual(exp, query)
        self.assertEqual(9, len(params))
Beispiel #8
0
    def test_can_build_single_node_create_query(self):
        name = 'mark {}'.format(random())
        n = OpenNode(properties={'name': name})
        q = Query(n)
        query, params = q.save()
        exp = 'CREATE ({var}:`{labels}` {{`name`: ${val}}}) RETURN {var}'.format(
            var=n.query_variable,
            val=get_dict_key(params, name),
            labels=n.labels[0])

        self.assertEqual(exp, query)
        self.assertEqual(1, len(params))
Beispiel #9
0
    def test_can_build_single_node_update_query(self):
        name = 'mark {}'.format(random())
        _id = 999
        n = OpenNode(id=_id, properties={'name': name})
        q = Query(n)
        query, params = q.save()
        exp = "MATCH ({var}) WHERE id({var}) = ${id} SET {var}.`name` = ${val} RETURN {var}".format(
            var=n.query_variable,
            val=get_dict_key(params, name),
            id=get_dict_key(params, _id))

        self.assertEqual(exp, query)
        self.assertEqual(2, len(params))
Beispiel #10
0
    def test_can_build_single_node_with_one_unique_property_create_query(self):
        name = 'mark {}'.format(random())
        loc = 'loc {}'.format(random())
        n = UniquePropertiesNode(properties={'name': name, 'location': loc})
        q = Query(n)
        query, params = q.save()
        exp = (
            'MERGE ({var}:`{label}` {{`name`: ${name_val}}})'
            ' ON CREATE SET {var}.`location` = ${loc_val}, {var}.`name` = ${name_val}'
            ' ON MATCH SET {var}.`location` = ${loc_val}, {var}.`name` = ${name_val}'
            ' RETURN {var}').format(var=n.query_variable,
                                    name_val=get_dict_key(params, name),
                                    loc_val=get_dict_key(params, loc),
                                    label=n.labels[0])

        self.assertEqual(exp, query)
        self.assertEqual(2, len(params))
Beispiel #11
0
    def test_can_build_mutiple_node_create_query(self):
        name = 'mark {}'.format(random())
        n = OpenNode(properties={'name': name})
        name2 = 'mark {}'.format(random())
        n2 = OpenNode(properties={'name': name2})
        q = Query([n, n2])
        query, params = q.save()
        exp = 'CREATE ({var}:`{var_label}` {{`name`: ${val}}}), ({var2}:`{var2_label}` {{`name`: ${val2}}}) RETURN {var}, {var2}'.format(
            var=n.query_variable,
            val=get_dict_key(params, name),
            var2=n2.query_variable,
            val2=get_dict_key(params, name2),
            var_label=n.labels[0],
            var2_label=n2.labels[0])

        self.assertEqual(exp, query)
        self.assertEqual(2, len(params))
Beispiel #12
0
    def test_can_build_single_create_multiple_relationship_with_the_same_existing_nodes_create_query(
            self):
        sid = 99
        n = 'mark {}'.format(random())
        start = OpenNode(id=sid, properties={'name': n})
        eid = 88
        n2 = 'kram {}'.format(random())
        end = OpenNode(id=eid, properties={'name': n2})
        since = 'yeserday'
        label2 = 'knows_two'
        rel2 = OpenRelationship(start=start,
                                end=end,
                                labels=label2,
                                properties={'since': since})
        rel = OpenRelationship(start=start,
                               end=end,
                               properties={'since': since})
        rel2.labels
        q = Query([rel, rel2])
        query, params = q.save()
        label = rel.type
        exp = (
            "MATCH ({var}) WHERE id({var}) = ${id}"
            " MATCH ({var2}) WHERE id({var2}) = ${id2}"
            " CREATE ({var})-[{var3}:`{rel_label}` {{`since`: ${since}}}]->({var2}),"
            " ({var})-[{var4}:`{label2}` {{`since`: ${since}}}]->({var2})"
            " SET {var}.`name` = ${val1}, {var2}.`name` = ${val2}"
            " RETURN {var}, {var2}, {var3}, {var4}").format(
                var=start.query_variable,
                var2=end.query_variable,
                var3=rel.query_variable,
                label=label,
                id=get_dict_key(params, sid),
                id2=get_dict_key(params, eid),
                val1=get_dict_key(params, n),
                val2=get_dict_key(params, n2),
                since=get_dict_key(params, since),
                var4=rel2.query_variable,
                label2=label2,
                rel_label=rel.labels)

        self.assertEqual(exp, query)
        self.assertEqual(5, len(params))
Beispiel #13
0
    def test_can_build_multiple_node_update_query(self):
        name = 'mark {}'.format(random())
        _id = 999
        n = OpenNode(id=_id, properties={'name': name})
        name2 = 'kram {}'.format(random())
        _id2 = 888
        n2 = OpenNode(id=_id2, properties={'name': name2})
        q = Query([n, n2])
        query, params = q.save()
        exp = "MATCH ({var}) WHERE id({var}) = ${id} MATCH ({var2}) WHERE id({var2}) = ${id2} SET {var}.`name` = ${val}, {var2}.`name` = ${val2} RETURN {var}, {var2}".format(
            var=n.query_variable,
            val=get_dict_key(params, name),
            id=get_dict_key(params, _id),
            var2=n2.query_variable,
            val2=get_dict_key(params, name2),
            id2=get_dict_key(params, _id2))

        self.assertEqual(exp, query)
        self.assertEqual(4, len(params))