/
test_commcare_minilinq.py
113 lines (96 loc) · 4.08 KB
/
test_commcare_minilinq.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
import unittest
from itertools import *
from jsonpath_rw import jsonpath
from commcare_export.minilinq import *
from commcare_export.env import *
from commcare_export.commcare_hq_client import MockCommCareHqClient
from commcare_export.commcare_minilinq import *
class TestCommCareMiniLinq(unittest.TestCase):
@classmethod
def setup_class(cls):
pass
def check_case(self, val, result):
if isinstance(result, list):
assert [datum.value if isinstance(datum, jsonpath.DatumInContext) else datum for datum in val] == result
def test_eval(self):
def die(msg): raise Exception(msg)
client = MockCommCareHqClient({
'form': [
(
{'limit': 1000, 'filter': 'test1'},
[1, 2, 3],
),
(
{'limit': 1000, 'filter': 'test2'},
[
{ 'x': [{ 'y': 1 }, {'y': 2}] },
{ 'x': [{ 'y': 3 }, {'z': 4}] },
{ 'x': [{ 'y': 5 }] }
]
),
(
{'limit': 1000, 'filter': 'laziness-test'},
(i if i < 5 else die('Not lazy enough') for i in range(12))
),
(
{'limit': 1000, 'cases__full': 'true'},
[1, 2, 3, 4, 5]
),
],
'case': [
(
{'limit': 1000, 'type': 'foo'},
[
{ 'x': 1 },
{ 'x': 2 },
{ 'x': 3 },
]
)
],
'user': [
(
{'limit': 1000},
[
{ 'x': 1 },
{ 'x': 2 },
{ 'x': 3 },
]
)
]
})
env = BuiltInEnv() | CommCareHqEnv(client) | JsonPathEnv({}) # {'form': api_client.iterate('form')})
assert list(Apply(Reference('api_data'),
Literal('form'),
Literal({"filter": 'test1'})).eval(env)) == [1, 2, 3]
# just check that we can still apply some deeper xpath by mapping; first ensure the basics work
assert list(Apply(Reference('api_data'),
Literal('form'),
Literal({"filter": 'test2'})).eval(env)) == [
{ 'x': [{ 'y': 1 }, {'y': 2}] },
{ 'x': [{ 'y': 3 }, {'z': 4}] },
{ 'x': [{ 'y': 5 }] }
]
self.check_case(FlatMap(source=Apply(Reference('api_data'),
Literal('form'),
Literal({"filter": 'test2'})),
body=Reference('x[*].y')).eval(env),
[1, 2, 3, 5])
self.check_case(islice(Apply(Reference('api_data'),
Literal('form'),
Literal({"filter": "laziness-test"})).eval(env), 5),
[0, 1, 2, 3, 4])
self.check_case(Apply(Reference('api_data'),
Literal('form'),
Literal(None),
Literal(['cases'])).eval(env),
[1, 2, 3, 4, 5])
self.check_case(FlatMap(source=Apply(Reference('api_data'),
Literal('case'),
Literal({'type': 'foo'})),
body=Reference('x')).eval(env),
[1, 2, 3])
self.check_case(FlatMap(source=Apply(Reference('api_data'),
Literal('user'),
Literal(None)),
body=Reference('x')).eval(env),
[1, 2, 3])