TestTraversal.test_bytecode()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 32
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 32
rs 9.16
c 0
b 0
f 0
cc 1
nop 1
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
__author__ = 'Marko A. Rodriguez (http://markorodriguez.com)'
21
22
from aiogremlin.structure.graph import Graph
23
from gremlin_python.process.traversal import P
24
from gremlin_python.process.traversal import Binding
25
from gremlin_python.process.graph_traversal import __
26
27
28
class TestTraversal:
29
    def test_bytecode(self):
30
        g = Graph().traversal()
31
        bytecode = g.V().out("created").bytecode
32
        assert 0 == len(bytecode.bindings.keys())
33
        assert 0 == len(bytecode.source_instructions)
34
        assert 2 == len(bytecode.step_instructions)
35
        assert "V" == bytecode.step_instructions[0][0]
36
        assert "out" == bytecode.step_instructions[1][0]
37
        assert "created" == bytecode.step_instructions[1][1]
38
        assert 1 == len(bytecode.step_instructions[0])
39
        assert 2 == len(bytecode.step_instructions[1])
40
        ##
41
        bytecode = g.withSack(1).E().groupCount().by("weight").bytecode
42
        assert 0 == len(bytecode.bindings.keys())
43
        assert 1 == len(bytecode.source_instructions)
44
        assert "withSack" == bytecode.source_instructions[0][0]
45
        assert 1 == bytecode.source_instructions[0][1]
46
        assert 3 == len(bytecode.step_instructions)
47
        assert "E" == bytecode.step_instructions[0][0]
48
        assert "groupCount" == bytecode.step_instructions[1][0]
49
        assert "by" == bytecode.step_instructions[2][0]
50
        assert "weight" == bytecode.step_instructions[2][1]
51
        assert 1 == len(bytecode.step_instructions[0])
52
        assert 1 == len(bytecode.step_instructions[1])
53
        assert 2 == len(bytecode.step_instructions[2])
54
        ##
55
        bytecode = g.V(('a',[1,2,3])).out(('b','created')).where(__.in_(('c','created'),('d','knows')).count().is_(('e',P.gt(2)))).bytecode
56
        assert 'V' == bytecode.step_instructions[0][0]
57
        assert 'out' == bytecode.step_instructions[1][0]
58
        assert 'where' == bytecode.step_instructions[2][0]
59
        assert ('b', 'created') == bytecode.step_instructions[1][1]
60
        assert isinstance(hash(bytecode.step_instructions[1][1]),int)
61
62
    def test_P(self):
63
        # verify that the order of operations is respected
64
        assert "and(eq(a),lt(b))" == str(P.eq("a").and_(P.lt("b")))
65
        assert "and(or(lt(b),gt(c)),neq(d))" == str(P.lt("b").or_(P.gt("c")).and_(P.neq("d")))
66
        assert "and(or(lt(b),gt(c)),or(neq(d),gte(e)))" == str(
67
            P.lt("b").or_(P.gt("c")).and_(P.neq("d").or_(P.gte("e"))))
68
69
    def test_anonymous_traversal(self):
70
        bytecode = __.__(1).bytecode
71
        assert 0 == len(bytecode.bindings.keys())
72
        assert 0 == len(bytecode.source_instructions)
73
        assert 1 == len(bytecode.step_instructions)
74
        assert "inject" == bytecode.step_instructions[0][0]
75
        assert 1 == bytecode.step_instructions[0][1]
76
        ##
77
        bytecode = __.start().bytecode
78
        assert 0 == len(bytecode.bindings.keys())
79
        assert 0 == len(bytecode.source_instructions)
80
        assert 0 == len(bytecode.step_instructions)
81