-
Notifications
You must be signed in to change notification settings - Fork 79
/
test_core.py
404 lines (301 loc) · 10.8 KB
/
test_core.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import unittest
from bubbles import *
# import bubbles.iterator
# FIXME: clean this up
import inspect
def default(ctx, left):
pass
def unary(ctx, left):
pass
def binary(ctx, left, right):
pass
class DummyDataObject(DataObject):
def __init__(self, reps=None, data=None):
"""Creates a dummy data object with bogus representations `reps` and
arbitrary data `data`"""
self.reps = reps or []
self.data = data
def representations(self):
return self.reps
class TextObject(DataObject):
def __init__(self, string):
self.string = string
def representations(self):
return ["rows", "text"]
def rows(self):
return iter(self.string)
def text(self):
return self.string
class OperationTestCase(unittest.TestCase):
def test_match(self):
self.assertTrue(Signature("sql").matches("sql"))
self.assertTrue(Signature("*").matches("sql"))
self.assertTrue(Signature("sql[]").matches("sql[]"))
self.assertTrue(Signature("*[]").matches("sql[]"))
self.assertFalse(Signature("sql").matches("rows"))
self.assertFalse(Signature("sql").matches("sql[]"))
def test_common_reps(self):
objs = [
DummyDataObject(["a", "b", "c"]),
DummyDataObject(["a", "b", "d"]),
DummyDataObject(["b", "d", "e"])
]
self.assertEqual(["b"], list(common_representations(*objs)))
objs = [
DummyDataObject(["a", "b", "c"]),
DummyDataObject(["a", "b", "d"]),
DummyDataObject(["d", "d", "e"])
]
self.assertEqual([], list(common_representations(*objs)))
def test_prototype(self):
proto = Signature("sql", "sql")
match = Signature("*", "*")
self.assertEqual(match, proto.as_prototype())
proto = Signature("sql[]", "sql")
match = Signature("*[]", "*")
self.assertEqual(match, proto.as_prototype())
proto = Signature("*[]", "*")
match = Signature("*[]", "*")
self.assertEqual(match, proto.as_prototype())
def test_create_op(self):
def one(ctx, obj):
pass
def fun(ctx, left, right):
pass
with self.assertRaises(ArgumentError):
Operation("fun", [])
op = Operation("fun")
self.assertEqual(1, op.opcount)
self.assertEqual(["obj"], op.operands)
op = Operation("fun", ["left", "right"])
self.assertEqual(2, op.opcount)
def test_register(self):
def func(ctx, obj):
pass
def func_sql(ctx, obj):
pass
def func_text(ctx, obj):
pass
op = Operation("select", ["obj"])
op.register(func)
self.assertEqual(1, len(op.registry))
self.assertEqual([Signature("*")], list(op.registry.keys()))
op.register(func_sql, "sql")
op.register(func_text, "text")
self.assertEqual(3, len(op.registry))
sigs = []
for s in op.signatures():
sigs.append([op.rep for op in s.operands])
self.assertSequenceEqual(sigs, [["*"], ["sql"], ["text"]])
f = op.function(Signature("*"))
self.assertEqual(f, func)
f = op.function(Signature("sql"))
self.assertEqual(f, func_sql)
f = op.function(Signature("text"))
self.assertEqual(f, func_text)
def test_register_invalid(self):
def func_invalid(obj):
pass
def func_invalid2(ctx, obj):
pass
op = Operation("select")
with self.assertRaisesRegex(ArgumentError, "Expected at least"):
op.register(func_invalid)
def test_decorator(self):
op = Operation("select", ["obj"])
@op.register
def func(ctx, obj):
pass
sig = Signature("*")
self.assertEqual(1, len(op.registry))
self.assertEqual([sig], list(op.registry.keys()))
self.assertEqual(func, op.registry[sig])
@op.register("sql")
def func_sql(ctx, obj):
pass
sig = Signature("sql")
self.assertEqual(2, len(op.registry))
self.assertEqual(func_sql, op.registry[sig])
# Test decorator implicitly creating Operation
@operation
def implicit(ctx, obj):
pass
self.assertIsInstance(implicit, Operation)
self.assertEqual("implicit", implicit.name)
def test_context(self):
c = OperationContext()
with self.assertRaises(KeyError):
c.operations["select"]
with self.assertRaises(OperationError):
c.operation("select")
c.add_operation(Operation("select"))
op = c.operations["select"]
@operation
def touch(ctx, obj):
pass
c.add_operation(touch)
op = c.operations["touch"]
def test_resolution_order(self):
@operation
def upper(ctx, obj):
pass
obj = DummyDataObject(["rows"])
order = upper.resolution_order(get_representations(obj))
self.assertEqual([Signature("*")], order)
@upper.register("rows")
def _(ctx, obj):
pass
order = upper.resolution_order(get_representations(obj))
self.assertEqual([Signature("rows"),
Signature("*")], order)
def test_call(self):
@operation
def upper(ctx, obj):
return obj.text().upper()
c = OperationContext()
c.add_operation(upper)
obj = TextObject("hi there")
result = c.call("upper", obj)
self.assertEqual("HI THERE", result)
def test_call_context_op(self):
op = Operation("upper")
@op.register("rows")
def _(ctx, obj):
rows = obj.rows()
text = "".join(rows)
return list(text.upper())
@op.register("text")
def _(ctx, obj):
text = obj.text()
return list(text.upper())
c = OperationContext()
c.add_operation(op)
obj = TextObject("windchimes")
result = c.op.upper(obj)
self.assertEqual(list("WINDCHIMES"), result)
def test_get_representations(self):
obj = DummyDataObject(["rows", "sql"])
self.assertEqual( [["rows", "sql"]], get_representations(obj))
obj = DummyDataObject(["rows", "sql"])
extr = get_representations([obj])
self.assertEqual( [["rows[]", "sql[]"]], extr)
def test_comparison(self):
sig1 = Signature("a", "b", "c")
sig2 = Signature("a", "b", "c")
sig3 = Signature("a", "b")
self.assertTrue(sig1 == sig1)
self.assertTrue(sig1 == sig2)
self.assertFalse(sig1 == sig3)
self.assertTrue(sig1 == ["a", "b", "c"])
self.assertFalse(sig1 == ["a", "b"])
def test_retry(self):
op = Operation("join", ["left", "right"])
@op.register("sql", "sql")
def _(ctx, l, r):
if l.data == r.data:
return "SQL"
else:
raise RetryOperation(["sql", "rows"])
@op.register("sql", "rows")
def _(ctx, l, r):
return "ITERATOR"
local = DummyDataObject(["sql", "rows"], "local")
remote = DummyDataObject(["sql", "rows"], "remote")
c = OperationContext()
c.add_operation(op)
result = c.op.join(local, local)
self.assertEqual(result, "SQL")
result = c.op.join(local, remote)
self.assertEqual(result, "ITERATOR")
fail = Operation("fail", ["left", "right"])
@fail.register("sql", "rows")
def _(ctx, l, r):
raise RetryOperation(["sql", "sql"])
c.add_operation(fail)
with self.assertRaises(OperationError):
c.op.fail(local, local)
# Already visited
repeat = Operation("repeat", ["left", "right"])
@repeat.register("sql", "sql")
def _(ctx, l, r):
raise RetryOperation(["sql", "sql"])
c.add_operation(repeat)
with self.assertRaises(RetryError):
c.op.repeat(local, local)
def test_allow_deny_retry(self):
swim = Operation("swim", ["obj"])
@swim.register("sql")
def _(ctx, obj):
raise RetryOperation(["rows"])
@swim.register("rows")
def _(ctx, obj):
obj.data = "good"
return obj
obj = DummyDataObject(["sql", "rows"], "")
c = OperationContext()
c.add_operation(swim)
result = c.op.swim(obj)
self.assertEqual("good", result.data)
c.retry_deny = ["swim"]
c.retry_allow = []
with self.assertRaises(RetryError):
c.op.swim(obj)
c.retry_deny = []
c.retry_allow = ["swim"]
result = c.op.swim(obj)
self.assertEqual("good", result.data)
c.retry_deny = ["swim"]
c.retry_allow = ["swim"]
with self.assertRaises(RetryError):
c.op.swim(obj)
def test_retry_nested(self):
"""Test whether failed nested operation fails correctly (Because of
Issue #4)."""
aggregate = Operation("aggregate", ["obj"])
@aggregate.register("sql")
def _(ctx, obj, fail):
if fail:
raise RetryOperation(["rows"])
else:
obj.data += "-SQL-"
return obj
@aggregate.register("rows")
def _(ctx, obj, fail):
obj.data += "-ROWS-"
return obj
window_aggregate = Operation("window_aggregate", ["obj"])
@window_aggregate.register("sql")
def _(ctx, obj, fail):
obj.data += "START"
ctx.op.aggregate(obj, fail)
obj.data += "END"
c = OperationContext()
c.add_operation(aggregate)
c.add_operation(window_aggregate)
# Expected order:
# 1. window_aggregate is called
# 2. sql aggregate is called, but fails
# 3. row aggregate is called
# 4. window aggregate continues
obj = DummyDataObject(["sql"], "")
c.op.window_aggregate(obj, fail=True)
self.assertEqual("START-ROWS-END", obj.data)
obj.data = ""
c.op.window_aggregate(obj, fail=False)
self.assertEqual("START-SQL-END", obj.data)
def test_priority(self):
objsql = DummyDataObject(["sql", "rows"])
objrows = DummyDataObject(["rows", "sql"])
meditate = Operation("meditate", ["obj"])
@meditate.register("sql")
def fsql(ctx, obj):
return "sql"
@meditate.register("rows")
def frows(ctx, obj):
return "rows"
c = OperationContext()
c.add_operation(meditate)
self.assertEqual("sql", c.op.meditate(objsql))
self.assertEqual("rows", c.op.meditate(objrows))
if __name__ == "__main__":
unittest.main()