def test_traversals(self):
        statics.load_statics(globals())
        connection = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g')
        assert "remoteconnection[ws://localhost:8182/gremlin,g]" == str(
            connection)
        g = Graph().traversal().withRemote(connection)

        assert long(6) == g.V().count().toList()[0]
        #
        assert Vertex(1) == g.V(1).next()
        assert 1 == g.V(1).id().next()
        assert Traverser(Vertex(1)) == g.V(1).nextTraverser()
        assert 1 == len(g.V(1).toList())
        assert isinstance(g.V(1).toList(), list)
        results = g.V().repeat(out()).times(2).name
        results = results.toList()
        assert 2 == len(results)
        assert "lop" in results
        assert "ripple" in results
        #
        assert 10 == g.V().repeat(both()).times(5)[0:10].count().next()
        assert 1 == g.V().repeat(both()).times(5)[0].count().next()
        assert 0 == g.V().repeat(both()).times(5)[0:0].count().next()
        assert 4 == g.V()[2:].count().next()
        assert 2 == g.V()[:2].count().next()
        # todo: need a traversal metrics deserializer
        g.V().out().profile().next()
        connection.close()
Exemple #2
0
 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  # FIXME : Test fails here.
     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()
    def test_traversals(self, remote_connection):
        statics.load_statics(globals())
        assert "remoteconnection[ws://localhost:45940/gremlin,g]" == str(
            remote_connection)
        g = Graph().traversal().withRemote(remote_connection)

        assert long(6) == g.V().count().toList()[0]
        # #
        assert Vertex(1) == g.V(1).next()
        assert 1 == g.V(1).id().next()
        assert Traverser(Vertex(1)) == g.V(1).nextTraverser()
        assert 1 == len(g.V(1).toList())
        assert isinstance(g.V(1).toList(), list)
        results = g.V().repeat(out()).times(2).name
        results = results.toList()
        assert 2 == len(results)
        assert "lop" in results
        assert "ripple" in results
        # #
        assert 10 == g.V().repeat(both()).times(5)[0:10].count().next()
        assert 1 == g.V().repeat(both()).times(5)[0:1].count().next()
        assert 0 == g.V().repeat(both()).times(5)[0:0].count().next()
        assert 4 == g.V()[2:].count().next()
        assert 2 == g.V()[:2].count().next()
        # #
        results = 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()
Exemple #4
0
 def test_numbers(self):
     x = GraphSONReader.readObject(
         json.dumps({
             "@type": "g:Int32",
             "@value": 31
         }))
     assert isinstance(x, int)
     assert 31 == x
     ##
     x = GraphSONReader.readObject(
         json.dumps({
             "@type": "g:Int64",
             "@value": 31
         }))
     assert isinstance(x, long)
     assert long(31) == x
     ##
     x = GraphSONReader.readObject(
         json.dumps({
             "@type": "g:Float",
             "@value": 31.3
         }))
     assert isinstance(x, float)
     assert 31.3 == x
     ##
     x = GraphSONReader.readObject(
         json.dumps({
             "@type": "g:Double",
             "@value": 31.2
         }))
     assert isinstance(x, float)
     assert 31.2 == x
    def test_traversals(self):
        statics.load_statics(globals())
        connection = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g')
        assert "remoteconnection[ws://localhost:8182/gremlin,g]" == str(connection)
        g = Graph().traversal().withRemote(connection)

        assert long(6) == g.V().count().toList()[0]
        #
        assert Vertex(1) == g.V(1).next()
        assert 1 == g.V(1).id().next()
        assert Traverser(Vertex(1)) == g.V(1).nextTraverser()
        assert 1 == len(g.V(1).toList())
        assert isinstance(g.V(1).toList(), list)
        results = g.V().repeat(out()).times(2).name
        results = results.toList()
        assert 2 == len(results)
        assert "lop" in results
        assert "ripple" in results
        #
        assert 10 == g.V().repeat(both()).times(5)[0:10].count().next()
        assert 1 == g.V().repeat(both()).times(5)[0].count().next()
        assert 0 == g.V().repeat(both()).times(5)[0:0].count().next()
        assert 4 == g.V()[2:].count().next()
        assert 2 == g.V()[:2].count().next()
        # todo: need a traversal metrics deserializer
        g.V().out().profile().next()
        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()
Exemple #7
0
def _convert(val, ctx):
    graph_name = ctx.graph_name
    if isinstance(val, dict):  # convert dictionary keys/values
        n = {}
        for key, value in val.items():
            k = _convert(key, ctx)
            # convert to tuple key if list/set as neither are hashable
            n[tuple(k) if isinstance(k, (set, list)) else k] = _convert(
                value, ctx)
        return n
    elif isinstance(val, str) and re.match(r"^l\[.*\]$", val):  # parse list
        return [] if val == "l[]" else list(
            map((lambda x: _convert(x, ctx)), val[2:-1].split(",")))
    elif isinstance(val, str) and re.match(r"^s\[.*\]$", val):  # parse set
        return set() if val == "s[]" else set(
            map((lambda x: _convert(x, ctx)), val[2:-1].split(",")))
    elif isinstance(val, str) and re.match(r"^d\[.*\]\.[ilfdm]$",
                                           val):  # parse numeric
        return float(val[2:-3]) if val[2:-3].__contains__(".") else long(
            val[2:-3])
    elif isinstance(val, str) and re.match(r"^v\[.*\]\.id$",
                                           val):  # parse vertex id
        return __find_cached_element(ctx, graph_name, val[2:-4], "v").id
    elif isinstance(val, str) and re.match(r"^v\[.*\]\.sid$",
                                           val):  # parse vertex id as string
        return str(__find_cached_element(ctx, graph_name, val[2:-5], "v").id)
    elif isinstance(val, str) and re.match(r"^v\[.*\]$", val):  # parse vertex
        return __find_cached_element(ctx, graph_name, val[2:-1], "v")
    elif isinstance(val, str) and re.match(r"^e\[.*\]\.id$",
                                           val):  # parse edge id
        return __find_cached_element(ctx, graph_name, val[2:-4], "e").id
    elif isinstance(val, str) and re.match(r"^e\[.*\]\.sid$",
                                           val):  # parse edge id as string
        return str(__find_cached_element(ctx, graph_name, val[2:-5], "e").id)
    elif isinstance(val, str) and re.match(r"^e\[.*\]$", val):  # parse edge
        return __find_cached_element(ctx, graph_name, val[2:-1], "e")
    elif isinstance(val, str) and re.match(r"^m\[.*\]$",
                                           val):  # parse json as a map
        return _convert(json.loads(val[2:-1]), ctx)
    elif isinstance(val, str) and re.match(r"^p\[.*\]$", val):  # parse path
        path_objects = list(
            map((lambda x: _convert(x, ctx)), val[2:-1].split(",")))
        return Path([set([])], path_objects)
    elif isinstance(val, str) and re.match(r"^c\[.*\]$",
                                           val):  # parse lambda/closure
        return lambda: (val[2:-1], "gremlin-groovy")
    elif isinstance(val, str) and re.match(r"^t\[.*\]$",
                                           val):  # parse instance of T enum
        return T[val[2:-1]]
    elif isinstance(val, str) and re.match(
            r"^D\[.*\]$", val):  # parse instance of Direction enum
        return Direction[val[2:-1]]
    elif isinstance(val, str) and re.match(r"^null$",
                                           val):  # parse null to None
        return None
    else:
        return val
Exemple #8
0
 def validate(self, val):
     if val is not None:
         try:
             if isinstance(val, long):
                 return long(val)
             return int(val)
         except (ValueError, TypeError) as e:
             raise exception.ValidationError(
                 'Not a valid integer: {}'.format(val)) from e
 def get_tweet_info(self):
     return {
         "tweet_id": long(self.get_tweet_id()),
         "text": self.tweet.text,
         "lang": self.tweet.lang,
         "timestamp_ms": self.tweet.timestamp_ms,
         "is_retweet": self.get_is_retweet(),
         "created_at": self.tweet.created_at
     }
Exemple #10
0
 def _objectify(self, dict):
     type = dict[_SymbolHelper._TYPE]
     value = dict[_SymbolHelper._VALUE]
     if type == "g:Int32":
         return int(value)
     elif type == "g:Int64":
         return long(value)
     else:
         return float(value)
Exemple #11
0
    def test_traversals(self, remote_connection):
        statics.load_statics(globals())
        g = traversal().withRemote(remote_connection)

        assert long(6) == g.V().count().toList()[0]
        # #
        assert Vertex(1) == g.V(1).next()
        assert 1 == g.V(1).id().next()
        assert Traverser(Vertex(1)) == g.V(1).nextTraverser()
        assert 1 == len(g.V(1).toList())
        assert isinstance(g.V(1).toList(), list)
        results = g.V().repeat(out()).times(2).name
        results = results.toList()
        assert 2 == len(results)
        assert "lop" in results
        assert "ripple" in results
        # #
        assert 10 == g.V().repeat(both()).times(5)[0:10].count().next()
        assert 1 == g.V().repeat(both()).times(5)[0:1].count().next()
        assert 0 == g.V().repeat(both()).times(5)[0:0].count().next()
        assert 4 == g.V()[2:].count().next()
        assert 2 == g.V()[:2].count().next()
        # #
        results = g.withSideEffect('a', ['josh', 'peter']).V(1).out('created').in_('created').values('name').where(P.within('a')).toList()
        assert 2 == len(results)
        assert 'josh' in results
        assert 'peter' in results
        # #
        results = g.V().out().profile().toList()
        assert 1 == len(results)
        assert 'metrics' in results[0]
        assert 'dur' in results[0]
        # #
        results = g.V().has('name', 'peter').as_('a').out('created').as_('b').select('a', 'b').by(
            __.valueMap()).toList()
        assert 1 == len(results)
        assert 'peter' == results[0]['a']['name'][0]
        assert 35 == results[0]['a']['age'][0]
        assert 'lop' == results[0]['b']['name'][0]
        assert 'java' == results[0]['b']['lang'][0]
        assert 2 == len(results[0]['a'])
        assert 2 == len(results[0]['b'])
        # #
        results = g.V(1).inject(g.V(2).next()).values('name').toList()
        assert 2 == len(results)
        assert 'marko' in results
        assert 'vadas' in results
        # #
        results = g.V().has('person', 'name', 'marko').map(lambda: ("it.get().value('name')", "gremlin-groovy")).toList()
        assert 1 == len(results)
        assert 'marko' in results
        # #
        # this test just validates that the underscored versions of steps conflicting with Gremlin work
        # properly and can be removed when the old steps are removed - TINKERPOP-2272
        results = g.V().filter_(__.values('age').sum_().and_(
            __.max_().is_(gt(0)), __.min_().is_(gt(0)))).range_(0, 1).id_().next()
        assert 1 == results
 def _objectify(self, dict):
     type = dict[_SymbolHelper._TYPE]
     value = dict[_SymbolHelper._VALUE]
     if type == "g:Int32":
         return int(value)
     elif type == "g:Int64":
         return long(value)
     else:
         return float(value)
Exemple #13
0
 def test_graph_objects(self):
     vertex = Vertex(1)
     assert "v[1]" == str(vertex)
     assert "vertex" == vertex.label
     assert "person" == Vertex(1, "person").label
     assert vertex == Vertex(1)
     #
     edge = Edge(2, Vertex(1), "said", Vertex("hello", "phrase"))
     assert "e[2][1-said->hello]" == str(edge)
     assert Vertex(1) == edge.outV
     assert Vertex("hello") == edge.inV
     assert "said" == edge.label
     assert "phrase" == edge.inV.label
     assert edge.inV != edge.outV
     #
     vertex_property = VertexProperty(long(24), "name", "marko", Vertex(1))
     assert "vp[name->marko]" == str(vertex_property)
     assert "name" == vertex_property.label
     assert "name" == vertex_property.key
     assert "marko" == vertex_property.value
     assert long(24) == vertex_property.id
     assert Vertex(1) == vertex_property.vertex
     assert isinstance(vertex_property.id, long)
     assert vertex_property == VertexProperty(long(24), "name", "marko",
                                              Vertex(1))
     #
     property = Property("age", 29, Vertex(1))
     assert "p[age->29]" == str(property)
     assert "age" == property.key
     assert 29 == property.value
     assert Vertex(1) == property.element
     assert isinstance(property.value, int)
     assert property == Property("age", 29, Vertex(1))
     if not six.PY3:
         assert property != Property("age", long(29), Vertex(1))
     #
     for i in [vertex, edge, vertex_property, property]:
         for j in [vertex, edge, vertex_property, property]:
             if type(i) != type(j):
                 assert i != j
             else:
                 assert i == j
                 assert i.__hash__() == hash(i)
Exemple #14
0
 def test_graph_objects(self):
     vertex = Vertex(1)
     assert "v[1]" == str(vertex)
     assert "vertex" == vertex.label
     assert "person" == Vertex(1, "person").label
     assert vertex == Vertex(1)
     #
     edge = Edge(2, Vertex(1), "said", Vertex("hello", "phrase"))
     assert "e[2][1-said->hello]" == str(edge)
     assert Vertex(1) == edge.outV
     assert Vertex("hello") == edge.inV
     assert "said" == edge.label
     assert "phrase" == edge.inV.label
     assert edge.inV != edge.outV
     #
     vertex_property = VertexProperty(long(24), "name", "marko", Vertex(1))
     assert "vp[name->marko]" == str(vertex_property)
     assert "name" == vertex_property.label
     assert "name" == vertex_property.key
     assert "marko" == vertex_property.value
     assert long(24) == vertex_property.id
     assert Vertex(1) == vertex_property.vertex
     assert isinstance(vertex_property.id, long)
     assert vertex_property == VertexProperty(long(24), "name", "marko", Vertex(1))
     #
     property = Property("age", 29, Vertex(1))
     assert "p[age->29]" == str(property)
     assert "age" == property.key
     assert 29 == property.value
     assert Vertex(1) == property.element
     assert isinstance(property.value, int)
     assert property == Property("age", 29, Vertex(1))
     if not six.PY3:
         assert property != Property("age", long(29), Vertex(1))
     #
     for i in [vertex, edge, vertex_property, property]:
         for j in [vertex, edge, vertex_property, property]:
             if type(i) != type(j):
                 assert i != j
             else:
                 assert i == j
                 assert i.__hash__() == hash(i)
    def dictify(cls, n, _):
        if six.PY3 and type(n) in six.integer_types and n > MAX_INT32:
            n = long(n)

        if isinstance(
                n, bool
        ):  # because isinstance(False, int) and isinstance(True, int)
            return n
        elif isinstance(n, long):
            return GraphSONUtil.typedValue('Int64', n)
        else:
            return GraphSONUtil.typedValue('Int32', n)
Exemple #16
0
 def test_numbers(self):
     assert {
         "@type": "g:Int64",
         "@value": 2
     } == json.loads(GraphSONWriter.writeObject(long(2)))
     assert {
         "@type": "g:Int32",
         "@value": 1
     } == json.loads(GraphSONWriter.writeObject(1))
     assert {
         "@type": "g:Float",
         "@value": 3.2
     } == json.loads(GraphSONWriter.writeObject(3.2))
     assert """true""" == GraphSONWriter.writeObject(True)
    def test_traversals(self, remote_connection):
        statics.load_statics(globals())
        assert "remoteconnection[ws://localhost:45940/gremlin,gmodern]" == str(remote_connection)
        g = traversal().withRemote(remote_connection)

        assert long(6) == g.V().count().toList()[0]
        # #
        assert Vertex(1) == g.V(1).next()
        assert 1 == g.V(1).id().next()
        assert Traverser(Vertex(1)) == g.V(1).nextTraverser()
        assert 1 == len(g.V(1).toList())
        assert isinstance(g.V(1).toList(), list)
        results = g.V().repeat(out()).times(2).name
        results = results.toList()
        assert 2 == len(results)
        assert "lop" in results
        assert "ripple" in results
        # #
        assert 10 == g.V().repeat(both()).times(5)[0:10].count().next()
        assert 1 == g.V().repeat(both()).times(5)[0:1].count().next()
        assert 0 == g.V().repeat(both()).times(5)[0:0].count().next()
        assert 4 == g.V()[2:].count().next()
        assert 2 == g.V()[:2].count().next()
        # #
        results = 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
        # #
        results = g.V().out().profile().toList()
        assert 1 == len(results)
        assert 'metrics' in results[0]
        assert 'dur' in results[0]
        # #
        results = g.V().has('name', 'peter').as_('a').out('created').as_('b').select('a', 'b').by(
            __.valueMap()).toList()
        assert 1 == len(results)
        assert 'peter' == results[0]['a']['name'][0]
        assert 35 == results[0]['a']['age'][0]
        assert 'lop' == results[0]['b']['name'][0]
        assert 'java' == results[0]['b']['lang'][0]
        assert 2 == len(results[0]['a'])
        assert 2 == len(results[0]['b'])
        # #
        results = g.V(1).inject(g.V(2).next()).values('name').toList()
        assert 2 == len(results)
        assert 'marko' in results
        assert 'vadas' in results
Exemple #18
0
def test_set_card_64bit_integer(place):
    place.important_numbers = set([long(1), long(2), long(3)])
    assert all(isinstance(i.value, long) for i in place.important_numbers)
Exemple #19
0
 def test_long(self):
     x = long(100)
     output = self.graphbinary_reader.readObject(
         self.graphbinary_writer.writeObject(x))
     assert x == output
    def test_traversals(self, remote_connection):
        statics.load_statics(globals())
        g = traversal().withRemote(remote_connection)

        assert long(6) == g.V().count().toList()[0]
        # #
        assert Vertex(1) == g.V(1).next()
        assert 1 == g.V(1).id().next()
        assert Traverser(Vertex(1)) == g.V(1).nextTraverser()
        assert 1 == len(g.V(1).toList())
        assert isinstance(g.V(1).toList(), list)
        results = g.V().repeat(out()).times(2).name
        results = results.toList()
        assert 2 == len(results)
        assert "lop" in results
        assert "ripple" in results
        # #
        assert 10 == g.V().repeat(both()).times(5)[0:10].count().next()
        assert 1 == g.V().repeat(both()).times(5)[0:1].count().next()
        assert 0 == g.V().repeat(both()).times(5)[0:0].count().next()
        assert 4 == g.V()[2:].count().next()
        assert 2 == g.V()[:2].count().next()
        # #
        results = g.withSideEffect(
            'a', ['josh', 'peter'
                  ]).V(1).out('created').in_('created').values('name').where(
                      P.within('a')).toList()
        assert 2 == len(results)
        assert 'josh' in results
        assert 'peter' in results
        # #
        results = g.V().out().profile().toList()
        assert 1 == len(results)
        assert 'metrics' in results[0]
        assert 'dur' in results[0]
        # #
        results = g.V().has('name',
                            'peter').as_('a').out('created').as_('b').select(
                                'a', 'b').by(__.valueMap()).toList()
        assert 1 == len(results)
        assert 'peter' == results[0]['a']['name'][0]
        assert 35 == results[0]['a']['age'][0]
        assert 'lop' == results[0]['b']['name'][0]
        assert 'java' == results[0]['b']['lang'][0]
        assert 2 == len(results[0]['a'])
        assert 2 == len(results[0]['b'])
        # #
        results = g.V(1).inject(g.V(2).next()).values('name').toList()
        assert 2 == len(results)
        assert 'marko' in results
        assert 'vadas' in results
        # #
        results = g.V().has('person', 'name', 'marko').map(
            lambda: ("it.get().value('name')", "gremlin-groovy")).toList()
        assert 1 == len(results)
        assert 'marko' in results
        # #
        # this test just validates that the underscored versions of steps conflicting with Gremlin work
        # properly and can be removed when the old steps are removed - TINKERPOP-2272
        results = g.V().filter_(
            __.values('age').sum_().and_(__.max_().is_(gt(0)),
                                         __.min_().is_(gt(0)))).range_(
                                             0, 1).id_().next()
        assert 1 == results
        # #
        # test binding in P
        results = g.V().has('person', 'age',
                            Bindings.of('x', lt(30))).count().next()
        assert 2 == results
        # #
        # test dict keys which can only work on GraphBinary and GraphSON3 which include specific serialization
        # types for dict
        if not isinstance(remote_connection._client._message_serializer,
                          GraphSONSerializersV2d0):
            results = g.V().has(
                'person', 'name',
                'marko').elementMap("name").groupCount().next()
            assert {
                HashableDict.of({
                    T.id: 1,
                    T.label: 'person',
                    'name': 'marko'
                }): 1
            } == results
    def test_authenticated(self, remote_connection_authenticated):
        statics.load_statics(globals())
        g = traversal().withRemote(remote_connection_authenticated)

        assert long(6) == g.V().count().toList()[0]
Exemple #22
0
 def to_db(self, val):
     # Make sure to serialize as long
     if val is not None:
         return long(val)
Exemple #23
0
def default_id_serializer(val):
    if isinstance(val, int):
        val = long(val)
    return val
 def objectify(cls, v, _):
     if six.PY3:
         return v
     return long(v)
Exemple #25
0
def default_id_serializer(val):
    if isinstance(val, int):
        val = long(val)
    return val