Exemplo n.º 1
0
    def test_can_apply_two_statements_with_args(self):
        one = 'one'
        two = 'two'

        class First(Statement):
            def build(self):
                g = self.gremlin

                g.first(one)

        class Second(Statement):
            def build(self):
                g = self.gremlin

                g.second(two)

        f = First()
        s = Second()
        g = Gremlin()

        g.apply_statement(f).apply_statement(s)

        string = str(g)
        params = g.bound_params
        arg_1 = get_dict_key(params, one)
        arg_2 = get_dict_key(params, two)
        expected = 'g.first(%s).second(%s)' % (arg_1, arg_2)

        self.assertTrue(string == expected)
Exemplo n.º 2
0
 def test_can_apply_two_statements_with_args(self):
     one = 'one'
     two = 'two'
     
     class First(Statement):
         def build(self):
             g = self.gremlin
             
             g.first(one)
     
     class Second(Statement):
         def build(self):
             g = self.gremlin
             
             g.second(two)
     
     f = First()
     s = Second()
     g = Gremlin()
     
     g.apply_statement(f).apply_statement(s)
     
     string   = str(g)
     params   = g.bound_params
     arg_1    = get_dict_key(params, one)
     arg_2    = get_dict_key(params, two)
     expected = 'g.first(%s).second(%s)' % (arg_1, arg_2)
     
     self.assertTrue(string == expected)
Exemplo n.º 3
0
    def test_can_nest_one_statement_into_another_via_unbound(self):

        class Outer(Statement):

            def build(self):
                g = self.gremlin
                i = Inner()

                g.unbound('outer', i)

        class Inner(Statement):

            def build(self):
                g = self.gremlin
                g.inner_statement

        g = Gremlin()
        o = Outer()

        g.apply_statement(o)

        string = str(g)
        params = g.bound_params
        expected = 'g.outer(g.inner_statement)'

        self.assertTrue(string == expected)
        self.assertTrue(len(params) == 0)
Exemplo n.º 4
0
    def test_can_apply_statement(self):
        class statement(Statement):
            def build(self):
                g = self.gremlin

                g.random().statement()

        s = statement()
        g = Gremlin()

        g.apply_statement(s)

        string = str(g)
        expected = 'g.random().statement()'

        self.assertTrue(string == expected)
Exemplo n.º 5
0
 def test_can_apply_statement(self):
     class statement(Statement):
         def build(self):
             g = self.gremlin
             
             g.random().statement()
             
     s = statement()
     g = Gremlin()
     
     g.apply_statement(s)
     
     string   = str(g)
     expected = 'g.random().statement()'
     
     self.assertTrue(string == expected)
Exemplo n.º 6
0
    def test_can_make_conditional_statement(self):
        g = Gremlin()
        c = Conditional()
        cond = '1 == 2'
        body = 'nope'
        else_c = 'one doesnt equal two'

        c.set_if(cond, body)
        c.set_else(else_c)
        g.apply_statement(c)

        string = str(g)
        params = g.bound_params
        expected = 'if(%s){%s}else{%s}' % (cond, body, else_c)

        self.assertTrue(string == expected)
        self.assertTrue(len(params) == 0)
Exemplo n.º 7
0
 def test_can_make_conditional_statement(self):
     g      = Gremlin()
     c      = Conditional()
     cond   = '1 == 2'
     body   = 'nope'
     else_c = 'one doesnt equal two'
     
     c.set_if(cond, body)
     c.set_else(else_c)
     g.apply_statement(c)
     
     string   = str(g)
     params   = g.bound_params
     expected = 'if(%s){%s}else{%s}' % (cond, body, else_c)
     
     self.assertTrue(string == expected)
     self.assertTrue(len(params) == 0)
Exemplo n.º 8
0
    def _build_save_statements(self, entity, query, **kwargs):
        statement_query = Query(self.mapper)
        query_gremlin = Gremlin(self.gremlin.gv)

        for entry in query.queries:
            query_gremlin.bind_params(entry['params'])

        for statement in self.save_statements:
            instance = statement(entity, self, query, **kwargs)

            query_gremlin.apply_statement(instance)

        statement_query._add_query(str(query_gremlin),
                                   query_gremlin.bound_params,
                                   entity=entity)

        return statement_query
Exemplo n.º 9
0
    def test_can_apply_statement_with_args(self):
        arg = 'statement_arg'

        class statement(Statement):
            def build(self):
                g = self.gremlin

                g.statementFunction(arg)

        s = statement()
        g = Gremlin()

        g.apply_statement(s)

        string = str(g)
        params = g.bound_params
        expected = 'g.statementFunction(%s)' % get_dict_key(params, arg)

        self.assertTrue(string == expected)
Exemplo n.º 10
0
 def test_can_apply_statement_with_args(self):
     arg = 'statement_arg'
     
     class statement(Statement):
         def build(self):
             g = self.gremlin
             
             g.statementFunction(arg)
     
     s = statement()
     g = Gremlin()
     
     g.apply_statement(s)
     
     string   = str(g)
     params   = g.bound_params
     expected = 'g.statementFunction(%s)' % get_dict_key(params, arg)
     
     self.assertTrue(string == expected)
Exemplo n.º 11
0
Arquivo: mapper.py Projeto: sns/Gizmo
    def _save_edge(self, model, bind_return=True):
        query = Query(self.gremlin, self.mapper)
        save = True
        import pudb
        pu.db

        out_v = model.out_v
        in_v = model.in_v
        import pudb
        pu.db
        out_v_ref = self.mapper.get_model_variable(out_v)
        in_v_ref = self.mapper.get_model_variable(in_v)

        if not out_v_ref and isinstance(out_v,
                                        Vertex) and out_v['_id'] is None:
            self.mapper.save(out_v)
            out_v = self.mapper.get_model_variable(out_v)
        else:
            out_v = out_v_ref

        if not in_v_ref and isinstance(in_v, Vertex) and in_v['_id'] is None:
            self.mapper.save(in_v)
            in_v = self.mapper.get_model_variable(in_v)
        else:
            in_v = in_v_ref

        out_v = out_v['_id'] if isinstance(out_v, Vertex) else out_v
        in_v = in_v['_id'] if isinstance(in_v, Vertex) else in_v

        if model['_id'] and self.unique:
            edge = GetEdge(out_v, in_v, model['_label'])
            gremlin = Gremlin(self.gremlin.gv)

            gremlin.apply_statement(edge)

            try:
                edge = self.mapper.query(gremlin=gremlin).first()
                save = False

                query.by_id(edge['_id'], model)
            except Exception, e:
                save = True
Exemplo n.º 12
0
    def test_can_make_get_edge_statement(self):
        out_id = 1
        in_id = 9
        label = 'knows'
        g = Gremlin()
        e = GetEdge(out_id, in_id, label)

        g.apply_statement(e)

        string = str(g)
        params = g.bound_params
        oid = get_dict_key(params, out_id)
        iid = get_dict_key(params, in_id)
        label_b = get_dict_key(params, label)
        as_b = get_dict_key(params, 'vertex')
        bound = (oid, label_b, as_b, iid, as_b)
        expected = 'g.v(%s).outE(%s).as(%s).inV.retain([g.v(%s)]).back(%s)' % bound

        self.assertEquals(expected, string)
        self.assertTrue(len(params) == 4)
Exemplo n.º 13
0
 def test_can_make_get_edge_statement(self):
     out_id = 1
     in_id  = 9
     label  = 'knows'
     g      = Gremlin()
     e      = GetEdge(out_id, in_id, label)
     
     g.apply_statement(e)
     
     string   = str(g)
     params   = g.bound_params
     oid      = get_dict_key(params, out_id)
     iid      = get_dict_key(params, in_id)
     label_b  = get_dict_key(params, label)
     as_b     = get_dict_key(params, 'vertex')
     bound    = (oid, label_b, as_b, iid, as_b)
     expected = 'g.v(%s).outE(%s).as(%s).inV.retain([g.v(%s)]).back(%s)' % bound
     
     self.assertEquals(expected, string)
     self.assertTrue(len(params) == 4)
Exemplo n.º 14
0
    def test_can_make_get_edge_statement_out_direction(self):
        out_id = 1
        in_id = 9
        label = 'knows'
        g = Gremlin()
        e = GetEdge(out_id, in_id, label, 'out')

        g.apply_statement(e)

        string = str(g)
        params = g.bound_params
        oid = get_dict_key(params, out_id)
        iid = get_dict_key(params, in_id)
        label_b = get_dict_key(params, label)
        as_b = e.bound_entity
        bound = (oid, label_b, as_b, iid, as_b)
        expected = 'g.V(%s).outE(%s).as(%s).inV().hasId(%s).select(%s)' % \
            bound

        self.assertEqual(expected, string)
Exemplo n.º 15
0
 def test_can_make_conditional_statement_with_two_elseif(self):
     g       = Gremlin()
     c       = Conditional()
     cond    = '1 == 2'
     body    = 'nope'
     else_i  = '2 == 2'
     else_b  = 'two does equal 2'
     else_i2 = '20 == 20'
     else_b2 = 'already caught above'
     else_c  = 'one doesnt equal two'
     
     c.set_if(cond, body)
     c.set_elif(else_i, else_b)
     c.set_elif(else_i2, else_b2)
     c.set_else(else_c)
     g.apply_statement(c)
     
     string   = str(g)
     params   = g.bound_params
     expected = 'if(%s){%s}elseif(%s){%s}elseif(%s){%s}else{%s}' % (cond, body, else_i, else_b, else_i2, else_b2, else_c)
     
     self.assertTrue(string == expected)
     self.assertTrue(len(params) == 0) 
Exemplo n.º 16
0
    def test_can_make_conditional_statement_with_two_elseif(self):
        g = Gremlin()
        c = Conditional()
        cond = '1 == 2'
        body = 'nope'
        else_i = '2 == 2'
        else_b = 'two does equal 2'
        else_i2 = '20 == 20'
        else_b2 = 'already caught above'
        else_c = 'one doesnt equal two'

        c.set_if(cond, body)
        c.set_elif(else_i, else_b)
        c.set_elif(else_i2, else_b2)
        c.set_else(else_c)
        g.apply_statement(c)

        string = str(g)
        params = g.bound_params
        expected = 'if(%s){%s}elseif(%s){%s}elseif(%s){%s}else{%s}' % (
            cond, body, else_i, else_b, else_i2, else_b2, else_c)

        self.assertTrue(string == expected)
        self.assertTrue(len(params) == 0)