-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_query.py
155 lines (121 loc) · 4.18 KB
/
test_query.py
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
import unittest
import gcl
from gcl import query
class QueryTests(unittest.TestCase):
def setUp(self):
self.model = gcl.loads("""
x = {
y = { z = 3 };
q = { z = 4 };
};
obj_list = [{x = 1}, {x = 2}, {x = 3}];
scalar_list = [1, 2, 3];
""")
def testSimpleParse(self):
sel = query.GPath('x.y.z')
self.assertEquals(3, sel.select(self.model).first())
def testSimpleParseDeep(self):
sel = query.GPath('x.y.z')
deep = sel.select(self.model).deep()
self.assertEquals({'x': {'y': {'z': 3}}}, deep)
def testStarParse(self):
sel = query.GPath('x.*.z')
self.assertEquals(set([3, 4]), set(sel.select(self.model).values()))
def testMultiParse(self):
sel = query.GPath('x.{y,q}.z')
self.assertEquals([3, 4], list(sel.select(self.model).values()))
def testMultiSelectors(self):
sel = query.GPath(['x.y.z', 'x.q.z'])
self.assertEquals([3, 4], list(sel.select(self.model).values()))
def testPathSelectors(self):
sel = query.GPath(['x.y.z', 'x.q.z'])
self.assertEquals([
(['x', 'y', 'z'], 3),
(['x', 'q', 'z'], 4),
], sel.select(self.model).paths_values())
def testListSelectorsSub(self):
sel = query.GPath('obj_list.*.x')
self.assertEquals([1, 2, 3], list(sel.select(self.model).values()))
def testListSelectors(self):
sel = query.GPath('obj_list.*')
result = sel.select(self.model).deep()
self.assertTrue(isinstance(result['obj_list'], list))
def testListIndex(self):
sel = query.GPath('scalar_list.[1]')
result = sel.select(self.model).first()
self.assertEquals(2, result)
def testListIndexDeep(self):
sel = query.GPath('scalar_list.[1]')
result = sel.select(self.model).deep()
self.assertEquals([2], result['scalar_list'])
def testMultipleQueriesWithStar(self):
"""Check that if we have multiple selectors with * in it, we keep the indexes consistent."""
sel = query.GPath([
'list.*.item1',
'list.*.item2',
])
# Kinda know it breaks if the orders are different
model = gcl.loads("""
list = [
{ item2 = 1 },
{ item1 = 5 },
]
""")
result = sel.select(model).deep()
self.assertEquals([{"item2": 1},
{"item1": 5}],
result["list"])
class TestFinder(unittest.TestCase):
def setUp(self):
self.obj = gcl.loads('''
Thing = {
type = 'thing';
};
some-stuff = {
thing1 = Thing;
thing2 = Thing { something = 'different'; parent = thing1 };
};
heres_a_thing = Thing { type = 'boo'; after = some-stuff.thing2 };
in_list = [Thing];
deep_scope = {
in_list = [{ type = 'nothing'; }, { kind = 'nice' }];
};
''')
def testFindObjectsNotInList(self):
finder = query.TupleFinder(query.HasKeyCondition('type'))
finder.find(self.obj)
self.assertEquals(3, len(set(finder.unordered)))
def testFindObjectsAlsoInList(self):
finder = query.TupleFinder(query.HasKeyCondition('type', search_lists=True))
finder.find(self.obj)
import pprint; pprint.pprint(finder.unordered)
self.assertEquals(5, len(set(finder.unordered)))
def testFindInList(self):
finder = query.TupleFinder(query.InListCondition('in_list'));
finder.find(self.obj)
self.assertEquals(3, len(set(finder.unordered)))
def testOrderDependencies(self):
finder = query.TupleFinder(query.HasKeyCondition('type'))
finder.find(self.obj)
finder.order()
self.assertEquals([], finder.unordered)
self.assertFalse(finder.has_recursive_dependency())
self.assertEquals(['thing', 'thing', 'boo'], [x.value['type'] for x in finder.ordered])
def testRecursiveDependencies(self):
self.obj = gcl.loads('''
Thing = {
type = 'thing';
};
thing1 = Thing {
friend = thing2;
};
thing2 = Thing {
friend = thing1;
}
''')
finder = query.TupleFinder(query.HasKeyCondition('type'))
finder.find(self.obj)
finder.order()
self.assertTrue(finder.has_recursive_dependency())
recursive_names = [n.key_name() for n in finder.find_recursive_dependency()]
self.assertEquals(['thing1', 'thing2', 'thing1'], recursive_names)