def test_custom_mapping(self): # extended mapping class X(object): pass serdes = Mock() writer = GraphSONWriter(serializer_map={X: serdes}) assert X in writer.serializers # base dicts are not modified assert X not in gremlin_python.structure.io.graphson._serializers obj = X() d = writer.toDict(obj) serdes.dictify.assert_called_once_with(obj, writer) assert d is serdes.dictify() # overridden mapping serdes = Mock() writer = GraphSONWriter(serializer_map={int: serdes}) assert gremlin_python.structure.io.graphson._serializers[int] is not writer.serializers[int] value = 3 d = writer.toDict(value) serdes.dictify.assert_called_once_with(value, writer) assert d is serdes.dictify()
def test_strategies(self): statics.load_statics(globals()) connection = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g') g = Graph().traversal().withRemote(connection). \ withStrategies(TraversalStrategy("SubgraphStrategy", {"vertices": __.hasLabel("person"), "edges": __.hasLabel("created")})) print GraphSONWriter.writeObject(g.bytecode) assert 4 == g.V().count().next() assert 0 == g.E().count().next() assert 1 == g.V().label().dedup().count().next() assert "person" == g.V().label().dedup().next() # g = Graph().traversal().withRemote(connection). \ withStrategies(SubgraphStrategy(vertices=__.hasLabel("person"), edges=__.hasLabel("created"))) print GraphSONWriter.writeObject(g.bytecode) assert 4 == g.V().count().next() assert 0 == g.E().count().next() assert 1 == g.V().label().dedup().count().next() assert "person" == g.V().label().dedup().next() # g = g.withoutStrategies(SubgraphStrategy). \ withComputer(workers=4, vertices=__.has("name", "marko"), edges=__.limit(0)) assert 1 == g.V().count().next() assert 0 == g.E().count().next() assert "person" == g.V().label().next() assert "marko" == g.V().name.next() connection.close()
def test_numbers(self): assert """{"@type":"g:Int32","@value":1}""" == GraphSONWriter.writeObject( 1) assert """{"@type":"g:Int64","@value":2}""" == GraphSONWriter.writeObject( 2L) assert """{"@type":"g:Float","@value":3.2}""" == GraphSONWriter.writeObject( 3.2) assert """true""" == GraphSONWriter.writeObject(True)
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 bytecode(self, args): gremlin = args['gremlin'] args['gremlin'] = GraphSONWriter.writeObject(gremlin) aliases = args.get('aliases', '') if not aliases: aliases = {'g': 'g'} args['aliases'] = aliases return args
def __init__(self, url, traversal_source, username="", password="", loop=None, graphson_reader=None, graphson_writer=None): super(DriverRemoteConnection, self).__init__(url, traversal_source) self._url = url self._username = username self._password = password if loop is None: self._loop = ioloop.IOLoop.current() self._websocket = self._loop.run_sync(lambda: websocket.websocket_connect(self.url)) self._graphson_reader = graphson_reader or GraphSONReader() self._graphson_writer = graphson_writer or GraphSONWriter()
def test_strategies(self): # we have a proxy model for now given that we don't want to have to have g:XXX all registered on the Gremlin traversal machine (yet) assert { "@type": "g:SubgraphStrategy", "@value": {} } == json.loads(GraphSONWriter.writeObject(SubgraphStrategy)) assert { "@type": "g:SubgraphStrategy", "@value": { "vertices": { "@type": "g:Bytecode", "@value": { "step": [["has", "name", "marko"]] } } } } == json.loads( GraphSONWriter.writeObject( SubgraphStrategy(vertices=__.has("name", "marko"))))
def test_custom_mapping(self): # extended mapping class X(object): pass serdes = Mock() writer = GraphSONWriter(serializer_map={X: serdes}) assert X in writer.serializers # base dicts are not modified assert X not in gremlin_python.structure.io.graphson._serializers obj = X() d = writer.toDict(obj) serdes.dictify.assert_called_once_with(obj, writer) assert d is serdes.dictify() # overridden mapping serdes = Mock() writer = GraphSONWriter(serializer_map={int: serdes}) assert gremlin_python.structure.io.graphson._serializers[ int] is not writer.serializers[int] value = 3 d = writer.toDict(value) serdes.dictify.assert_called_once_with(value, writer) assert d is serdes.dictify()
def submit_traversal_bytecode(self, request_id, bytecode): message = { "requestId": { "@type": "g:UUID", "@value": request_id }, "op": "bytecode", "processor": "traversal", "args": { "gremlin": GraphSONWriter.writeObject(bytecode), "aliases": {"g": self.traversal_source} } } traversers = yield self._execute_message(message) raise gen.Return(traversers)
def submit_traversal_bytecode(self, request_id, bytecode): message = { "requestId": { "@type": "g:UUID", "@value": request_id }, "op": "bytecode", "processor": "traversal", "args": { "gremlin": GraphSONWriter.writeObject(bytecode), "aliases": { "g": self.traversal_source } } } traversers = yield self._execute_message(message) raise gen.Return(traversers)
from dse_graph.serializers import serializers, deserializers, dse_deserializers from dse_graph._version import __version__, __version_info__ class NullHandler(logging.Handler): def emit(self, record): pass logging.getLogger('dse_graph').addHandler(NullHandler()) log = logging.getLogger(__name__) # Create our custom GraphSONReader/Writer dse_graphson_reader = GraphSONReader(deserializer_map=dse_deserializers) graphson_reader = GraphSONReader(deserializer_map=deserializers) graphson_writer = GraphSONWriter(serializer_map=serializers) def graph_traversal_row_factory(column_names, rows): """ Row Factory that returns the decoded graphson. """ return [graphson_reader.readObject(row[0])['result'] for row in rows] def graph_traversal_dse_object_row_factory(column_names, rows): """ Row Factory that returns the decoded graphson as DSE types. """ return [dse_graphson_reader.readObject(row[0])['result'] for row in rows]
class TestGraphSONWriter(object): graphson_writer = GraphSONWriter() def test_number_output(self): assert { "@type": "g:Int64", "@value": 2 } == json.loads(self.graphson_writer.writeObject(long(2))) assert { "@type": "g:Int32", "@value": 1 } == json.loads(self.graphson_writer.writeObject(1)) assert { "@type": "g:Double", "@value": 3.2 } == json.loads(self.graphson_writer.writeObject(3.2)) assert """true""" == self.graphson_writer.writeObject(True) def test_numbers(self): assert { "@type": "g:Int64", "@value": 2 } == json.loads(self.graphson_writer.writeObject(long(2))) assert { "@type": "g:Int32", "@value": 1 } == json.loads(self.graphson_writer.writeObject(1)) assert { "@type": "g:Double", "@value": 3.2 } == json.loads(self.graphson_writer.writeObject(3.2)) assert """true""" == self.graphson_writer.writeObject(True) def test_P(self): result = { '@type': 'g:P', '@value': { 'predicate': 'and', 'value': [{ '@type': 'g:P', '@value': { 'predicate': 'or', 'value': [{ '@type': 'g:P', '@value': { 'predicate': 'lt', 'value': 'b' } }, { '@type': 'g:P', '@value': { 'predicate': 'gt', 'value': 'c' } }] } }, { '@type': 'g:P', '@value': { 'predicate': 'neq', 'value': 'd' } }] } } assert result == json.loads( self.graphson_writer.writeObject( P.lt("b").or_(P.gt("c")).and_(P.neq("d")))) def test_strategies(self): # we have a proxy model for now given that we don't want to have to have g:XXX all registered on the Gremlin traversal machine (yet) assert { "@type": "g:SubgraphStrategy", "@value": {} } == json.loads(self.graphson_writer.writeObject(SubgraphStrategy)) assert { "@type": "g:SubgraphStrategy", "@value": { "vertices": { "@type": "g:Bytecode", "@value": { "step": [["has", "name", "marko"]] } } } } == json.loads( self.graphson_writer.writeObject( SubgraphStrategy(vertices=__.has("name", "marko")))) def test_graph(self): assert { "@type": "g:Vertex", "@value": { "id": { "@type": "g:Int64", "@value": 12 }, "label": "person" } } == json.loads(self.graphson_writer.writeObject(Vertex(12l, "person"))) assert { "@type": "g:Edge", "@value": { "id": { "@type": "g:Int32", "@value": 7 }, "outV": { "@type": "g:Int32", "@value": 0 }, "outVLabel": "person", "label": "knows", "inV": { "@type": "g:Int32", "@value": 1 }, "inVLabel": "dog" } } == json.loads( self.graphson_writer.writeObject( Edge(7, Vertex(0, "person"), "knows", Vertex(1, "dog")))) assert { "@type": "g:VertexProperty", "@value": { "id": "blah", "label": "keyA", "value": True, "vertex": "stephen" } } == json.loads( self.graphson_writer.writeObject( VertexProperty("blah", "keyA", True, Vertex("stephen")))) assert { "@type": "g:Property", "@value": { "key": "name", "value": "marko", "element": { "@type": "g:VertexProperty", "@value": { "vertex": "vertexId", "id": { "@type": "g:Int32", "@value": 1234 }, "label": "aKey" } } } } == json.loads( self.graphson_writer.writeObject( Property( "name", "marko", VertexProperty(1234, "aKey", 21345, Vertex("vertexId"))))) def test_custom_mapping(self): # extended mapping class X(object): pass serdes = Mock() writer = GraphSONWriter(serializer_map={X: serdes}) assert X in writer.serializers # base dicts are not modified assert X not in gremlin_python.structure.io.graphson._serializers obj = X() d = writer.toDict(obj) serdes.dictify.assert_called_once_with(obj, writer) assert d is serdes.dictify() # overridden mapping serdes = Mock() writer = GraphSONWriter(serializer_map={int: serdes}) assert gremlin_python.structure.io.graphson._serializers[ int] is not writer.serializers[int] value = 3 d = writer.toDict(value) serdes.dictify.assert_called_once_with(value, writer) assert d is serdes.dictify() def test_write_long(self): mapping = self.graphson_writer.toDict(1) assert mapping['@type'] == 'g:Int32' assert mapping['@value'] == 1 mapping = self.graphson_writer.toDict(long(1)) assert mapping['@type'] == 'g:Int64' assert mapping['@value'] == 1
def test_P(self): assert """{"@type":"g:P","@value":{"predicate":"and","value":[{"@type":"g:P","@value":{"predicate":"or","value":[{"@type":"g:P","@value":{"predicate":"lt","value":"b"}},{"@type":"g:P","@value":{"predicate":"gt","value":"c"}}]}},{"@type":"g:P","@value":{"predicate":"neq","value":"d"}}]}}""" == GraphSONWriter.writeObject( P.lt("b").or_(P.gt("c")).and_(P.neq("d")))
class TestGraphSONWriter(TestCase): graphson_writer = GraphSONWriter() def test_number_output(self): assert { "@type": "g:Int64", "@value": 2 } == json.loads(self.graphson_writer.writeObject(long(2))) assert { "@type": "g:Int32", "@value": 1 } == json.loads(self.graphson_writer.writeObject(1)) assert { "@type": "g:Double", "@value": 3.2 } == json.loads(self.graphson_writer.writeObject(3.2)) assert """true""" == self.graphson_writer.writeObject(True) def test_numbers(self): assert { "@type": "g:Int64", "@value": 2 } == json.loads(self.graphson_writer.writeObject(long(2))) assert { "@type": "g:Int32", "@value": 1 } == json.loads(self.graphson_writer.writeObject(1)) assert { "@type": "g:Double", "@value": 3.2 } == json.loads(self.graphson_writer.writeObject(3.2)) assert """true""" == self.graphson_writer.writeObject(True) def test_P(self): assert """{"@type":"g:P","@value":{"predicate":"and","value":[{"@type":"g:P","@value":{"predicate":"or","value":[{"@type":"g:P","@value":{"predicate":"lt","value":"b"}},{"@type":"g:P","@value":{"predicate":"gt","value":"c"}}]}},{"@type":"g:P","@value":{"predicate":"neq","value":"d"}}]}}""" == self.graphson_writer.writeObject( P.lt("b").or_(P.gt("c")).and_(P.neq("d"))) def test_strategies(self): # we have a proxy model for now given that we don't want to have to have g:XXX all registered on the Gremlin traversal machine (yet) assert { "@type": "g:SubgraphStrategy", "@value": {} } == json.loads(self.graphson_writer.writeObject(SubgraphStrategy)) assert { "@type": "g:SubgraphStrategy", "@value": { "vertices": { "@type": "g:Bytecode", "@value": { "step": [["has", "name", "marko"]] } } } } == json.loads( self.graphson_writer.writeObject( SubgraphStrategy(vertices=__.has("name", "marko")))) def test_custom_mapping(self): # extended mapping class X(object): pass serdes = Mock() writer = GraphSONWriter(serializer_map={X: serdes}) assert X in writer.serializers # base dicts are not modified assert X not in gremlin_python.structure.io.graphson._serializers obj = X() d = writer.toDict(obj) serdes.dictify.assert_called_once_with(obj, writer) assert d is serdes.dictify() # overridden mapping serdes = Mock() writer = GraphSONWriter(serializer_map={int: serdes}) assert gremlin_python.structure.io.graphson._serializers[ int] is not writer.serializers[int] value = 3 d = writer.toDict(value) serdes.dictify.assert_called_once_with(value, writer) assert d is serdes.dictify()