summaryrefslogtreecommitdiff
path: root/tests/utils_tests
diff options
context:
space:
mode:
authorJake Howard <git@theorangeone.net>2025-07-17 12:51:09 +0100
committernessita <124304+nessita@users.noreply.github.com>2025-09-16 17:28:32 -0300
commit4289966d1b8e848e5e460b7c782dac009d746b20 (patch)
treeef1d61a33562579d985c762036db5f7aa01406fc /tests/utils_tests
parent218f69f05eb51da1ea17d62a914a67ceff5bfd55 (diff)
Fixed #35859 -- Added background Tasks framework interface.
This work implements what was defined in DEP 14 (https://github.com/django/deps/blob/main/accepted/0014-background-workers.rst). Thanks to Raphael Gaschignard, Eric Holscher, Ran Benita, Sarah Boyce, Jacob Walls, and Natalia Bidart for the reviews.
Diffstat (limited to 'tests/utils_tests')
-rw-r--r--tests/utils_tests/test_inspect.py49
-rw-r--r--tests/utils_tests/test_json.py46
2 files changed, 95 insertions, 0 deletions
diff --git a/tests/utils_tests/test_inspect.py b/tests/utils_tests/test_inspect.py
index b8359c2508..38ea35ecfb 100644
--- a/tests/utils_tests/test_inspect.py
+++ b/tests/utils_tests/test_inspect.py
@@ -1,5 +1,7 @@
+import subprocess
import unittest
+from django.shortcuts import aget_object_or_404
from django.utils import inspect
@@ -100,3 +102,50 @@ class TestInspectMethods(unittest.TestCase):
self.assertIs(inspect.func_accepts_kwargs(Person().just_args), False)
self.assertIs(inspect.func_accepts_kwargs(Person.all_kinds), True)
self.assertIs(inspect.func_accepts_kwargs(Person().just_args), False)
+
+
+class IsModuleLevelFunctionTestCase(unittest.TestCase):
+ @classmethod
+ def _class_method(cls) -> None:
+ return None
+
+ @staticmethod
+ def _static_method() -> None:
+ return None
+
+ def test_builtin(self):
+ self.assertIs(inspect.is_module_level_function(any), False)
+ self.assertIs(inspect.is_module_level_function(isinstance), False)
+
+ def test_from_module(self):
+ self.assertIs(inspect.is_module_level_function(subprocess.run), True)
+ self.assertIs(inspect.is_module_level_function(subprocess.check_output), True)
+ self.assertIs(
+ inspect.is_module_level_function(inspect.is_module_level_function), True
+ )
+
+ def test_private_function(self):
+ def private_function():
+ pass
+
+ self.assertIs(inspect.is_module_level_function(private_function), False)
+
+ def test_coroutine(self):
+ self.assertIs(inspect.is_module_level_function(aget_object_or_404), True)
+
+ def test_method(self):
+ self.assertIs(inspect.is_module_level_function(self.test_method), False)
+ self.assertIs(inspect.is_module_level_function(self.setUp), False)
+
+ def test_unbound_method(self):
+ self.assertIs(
+ inspect.is_module_level_function(self.__class__.test_unbound_method), True
+ )
+ self.assertIs(inspect.is_module_level_function(self.__class__.setUp), True)
+
+ def test_lambda(self):
+ self.assertIs(inspect.is_module_level_function(lambda: True), False)
+
+ def test_class_and_static_method(self):
+ self.assertIs(inspect.is_module_level_function(self._static_method), True)
+ self.assertIs(inspect.is_module_level_function(self._class_method), False)
diff --git a/tests/utils_tests/test_json.py b/tests/utils_tests/test_json.py
new file mode 100644
index 0000000000..9d137149ed
--- /dev/null
+++ b/tests/utils_tests/test_json.py
@@ -0,0 +1,46 @@
+import json
+from collections import UserList, defaultdict
+from datetime import datetime
+from decimal import Decimal
+
+from django.test import SimpleTestCase
+from django.utils.json import normalize_json
+
+
+class JSONNormalizeTestCase(SimpleTestCase):
+ def test_converts_json_types(self):
+ for test_case, expected in [
+ (None, "null"),
+ (True, "true"),
+ (False, "false"),
+ (2, "2"),
+ (3.0, "3.0"),
+ (1e23 + 1, "1e+23"),
+ ("1", '"1"'),
+ (b"hello", '"hello"'),
+ ([], "[]"),
+ (UserList([1, 2]), "[1, 2]"),
+ ({}, "{}"),
+ ({1: "a"}, '{"1": "a"}'),
+ ({"foo": (1, 2, 3)}, '{"foo": [1, 2, 3]}'),
+ (defaultdict(list), "{}"),
+ (float("nan"), "NaN"),
+ (float("inf"), "Infinity"),
+ (float("-inf"), "-Infinity"),
+ ]:
+ with self.subTest(test_case):
+ normalized = normalize_json(test_case)
+ # Ensure that the normalized result is serializable.
+ self.assertEqual(json.dumps(normalized), expected)
+
+ def test_bytes_decode_error(self):
+ with self.assertRaisesMessage(ValueError, "Unsupported value"):
+ normalize_json(b"\xff")
+
+ def test_encode_error(self):
+ for test_case in [self, any, object(), datetime.now(), set(), Decimal("3.42")]:
+ with (
+ self.subTest(test_case),
+ self.assertRaisesMessage(TypeError, "Unsupported type"),
+ ):
+ normalize_json(test_case)