summaryrefslogtreecommitdiff
path: root/src/server-tests/neo4j_test_util.py
blob: ef777150848590f8508c43ef965e6d79470f23c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#    This file is part of rhizi, a collaborative knowledge graph editor.
#    Copyright (C) 2014-2015  Rhizi
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


from random import choice
import string
import uuid

from db_op import DB_op, DBO_raw_query_set
import neo4j_cypher
import neo4j_schema

class DBO_flush_db(DBO_raw_query_set):
    """
    complete DB flush: remove all nodes & links
    """
    def __init__(self):
        q_arr = ['match (n)',
                 'optional match (n)-[r]-()',
                 'delete n,r'
                ]
        super(DBO_flush_db, self).__init__(q_arr)

class DBO_random_data_generation(DB_op):

    def __init__(self, lim_n=128, lim_r=256, prob_link_create=0.3):
        """
        generate random DB data
        
        @return: tuple consisting of the random node,link labels generated
        """
        assert 2 <= lim_n

        super(DBO_random_data_generation, self).__init__()

        self.n_label = rand_label()
        self.r_label = rand_label()
        q_arr = ['with 0 as _',  # TODO clean: foreach triggers SyntaxException: otherwise
                 'foreach (rid in range(0,%d)' % (lim_n - 1),
                 '|',
                 'create (:%s {id: \'test-id_\' + toString(rid), n_attr_0:toInt(%d * rand())}))' % (self.n_label, lim_n)
                ]
        self.add_statement(q_arr)

        q_arr = ['match (s:%s),(d:%s)' % (self.n_label, self.n_label),
                 'with s,d',
                 'limit %d' % (lim_r - 1),
                 'where rand() < %.2f' % (prob_link_create),
                 'create (s)-[:%s {l_attr_0:toInt(%d * rand())}]->(d)' % (self.r_label, lim_r)]
        self.add_statement(q_arr)

    @property
    def node_set_label(self):
        return self.n_label

    @property
    def link_set_label(self):
        return self.r_label

def rand_label(prefix='T_', length=8):
    """
    return a prefixed random label, where the default prefix is 'T_'
    """
    char_set = string.ascii_lowercase + string.ascii_uppercase + string.digits
    ret = ''.join([choice(string.ascii_lowercase)] + [choice(char_set) for _ in range(length - 1)])
    ret = prefix + ret
    return ret

def rand_label__doc(length=8):
    return rand_label(prefix=neo4j_schema.META_LABEL__RZDOC_NS_PREFIX, length=length)