Ejemplo n.º 1
0
    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.graphsonV3d0._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.graphsonV3d0._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()
Ejemplo n.º 2
0
    def build(self):
        """ The method registers JanusGraph specific serializers into Gremlin GraphSON Writer class.

        Returns:
            GraphSONWriter
        """
        self.__register_default_serializers()
        self.writer = GraphSONWriter(self.serializers)
        return self.writer
Ejemplo n.º 3
0
class TestGraphSONWriter(object):
    graphson_writer = GraphSONWriter()
    graphson_reader = GraphSONReader()

    def test_collections(self):
        assert {
            "@type":
            "g:List",
            "@value": [{
                "@type": "g:Int32",
                "@value": 1
            }, {
                "@type": "g:Int32",
                "@value": 2
            }, {
                "@type": "g:Int32",
                "@value": 3
            }]
        } == json.loads(self.graphson_writer.writeObject([1, 2, 3]))
        assert {
            "@type":
            "g:Set",
            "@value": [{
                "@type": "g:Int32",
                "@value": 1
            }, {
                "@type": "g:Int32",
                "@value": 2
            }, {
                "@type": "g:Int32",
                "@value": 3
            }]
        } == json.loads(self.graphson_writer.writeObject(set([1, 2, 3, 3])))
        assert {
            "@type": "g:Map",
            "@value": ['a', {
                "@type": "g:Int32",
                "@value": 1
            }]
        } == json.loads(self.graphson_writer.writeObject({'a': 1}))

    def test_numbers(self):
        assert {
            "@type": "gx:Byte",
            "@value": 1
        } == json.loads(
            self.graphson_writer.writeObject(int.__new__(SingleByte, 1)))
        assert {
            "@type": "g:Int64",
            "@value": 2
        } == json.loads(self.graphson_writer.writeObject(long(2)))
        assert {
            "@type": "g:Int64",
            "@value": 851401972585122
        } == json.loads(self.graphson_writer.writeObject(
            long(851401972585122)))
        assert {
            "@type": "g:Int64",
            "@value": -2
        } == json.loads(self.graphson_writer.writeObject(long(-2)))
        assert {
            "@type": "g:Int64",
            "@value": -851401972585122
        } == json.loads(
            self.graphson_writer.writeObject(long(-851401972585122)))
        assert {
            "@type": "g:Int32",
            "@value": 1
        } == json.loads(self.graphson_writer.writeObject(1))
        assert {
            "@type": "g:Int32",
            "@value": -1
        } == json.loads(self.graphson_writer.writeObject(-1))
        assert {
            "@type": "g:Int64",
            "@value": 851401972585122
        } == json.loads(self.graphson_writer.writeObject(851401972585122))
        assert {
            "@type": "g:Double",
            "@value": 3.2
        } == json.loads(self.graphson_writer.writeObject(3.2))
        assert {
            "@type": "g:Double",
            "@value": "NaN"
        } == json.loads(self.graphson_writer.writeObject(float('nan')))
        assert {
            "@type": "g:Double",
            "@value": "Infinity"
        } == json.loads(self.graphson_writer.writeObject(float('inf')))
        assert {
            "@type": "g:Double",
            "@value": "-Infinity"
        } == json.loads(self.graphson_writer.writeObject(float('-inf')))
        assert {
            "@type": "gx:BigDecimal",
            "@value": "123456789987654321123456789987654321"
        } == json.loads(
            self.graphson_writer.writeObject(
                Decimal('123456789987654321123456789987654321')))
        assert {
            "@type": "gx:BigDecimal",
            "@value": "NaN"
        } == json.loads(self.graphson_writer.writeObject(Decimal('nan')))
        assert {
            "@type": "gx:BigDecimal",
            "@value": "Infinity"
        } == json.loads(self.graphson_writer.writeObject(Decimal('inf')))
        assert {
            "@type": "gx:BigDecimal",
            "@value": "-Infinity"
        } == json.loads(self.graphson_writer.writeObject(Decimal('-inf')))
        assert {
            "@type": "gx:BigInteger",
            "@value": "123456789987654321123456789987654321"
        } == json.loads(
            self.graphson_writer.writeObject(
                long(123456789987654321123456789987654321)))
        assert {
            "@type": "gx:BigInteger",
            "@value": "123456789987654321123456789987654321"
        } == json.loads(
            self.graphson_writer.writeObject(
                123456789987654321123456789987654321))
        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"))))

        result = {
            '@type': 'g:P',
            '@value': {
                'predicate': 'within',
                'value': {
                    '@type':
                    'g:List',
                    '@value': [{
                        "@type": "g:Int32",
                        "@value": 1
                    }, {
                        "@type": "g:Int32",
                        "@value": 2
                    }]
                }
            }
        }
        assert result == json.loads(
            self.graphson_writer.writeObject(P.within([1, 2])))
        assert result == json.loads(
            self.graphson_writer.writeObject(P.within(1, 2)))

        result = {
            '@type': 'g:P',
            '@value': {
                'predicate': 'within',
                'value': {
                    '@type': 'g:List',
                    '@value': [{
                        "@type": "g:Int32",
                        "@value": 1
                    }]
                }
            }
        }
        assert result == json.loads(
            self.graphson_writer.writeObject(P.within([1])))
        assert result == json.loads(
            self.graphson_writer.writeObject(P.within(1)))

    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):
        # TODO: this assert is not compatible with python 3 and now that we test with both 2 and 3 it fails
        assert {
            "@type": "g:Vertex",
            "@value": {
                "id": {
                    "@type": "g:Int64",
                    "@value": 12
                },
                "label": "person"
            }
        } == json.loads(
            self.graphson_writer.writeObject(Vertex(long(12), "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")))))

        vertex = self.graphson_reader.readObject(
            self.graphson_writer.writeObject(Vertex(1, "person")))
        assert 1 == vertex.id
        assert "person" == vertex.label

        edge = self.graphson_reader.readObject(
            self.graphson_writer.writeObject(
                Edge(3, Vertex(1, "person"), "knows", Vertex(2, "dog"))))
        assert "knows" == edge.label
        assert 3 == edge.id
        assert 1 == edge.outV.id
        assert 2 == edge.inV.id

        vertex_property = self.graphson_reader.readObject(
            self.graphson_writer.writeObject(
                VertexProperty(1, "age", 32, Vertex(1))))
        assert 1 == vertex_property.id
        assert "age" == vertex_property.key
        assert 32 == vertex_property.value

        property = self.graphson_reader.readObject(
            self.graphson_writer.writeObject(
                Property("age", 32.2, Edge(1, Vertex(2), "knows", Vertex(3)))))
        assert "age" == property.key
        assert 32.2 == property.value

    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.graphsonV3d0._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.graphsonV3d0._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_datetime(self):
        expected = json.dumps({
            "@type": "g:Date",
            "@value": 1481750076295
        },
                              separators=(',', ':'))
        dt = datetime.datetime.utcfromtimestamp(1481750076295 / 1000.0)
        output = self.graphson_writer.writeObject(dt)
        assert expected == output

    def test_timestamp(self):
        expected = json.dumps({
            "@type": "g:Timestamp",
            "@value": 1481750076295
        },
                              separators=(',', ':'))
        ts = timestamp(1481750076295 / 1000.0)
        output = self.graphson_writer.writeObject(ts)
        assert expected == output

    def test_duration(self):
        expected = json.dumps({
            "@type": "gx:Duration",
            "@value": "P5D"
        },
                              separators=(',', ':'))
        d = datetime.timedelta(hours=120)
        output = self.graphson_writer.writeObject(d)
        assert expected == output

    def test_uuid(self):
        expected = json.dumps(
            {
                '@type': 'g:UUID',
                '@value': "41d2e28a-20a4-4ab0-b379-d810dede3786"
            },
            separators=(',', ':'))
        prop = uuid.UUID("41d2e28a-20a4-4ab0-b379-d810dede3786")
        output = self.graphson_writer.writeObject(prop)
        assert expected == output

    def test_bytebuffer(self):
        expected = json.dumps(
            {
                '@type': 'gx:ByteBuffer',
                '@value': 'c29tZSBieXRlcyBmb3IgeW91'
            },
            separators=(',', ':'))
        bb = ByteBufferType("c29tZSBieXRlcyBmb3IgeW91", "utf8")
        output = self.graphson_writer.writeObject(bb)
        assert expected == output

    def test_char(self):
        expected = json.dumps({
            '@type': 'gx:Char',
            '@value': 'L'
        },
                              separators=(',', ':'))
        c = str.__new__(SingleChar, chr(76))
        output = self.graphson_writer.writeObject(c)
        assert expected == output
Ejemplo n.º 4
0
class TestGraphSONWriter(object):
    graphson_writer = GraphSONWriter()
    graphson_reader = GraphSONReader()

    def test_collections(self):
        assert {"@type": "g:List", "@value": [{"@type": "g:Int32", "@value": 1},
                                              {"@type": "g:Int32", "@value": 2},
                                              {"@type": "g:Int32", "@value": 3}]} == json.loads(
            self.graphson_writer.writeObject([1, 2, 3]))
        assert {"@type": "g:Set", "@value": [{"@type": "g:Int32", "@value": 1},
                                             {"@type": "g:Int32", "@value": 2},
                                             {"@type": "g:Int32", "@value": 3}]} == json.loads(
            self.graphson_writer.writeObject(set([1, 2, 3, 3])))
        assert {"@type": "g:Map",
                "@value": ['a', {"@type": "g:Int32", "@value": 1}]} == json.loads(
            self.graphson_writer.writeObject({'a': 1}))

    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"))))

        result = {'@type': 'g:P', '@value': {'predicate': 'within', 'value': {'@type': 'g:List', '@value': [
            {"@type": "g:Int32", "@value": 1}, {"@type": "g:Int32", "@value": 2}]}}}
        assert result == json.loads(self.graphson_writer.writeObject(P.within([1, 2])))

    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):
        # TODO: this assert is not compatible with python 3 and now that we test with both 2 and 3 it fails
        assert {"@type": "g:Vertex",
                "@value": {"id": {"@type": "g:Int64", "@value": 12}, "label": "person"}} == json.loads(
            self.graphson_writer.writeObject(Vertex(long(12), "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")))))

        vertex = self.graphson_reader.readObject(self.graphson_writer.writeObject(Vertex(1, "person")))
        assert 1 == vertex.id
        assert "person" == vertex.label

        edge = self.graphson_reader.readObject(
            self.graphson_writer.writeObject(Edge(3, Vertex(1, "person"), "knows", Vertex(2, "dog"))))
        assert "knows" == edge.label
        assert 3 == edge.id
        assert 1 == edge.outV.id
        assert 2 == edge.inV.id

        vertex_property = self.graphson_reader.readObject(
            self.graphson_writer.writeObject(VertexProperty(1, "age", 32, Vertex(1))))
        assert 1 == vertex_property.id
        assert "age" == vertex_property.key
        assert 32 == vertex_property.value

        property = self.graphson_reader.readObject(self.graphson_writer.writeObject(Property("age", 32.2, Edge(1,Vertex(2),"knows",Vertex(3)))))
        assert "age" == property.key
        assert 32.2 == property.value

    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.graphsonV3d0._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.graphsonV3d0._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
Ejemplo n.º 5
0
import collections
try:
    import ujson as json
except ImportError:
    import json

from gremlin_python.structure.io.graphsonV3d0 import GraphSONWriter
from goblin.element import Vertex, Edge, VertexProperty
from goblin.manager import ListVertexPropertyManager


writer = GraphSONWriter()


AdjList = collections.namedtuple("AdjList", "vertex inE outE")

vp_id = 10


def dump(fpath, *adj_lists, mode="w"):
    """Convert Goblin elements to GraphSON"""
    with open(fpath, mode) as f:
        for adj_list in adj_lists:
            dumped = dumps(adj_list)
            f.write(dumped + '\n')


def dumps(adj_list):
    """Convert Goblin elements to GraphSON"""
    vertex = _prep_vertex(adj_list.vertex)
    for inE in adj_list.inE:
Ejemplo n.º 6
0
 def get(self):
     writer = GraphSONWriter(self.serializers)
     return writer