Download tests/unit/test_js_variable_to_python.py from Openyx/xcdv: direct link, hf CLI and curl.
- Browser
- Download file 10.5 kB
-
https://huggingface.co/spaces/Openyx/xcdv/resolve/main/tests/unit/test_js_variable_to_python.py
- Command line
-
hf download hf://spaces/Openyx/xcdv/tests/unit/test_js_variable_to_python.py
-
curl -L -o test_js_variable_to_python.py https://huggingface.co/spaces/Openyx/xcdv/resolve/main/tests/unit/test_js_variable_to_python.py
10.5 kB
| # -*- coding: utf-8 -*- | |
| """Tests for the function ``searx.utils.js_obj_str_to_python`` | |
| The tests are copied from: | |
| https://github.com/Nykakin/chompjs/blob/c1501b5cd82c0044539875331745b820e7bfd067/chompjs/test_parser.py | |
| The commented-out tests are not yet supported by the current implementation. | |
| """ | |
| # pylint: disable=missing-class-docstring, invalid-name | |
| import math | |
| from parameterized import parameterized | |
| from searx.utils import js_obj_str_to_python | |
| from tests import SearxTestCase | |
| class TestParser(SearxTestCase): | |
| def test_parse_object(self, js, expected_py): | |
| py = js_obj_str_to_python(js) | |
| self.assertEqual(py, expected_py) | |
| def test_parse_list(self, js, expected_py): | |
| py = js_obj_str_to_python(js) | |
| self.assertEqual(py, expected_py) | |
| def test_parse_mixed(self, js, expected_py): | |
| py = js_obj_str_to_python(js) | |
| self.assertEqual(py, expected_py) | |
| def test_parse_standard_values(self, js, expected_py): | |
| py = js_obj_str_to_python(js) | |
| self.assertEqual(py, expected_py) | |
| def test_parse_nan(self): | |
| js = '{"A": NaN}' | |
| py = js_obj_str_to_python(js) | |
| self.assertTrue(math.isnan(py["A"])) | |
| def test_parse_strange_values(self, js, expected_py): | |
| py = js_obj_str_to_python(js) | |
| self.assertEqual(py, expected_py) | |
| def test_strange_input(self, js, expected_py): | |
| py = js_obj_str_to_python(js) | |
| self.assertEqual(py, expected_py) | |
| def test_integer_numeric_values(self, js, expected_py): | |
| py = js_obj_str_to_python(js) | |
| self.assertEqual(py, expected_py) | |
| def test_float_numeric_values(self, js, expected_py): | |
| py = js_obj_str_to_python(js) | |
| self.assertEqual(py, expected_py) | |
| # @parameterized.expand([ | |
| # ('["Test\\nDrive"]\n{"Test": "Drive"}', [['Test\nDrive'], {'Test': 'Drive'}]), | |
| # ]) | |
| # def test_jsonlines(self, js, expected_py): | |
| # py = js_obj_str_to_python(js) | |
| # self.assertEqual(py, expected_py) | |
| class TestParserExceptions(SearxTestCase): | |
| def test_exceptions(self, js, expected_exception): | |
| with self.assertRaises(expected_exception): | |
| js_obj_str_to_python(js) | |
| def test_malformed_input(self, in_data, expected_exception): | |
| with self.assertRaises(expected_exception): | |
| js_obj_str_to_python(in_data) | |
| def test_error_messages(self, js, expected_exception, expected_exception_text): | |
| with self.assertRaisesRegex(expected_exception, expected_exception_text): | |
| js_obj_str_to_python(js) | |
| # class TestOptions(SearxTestCase): | |
| # @parameterized.expand( | |
| # [ | |
| # ('{\\\"a\\\": 12}', {'a': 12}), | |
| # ] | |
| # ) | |
| # def test_unicode_escape(self, js, expected_py): | |
| # py = js_obj_str_to_python(js) | |
| # self.assertEqual(py, expected_py) | |
| class TestParseJsonObjects(SearxTestCase): | |
| def test_parse_json_objects(self, js, expected_py): | |
| py_in_list = [js_obj_str_to_python(js)] | |
| self.assertEqual(py_in_list, expected_py) | |