test_driver_remote_connection.py 7.57 KB
Newer Older
davebshow's avatar
davebshow committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'''
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
'''
'''THIS FILE HAS BEEN MODIFIED BY DAVID M. BROWN TO SUPPORT PEP 492'''
import pytest

from aiogremlin.gremlin_python import statics
from aiogremlin.gremlin_python.statics import long
from aiogremlin.remote.driver_remote_connection import (
    DriverRemoteConnection)
from aiogremlin.gremlin_python.process.traversal import Traverser
from aiogremlin.gremlin_python.process.traversal import TraversalStrategy
from aiogremlin.gremlin_python.process.graph_traversal import __
from aiogremlin.gremlin_python.structure.graph import Graph
from aiogremlin.gremlin_python.structure.graph import Vertex
from aiogremlin.gremlin_python.process.strategies import SubgraphStrategy

__author__ = 'Marko A. Rodriguez (http://markorodriguez.com)'


class TestDriverRemoteConnection(object):

    @pytest.mark.asyncio
    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()

    @pytest.mark.asyncio
    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()

    @pytest.mark.asyncio
    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()

    @pytest.mark.asyncio
    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()