forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_utils_test.py
More file actions
113 lines (95 loc) · 4.07 KB
/
python_utils_test.py
File metadata and controls
113 lines (95 loc) · 4.07 KB
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
# 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 annotations
import ast
import builtins
import urllib
from core import python_utils
from core.tests import test_utils
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_parse_query_string(self):
response = urllib.parse.parse_qs(
'http://www.google.com?search=oppia')
self.assertEqual(response, {'http://www.google.com?search': ['oppia']})
def test_recursively_convert_to_str_with_dict(self):
test_var_1_in_unicode = str('test_var_1')
test_var_2_in_unicode = str('test_var_2')
test_var_3_in_bytes = test_var_1_in_unicode.encode(encoding='utf-8')
test_var_4_in_bytes = test_var_2_in_unicode.encode(encoding='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), str)
self.assertEqual(type(val), builtins.bytes)
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), str)
self.assertEqual(type(val), str)
def test_recursively_convert_to_str_with_nested_structure(self):
test_var_1_in_unicode = str('test_var_1')
test_list_1 = [
test_var_1_in_unicode,
test_var_1_in_unicode.encode(encoding='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', '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), builtins.bytes)
self.assertTrue(isinstance(key, str))
for item in value:
self.assertNotEqual(type(item), builtins.bytes)
self.assertTrue(isinstance(item, (str, bytes, dict)))
for k, v in value[-1].items():
self.assertEqual(type(k), str)
self.assertEqual(type(v), str)