Debian Codegraph @

src/polymorphic/tests/test_query_translate.py

raw
1 import copy
2 import tempfile
3 import pickle
4 import threading
5
6 from django.db.models import Q
7 from django.test import TestCase
8
9 from polymorphic.tests.models import Bottom, Middle, Top
10 from polymorphic.query_translate import translate_polymorphic_filter_definitions_in_args
11
12
13 class QueryTranslateTests(TestCase):
14 def test_translate_with_not_pickleable_query(self):
15 """
16 In some cases, Django may attacha _thread object to the query and we
17 will get the following when we try to deepcopy inside of
18 translate_polymorphic_filter_definitions_in_args:
19
20 TypeError: cannot pickle '_thread.lock' object
21
22
23 For this to trigger, we need to somehoe go down this path:
24
25 File "/perfdash/.venv/lib64/python3.12/site-packages/polymorphic/query_translate.py", line 95, in translate_polymorphic_filter_definitions_in_args
26 translate_polymorphic_Q_object(queryset_model, copy.deepcopy(q), using=using) for q in args
27 ^^^^^^^^^^^^^^^^
28 File "/usr/lib64/python3.12/copy.py", line 143, in deepcopy
29 y = copier(memo)
30 ^^^^^^^^^^^^
31 File "/perfdash/.venv/lib64/python3.12/site-packages/django/utils/tree.py", line 53, in __deepcopy__
32 obj.children = copy.deepcopy(self.children, memodict)
33 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34 File "/usr/lib64/python3.12/copy.py", line 136, in deepcopy
35 y = copier(x, memo)
36 ^^^^^^^^^^^^^^^
37
38 Internals in Django, somehow we must trigger this tree.py code in django via
39 the deepcopy in order to trigger this.
40
41 """
42
43 with tempfile.TemporaryFile() as fd:
44 # verify this is definitely not pickleable
45 with self.assertRaises(TypeError):
46 pickle.dumps(threading.Lock())
47
48 # I know this doesn't make sense to pass as a Q(), but
49 # I haven't found another way to trigger the copy.deepcopy failing.
50 q = Q(blog__info="blog info") | Q(blog__info=threading.Lock())
51
52 translate_polymorphic_filter_definitions_in_args(Bottom, args=[q])
53
54 def test_deep_copy_of_q_objects(self):
55 import os
56 from polymorphic.tests.models import DeepCopyTester, DeepCopyTester2
57 # binary fields can have an unpickleable memoryview object in them
58 # see https://github.com/jazzband/django-polymorphic/issues/524
59
60 d1_bf = os.urandom(32)
61 d2_bf1 = os.urandom(32)
62 d2_bf2 = os.urandom(32)
63
64 dct1 = DeepCopyTester.objects.create(binary_field=d1_bf)
65 dct2 = DeepCopyTester2.objects.create(binary_field=d2_bf1, binary_field2=d2_bf2)
66
67 self.assertEqual(list(DeepCopyTester.objects.filter(binary_field=d1_bf).all()), [dct1])
68
69 q1 = Q(DeepCopyTester2___binary_field2=d2_bf1)
70 self.assertEqual(list(DeepCopyTester.objects.filter(q1).all()), [])
71 assert q1.children[0][0] == "DeepCopyTester2___binary_field2"
72 q2 = Q(DeepCopyTester2___binary_field2=d2_bf2)
73 self.assertEqual(list(DeepCopyTester.objects.filter(q2).all()), [dct2])
74 assert q2.children[0][0] == "DeepCopyTester2___binary_field2"
75
76 assert len(DeepCopyTester.objects.filter(Q(binary_field=memoryview(d1_bf)))) == 1
77
78 self.assertEqual(DeepCopyTester.objects.all().delete()[0], 3)
79 self.assertEqual(DeepCopyTester.objects.count(), 0)
80
81 def test_proxy_model_query_related_name(self):
82 """Test _get_query_related_name fallback for proxy models"""
83 from polymorphic.query_translate import _get_query_related_name
84 from polymorphic.tests.models import ProxyChild, SubclassSelectorProxyModel
85
86 # Test that proxy models use the fallback (lowercase class name)
87 # since they don't have a OneToOneField parent link
88 result = _get_query_related_name(ProxyChild)
89 assert result == "proxychild"
90
91 result = _get_query_related_name(SubclassSelectorProxyModel)
92 assert result == "subclassselectorproxymodel"