Debian Codegraph @

tests/backends/base/test_operations.py

raw
1 import decimal
2 from unittest import mock
3
4 from django.core.management.color import no_style
5 from django.db import NotSupportedError, connection, transaction
6 from django.db.backends.base.operations import BaseDatabaseOperations
7 from django.db.models import DurationField
8 from django.db.models.expressions import Col
9 from django.db.models.lookups import Exact
10 from django.test import (
11 SimpleTestCase,
12 TestCase,
13 TransactionTestCase,
14 override_settings,
15 skipIfDBFeature,
16 )
17 from django.utils import timezone
18 from django.utils.deprecation import RemovedInDjango60Warning
19
20 from ..models import Author, Book
21
22
23 class SimpleDatabaseOperationTests(SimpleTestCase):
24 may_require_msg = "subclasses of BaseDatabaseOperations may require a %s() method"
25
26 def setUp(self):
27 self.ops = BaseDatabaseOperations(connection=connection)
28
29 def test_deferrable_sql(self):
30 self.assertEqual(self.ops.deferrable_sql(), "")
31
32 def test_end_transaction_rollback(self):
33 self.assertEqual(self.ops.end_transaction_sql(success=False), "ROLLBACK;")
34
35 def test_no_limit_value(self):
36 with self.assertRaisesMessage(
37 NotImplementedError, self.may_require_msg % "no_limit_value"
38 ):
39 self.ops.no_limit_value()
40
41 def test_quote_name(self):
42 with self.assertRaisesMessage(
43 NotImplementedError, self.may_require_msg % "quote_name"
44 ):
45 self.ops.quote_name("a")
46
47 def test_regex_lookup(self):
48 with self.assertRaisesMessage(
49 NotImplementedError, self.may_require_msg % "regex_lookup"
50 ):
51 self.ops.regex_lookup(lookup_type="regex")
52
53 def test_set_time_zone_sql(self):
54 self.assertEqual(self.ops.set_time_zone_sql(), "")
55
56 def test_sql_flush(self):
57 msg = "subclasses of BaseDatabaseOperations must provide an sql_flush() method"
58 with self.assertRaisesMessage(NotImplementedError, msg):
59 self.ops.sql_flush(None, None)
60
61 def test_pk_default_value(self):
62 self.assertEqual(self.ops.pk_default_value(), "DEFAULT")
63
64 def test_tablespace_sql(self):
65 self.assertEqual(self.ops.tablespace_sql(None), "")
66
67 def test_sequence_reset_by_name_sql(self):
68 self.assertEqual(self.ops.sequence_reset_by_name_sql(None, []), [])
69
70 def test_adapt_unknown_value_decimal(self):
71 value = decimal.Decimal("3.14")
72 self.assertEqual(
73 self.ops.adapt_unknown_value(value),
74 self.ops.adapt_decimalfield_value(value),
75 )
76
77 def test_adapt_unknown_value_date(self):
78 value = timezone.now().date()
79 self.assertEqual(
80 self.ops.adapt_unknown_value(value), self.ops.adapt_datefield_value(value)
81 )
82
83 def test_adapt_unknown_value_time(self):
84 value = timezone.now().time()
85 self.assertEqual(
86 self.ops.adapt_unknown_value(value), self.ops.adapt_timefield_value(value)
87 )
88
89 def test_adapt_timefield_value_none(self):
90 self.assertIsNone(self.ops.adapt_timefield_value(None))
91
92 def test_adapt_datetimefield_value_none(self):
93 self.assertIsNone(self.ops.adapt_datetimefield_value(None))
94
95 def test_adapt_timefield_value(self):
96 msg = "Django does not support timezone-aware times."
97 with self.assertRaisesMessage(ValueError, msg):
98 self.ops.adapt_timefield_value(timezone.make_aware(timezone.now()))
99
100 @override_settings(USE_TZ=False)
101 def test_adapt_timefield_value_unaware(self):
102 now = timezone.now()
103 self.assertEqual(self.ops.adapt_timefield_value(now), str(now))
104
105 def test_format_for_duration_arithmetic(self):
106 msg = self.may_require_msg % "format_for_duration_arithmetic"
107 with self.assertRaisesMessage(NotImplementedError, msg):
108 self.ops.format_for_duration_arithmetic(None)
109
110 def test_date_extract_sql(self):
111 with self.assertRaisesMessage(
112 NotImplementedError, self.may_require_msg % "date_extract_sql"
113 ):
114 self.ops.date_extract_sql(None, None, None)
115
116 def test_time_extract_sql(self):
117 with self.assertRaisesMessage(
118 NotImplementedError, self.may_require_msg % "date_extract_sql"
119 ):
120 self.ops.time_extract_sql(None, None, None)
121
122 def test_date_trunc_sql(self):
123 with self.assertRaisesMessage(
124 NotImplementedError, self.may_require_msg % "date_trunc_sql"
125 ):
126 self.ops.date_trunc_sql(None, None, None)
127
128 def test_time_trunc_sql(self):
129 with self.assertRaisesMessage(
130 NotImplementedError, self.may_require_msg % "time_trunc_sql"
131 ):
132 self.ops.time_trunc_sql(None, None, None)
133
134 def test_datetime_trunc_sql(self):
135 with self.assertRaisesMessage(
136 NotImplementedError, self.may_require_msg % "datetime_trunc_sql"
137 ):
138 self.ops.datetime_trunc_sql(None, None, None, None)
139
140 def test_datetime_cast_date_sql(self):
141 with self.assertRaisesMessage(
142 NotImplementedError, self.may_require_msg % "datetime_cast_date_sql"
143 ):
144 self.ops.datetime_cast_date_sql(None, None, None)
145
146 def test_datetime_cast_time_sql(self):
147 with self.assertRaisesMessage(
148 NotImplementedError, self.may_require_msg % "datetime_cast_time_sql"
149 ):
150 self.ops.datetime_cast_time_sql(None, None, None)
151
152 def test_datetime_extract_sql(self):
153 with self.assertRaisesMessage(
154 NotImplementedError, self.may_require_msg % "datetime_extract_sql"
155 ):
156 self.ops.datetime_extract_sql(None, None, None, None)
157
158 def test_prepare_join_on_clause(self):
159 author_table = Author._meta.db_table
160 author_id_field = Author._meta.get_field("id")
161 book_table = Book._meta.db_table
162 book_fk_field = Book._meta.get_field("author")
163 lhs_expr, rhs_expr = self.ops.prepare_join_on_clause(
164 author_table,
165 author_id_field,
166 book_table,
167 book_fk_field,
168 )
169 self.assertEqual(lhs_expr, Col(author_table, author_id_field))
170 self.assertEqual(rhs_expr, Col(book_table, book_fk_field))
171
172
173 class DatabaseOperationTests(TestCase):
174 def setUp(self):
175 self.ops = BaseDatabaseOperations(connection=connection)
176
177 @skipIfDBFeature("can_distinct_on_fields")
178 def test_distinct_on_fields(self):
179 msg = "DISTINCT ON fields is not supported by this database backend"
180 with self.assertRaisesMessage(NotSupportedError, msg):
181 self.ops.distinct_sql(["a", "b"], None)
182
183 @skipIfDBFeature("supports_temporal_subtraction")
184 def test_subtract_temporals(self):
185 duration_field = DurationField()
186 duration_field_internal_type = duration_field.get_internal_type()
187 msg = (
188 "This backend does not support %s subtraction."
189 % duration_field_internal_type
190 )
191 with self.assertRaisesMessage(NotSupportedError, msg):
192 self.ops.subtract_temporals(duration_field_internal_type, None, None)
193
194
195 class SqlFlushTests(TransactionTestCase):
196 available_apps = ["backends"]
197
198 def test_sql_flush_no_tables(self):
199 self.assertEqual(connection.ops.sql_flush(no_style(), []), [])
200
201 def test_execute_sql_flush_statements(self):
202 with transaction.atomic():
203 author = Author.objects.create(name="George Orwell")
204 Book.objects.create(author=author)
205 author = Author.objects.create(name="Harper Lee")
206 Book.objects.create(author=author)
207 Book.objects.create(author=author)
208 self.assertIs(Author.objects.exists(), True)
209 self.assertIs(Book.objects.exists(), True)
210
211 sql_list = connection.ops.sql_flush(
212 no_style(),
213 [Author._meta.db_table, Book._meta.db_table],
214 reset_sequences=True,
215 allow_cascade=True,
216 )
217 connection.ops.execute_sql_flush(sql_list)
218
219 with transaction.atomic():
220 self.assertIs(Author.objects.exists(), False)
221 self.assertIs(Book.objects.exists(), False)
222 if connection.features.supports_sequence_reset:
223 author = Author.objects.create(name="F. Scott Fitzgerald")
224 self.assertEqual(author.pk, 1)
225 book = Book.objects.create(author=author)
226 self.assertEqual(book.pk, 1)
227
228
229 class DeprecationTests(TestCase):
230 def test_field_cast_sql_warning(self):
231 base_ops = BaseDatabaseOperations(connection=connection)
232 msg = (
233 "DatabaseOperations.field_cast_sql() is deprecated use "
234 "DatabaseOperations.lookup_cast() instead."
235 )
236 with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx:
237 base_ops.field_cast_sql("integer", "IntegerField")
238 self.assertEqual(ctx.filename, __file__)
239
240 def test_field_cast_sql_usage_warning(self):
241 compiler = Author.objects.all().query.get_compiler(connection.alias)
242 msg = (
243 "The usage of DatabaseOperations.field_cast_sql() is deprecated. Implement "
244 "DatabaseOperations.lookup_cast() instead."
245 )
246 with mock.patch.object(connection.ops.__class__, "field_cast_sql"):
247 with self.assertRaisesMessage(RemovedInDjango60Warning, msg):
248 Exact("name", "book__author__name").as_sql(compiler, connection)