TestDriverRemoteConnection.test_traversals()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 33
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 33
rs 9.208
c 0
b 0
f 0
cc 1
nop 2
1
'''
2
Licensed to the Apache Software Foundation (ASF) under one
3
or more contributor license agreements.  See the NOTICE file
4
distributed with this work for additional information
5
regarding copyright ownership.  The ASF licenses this file
6
to you under the Apache License, Version 2.0 (the
7
"License"); you may not use this file except in compliance
8
with the License.  You may obtain a copy of the License at
9
10
http://www.apache.org/licenses/LICENSE-2.0
11
12
Unless required by applicable law or agreed to in writing,
13
software distributed under the License is distributed on an
14
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
KIND, either express or implied.  See the License for the
16
specific language governing permissions and limitations
17
under the License.
18
'''
19
'''THIS FILE HAS BEEN MODIFIED BY DAVID M. BROWN TO SUPPORT PEP 492'''
20
import pytest
21
22
from gremlin_python import statics
23
from gremlin_python.statics import long
24
from gremlin_python.process.traversal import Traverser
25
from gremlin_python.process.traversal import TraversalStrategy
26
from gremlin_python.process.graph_traversal import __
27
from aiogremlin.structure.graph import Graph
28
from gremlin_python.structure.graph import Vertex
29
from gremlin_python.process.strategies import SubgraphStrategy
30
31
__author__ = 'Marko A. Rodriguez (http://markorodriguez.com)'
32
33
34
class TestDriverRemoteConnection(object):
35
36
    @pytest.mark.asyncio
37
    async def test_label(self, remote_connection):
38
        statics.load_statics(globals())
39
        g = Graph().traversal().withRemote(remote_connection)
40
        result = await g.V().limit(1).toList()
41
        await remote_connection.close()
42
43
    @pytest.mark.asyncio
44
    async def test_traversals(self, remote_connection):
45
        statics.load_statics(globals())
46
        g = Graph().traversal().withRemote(remote_connection)
47
        result = await g.V().count().toList()
48
        assert long(6) == result[0]
49
        # #
50
        assert Vertex(1) == await g.V(1).next()
51
        assert 1 == await g.V(1).id().next()
52
        assert Traverser(Vertex(1)) == await g.V(1).nextTraverser()
53
        result = await g.V(1).toList()
54
        assert 1 == len(result)
55
        result = await g.V(1).toList()
56
        assert isinstance(result, list)
57
        results = g.V().repeat(out()).times(2).name
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable out does not seem to be defined.
Loading history...
58
        results = await results.toList()
59
        assert 2 == len(results)
60
        assert "lop" in results
61
        assert "ripple" in results
62
        # # #
63
        assert 10 == await g.V().repeat(both()).times(5)[0:10].count().next()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable both does not seem to be defined.
Loading history...
64
        assert 1 == await g.V().repeat(both()).times(5)[0:1].count().next()
65
        assert 0 == await g.V().repeat(both()).times(5)[0:0].count().next()
66
        assert 4 == await g.V()[2:].count().next()
67
        assert 2 == await g.V()[:2].count().next()
68
        # # #
69
        results = await g.withSideEffect('a',['josh','peter']).V(1).out('created').in_('created').values('name').where(within('a')).toList()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable within does not seem to be defined.
Loading history...
70
        assert 2 == len(results)
71
        assert 'josh' in results
72
        assert 'peter' in results
73
        # # # todo: need a traversal metrics deserializer
74
        # g.V().out().profile().next()
75
        await remote_connection.close()
76
77
    @pytest.mark.asyncio
78
    async def test_strategies(self, remote_connection):
79
        statics.load_statics(globals())
80
        #
81
        g = Graph().traversal().withRemote(remote_connection). \
82
            withStrategies(TraversalStrategy("SubgraphStrategy",
83
                                             {"vertices": __.hasLabel("person"),
84
                                              "edges": __.hasLabel("created")}))
85
        assert 4 == await g.V().count().next()
86
        assert 0 == await g.E().count().next()
87
        assert 1 == await g.V().label().dedup().count().next()
88
        assert "person" == await g.V().label().dedup().next()
89
        #
90
        g = Graph().traversal().withRemote(remote_connection). \
91
            withStrategies(SubgraphStrategy(vertices=__.hasLabel("person"), edges=__.hasLabel("created")))
92
        assert 4 == await g.V().count().next()
93
        assert 0 == await g.E().count().next()
94
        assert 1 == await g.V().label().dedup().count().next()
95
        assert "person" == await g.V().label().dedup().next()
96
        #
97
        g = g.withoutStrategies(SubgraphStrategy). \
98
            withComputer(vertices=__.has("name", "marko"), edges=__.limit(0))
99
        assert 1 == await g.V().count().next()
100
        assert 0 == await g.E().count().next()
101
        assert "person" == await g.V().label().next()
102
        assert "marko" == await g.V().name.next()
103
        #
104
        g = Graph().traversal().withRemote(remote_connection).withComputer()
105
        assert 6 == await g.V().count().next()
106
        assert 6 == await g.E().count().next()
107
        await remote_connection.close()
108
109
    @pytest.mark.asyncio
110
    async def test_side_effects(self, remote_connection):
111
        statics.load_statics(globals())
112
        g = Graph().traversal().withRemote(remote_connection)
113
        t = await g.V().hasLabel("project").name.iterate()
114
        keys = await t.side_effects.keys()
115
        assert 0 == len(keys)
116
        with pytest.raises(Exception):
117
            m = await t.side_effects["m"]
118
        t = g.V().out("created").groupCount("m").by("name")
119
        results = await t.toSet()
120
        assert 2 == len(results)
121
        assert Vertex(3) in results
122
        assert Vertex(5) in results
123
        keys = await t.side_effects.keys()
124
        assert 1 == len(keys)
125
        assert "m" in keys
126
        m = await t.side_effects["m"]
127
        assert isinstance(m, dict)
128
        assert 2 == len(m)
129
        assert 3 == m["lop"]
130
        assert 1 == m["ripple"]
131
        assert isinstance(m["lop"], long)
132
        assert isinstance(m["ripple"], long)
133
        # ##
134
        t = g.V().out("created").groupCount("m").by("name").name.aggregate("n")
135
        results = await t.toSet()
136
        assert 2 == len(results)
137
        assert "lop" in results
138
        assert "ripple" in results
139
        keys = await t.side_effects.keys()
140
        assert 2 == len(keys)
141
        assert "m" in keys
142
        assert "n" in keys
143
        n = await t.side_effects.get("n")
144
        assert isinstance(n, dict)
145
        assert 2 == len(n)
146
        assert "lop" in n.keys()
147
        assert "ripple" in n.keys()
148
        assert 3 == n["lop"]
149
        assert 1 == n["ripple"]
150
        #
151
        t = g.withSideEffect('m', 32).V().map(lambda: "x: x.sideEffects('m')")
152
        results = await t.toSet()
153
        assert 1 == len(results)
154
        assert 32 == list(results)[0]
155
        assert 32 == await t.side_effects['m']
156
        keys = await t.side_effects.keys()
157
        assert 1 == len(keys)
158
        with pytest.raises(Exception):
159
            x = await t.side_effects["x"]
160
        await remote_connection.close()
161
162
    @pytest.mark.asyncio
163
    async def test_side_effect_close(self, remote_connection):
164
        g = Graph().traversal().withRemote(remote_connection)
165
        t = g.V().aggregate('a').aggregate('b')
166
        await t.iterate()
167
168
        # The 'a' key should return some side effects
169
        results = await t.side_effects.get('a')
170
        assert results
171
172
        # Close result is None
173
        results = await t.side_effects.close()
174
        assert not results
175
176
        # Shouldn't get any new info from server
177
        # 'b' isn't in local cache
178
        results = await t.side_effects.get('b')
179
        assert not results
180
181
        # But 'a' should still be cached locally
182
        results = await t.side_effects.get('a')
183
        assert results
184
185
        # 'a' should have been added to local keys cache, but not 'b'
186
        results = await t.side_effects.keys()
187
        assert len(results) == 1
188
        a, = results
189
        assert a == 'a'
190
191
        # Try to get 'b' directly from server, should throw error
192
        with pytest.raises(Exception):
193
            await t.side_effects._get('b')
194
        await remote_connection.close()
195