forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_utils_test.py
332 lines (266 loc) · 12.3 KB
/
python_utils_test.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
# coding: utf-8
#
# Copyright 2019 The Oppia Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for feature detection utilities for Python 2 and Python 3."""
from __future__ import absolute_import # pylint: disable=import-only-modules
from __future__ import unicode_literals # pylint: disable=import-only-modules
import StringIO
import ast
import io
import sys
import tempfile
import unittest
from core.tests import test_utils
from core.tests.data import unicode_and_str_handler
import python_utils
import future # isort:skip
class PythonUtilsTests(test_utils.GenericTestBase):
"""Tests for feature detection utilities that are common for Python 2 and
Python 3.
"""
def test_get_args_of_function_node(self):
function_txt = b"""def _mock_function(arg1, arg2):
pass"""
ast_node = ast.walk(ast.parse(function_txt))
function_node = [n for n in ast_node if isinstance(n, ast.FunctionDef)]
args_list = python_utils.get_args_of_function_node(function_node[0], [])
self.assertEqual(args_list, ['arg1', 'arg2'])
def test_open_file(self):
with python_utils.open_file('python_utils.py', 'r') as f:
file_content = f.readlines()
self.assertIsNotNone(file_content)
def test_can_not_open_file(self):
with self.assertRaisesRegexp(
IOError, 'Unable to open file: invalid_file.py'):
with python_utils.open_file('invalid_file.py', 'r') as f:
f.readlines()
def test_url_quote(self):
self.assertEqual(
python_utils.url_quote(b'/~connolly/'), b'/%7Econnolly/')
def test_url_encode(self):
url_dict = {'url': 'http://myapp/my%20test/'}
self.assertEqual(
python_utils.url_encode(url_dict, doseq=True),
'url=http%3A%2F%2Fmyapp%2Fmy%2520test%2F')
self.assertEqual(
python_utils.url_encode(url_dict, doseq=False),
'url=http%3A%2F%2Fmyapp%2Fmy%2520test%2F')
def test_url_retrieve(self):
tmp_file = tempfile.NamedTemporaryFile()
tmp_file.name = 'temp_file.txt'
python_utils.url_retrieve(
'http://www.google.com', filename='temp_file.txt')
with python_utils.open_file('temp_file.txt', 'rb', encoding=None) as f:
content = f.read()
self.assertIn(b'<title>Google</title>', content)
tmp_file.close()
def test_url_open(self):
response = python_utils.url_open('http://www.google.com')
self.assertEqual(response.getcode(), 200)
self.assertEqual(response.url, 'http://www.google.com')
def test_url_request(self):
response = python_utils.url_request('http://www.google.com', None, {})
self.assertEqual(response.get_full_url(), 'http://www.google.com')
def test_url_unquote_plus(self):
self.assertEqual(
python_utils.url_unquote_plus(b'/El+Ni%C3%B1o/'), b'/El Niño/')
def test_divide(self):
self.assertEqual(python_utils.divide(4, 2), 2)
self.assertEqual(python_utils.divide(5, 2), 2)
def test_with_metaclass(self):
class BaseForm(python_utils.OBJECT):
"""Test baseclass."""
pass
class FormType1(type):
"""Test metaclass."""
pass
class FormType2(type):
"""Test metaclass."""
pass
class Form(python_utils.with_metaclass(FormType1, BaseForm)): # pylint: disable=inherit-non-class
"""Test class."""
pass
self.assertTrue(isinstance(Form, FormType1))
self.assertFalse(isinstance(Form, FormType2))
self.assertTrue(issubclass(Form, BaseForm))
def test_with_metaclass_without_bases(self):
class FormType(type):
"""Test metaclass."""
pass
class Form(python_utils.with_metaclass(FormType)): # pylint: disable=inherit-non-class
"""Test class."""
def __init__(self):
pass
self.assertTrue(isinstance(Form, FormType))
self.assertTrue(issubclass(Form, python_utils.OBJECT))
def test_convert_to_bytes(self):
string1 = 'Home'
string2 = u'Лорем'
self.assertEqual(python_utils.convert_to_bytes(string1), string1)
self.assertEqual(
python_utils.convert_to_bytes(string2),
string2.encode(encoding='utf-8'))
def test_url_split(self):
response = python_utils.url_split('http://www.google.com')
self.assertEqual(response.geturl(), 'http://www.google.com')
def test_url_unsplit(self):
response = python_utils.url_split('http://www.google.com')
self.assertEqual(
python_utils.url_unsplit(response), 'http://www.google.com')
def test_parse_query_string(self):
response = python_utils.parse_query_string(
'http://www.google.com?search=oppia')
self.assertEqual(response, {'http://www.google.com?search': ['oppia']})
def test_urllib_unquote(self):
response = python_utils.urllib_unquote(b'/El%20Ni%C3%B1o/')
self.assertEqual(response, b'/El Niño/')
def test_url_parse(self):
response = python_utils.url_parse('http://www.google.com')
self.assertEqual(response.geturl(), 'http://www.google.com')
def test_url_join(self):
response = python_utils.url_join(
'http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')
self.assertEqual(response, 'http://www.cwi.nl/%7Eguido/FAQ.html')
def test_recursively_convert_to_str_with_dict(self):
test_var_1_in_unicode = python_utils.UNICODE('test_var_1')
test_var_2_in_unicode = python_utils.UNICODE('test_var_2')
test_var_3_in_bytes = test_var_1_in_unicode.encode('utf-8')
test_var_4_in_bytes = test_var_2_in_unicode.encode('utf-8')
test_dict = {
test_var_1_in_unicode: test_var_3_in_bytes,
test_var_2_in_unicode: test_var_4_in_bytes
}
self.assertEqual(
test_dict,
{'test_var_1': b'test_var_1', 'test_var_2': b'test_var_2'})
for key, val in test_dict.items():
self.assertEqual(type(key), future.types.newstr)
self.assertEqual(type(val), future.types.newbytes)
dict_in_str = python_utils._recursively_convert_to_str(test_dict) # pylint: disable=protected-access
self.assertEqual(
dict_in_str,
{'test_var_1': 'test_var_1', 'test_var_2': 'test_var_2'})
for key, val in dict_in_str.items():
self.assertEqual(type(key), unicode)
self.assertEqual(type(val), bytes)
def test_recursively_convert_to_str_with_nested_structure(self):
test_var_1_in_unicode = python_utils.UNICODE('test_var_1')
test_list_1 = [
test_var_1_in_unicode, test_var_1_in_unicode.encode('utf-8'),
'test_var_2', b'test_var_3', {'test_var_4': b'test_var_5'}]
test_dict = {test_var_1_in_unicode: test_list_1}
self.assertEqual(
test_dict,
{
'test_var_1': [
'test_var_1', b'test_var_1', 'test_var_2', b'test_var_3',
{'test_var_4': b'test_var_5'}]
}
)
dict_in_str = python_utils._recursively_convert_to_str(test_dict) # pylint: disable=protected-access
self.assertEqual(
dict_in_str,
{
'test_var_1': [
'test_var_1', b'test_var_1', 'test_var_2', 'test_var_3',
{'test_var_4': 'test_var_5'}]
}
)
for key, value in dict_in_str.items():
self.assertNotEqual(type(key), future.types.newstr)
self.assertNotEqual(type(key), future.types.newbytes)
self.assertTrue(isinstance(key, unicode))
for item in value:
self.assertNotEqual(type(item), future.types.newstr)
self.assertNotEqual(type(item), future.types.newbytes)
self.assertTrue(isinstance(item, (unicode, bytes, dict)))
for k, v in value[-1].items():
self.assertNotEqual(type(k), future.types.newstr)
self.assertNotEqual(type(k), future.types.newbytes)
self.assertNotEqual(type(v), future.types.newstr)
self.assertNotEqual(type(v), future.types.newbytes)
self.assertEqual(type(k), unicode)
self.assertEqual(type(v), bytes)
def test_is_string(self):
self.assertTrue(python_utils.is_string('abc'))
self.assertFalse(python_utils.is_string(123))
self.assertFalse(python_utils.is_string(['a', 'b', 'c']))
def test_get_args_of_function(self):
def func(a, b, *c, **d): # pylint: disable=unused-argument
"""Does nothing."""
pass
self.assertEqual(
python_utils.get_args_of_function(func), ['a', 'b'])
def test_create_enum_method_and_check_its_values(self):
"""Test create_enum method."""
enums = python_utils.create_enum('first', 'second', 'third')
self.assertEqual(enums.first.value, 'first')
self.assertEqual(enums.second.value, 'second')
self.assertEqual(enums.third.value, 'third')
def test_create_enum_method_and_check_its_names(self):
"""Test create_enum method."""
enums = python_utils.create_enum('first', 'second', 'third')
self.assertEqual(enums.first.name, 'first')
self.assertEqual(enums.second.name, 'second')
self.assertEqual(enums.third.name, 'third')
def test_enum_for_invalid_attribute(self):
enums = python_utils.create_enum('first', 'second', 'third')
with self.assertRaisesRegexp(AttributeError, 'fourth'):
getattr(enums, 'fourth')
def test_zip_longest(self):
self.assertEqual(
[list(g) for g in python_utils.zip_longest(
[0, 1, 2, 3], [4, 5, 6], [7, 8])],
[[0, 4, 7], [1, 5, 8], [2, 6, None], [3, None, None]])
# Zip longest with fillvalue.
self.assertEqual(
[''.join(g) for g in python_utils.zip_longest(
'ABC', 'DE', 'F', fillvalue='x')],
['ADF', 'BEx', 'Cxx'])
@unittest.skipUnless(
sys.version[0] == '2', 'Test cases for ensuring Python 2 behavior only')
class PythonUtilsForPython2Tests(test_utils.GenericTestBase):
"""Tests for feature detection utilities for Python 2."""
def test_string_io(self):
stdout = python_utils.string_io()
self.assertIsInstance(stdout, StringIO.StringIO)
def test_unicode_and_str_chars_in_file(self):
self.assertIsInstance(
unicode_and_str_handler.SOME_STR_TEXT, python_utils.UNICODE)
self.assertIsInstance(
unicode_and_str_handler.SOME_UNICODE_TEXT, python_utils.UNICODE)
self.assertIsInstance(
unicode_and_str_handler.SOME_BINARY_TEXT, bytes)
with python_utils.open_file(
'core/tests/data/unicode_and_str_handler.py', 'r') as f:
file_content = f.read()
self.assertIsInstance(file_content, python_utils.UNICODE)
@unittest.skipUnless(
sys.version[0] == '3', 'Test cases for ensuring Python 3 behavior only')
class PythonUtilsForPython3Tests(test_utils.GenericTestBase):
"""Tests for feature detection utilities for Python 3."""
def test_string_io(self):
stdout = python_utils.string_io()
self.assertIsInstance(stdout, io.StringIO)
def test_unicode_and_str_chars_in_file(self):
self.assertIsInstance(unicode_and_str_handler.SOME_STR_TEXT, str)
self.assertIsInstance(
unicode_and_str_handler.SOME_UNICODE_TEXT, str)
self.assertIsInstance(
unicode_and_str_handler.SOME_BINARY_TEXT, bytes)
with python_utils.open_file(
'core/tests/data/unicode_and_str_handler.py', 'r') as f:
file_content = f.read()
self.assertIsInstance(file_content, str)