def test_bytecode(self): g = Graph().traversal() bytecode = g.V().out("created").bytecode assert 0 == len(bytecode.bindings.keys()) assert 0 == len(bytecode.source_instructions) assert 2 == len(bytecode.step_instructions) assert "V" == bytecode.step_instructions[0][0] assert "out" == bytecode.step_instructions[1][0] assert "created" == bytecode.step_instructions[1][1] assert 1 == len(bytecode.step_instructions[0]) assert 2 == len(bytecode.step_instructions[1]) ## bytecode = g.withSack(1).E().groupCount().by("weight").bytecode assert 0 == len(bytecode.bindings.keys()) assert 1 == len(bytecode.source_instructions) assert "withSack" == bytecode.source_instructions[0][0] assert 1 == bytecode.source_instructions[0][1] assert 3 == len(bytecode.step_instructions) assert "E" == bytecode.step_instructions[0][0] assert "groupCount" == bytecode.step_instructions[1][0] assert "by" == bytecode.step_instructions[2][0] assert "weight" == bytecode.step_instructions[2][1] assert 1 == len(bytecode.step_instructions[0]) assert 1 == len(bytecode.step_instructions[1]) assert 2 == len(bytecode.step_instructions[2]) ## bytecode = g.V(('a', [1, 2, 3])).out(('b', 'created')).where( __.in_(('c', 'created'), ('d', 'knows')).count().is_( ('e', P.gt(2)))).bytecode assert 'V' == bytecode.step_instructions[0][0] assert 'out' == bytecode.step_instructions[1][0] assert 'where' == bytecode.step_instructions[2][0] assert ('b', 'created') == bytecode.step_instructions[1][1] assert isinstance(hash(bytecode.step_instructions[1][1]), int)
async def test_side_effect_close(self, remote_connection): g = Graph().traversal().withRemote(remote_connection) t = g.V().aggregate('a').aggregate('b') await t.iterate() # The 'a' key should return some side effects results = await t.side_effects.get('a') assert results # Close result is None results = await t.side_effects.close() assert not results # Shouldn't get any new info from server # 'b' isn't in local cache results = await t.side_effects.get('b') assert not results # But 'a' should still be cached locally results = await t.side_effects.get('a') assert results # 'a' should have been added to local keys cache, but not 'b' results = await t.side_effects.keys() assert len(results) == 1 a, = results assert a == 'a' # Try to get 'b' directly from server, should throw error with pytest.raises(Exception): await t.side_effects._get('b') await remote_connection.close()
async def test_traversals(self, remote_connection): statics.load_statics(globals()) g = Graph().traversal().withRemote(remote_connection) result = await g.V().count().toList() assert long(6) == result[0] # # assert Vertex(1) == await g.V(1).next() assert 1 == await g.V(1).id().next() assert Traverser(Vertex(1)) == await g.V(1).nextTraverser() result = await g.V(1).toList() assert 1 == len(result) result = await g.V(1).toList() assert isinstance(result, list) results = g.V().repeat(out()).times(2).name results = await results.toList() assert 2 == len(results) assert "lop" in results assert "ripple" in results # # # assert 10 == await g.V().repeat(both()).times(5)[0:10].count().next() assert 1 == await g.V().repeat(both()).times(5)[0:1].count().next() assert 0 == await g.V().repeat(both()).times(5)[0:0].count().next() assert 4 == await g.V()[2:].count().next() assert 2 == await g.V()[:2].count().next() # # # results = await g.withSideEffect('a', [ 'josh', 'peter' ]).V(1).out('created').in_('created').values('name').where(within('a') ).toList() assert 2 == len(results) assert 'josh' in results assert 'peter' in results # # # todo: need a traversal metrics deserializer # g.V().out().profile().next() await remote_connection.close()
async def test_strategies(self, remote_connection): statics.load_statics(globals()) # g = Graph().traversal().withRemote(remote_connection). \ withStrategies(TraversalStrategy("SubgraphStrategy", {"vertices": __.hasLabel("person"), "edges": __.hasLabel("created")})) assert 4 == await g.V().count().next() assert 0 == await g.E().count().next() assert 1 == await g.V().label().dedup().count().next() assert "person" == await g.V().label().dedup().next() # g = Graph().traversal().withRemote(remote_connection). \ withStrategies(SubgraphStrategy(vertices=__.hasLabel("person"), edges=__.hasLabel("created"))) assert 4 == await g.V().count().next() assert 0 == await g.E().count().next() assert 1 == await g.V().label().dedup().count().next() assert "person" == await g.V().label().dedup().next() # g = g.withoutStrategies(SubgraphStrategy). \ withComputer(vertices=__.has("name", "marko"), edges=__.limit(0)) assert 1 == await g.V().count().next() assert 0 == await g.E().count().next() assert "person" == await g.V().label().next() assert "marko" == await g.V().name.next() # g = Graph().traversal().withRemote(remote_connection).withComputer() assert 6 == await g.V().count().next() assert 6 == await g.E().count().next() await remote_connection.close()
async def test_client_bytecode(client): g = Graph().traversal() t = g.V() message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode}) result_set = await client.submit(message) results = await result_set.all() assert len(results) == 6 await client.close()
async def test_connection(connection): g = Graph().traversal() t = g.V() message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode}) results_set = await connection.write(message) results = await results_set.all() assert len(results) == 6 assert isinstance(results, list) await connection.close()
async def test_iterate_result_set(client): g = Graph().traversal() t = g.V() message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode}) result_set = await client.submit(message) results = [] async for result in result_set: results.append(result) assert len(results) == 6 await client.close()
async def test_side_effects(self, remote_connection): statics.load_statics(globals()) g = Graph().traversal().withRemote(remote_connection) t = await g.V().hasLabel("project").name.iterate() keys = await t.side_effects.keys() assert 0 == len(keys) with pytest.raises(Exception): m = await t.side_effects["m"] t = g.V().out("created").groupCount("m").by("name") results = await t.toSet() assert 2 == len(results) assert Vertex(3) in results assert Vertex(5) in results keys = await t.side_effects.keys() assert 1 == len(keys) assert "m" in keys m = await t.side_effects["m"] assert isinstance(m, dict) assert 2 == len(m) assert 3 == m["lop"] assert 1 == m["ripple"] assert isinstance(m["lop"], long) assert isinstance(m["ripple"], long) # ## t = g.V().out("created").groupCount("m").by("name").name.aggregate("n") results = await t.toSet() assert 2 == len(results) assert "lop" in results assert "ripple" in results keys = await t.side_effects.keys() assert 2 == len(keys) assert "m" in keys assert "n" in keys n = await t.side_effects.get("n") assert isinstance(n, dict) assert 2 == len(n) assert "lop" in n.keys() assert "ripple" in n.keys() assert 3 == n["lop"] assert 1 == n["ripple"] # t = g.withSideEffect('m', 32).V().map(lambda: "x: x.sideEffects('m')") results = await t.toSet() assert 1 == len(results) assert 32 == list(results)[0] assert 32 == await t.side_effects['m'] keys = await t.side_effects.keys() assert 1 == len(keys) with pytest.raises(Exception): x = await t.side_effects["x"] await remote_connection.close()
async def test_connection(connection): log.debug("begin: test_connection") g = Graph().traversal() t = g.V() message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode}) log.debug(f"{g=}, {t=}, {message=}") results_set = await connection.write(message) log.debug(f"{results_set=}") results = await results_set.all() log.debug(f"{results=}") assert len(results) == 6 assert isinstance(results, list) # await connection.close() log.debug("end: test_connection")
def run_around_tests(remote_connection, event_loop): g = Graph().traversal().withRemote(remote_connection) async def create_graph(): await g.V().drop().iterate() software1 = await g.addV("software").property("name", "lop").property( "lang", "java").property(T.id, 3).next() software2 = await g.addV("software").property( "name", "ripple").property("lang", "java").property(T.id, 5).next() person1 = await g.addV("person").property("name", "marko").property( "age", "29").property(T.id, 1).next() person2 = await g.addV("person").property("name", "vadas").property( "age", "27").property(T.id, 2).next() person3 = await g.addV("person").property("name", "josh").property( "age", "32").property(T.id, 4).next() person4 = await g.addV("person").property("name", "peter").property( "age", "35").property(T.id, 6).next() knows1 = await g.addE("knows").from_(person1).to(person2).property( "weight", 0.5).property(T.id, 7).next() knows2 = await g.addE("knows").from_(person1).to(person3).property( "weight", 1, 0).property(T.id, 8).next() created1 = await g.addE("created").from_(person1).to( software1).property("weight", 0.4).property(T.id, 9).next() created2 = await g.addE("created").from_(person3).to( software2).property("weight", 1.0).property(T.id, 10).next() created3 = await g.addE("created").from_(person3).to( software1).property("weight", 1.0).property(T.id, 11).next() created4 = await g.addE("created").from_(person4).to( software1).property("weight", 0.2).property(T.id, 12).next() event_loop.run_until_complete(create_graph()) yield
def test_configurable(self): g = Graph().traversal() bytecode = g.withStrategies(MatchAlgorithmStrategy("greedy")).bytecode assert 1 == len(bytecode.source_instructions) assert 2 == len(bytecode.source_instructions[0]) assert "withStrategies" == bytecode.source_instructions[0][0] assert MatchAlgorithmStrategy() == bytecode.source_instructions[0][1] assert "MatchAlgorithmStrategy" == str(bytecode.source_instructions[0][1]) assert hash(MatchAlgorithmStrategy()) == hash( bytecode.source_instructions[0][1]) # even though different confs, same strategy assert 0 == len(g.traversal_strategies.traversal_strategies) # these strategies are proxies ### bytecode = g.withStrategies(SubgraphStrategy(vertices=__.has("name","marko"))).bytecode assert 1 == len(bytecode.source_instructions) assert 2 == len(bytecode.source_instructions[0]) assert "withStrategies" == bytecode.source_instructions[0][0] assert SubgraphStrategy() == bytecode.source_instructions[0][1] strategy = bytecode.source_instructions[0][1] assert 1 == len(strategy.configuration) assert __.has("name","marko") == strategy.configuration["vertices"]
def test_singletons(self): g = Graph().traversal() bytecode = g.withStrategies(ReadOnlyStrategy()).bytecode assert 1 == len(bytecode.source_instructions) assert 2 == len(bytecode.source_instructions[0]) assert "withStrategies" == bytecode.source_instructions[0][0] assert ReadOnlyStrategy() == bytecode.source_instructions[0][1] assert "ReadOnlyStrategy" == str(bytecode.source_instructions[0][1]) assert hash(ReadOnlyStrategy()) == hash(bytecode.source_instructions[0][1]) assert 0 == len(g.traversal_strategies.traversal_strategies) # these strategies are proxies ## g = g.withStrategies(ReadOnlyStrategy(), IncidentToAdjacentStrategy()) bytecode = g.bytecode assert 1 == len(bytecode.source_instructions) assert 3 == len(bytecode.source_instructions[0]) assert "withStrategies" == bytecode.source_instructions[0][0] assert ReadOnlyStrategy() == bytecode.source_instructions[0][1] assert IncidentToAdjacentStrategy() == bytecode.source_instructions[0][2] ## bytecode = g.V().bytecode assert 1 == len(bytecode.source_instructions) assert 3 == len(bytecode.source_instructions[0]) assert "withStrategies" == bytecode.source_instructions[0][0] assert ReadOnlyStrategy() == bytecode.source_instructions[0][1] assert IncidentToAdjacentStrategy() == bytecode.source_instructions[0][2] assert 1 == len(bytecode.step_instructions) assert "V" == bytecode.step_instructions[0][0] ## bytecode = g.withoutStrategies(ReadOnlyStrategy()).V().bytecode assert 2 == len(bytecode.source_instructions) assert 3 == len(bytecode.source_instructions[0]) assert 2 == len(bytecode.source_instructions[1]) assert "withStrategies" == bytecode.source_instructions[0][0] assert ReadOnlyStrategy() == bytecode.source_instructions[0][1] assert IncidentToAdjacentStrategy() == bytecode.source_instructions[0][2] assert "withoutStrategies" == bytecode.source_instructions[1][0] assert ReadOnlyStrategy() == bytecode.source_instructions[1][1] assert 1 == len(bytecode.step_instructions) assert "V" == bytecode.step_instructions[0][0] ## bytecode = g.withoutStrategies(ReadOnlyStrategy(), LazyBarrierStrategy()).V().bytecode assert 2 == len(bytecode.source_instructions) assert 3 == len(bytecode.source_instructions[0]) assert 3 == len(bytecode.source_instructions[1]) assert "withStrategies" == bytecode.source_instructions[0][0] assert ReadOnlyStrategy() == bytecode.source_instructions[0][1] assert IncidentToAdjacentStrategy() == bytecode.source_instructions[0][2] assert "withoutStrategies" == bytecode.source_instructions[1][0] assert ReadOnlyStrategy() == bytecode.source_instructions[1][1] assert LazyBarrierStrategy() == bytecode.source_instructions[1][2] assert 1 == len(bytecode.step_instructions) assert "V" == bytecode.step_instructions[0][0]
def test_bytecode(self): g = Graph().traversal() bytecode = g.V().out("created").bytecode assert 0 == len(bytecode.bindings.keys()) assert 0 == len(bytecode.source_instructions) assert 2 == len(bytecode.step_instructions) assert "V" == bytecode.step_instructions[0][0] assert "out" == bytecode.step_instructions[1][0] assert "created" == bytecode.step_instructions[1][1] assert 1 == len(bytecode.step_instructions[0]) assert 2 == len(bytecode.step_instructions[1]) ## bytecode = g.withSack(1).E().groupCount().by("weight").bytecode assert 0 == len(bytecode.bindings.keys()) assert 1 == len(bytecode.source_instructions) assert "withSack" == bytecode.source_instructions[0][0] assert 1 == bytecode.source_instructions[0][1] assert 3 == len(bytecode.step_instructions) assert "E" == bytecode.step_instructions[0][0] assert "groupCount" == bytecode.step_instructions[1][0] assert "by" == bytecode.step_instructions[2][0] assert "weight" == bytecode.step_instructions[2][1] assert 1 == len(bytecode.step_instructions[0]) assert 1 == len(bytecode.step_instructions[1]) assert 2 == len(bytecode.step_instructions[2]) ## bytecode = g.V(('a',[1,2,3])).out(('b','created')).where(__.in_(('c','created'),('d','knows')).count().is_(('e',P.gt(2)))).bytecode assert 5 == len(bytecode.bindings.keys()) assert [1,2,3] == bytecode.bindings['a'] assert 'created' == bytecode.bindings['b'] assert 'created' == bytecode.bindings['c'] assert 'knows' == bytecode.bindings['d'] assert P.gt(2) == bytecode.bindings['e'] assert Binding('b','created') == bytecode.step_instructions[1][1] assert 'binding[b=created]' == str(bytecode.step_instructions[1][1]) assert isinstance(hash(bytecode.step_instructions[1][1]),int)
async def test_label(self, remote_connection): statics.load_statics(globals()) g = Graph().traversal().withRemote(remote_connection) result = await g.V().limit(1).toList() await remote_connection.close()