def test_field_option_defaults(engine, graph): result = engine.execute(graph, read('[{:x1s [:buz2]}]')) check_result( result, { 'x1s': [ { 'buz2': 'a1 - 100' }, { 'buz2': 'a3 - 100' }, { 'buz2': 'a2 - 100' }, ] }) result = engine.execute(graph, read('[{:x1s [(:buz2 {:size 200})]}]')) check_result( result, { 'x1s': [ { 'buz2': 'a1 - 200' }, { 'buz2': 'a3 - 200' }, { 'buz2': 'a2 - 200' }, ] })
def test_sequence_in_arg_type(engine, graph): result = engine.execute(graph, read('[{:x1s [:baz]}]')) check_result(result, { 'x1s': [ { 'baz': 'D3 [B1]' }, { 'baz': 'D1 [B3]' }, { 'baz': 'D2 [B2]' }, ] }) result = engine.execute(graph, read('[{:y1s [:baz]}]')) check_result(result, { 'y1s': [ { 'baz': 'D3 [B1]' }, { 'baz': 'D1 [B3]' }, { 'baz': 'D2 [B2]' }, ] })
def testSequenceInArgType(self): result = self.engine.execute(GRAPH, read('[{:x1s [:baz]}]')) self.assertResult(result, {'x1s': [ {'baz': 'D3 [B1]'}, {'baz': 'D1 [B3]'}, {'baz': 'D2 [B2]'}, ]}) result = self.engine.execute(GRAPH, read('[{:y1s [:baz]}]')) self.assertResult(result, {'y1s': [ {'baz': 'D3 [B1]'}, {'baz': 'D1 [B3]'}, {'baz': 'D2 [B2]'}, ]})
def testFieldOptionDefaults(self): result = self.engine.execute(GRAPH, read('[{:x1s [:buz2]}]')) self.assertResult(result, {'x1s': [ {'buz2': 'a1 - 100'}, {'buz2': 'a3 - 100'}, {'buz2': 'a2 - 100'}, ]}) result = self.engine.execute(GRAPH, read('[{:x1s [(:buz2 {:size 200})]}]')) self.assertResult(result, {'x1s': [ {'buz2': 'a1 - 200'}, {'buz2': 'a3 - 200'}, {'buz2': 'a2 - 200'}, ]})
def testField(self): result = self.engine.execute(GRAPH, read('[{:x1s [:a :f]}]')) self.assertResult(result, {'x1s': [ {'a': 'a1', 'f': 7}, {'a': 'a3', 'f': 7}, {'a': 'a2', 'f': 7}, ]})
def testSubQuery(self): thread_pool = ThreadPoolExecutor(2) e = Engine(ThreadsExecutor(thread_pool)) def query_a(fields, ids): data = {1: {'f': 2}} return [[data[i][f] for f in fields] for i in ids] r = graph.Edge(None, [ graph.Edge('a', [ graph.Field('f', query_a), ]), ]) # ---------------------------------------------- @define('[[:f]]', _name='inc_f') def inc_f(obj): return obj['f'] + 1 r1 = graph.Edge(None, [ graph.Edge('a1', subquery_fields(r, 'a', { 'f1': inc_f(S.this), })), graph.Link('la1', None, 'a1', lambda: [1], True), ]) self.assertEqual( e.execute(r1, read('[{:la1 [:f1]}]'))['la1'], [{ 'f1': 3 }])
def testField(self): result = self.engine.execute(HIGH_ENV, read('[{:xs1 [:a]}]')) self.assertResult(result, {'xs1': [ {'a': 'a1'}, {'a': 'a2'}, {'a': 'a3'}, ]})
def testSubQuery(self): thread_pool = ThreadPoolExecutor(2) e = Engine(ThreadsExecutor(thread_pool)) def query_a(fields, ids): data = {1: {'f': 2}} return [[data[i][f] for f in fields] for i in ids] r = graph.Edge(None, [ graph.Edge('a', [ graph.Field('f', query_a), ]), ]) # ---------------------------------------------- @define('[[:f]]', _name='inc_f') def inc_f(obj): return obj['f'] + 1 r1 = graph.Edge(None, [ graph.Edge('a1', subquery_fields(r, 'a', { 'f1': inc_f(S.this), })), graph.Link('la1', None, 'a1', lambda: [1], True), ]) self.assertEqual(e.execute(r1, read('[{:la1 [:f1]}]'))['la1'], [{'f1': 3}])
def testFieldOptions(self): result = self.engine.execute(GRAPH, read('[{:x1s [(:buz {:size "100"})]}]')) self.assertResult(result, {'x1s': [ {'buz': 'a1 - 100'}, {'buz': 'a3 - 100'}, {'buz': 'a2 - 100'}, ]})
def testFieldWithoutOptions(self): result = self.engine.execute(GRAPH, read('[{:x1s [:buz]}]')) self.assertResult(result, {'x1s': [ {'buz': 'a1 - None'}, {'buz': 'a3 - None'}, {'buz': 'a2 - None'}, ]})
async def _check(self, src, value): sa_engine = create_async_engine(self.db_dsn) engine = Engine(AsyncIOExecutor()) try: result = await engine.execute(self.graph, read(src), {SA_ENGINE_KEY: sa_engine}) check_result(result, value) finally: await greenlet_spawn(sa_engine.sync_engine.dispose)
def _check(self, src, value, event_loop): sa_engine = yield from aiopg.sa.create_engine(self.db_dsn, minsize=0, loop=event_loop) try: engine = Engine(AsyncIOExecutor(event_loop)) result = yield from engine.execute(self.graph, read(src), {SA_ENGINE_KEY: sa_engine}) check_result(result, value) finally: sa_engine.close() yield from sa_engine.wait_closed()
async def _check(self, src, value): sa_engine = await aiopg.sa.create_engine(self.db_dsn, minsize=0) engine = Engine(AsyncIOExecutor()) try: result = await engine.execute(self.graph, read(src), {SA_ENGINE_KEY: sa_engine}) check_result(result, value) finally: sa_engine.close() await sa_engine.wait_closed()
def index(): fn = lookup.get('index/view') query_str = str(fn.query()) print('QUERY:', query_str) hiku_query = read(query_str) result = engine.execute(GRAPH, hiku_query, ctx=hiku_engine_ctx) print('RESULT:') pprint(denormalize(GRAPH, result, hiku_query)) return fn.render(result)
def check(self, src, value): sa_engine = create_engine( 'sqlite://', connect_args={'check_same_thread': False}, poolclass=StaticPool, ) setup_db(sa_engine) engine = Engine(ThreadsExecutor(thread_pool)) result = engine.execute(self.graph, read(src), {SA_ENGINE_KEY: sa_engine}) check_result(result, value)
def test_link_invalid_options(): # missing options with pytest.raises(TypeError): read('[{(:foo) [:baz]}]') # invalid options type with pytest.raises(TypeError): read('[{(:foo :bar) [:baz]}]') # more arguments than expected with pytest.raises(TypeError): read('[{(:foo 1 2) [:bar]}]') # invalid option key with pytest.raises(TypeError): read('[{(:foo {1 2}) [:bar]}]')
def test_field_invalid_options(): # missing options with pytest.raises(TypeError): read('[(:foo)]') # invalid options type with pytest.raises(TypeError): read('[(:foo :bar)]') # more arguments than expected with pytest.raises(TypeError): read('[(:foo 1 2)]') # invalid option key with pytest.raises(TypeError): read('[(:foo {1 2})]')
def testLinkInvalidOptions(self): # missing options with self.assertRaises(TypeError): read('[{(:foo) [:baz]}]') # invalid options type with self.assertRaises(TypeError): read('[{(:foo :bar) [:baz]}]') # more arguments than expected with self.assertRaises(TypeError): read('[{(:foo 1 2) [:bar]}]') # invalid option key with self.assertRaises(TypeError): read('[{(:foo {1 2}) [:bar]}]')
def testFieldInvalidOptions(self): # missing options with self.assertRaises(TypeError): read('[(:foo)]') # invalid options type with self.assertRaises(TypeError): read('[(:foo :bar)]') # more arguments than expected with self.assertRaises(TypeError): read('[(:foo 1 2)]') # invalid option key with self.assertRaises(TypeError): read('[(:foo {1 2})]')
def test_field_options(engine, graph): result = engine.execute(graph, read('[{:x1s [(:buz {:size "100"})]}]')) check_result(result, { 'x1s': [ { 'buz': 'a1 - 100' }, { 'buz': 'a3 - 100' }, { 'buz': 'a2 - 100' }, ] })
def test_field_without_options(engine, graph): result = engine.execute(graph, read('[{:x1s [:buz]}]')) check_result( result, { 'x1s': [ { 'buz': 'a1 - None' }, { 'buz': 'a3 - None' }, { 'buz': 'a2 - None' }, ] })
def test_field(engine, graph): result = engine.execute(graph, read('[{:x1s [:a :f]}]')) check_result(result, { 'x1s': [ { 'a': 'a1', 'f': 7 }, { 'a': 'a3', 'f': 7 }, { 'a': 'a2', 'f': 7 }, ] })
def test_mixed_query(engine, graph): result = engine.execute(graph, read('[{:x1s [(:with_option {:opt 123}) :a]}]')) check_result( result, { 'x1s': [ { 'a': 'a1', 'with_option': 7 }, { 'a': 'a3', 'with_option': 9 }, { 'a': 'a2', 'with_option': 8 }, ] })
def testInvalidRoot(self): with self.assertRaises(TypeError): read('{:foo []}') with self.assertRaises(TypeError): read(':foo')
def check_read(source, query): first = read(source) assert first == query
def execute(self, query): return self.engine.execute(TEST_ENV, read(query))
def check_read(source, query): first = read(source) with reqs_eq_patcher(): assert first == query
def test_field_invalid(): with pytest.raises(TypeError): read('["foo"]') with pytest.raises(TypeError): read('[1]')
def test_field_without_required_option(engine, graph): with pytest.raises(TypeError) as err: engine.execute(graph, read('[{:x1s [:buz3]}]')) err.match('^Required option "size" for (.*)buz3(.*) was not provided$')
def testField(self): result = self.engine.execute(HIGH_ENV, read("[{:xs1 [:a]}]")) self.assertResult(result, {"xs1": [{"a": "a1"}, {"a": "a2"}, {"a": "a3"}]})
def assertReads(self, source, query): first = read(source) with reqs_eq_patcher(): self.assertEqual(first, query)
def assertExecute(self, src, result): result = self.engine.execute(ENV, read(src)) self.assertResult(result, result)
def check_result(query_string, result): new_result = denormalize(GRAPH, RESULT, read(query_string)) json.dumps(new_result) # using json to check for circular references assert new_result == result
def check_pb(query, expected): pb_root = t.Root() populate(pb_root, GRAPH, RESULT, read(query)) assert pb_root == msg(t.Root, expected)
def check_result(query_string, result): query = merge([read(query_string)]) new_result = denormalize(GRAPH, get_result(query)) json.dumps(new_result) # using json to check for circular references assert new_result == result
def testFieldInvalid(self): with self.assertRaises(TypeError): read('["foo"]') with self.assertRaises(TypeError): read('[1]')
async def execute(hiku_engine, sa_engine, graph, query_string): query = read(query_string) result = await hiku_engine.execute(graph, query, {SA_ENGINE_KEY: sa_engine}) return denormalize(graph, result, query)
def test_link_invalid(): with pytest.raises(TypeError): read('[{"foo" [:baz]}]') with pytest.raises(TypeError): read('[{foo [:baz]}]')
def test_invalid_root(): with pytest.raises(TypeError): read('{:foo []}') with pytest.raises(TypeError): read(':foo')
def graph_endpoint(): request_data = request.data.decode("utf-8") request_data = read(request_data) result = hiku_engine.execute(GRAPH, request_data) result = denormalize(GRAPH, result, request_data) return jsonify(result)
def testLinkInvalid(self): with self.assertRaises(TypeError): read('[{"foo" [:baz]}]') with self.assertRaises(TypeError): read('[{foo [:baz]}]')
def execute(graph, query_string): query = read(query_string) result = hiku_engine.execute(graph, query, {SA_ENGINE_KEY: sa_engine}) return denormalize(graph, result, query)
def execute(graph, query_string): query = read(query_string) result = hiku_engine.execute(graph, query, {SA_ENGINE_KEY: sa_engine}) return denormalize(graph, result)
def execute(graph, query_string): query = read(query_string) result = hiku_engine.execute(graph, query) return denormalize(graph, result, query)
def execute(graph, query_string): query = read(query_string) result = hiku_engine.execute(graph, query) return denormalize(graph, result)
def execute(query_, ctx=None): engine = Engine(ThreadsExecutor(thread_pool)) return engine.execute(get_graph(), read(query_), ctx=ctx)