forked from doloopwhile/pyjq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pyjq.py
148 lines (122 loc) · 4.35 KB
/
test_pyjq.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
# encoding: utf8
from __future__ import unicode_literals
import unittest
import re
from mock import patch
import tempfile
import shutil
import os.path
import io
import pyjq
import _pyjq
class TestCaseBackwardCompatMixin:
def assertRaisesRegex(self, *a, **kw):
return self.assertRaisesRegexp(*a, **kw)
class TestJq(unittest.TestCase, TestCaseBackwardCompatMixin):
def test_compile_dot(self):
s = pyjq.compile('.')
self.assertIsInstance(s, _pyjq.Script)
def test_syntax_error(self):
expected_message = re.escape(r"error: syntax error")
with self.assertRaisesRegexp(ValueError, expected_message):
pyjq.compile('**')
def test_conversion_between_python_object_and_jv(self):
objects = [
None,
False,
True,
1,
1.5,
"string",
[None, False, True, 1, 1.5, [None, False, True], {'foo': 'bar'}],
{
'key1': None,
'key2': False,
'key3': True,
'key4': 1,
'key5': 1.5,
'key6': [None, False, True, 1, 1.5,
[None, False, True], {'foo': 'bar'}],
},
]
s = pyjq.compile('.')
for obj in objects:
self.assertEqual([obj], s.all(obj))
def test_assigning_values(self):
self.assertEqual(pyjq.one('$foo', {}, vars=dict(foo='bar')), 'bar')
self.assertEqual(pyjq.one('$foo', {}, vars=dict(foo=['bar'])), ['bar'])
def test_all(self):
self.assertEqual(
pyjq.all('.[] | . + $foo', ['val1', 'val2'], vars=dict(foo='bar')),
['val1bar', 'val2bar']
)
self.assertEqual(
pyjq.all('. + $foo', 'val', vars=dict(foo='bar')),
['valbar']
)
def test_first(self):
self.assertEqual(
pyjq.first('.[] | . + $foo', ['val1', 'val2'], vars=dict(foo='bar')),
'val1bar'
)
def test_one(self):
self.assertEqual(
pyjq.one('. + $foo', 'val', vars=dict(foo='bar')),
'valbar'
)
# raise IndexError if got multiple elements
with self.assertRaises(IndexError):
pyjq.one('.[]', [1, 2])
# raise IndexError if got no elements
with self.assertRaises(IndexError):
pyjq.one('.[]', [])
def test_url_argument(self):
class FakeResponse:
def getheader(self, name):
return 'application/json;charset=SHIFT_JIS'
def read(self):
return '["Hello", "世界", "!"]'.encode('shift-jis')
try:
import urllib.request
del urllib
except ImportError:
to_patch = 'urllib2.urlopen'
else:
to_patch = 'urllib.request.urlopen'
to_patch = 'six.moves.urllib.request.urlopen'
with patch(to_patch, return_value=FakeResponse()):
self.assertEqual(
pyjq.all('.[] | . + .', url='http://example.com'),
["HelloHello", "世界世界", "!!"]
)
def opener(url):
return [1, 2, 3]
self.assertEqual(
pyjq.all('.[] | . + .', url='http://example.com', opener=opener),
[2, 4, 6]
)
def test_library_path(self):
library_path = tempfile.mkdtemp()
library_path2 = tempfile.mkdtemp()
library_file = os.path.join(library_path, "greeting.jq")
library_file2 = os.path.join(library_path2, "increment.jq")
try:
with io.open(library_file, 'w', encoding='ascii') as f:
f.write('def hello: "HELLO";')
f.write('def world: "WORLD";')
with io.open(library_file2, 'w', encoding='ascii') as f:
f.write('def increment: . + 1;\n')
values = pyjq.all(
'include "greeting"; include "increment"; .[] | [. | increment, hello, world]',
[1, 2, 3],
library_paths = [library_path, library_path2]
)
self.assertEquals(
[[2, "HELLO", "WORLD"], [3, "HELLO", "WORLD"], [4, "HELLO", "WORLD"]],
values
)
finally:
shutil.rmtree(library_path)
shutil.rmtree(library_path2)
if __name__ == '__main__':
unittest.main()