Code

Ticket #16759: 16759_test.2.diff

File 16759_test.2.diff, 2.7 KB (added by lrekucki, 2 years ago)

Regression tests without Query internals introspection.

Line 
1commit 97f7c76de682a4062032195f9bfeb3b8e5e67883
2Author: Łukasz Rekucki <lrekucki@gmail.com>
3Date:   Mon Feb 6 22:44:37 2012 +0100
4
5    TESTS
6
7diff --git a/tests/regressiontests/queries/tests.py b/tests/regressiontests/queries/tests.py
8index ded3e8f..620bb91 100644
9--- a/tests/regressiontests/queries/tests.py
10+++ b/tests/regressiontests/queries/tests.py
11@@ -7,7 +7,7 @@ import sys
12 from django.conf import settings
13 from django.core.exceptions import FieldError
14 from django.db import DatabaseError, connection, connections, DEFAULT_DB_ALIAS
15-from django.db.models import Count
16+from django.db.models import Count, F
17 from django.db.models.query import Q, ITER_CHUNK_SIZE, EmptyQuerySet
18 from django.test import TestCase, skipUnlessDBFeature
19 from django.utils import unittest
20@@ -1593,6 +1593,7 @@ class SubqueryTests(TestCase):
21 
22 
23 class CloneTests(TestCase):
24+   
25     def test_evaluated_queryset_as_argument(self):
26         "#13227 -- If a queryset is already evaluated, it can still be used as a query arg"
27         n = Note(note='Test1', misc='misc')
28@@ -1610,6 +1611,39 @@ class CloneTests(TestCase):
29         except:
30             self.fail('Query should be clonable')
31 
32+    def test_no_model_options_cloning(self):
33+        """
34+        Test that cloning a queryset does not get out of hand. While complete
35+        testing is impossible, this is a sanity check against invalid use of
36+        deepcopy. refs #16759.
37+        """
38+        opts_class = type(Note._meta)
39+        note_deepcopy = getattr(opts_class, "__deepcopy__", None)
40+        opts_class.__deepcopy__ = lambda obj, memo: self.fail("Model options shouldn't be cloned.")
41+        try:
42+            Note.objects.filter(pk__lte=F('pk') + 1).all()
43+        finally:
44+            if note_deepcopy is None:
45+                delattr(opts_class, "__deepcopy__")
46+            else:
47+                opts_class.__deepcopy__ = note_deepcopy
48+   
49+    def test_no_fields_cloning(self):
50+        """
51+        Test that cloning a queryset does not get out of hand. While complete
52+        testing is impossible, this is a sanity check against invalid use of
53+        deepcopy. refs #16759.
54+        """
55+        opts_class = type(Note._meta.get_field_by_name("misc")[0])
56+        note_deepcopy = getattr(opts_class, "__deepcopy__", None)
57+        opts_class.__deepcopy__ = lambda obj, memo: self.fail("Model fields shouldn't be cloned")
58+        try:
59+            Note.objects.filter(note=F('misc')).all()
60+        finally:
61+            if note_deepcopy is None:
62+                delattr(opts_class, "__deepcopy__")
63+            else:
64+                opts_class.__deepcopy__ = note_deepcopy
65 
66 class EmptyQuerySetTests(TestCase):
67     def test_emptyqueryset_values(self):