Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
53 lines (42 loc) · 1.6 KB

File metadata and controls

53 lines (42 loc) · 1.6 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import unittest
from jsonquerylang import jsonquery
data = [
{"name": "Chris", "age": 23, "scores": [7.2, 5, 8.0]},
{"name": "Joe", "age": 32, "scores": [6.1, 8.1]},
{"name": "Emily", "age": 19},
]
class JSONQueryTestCase(unittest.TestCase):
def test_jsonquery(self):
"""Test jsonquery (test and execute)"""
self.assertEqual(
jsonquery(data, ["sort", ["get", "name"]]),
[
{"name": "Chris", "age": 23, "scores": [7.2, 5, 8.0]},
{"name": "Emily", "age": 19},
{"name": "Joe", "age": 32, "scores": [6.1, 8.1]},
],
)
def test_jsonquery_str1(self):
"""Test jsonquery parsing a query text"""
self.assertEqual(
jsonquery(data, "sort(.name)"),
[
{"name": "Chris", "age": 23, "scores": [7.2, 5, 8.0]},
{"name": "Emily", "age": 19},
{"name": "Joe", "age": 32, "scores": [6.1, 8.1]},
],
)
def test_jsonquery_str2(self):
"""Test jsonquery parsing a query text with options"""
options = {"functions": {"times": lambda factor: lambda x: x * factor}}
self.assertEqual(jsonquery(4, "times(3)", options), 12)
def test_options1(self):
"""Test defining a custom function"""
def times(value):
return lambda array: list(map(lambda item: item * value, array))
query = ["times", 2]
self.assertEqual(
jsonquery([2, 3, 4], query, {"functions": {"times": times}}), [4, 6, 8]
)
if __name__ == "__main__":
unittest.main()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.