Example #1
0
    def test_can_ensure_that_a_value_the_same_as_a_previously_bound_param_returns_previous_params_value(
            self):
        n = 'some_name'
        v = 'value {}'.format(random())
        p = Pypher()
        param = p.bind_param(v, n)
        param2 = p.bind_param(n)

        str(p)
        params = p.bound_params

        self.assertEqual(1, len(params))
        self.assertEqual(param.name, param2.name)
Example #2
0
    def test_can_use_subclassed_function_by_classname(self):
        class SubClassedFunc(Func):
            name = 'sub_class_func'

        p = Pypher()
        y = 12
        p.SubClassedFunc(y)
        x = str(p)
        params = p.bound_params
        expected = 'sub_class_func(${})'.format(get_dict_key(params, y))

        self.assertEqual(x, expected)
        self.assertEqual(1, len(params))
Example #3
0
    def test_can_use_subclassed_statement_by_classname(self):
        class SubClassedStatement(Statement):
            name = 'sub_class_stmt'

        p = Pypher()
        y = 12
        p.SubClassedStatement(y)
        x = str(p)
        params = p.bound_params
        expected = 'sub_class_stmt {}'.format(y)

        self.assertEqual(x, expected)
        self.assertEqual(0, len(params))
Example #4
0
 def get_show_all_nodes_query(node_type=None):
     """
     :param node_type: type of nodes to show.
     :return: the query to show all nodes. Optional: Type of nodes to be recognized.
     """
     # should be bundle, copilationUnit or package. Could be extracted via tracker.slots.get('nodeType').
     pypher_object = Pypher()
     if node_type is None:
         pypher_object.Match.node('u')
     else:
         pypher_object.Match.node('u', labels=node_type)
     pypher_object.RETURN('u')
     return GenerateQuery.reformat_query(pypher_object)
Example #5
0
    def test_can_nest_AND_and_OR_conditionals(self):
        p = Pypher()
        p.COR(1, __.CAND(2, 3))

        s = str(p)
        params = p.bound_params
        exp = '(${one} OR (${two} AND ${three}))'.format(
            one=get_dict_key(params, 1),
            two=get_dict_key(params, 2),
            three=get_dict_key(params, 3))

        self.assertEqual(exp, s)
        self.assertEqual(3, len(params))
Example #6
0
    def test_can_use_base_conditional(self):
        p = Pypher()
        p.CONDITIONAL(1, 2, 3)

        s = str(p)
        params = p.bound_params
        exp = '(${one}, ${two}, ${three})'.format(one=get_dict_key(params, 1),
                                                  two=get_dict_key(params, 2),
                                                  three=get_dict_key(
                                                      params, 3))

        self.assertEqual(exp, s)
        self.assertEqual(3, len(params))
Example #7
0
        def _build_start():
            pypher = Pypher()

            if self.start_entity.id is not None:
                qv = self.start_entity.query_variable
                where = __.ID(qv) == self.start_entity.id

                pypher.NODE(qv)
                self.wheres.append(where)
            else:
                pypher.NODE(self.start_query_variable)

            return pypher
Example #8
0
    def _build_end(self):
        pypher = Pypher()
        qv = self.end_query_variable
        labels = None

        if self.end_entity:
            qv = self.end_entity.query_variable
            labels = self.end_entity.labels

        pypher.NODE(qv, labels=labels)
        self.returns.append(qv)

        return pypher
Example #9
0
    def test_can_add_random_function_with_args(self):
        p = Pypher()
        f = 'someFunction{}'.format(random())
        one = 'one'
        two = 2
        p.func(f, one, two)
        c = str(p)
        params = p.bound_params
        exp = '{}(${}, ${})'.format(f, get_dict_key(params, one),
                                    get_dict_key(params, two))

        self.assertEqual(c, exp)
        self.assertEqual(2, len(params))
Example #10
0
    def test_can_use_subclassed_function_by_alias(self):
        class SubClassedFunc2(Func):
            name = 'sub_class_func2'
            _ALIASES = ['scf']

        p = Pypher()
        y = 12
        p.scf(y)
        x = str(p)
        params = p.bound_params
        expected = 'sub_class_func2(${})'.format(get_dict_key(params, y))

        self.assertEqual(x, expected)
        self.assertEqual(1, len(params))
Example #11
0
    def _build_start(self):
        pypher = Pypher()

        if self.start_entity.id:
            qv = self.start_entity.query_variable
            where = __.ID(qv) == self.start_entity.id

            pypher.NODE(qv)
            self.wheres.append(where)
        else:
            pypher.NODE(self.start_query_variable,
                        labels=self.start_entity.labels)

        return pypher
Example #12
0
    def test_can_use_subclassed_statement_by_alias_and_classname(self):
        class SubClassedStmt3(Statement):
            name = 'sub_class_stmt3'
            _ALIASES = ['scs3']

        p = Pypher()
        y = 12
        p.scs3(y).SubClassedStmt3(y)
        x = str(p)
        params = p.bound_params
        expected = 'sub_class_stmt3 {} sub_class_stmt3 {}'.format(y, y)

        self.assertEqual(x, expected)
        self.assertEqual(0, len(params))
Example #13
0
    def test_can_append_two_instances_first_is_empty(self):
        p = Pypher()
        p2 = Pypher()
        p2.two

        p.append(p2)

        exp = 'two'
        exp2 = 'two'
        s = str(p)
        s2 = str(p2)

        self.assertEqual(exp, s)
        self.assertEqual(exp2, s2)
Example #14
0
    def test_can_clone_nested_pypher(self):
        p = Pypher()
        d = Pypher()
        e = Pypher()
        e.CAND(1, 2, 3, 4, 5, __.test.this.out.CONDITIONAL(9, 9, 8, __.node(6)))
        d.id(123).raw(e)
        p.a.b.c.d.node(d == 122)
        c = p.clone()
        x = str(p)
        y = str(c)

        self.assertEqual(x, y)
        self.assertEqual(len(p.bound_params), len(c.bound_params))
        self.assertTrue(id(p.bound_params) == id(c.bound_params))
Example #15
0
    def test_can_add_named_node_labeled_out_relationship_node_with_properties(self):
        n = 'name'
        l = 'KNOWS'
        name = 'somename'
        age = 99
        p = Pypher()
        p.node(n).rel_out(labels=l).node(name=name, age=age)
        c = str(p)
        params = p.bound_params
        exp = '({n})-[:`{l}`]->( {{`age`: ${age}, `name`: ${name}}})'.format(n=n, l=l,
            name=get_dict_key(params, name), age=get_dict_key(params, age))

        self.assertEqual(c, exp)
        self.assertEqual(2, len(params))
Example #16
0
    def test_can_bind_multiple_ununique_primitives_to_pypher(self):
        v = 'value {}'.format(random())
        v2 = v
        p = Pypher()
        par = p.bind_param(v)
        par2 = p.bind_param(v2)

        str(p)
        params = p.bound_params

        self.assertIn(v, params.values())
        self.assertIn(v2, params.values())
        self.assertEqual(1, len(params))
        self.assertEqual(par.name, par2.name)
Example #17
0
    def test_can_bind_param_object_to_pypher(self):
        n = 'some name'
        v = 'value {}'.format(random())

        v = Param(n, v)
        p = Pypher()
        param = p.bind_param(v)

        str(p)
        params = p.bound_params

        self.assertNotEqual(id(v), id(param))
        self.assertIn(v.value, params.values())
        self.assertIn(n, params)
        self.assertEqual(1, len(params))
Example #18
0
 def get_node_information_query(node_name, node_type=None):
     """
     :param node_name: name of the node to get information about.
     :return: the query for showing general information of one node (any entity) with a specific name.
     """
     pypher_object = Pypher()
     if node_type is None:
         pypher_object.Match.node('u').where.u.__name__.CONTAINS(
             Param('per_param', node_name))
     else:
         pypher_object.Match.node(
             'u', labels=node_type).where.u.__name__.CONTAINS(
                 Param('per_param', node_name))
     pypher_object.RETURN('u')
     return GenerateQuery.reformat_query(pypher_object)
Example #19
0
    def test_can_append_two_instances(self):
        p = Pypher()
        p2 = Pypher()
        p.one
        p2.two

        p.append(p2)

        exp = 'one two'
        exp2 = 'two'
        s = str(p)
        s2 = str(p2)

        self.assertEqual(exp, s)
        self.assertEqual(exp2, s2)
Example #20
0
    def test_can_add_named_undirected_relationship_with_labels_and_properties(
            self):
        p = Pypher()
        name = 'somename'
        age = 99
        p.relationship(variable='test',
                       labels=['one', 'two', 'three'],
                       name=name,
                       age=age)
        c = str(p)
        params = p.bound_params
        exp = '-[test:`one`|`two`|`three` {{`age`: ${a}, `name`: ${n}}}]-'.format(
            n=get_dict_key(params, name), a=get_dict_key(params, age))

        self.assertEqual(str(p), exp)
Example #21
0
    def test_can_add_list(self):
        p = Pypher()
        one = 1
        two = 2
        three = 3
        p.List(one, two, three)
        c = str(p)
        params = p.bound_params
        exp = '[${one}, ${two}, ${three}]'.format(
            one=get_dict_key(params, one),
            two=get_dict_key(params, two),
            three=get_dict_key(params, three))

        self.assertEqual(exp, c)
        self.assertEqual(3, len(params))
Example #22
0
    def delete_by_entity_id(self, *ids):
        if not ids:
            msg = 'There must be ids passed in to the delete method'
            raise AttributeError(msg)

        def _build_start():
            pypher = Pypher()

            if self.start_entity.id is not None:
                qv = self.start_entity.query_variable
                where = __.ID(qv) == self.start_entity.id

                pypher.NODE(qv)
                self.wheres.append(where)
            else:
                pypher.NODE(self.start_query_variable)

            return pypher

        self.pypher = Pypher()
        self.matches.insert(0, self._build_end())
        self.matches.insert(0, self._build_relationship())
        self.matches.insert(0, _build_start())

        self.pypher.MATCH

        for match in self.matches:
            self.pypher.append(match)

        id_params = []

        for i in ids:
            key = 'end_id_{}'.format(i)
            id_params.append(Param(key, i))

        self.wheres.append(__.ID(self.end_query_variable).IN(*id_params))
        _id = __.ID(self.start_query_variable)

        if self.start_entity.id is not None:
            self.wheres.append(_id == self.start_entity.id)
        # else:
        #     wheres.append(_id)

        self.pypher.WHERE.CAND(*self.wheres)
        self.pypher.DELETE(self.relationship_query_variable)
        self.reset()

        return str(self.pypher), self.pypher.bound_params
Example #23
0
    def test_can_add_single_label(self):
        p = Pypher()

        p.n.label('one')
        exp = 'n:`one`'

        self.assertEqual(str(p), exp)
Example #24
0
    def test_can_add_raw(self):
        p = Pypher()
        s = 'raw content {}'.format(random())
        p.this.will.be.raw(s)
        exp = 'this will be {}'.format(s)

        self.assertEqual(str(p), exp)
Example #25
0
    def test_can_use_nested_dictionary_in_operator(self):
        lat = random()
        lng = random()
        d = {
            'name': 'name_{}'.format(str(random())),
            'loc': {
                'city': 'city_{}'.format(str(random())),
                'latlng': [lat, lng],
            },
        }
        p = Pypher()
        p.SET.user += d
        q = str(p)
        params = p.bound_params
        exp = (
            'SET user += {{`loc`: {{`city`: ${city}, `latlng`: [${lat}, ${lng}]}}, `name`: ${name}}}'
        ).format(name=get_dict_key(params, d['name']),
                 city=get_dict_key(params, d['loc']['city']),
                 lat=get_dict_key(params, lat),
                 lng=get_dict_key(params, lng))

        self.assertEqual(exp, q)
        self.assertEqual(4, len(params))
        self.assertIn(d['name'], params.values())
        self.assertIn(d['loc']['city'], params.values())
Example #26
0
    def test_can_add_two_statements(self):
        p = Pypher()
        p.some_statement.some_other_statement

        expected = 'some_statement some_other_statement'
        c = str(p)

        self.assertEqual(c, expected)
Example #27
0
    def test_can_add_one_statement(self):
        p = Pypher()
        p.some_attribute

        expected = 'some_attribute'
        c = str(p)

        self.assertEqual(c, expected)
Example #28
0
    def test_can_add_two_properties_underscore(self):
        p = Pypher()
        p.__prop1__.__prop2__

        expected = '.`prop1`.`prop2`'
        c = str(p)

        self.assertEqual(c, expected)
Example #29
0
    def test_can_bind_nonunique_mixed_primitive_and_param_object_to_pypher(self):
        n = 'some name'
        v = 'value {}'.format(random())
        v2 = v
        v = Param(n, v)
        p = Pypher()
        param = p.bind_param(v)
        param2 = p.bind_param(v2)

        str(p)
        params = p.bound_params

        self.assertNotEqual(id(v), id(param))
        self.assertIn(v.value, params.values())
        self.assertIn(n, params)
        self.assertEqual(1, len(params))
        self.assertEqual(param.name, param2.name)
Example #30
0
    def test_can_add_one_property_underscore(self):
        p = Pypher()
        p.__property__

        expected = '.`property`'
        c = str(p)

        self.assertEqual(c, expected)