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)
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)
def test_can_add_function_where_no_params_are_bound(self): g = Gremlin() g.unbound('func', 'val1', 'val2') expected = 'g.func(val1, val2)' self.assertTrue(str(g) == expected) self.assertTrue(len(g.bound_params) == 0)
def test_can_add_an_index_with_single_value(self): g = Gremlin() g.func()[1] s = str(g) expected = 'g.func()[1]' self.assertTrue(s == expected)
def test_can_add_an_index_with_range(self): g = Gremlin() g.func()[1:2] s = str(g) expected = 'g.func()[1..2]' self.assertTrue(s == expected)
def test_can_add_return_variable(self): g = Gremlin() v = 'ret_var' g.set_ret_variable(v).function() expected = '%s = g.function()' % v script = str(g) self.assertEquals(expected, script)
def test_can_add_and_remove_return_variable(self): g = Gremlin() v = 'ret_var' g.set_ret_variable(v).function().set_ret_variable(None) expected = 'g.function()' script = str(g) self.assertEquals(expected, script)
def test_can_add_function_with_arg_before_attribute(self): g = Gremlin() g.function('val').a string = str(g) bind, value = g.bound_params.copy().popitem() expected = 'g.function(%s).a' % bind self.assertTrue(string == expected) self.assertTrue(len(g.bound_params) == 1)
def test_can_add_function_with_closure(self): g = Gremlin() g.condition('x').close('body') s = str(g) params = g.bound_params x = get_dict_key(params, 'x') expected = 'g.condition(%s){body}' % x self.assertTrue(s == expected) self.assertTrue(len(params) == 1)
def test_can_add_function_where_no_params_are_bound_and_func_with_bound_params(self): g = Gremlin() g.unbound('func', 'val1', 'val2').isbound('hello') s = str(g) params = g.bound_params hello = get_dict_key(params, 'hello') expected = 'g.func(val1, val2).isbound(%s)' % hello self.assertTrue(str(g) == expected) self.assertTrue(len(g.bound_params) == 1)
def test_can_nest_gremlin(self): g = Gremlin() n = Gremlin() g.nest(n.nested()) expected = 'g.nest(g.nested())' string = str(g) params = g.bound_params self.assertTrue(expected == string) self.assertTrue(len(params) == 0)
def test_can_add_an_index_after_closure(self): g = Gremlin() g.condition('x').close('body')[12:44] s = str(g) params = g.bound_params x = get_dict_key(params, 'x') expected = 'g.condition(%s){body}[12..44]' % x self.assertTrue(s == expected) self.assertTrue(len(params) == 1)
def test_can_add_raw_after_function(self): g = Gremlin() r = '--raw-text--' g.func().raw(r) s = str(g) params = g.bound_params expected = 'g.func()%s' % r self.assertTrue(s == expected) self.assertTrue(len(params) == 0)
def test_can_add_functon_manually_bind_two_args(self): g = Gremlin() bound = g.bind_param('arg') bound2 = g.bind_param('arg2') g.function(bound[0], bound2[0]) string = str(g) expected = 'g.function(%s, %s)' % (bound[0], bound2[0]) self.assertTrue(string == expected) self.assertTrue(len(g.bound_params) == 2)
def test_can_add_raw_drop_graph_variable(self): g = Gremlin() r = '--raw-text--' g.raw(r).set_graph_variable('') s = str(g) params = g.bound_params expected = '%s' % r self.assertTrue(s == expected) self.assertTrue(len(params) == 0)
def test_can_nest_double_nest_gremlin(self): g = Gremlin() n = Gremlin() d = Gremlin() g.nest(n.nested(d.deep())) expected = 'g.nest(g.nested(g.deep()))' string = str(g) params = g.bound_params self.assertTrue(expected == string) self.assertTrue(len(params) == 0)
def test_can_add_a_reserved_word_as_apart_of_the_query_as_function_with_args(self): g = Gremlin(); init = Function(g, '__init__', ['arg']) g.add_token(init) s = str(g) params = g.bound_params arg = get_dict_key(params, 'arg') expected = 'g.__init__(%s)' % arg self.assertTrue(s == expected) self.assertTrue(len(params) == 1)
def test_can_add_raw_after_closure(self): g = Gremlin() r = '--raw-text--' c = '[[[]]]' g.func().close(c).raw(r) s = str(g) params = g.bound_params expected = 'g.func(){%s}%s' % (c, r) self.assertTrue(s == expected) self.assertTrue(len(params) == 0)
def test_can_add_function_where_no_params_are_bound_and_func_with_bound_params( self): g = Gremlin() g.unbound('func', 'val1', 'val2').isbound('hello') s = str(g) params = g.bound_params hello = get_dict_key(params, 'hello') expected = 'g.func(val1, val2).isbound(%s)' % hello self.assertTrue(str(g) == expected) self.assertTrue(len(g.bound_params) == 1)
def test_can_add_a_reserved_word_as_apart_of_the_query_as_function_without_args(self): g = Gremlin(); unbound = ['arg', '2'] init = UnboudFunction(g, '__init__', unbound) g.add_token(init) s = str(g) params = g.bound_params expected = 'g.__init__(%s, %s)' % (unbound[0], unbound[1]) self.assertTrue(s == expected) self.assertTrue(len(params) == 0)
def test_can_add_functon_manually_bind_one_arg(self): g = Gremlin() bound = g.bind_param('arg') g.function(bound[0]) string = str(g) bind, value = g.bound_params.copy().popitem() expected = 'g.function(%s)' % bind self.assertTrue(string == expected) self.assertTrue(bind == bound[0]) self.assertTrue(value == bound[1]) self.assertTrue(len(g.bound_params) == 1)
def test_can_add_a_reserved_word_as_apart_of_the_query_as_function_without_args( self): g = Gremlin() unbound = ['arg', '2'] init = UnboudFunction(g, '__init__', unbound) g.add_token(init) s = str(g) params = g.bound_params expected = 'g.__init__(%s, %s)' % (unbound[0], unbound[1]) self.assertTrue(s == expected) self.assertTrue(len(params) == 0)
def test_can_add_raw_between_functions(self): g = Gremlin() r = '--raw-text--' a = 'arg' g.func().raw(r).func2(a) s = str(g) params = g.bound_params arg = get_dict_key(params, a) expected = 'g.func()%sfunc2(%s)' % (r, arg) self.assertTrue(s == expected) self.assertTrue(len(params) == 1)
def test_can_add_a_reserved_word_as_apart_of_the_query_as_function_without_args_after_other_calls(self): g = Gremlin(); unbound = ['arg', '2'] arg = 'some_arg' init = UnboudFunction(g, '__init__', unbound) g.someFunc(arg).add_token(init) s = str(g) params = g.bound_params expected = 'g.someFunc(%s).__init__(%s, %s)' % (get_dict_key(params, arg), unbound[0], unbound[1]) self.assertTrue(s == expected) self.assertTrue(len(params) == 1)
def test_can_add_a_reserved_word_as_apart_of_the_query_as_function_with_args( self): g = Gremlin() init = Function(g, '__init__', ['arg']) g.add_token(init) s = str(g) params = g.bound_params arg = get_dict_key(params, 'arg') expected = 'g.__init__(%s)' % arg self.assertTrue(s == expected) self.assertTrue(len(params) == 1)
def __init__(self, mapper): self.mapper = mapper self.gremlin = Gremlin(self.mapper.gremlin.gv) self.queries = [] self.fields = [] self.reset()
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)
def test_can_add_unbound_functon_two_args(self): g = Gremlin().unbound('function', 'arg', 'two') string = str(g) expected = 'g.function(arg, two)' self.assertEquals(string, expected) self.assertEquals(len(g.bound_params), 0)
def setUp(self): super().setUp() self.request = TestRequest() self.mapper = Mapper(request=self.request) self.query = Query(mapper=self.mapper) self.gremlin = Gremlin() self.ioloop = asyncio.get_event_loop()
def __init__(self, gremlin=None, mapper=None): if gremlin is None: gremlin = Gremlin() self.gremlin = gremlin self.mapper = mapper self.reset()
def test_can_add_a_reserved_word_as_apart_of_the_query_as_function_without_args_after_other_calls( self): g = Gremlin() unbound = ['arg', '2'] arg = 'some_arg' init = UnboudFunction(g, '__init__', unbound) g.someFunc(arg).add_token(init) s = str(g) params = g.bound_params expected = 'g.someFunc(%s).__init__(%s, %s)' % (get_dict_key( params, arg), unbound[0], unbound[1]) self.assertTrue(s == expected) self.assertTrue(len(params) == 1)
def test_can_add_a_reserved_word_as_apart_of_the_query_as_function_with_args_after_other_calls(self): g = Gremlin(); args = ['arg', 'LOOK', 'AT'] init = Function(g, '__init__', [args[0]]) g.look(args[1]).at(args[2]).close('--this--').add_token(init) s = str(g) params = g.bound_params arg = get_dict_key(params, args[0]) look = get_dict_key(params, args[1]) at = get_dict_key(params, args[2]) expected = 'g.look(%s).at(%s){--this--}.__init__(%s)' % (look, at, arg) self.assertTrue(s == expected) self.assertTrue(len(params) == 3)
def __init__(self, gremlin=None, mapper=None): if gremlin is None: gremlin = Gremlin() self.gremlin = gremlin self.mapper = mapper self.fields = [] self.queries = []
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)
def test_can_add_functon_one_arg(self): g = Gremlin().function('arg') string = str(g) bind, value = g.bound_params.copy().popitem() expected = 'g.function(%s)' % bind self.assertTrue(string == expected) self.assertTrue(len(g.bound_params) == 1)
def test_can_add_functon_three_args(self): g = Gremlin().function('one', 'two', 'three') string = str(g) bind, value = g.bound_params.copy().popitem() expected = 'g.function(one, two, %s)' % bind self.assertTrue(string == expected) self.assertTrue(len(g.bound_params) == 1)
def test_can_add_a_reserved_word_as_apart_of_the_query_as_function_with_args_after_other_calls( self): g = Gremlin() args = ['arg', 'LOOK', 'AT'] init = Function(g, '__init__', [args[0]]) g.look(args[1]).at(args[2]).close('--this--').add_token(init) s = str(g) params = g.bound_params arg = get_dict_key(params, args[0]) look = get_dict_key(params, args[1]) at = get_dict_key(params, args[2]) expected = 'g.look(%s).at(%s){--this--}.__init__(%s)' % (look, at, arg) self.assertTrue(s == expected) self.assertTrue(len(params) == 3)
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)
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)
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)
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)
def test_can_nest_with_bound_params(self): g = Gremlin() d = {'name': 'parent'} n = Gremlin() p = {'prop': 'child'} n.set_graph_variable('').setSubProp('prop', p['prop']) g.func('name', d['name']).nest(n) string = str(g) params = g.bound_params name = get_dict_key(params, 'parent') child = get_dict_key(params, 'child') expected = 'g.func(name, %s).nest(setSubProp(prop, %s))' % (name, child) self.assertTrue(expected == string) self.assertTrue(len(params) == 2)