2017-09-30 13:14:51 +00:00
|
|
|
import decimal
|
2023-08-18 13:16:57 +00:00
|
|
|
from unittest import mock
|
2017-09-30 13:14:51 +00:00
|
|
|
|
2020-04-15 09:20:46 +00:00
|
|
|
from django.core.management.color import no_style
|
|
|
|
from django.db import NotSupportedError, connection, transaction
|
2017-09-30 13:14:51 +00:00
|
|
|
from django.db.backends.base.operations import BaseDatabaseOperations
|
2024-01-13 21:16:58 +00:00
|
|
|
from django.db.models import DurationField
|
2023-04-18 08:19:06 +00:00
|
|
|
from django.db.models.expressions import Col
|
2023-08-18 13:16:57 +00:00
|
|
|
from django.db.models.lookups import Exact
|
2018-10-02 12:22:38 +00:00
|
|
|
from django.test import (
|
2020-04-15 09:20:46 +00:00
|
|
|
SimpleTestCase,
|
|
|
|
TestCase,
|
|
|
|
TransactionTestCase,
|
|
|
|
override_settings,
|
|
|
|
skipIfDBFeature,
|
2018-10-02 12:22:38 +00:00
|
|
|
)
|
2017-09-30 13:14:51 +00:00
|
|
|
from django.utils import timezone
|
2023-08-18 13:16:57 +00:00
|
|
|
from django.utils.deprecation import RemovedInDjango60Warning
|
2017-09-18 13:42:29 +00:00
|
|
|
|
2020-04-15 09:20:46 +00:00
|
|
|
from ..models import Author, Book
|
|
|
|
|
2017-09-18 13:42:29 +00:00
|
|
|
|
2018-10-02 12:22:38 +00:00
|
|
|
class SimpleDatabaseOperationTests(SimpleTestCase):
|
2020-04-08 18:06:31 +00:00
|
|
|
may_require_msg = "subclasses of BaseDatabaseOperations may require a %s() method"
|
2017-09-30 13:14:51 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.ops = BaseDatabaseOperations(connection=connection)
|
|
|
|
|
|
|
|
def test_deferrable_sql(self):
|
|
|
|
self.assertEqual(self.ops.deferrable_sql(), "")
|
|
|
|
|
|
|
|
def test_end_transaction_rollback(self):
|
|
|
|
self.assertEqual(self.ops.end_transaction_sql(success=False), "ROLLBACK;")
|
|
|
|
|
|
|
|
def test_no_limit_value(self):
|
2020-04-08 18:06:31 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
NotImplementedError, self.may_require_msg % "no_limit_value"
|
|
|
|
):
|
2017-09-30 13:14:51 +00:00
|
|
|
self.ops.no_limit_value()
|
|
|
|
|
|
|
|
def test_quote_name(self):
|
2020-04-08 18:06:31 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
NotImplementedError, self.may_require_msg % "quote_name"
|
|
|
|
):
|
2017-09-30 13:14:51 +00:00
|
|
|
self.ops.quote_name("a")
|
|
|
|
|
|
|
|
def test_regex_lookup(self):
|
2020-04-08 18:06:31 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
NotImplementedError, self.may_require_msg % "regex_lookup"
|
|
|
|
):
|
2017-09-30 13:14:51 +00:00
|
|
|
self.ops.regex_lookup(lookup_type="regex")
|
|
|
|
|
|
|
|
def test_set_time_zone_sql(self):
|
|
|
|
self.assertEqual(self.ops.set_time_zone_sql(), "")
|
|
|
|
|
|
|
|
def test_sql_flush(self):
|
2020-05-06 04:35:26 +00:00
|
|
|
msg = "subclasses of BaseDatabaseOperations must provide an sql_flush() method"
|
2017-09-30 13:14:51 +00:00
|
|
|
with self.assertRaisesMessage(NotImplementedError, msg):
|
2020-04-15 09:20:46 +00:00
|
|
|
self.ops.sql_flush(None, None)
|
2017-09-30 13:14:51 +00:00
|
|
|
|
|
|
|
def test_pk_default_value(self):
|
|
|
|
self.assertEqual(self.ops.pk_default_value(), "DEFAULT")
|
|
|
|
|
|
|
|
def test_tablespace_sql(self):
|
|
|
|
self.assertEqual(self.ops.tablespace_sql(None), "")
|
|
|
|
|
|
|
|
def test_sequence_reset_by_name_sql(self):
|
|
|
|
self.assertEqual(self.ops.sequence_reset_by_name_sql(None, []), [])
|
|
|
|
|
|
|
|
def test_adapt_unknown_value_decimal(self):
|
|
|
|
value = decimal.Decimal("3.14")
|
|
|
|
self.assertEqual(
|
|
|
|
self.ops.adapt_unknown_value(value),
|
|
|
|
self.ops.adapt_decimalfield_value(value),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_adapt_unknown_value_date(self):
|
|
|
|
value = timezone.now().date()
|
|
|
|
self.assertEqual(
|
|
|
|
self.ops.adapt_unknown_value(value), self.ops.adapt_datefield_value(value)
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2017-09-30 13:14:51 +00:00
|
|
|
|
|
|
|
def test_adapt_unknown_value_time(self):
|
|
|
|
value = timezone.now().time()
|
|
|
|
self.assertEqual(
|
|
|
|
self.ops.adapt_unknown_value(value), self.ops.adapt_timefield_value(value)
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2017-09-30 13:14:51 +00:00
|
|
|
|
|
|
|
def test_adapt_timefield_value_none(self):
|
|
|
|
self.assertIsNone(self.ops.adapt_timefield_value(None))
|
|
|
|
|
2021-11-12 16:53:25 +00:00
|
|
|
def test_adapt_datetimefield_value_none(self):
|
2017-09-30 13:14:51 +00:00
|
|
|
self.assertIsNone(self.ops.adapt_datetimefield_value(None))
|
|
|
|
|
|
|
|
def test_adapt_timefield_value(self):
|
|
|
|
msg = "Django does not support timezone-aware times."
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
self.ops.adapt_timefield_value(timezone.make_aware(timezone.now()))
|
|
|
|
|
|
|
|
@override_settings(USE_TZ=False)
|
|
|
|
def test_adapt_timefield_value_unaware(self):
|
|
|
|
now = timezone.now()
|
|
|
|
self.assertEqual(self.ops.adapt_timefield_value(now), str(now))
|
|
|
|
|
2021-10-22 05:55:25 +00:00
|
|
|
def test_format_for_duration_arithmetic(self):
|
|
|
|
msg = self.may_require_msg % "format_for_duration_arithmetic"
|
|
|
|
with self.assertRaisesMessage(NotImplementedError, msg):
|
|
|
|
self.ops.format_for_duration_arithmetic(None)
|
|
|
|
|
2017-09-30 13:14:51 +00:00
|
|
|
def test_date_extract_sql(self):
|
2020-04-08 18:06:31 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
NotImplementedError, self.may_require_msg % "date_extract_sql"
|
|
|
|
):
|
2022-06-20 03:46:22 +00:00
|
|
|
self.ops.date_extract_sql(None, None, None)
|
2017-09-30 13:14:51 +00:00
|
|
|
|
|
|
|
def test_time_extract_sql(self):
|
2020-04-08 18:06:31 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
NotImplementedError, self.may_require_msg % "date_extract_sql"
|
|
|
|
):
|
2022-06-20 03:46:22 +00:00
|
|
|
self.ops.time_extract_sql(None, None, None)
|
2017-09-30 13:14:51 +00:00
|
|
|
|
|
|
|
def test_date_trunc_sql(self):
|
2020-04-08 18:06:31 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
NotImplementedError, self.may_require_msg % "date_trunc_sql"
|
|
|
|
):
|
2022-06-20 03:46:22 +00:00
|
|
|
self.ops.date_trunc_sql(None, None, None)
|
2017-09-30 13:14:51 +00:00
|
|
|
|
|
|
|
def test_time_trunc_sql(self):
|
2020-04-08 18:06:31 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
NotImplementedError, self.may_require_msg % "time_trunc_sql"
|
|
|
|
):
|
2022-06-20 03:46:22 +00:00
|
|
|
self.ops.time_trunc_sql(None, None, None)
|
2017-09-30 13:14:51 +00:00
|
|
|
|
|
|
|
def test_datetime_trunc_sql(self):
|
2020-04-08 18:06:31 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
NotImplementedError, self.may_require_msg % "datetime_trunc_sql"
|
|
|
|
):
|
2022-06-20 03:46:22 +00:00
|
|
|
self.ops.datetime_trunc_sql(None, None, None, None)
|
2017-09-30 13:14:51 +00:00
|
|
|
|
|
|
|
def test_datetime_cast_date_sql(self):
|
2020-04-08 18:06:31 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
NotImplementedError, self.may_require_msg % "datetime_cast_date_sql"
|
|
|
|
):
|
2022-06-20 03:46:22 +00:00
|
|
|
self.ops.datetime_cast_date_sql(None, None, None)
|
2017-09-30 13:14:51 +00:00
|
|
|
|
|
|
|
def test_datetime_cast_time_sql(self):
|
2020-04-08 18:06:31 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
NotImplementedError, self.may_require_msg % "datetime_cast_time_sql"
|
|
|
|
):
|
2022-06-20 03:46:22 +00:00
|
|
|
self.ops.datetime_cast_time_sql(None, None, None)
|
2017-09-30 13:14:51 +00:00
|
|
|
|
|
|
|
def test_datetime_extract_sql(self):
|
2020-04-08 18:06:31 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
NotImplementedError, self.may_require_msg % "datetime_extract_sql"
|
|
|
|
):
|
2022-06-20 03:46:22 +00:00
|
|
|
self.ops.datetime_extract_sql(None, None, None, None)
|
2017-09-30 13:14:51 +00:00
|
|
|
|
2023-04-18 08:19:06 +00:00
|
|
|
def test_prepare_join_on_clause(self):
|
|
|
|
author_table = Author._meta.db_table
|
|
|
|
author_id_field = Author._meta.get_field("id")
|
|
|
|
book_table = Book._meta.db_table
|
|
|
|
book_fk_field = Book._meta.get_field("author")
|
|
|
|
lhs_expr, rhs_expr = self.ops.prepare_join_on_clause(
|
|
|
|
author_table,
|
|
|
|
author_id_field,
|
|
|
|
book_table,
|
|
|
|
book_fk_field,
|
|
|
|
)
|
|
|
|
self.assertEqual(lhs_expr, Col(author_table, author_id_field))
|
|
|
|
self.assertEqual(rhs_expr, Col(book_table, book_fk_field))
|
|
|
|
|
2019-01-12 21:14:54 +00:00
|
|
|
|
|
|
|
class DatabaseOperationTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.ops = BaseDatabaseOperations(connection=connection)
|
|
|
|
|
|
|
|
@skipIfDBFeature("can_distinct_on_fields")
|
|
|
|
def test_distinct_on_fields(self):
|
|
|
|
msg = "DISTINCT ON fields is not supported by this database backend"
|
|
|
|
with self.assertRaisesMessage(NotSupportedError, msg):
|
|
|
|
self.ops.distinct_sql(["a", "b"], None)
|
|
|
|
|
2017-09-30 13:14:51 +00:00
|
|
|
@skipIfDBFeature("supports_temporal_subtraction")
|
|
|
|
def test_subtract_temporals(self):
|
|
|
|
duration_field = DurationField()
|
|
|
|
duration_field_internal_type = duration_field.get_internal_type()
|
|
|
|
msg = (
|
|
|
|
"This backend does not support %s subtraction."
|
|
|
|
% duration_field_internal_type
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(NotSupportedError, msg):
|
|
|
|
self.ops.subtract_temporals(duration_field_internal_type, None, None)
|
2020-04-15 09:20:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SqlFlushTests(TransactionTestCase):
|
|
|
|
available_apps = ["backends"]
|
|
|
|
|
|
|
|
def test_sql_flush_no_tables(self):
|
2020-04-15 09:20:46 +00:00
|
|
|
self.assertEqual(connection.ops.sql_flush(no_style(), []), [])
|
2020-04-15 09:20:46 +00:00
|
|
|
|
|
|
|
def test_execute_sql_flush_statements(self):
|
|
|
|
with transaction.atomic():
|
|
|
|
author = Author.objects.create(name="George Orwell")
|
|
|
|
Book.objects.create(author=author)
|
|
|
|
author = Author.objects.create(name="Harper Lee")
|
|
|
|
Book.objects.create(author=author)
|
|
|
|
Book.objects.create(author=author)
|
|
|
|
self.assertIs(Author.objects.exists(), True)
|
|
|
|
self.assertIs(Book.objects.exists(), True)
|
|
|
|
|
|
|
|
sql_list = connection.ops.sql_flush(
|
|
|
|
no_style(),
|
|
|
|
[Author._meta.db_table, Book._meta.db_table],
|
2020-04-15 09:20:46 +00:00
|
|
|
reset_sequences=True,
|
2020-04-15 09:20:46 +00:00
|
|
|
allow_cascade=True,
|
|
|
|
)
|
2020-04-20 05:49:35 +00:00
|
|
|
connection.ops.execute_sql_flush(sql_list)
|
2020-04-15 09:20:46 +00:00
|
|
|
|
|
|
|
with transaction.atomic():
|
|
|
|
self.assertIs(Author.objects.exists(), False)
|
|
|
|
self.assertIs(Book.objects.exists(), False)
|
|
|
|
if connection.features.supports_sequence_reset:
|
|
|
|
author = Author.objects.create(name="F. Scott Fitzgerald")
|
|
|
|
self.assertEqual(author.pk, 1)
|
2020-04-15 09:20:46 +00:00
|
|
|
book = Book.objects.create(author=author)
|
|
|
|
self.assertEqual(book.pk, 1)
|
2023-08-18 13:16:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeprecationTests(TestCase):
|
|
|
|
def test_field_cast_sql_warning(self):
|
|
|
|
base_ops = BaseDatabaseOperations(connection=connection)
|
|
|
|
msg = (
|
|
|
|
"DatabaseOperations.field_cast_sql() is deprecated use "
|
|
|
|
"DatabaseOperations.lookup_cast() instead."
|
|
|
|
)
|
2024-08-09 16:57:58 +00:00
|
|
|
with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx:
|
2023-08-18 13:16:57 +00:00
|
|
|
base_ops.field_cast_sql("integer", "IntegerField")
|
2024-08-09 16:57:58 +00:00
|
|
|
self.assertEqual(ctx.filename, __file__)
|
2023-08-18 13:16:57 +00:00
|
|
|
|
|
|
|
def test_field_cast_sql_usage_warning(self):
|
|
|
|
compiler = Author.objects.all().query.get_compiler(connection.alias)
|
|
|
|
msg = (
|
|
|
|
"The usage of DatabaseOperations.field_cast_sql() is deprecated. Implement "
|
|
|
|
"DatabaseOperations.lookup_cast() instead."
|
|
|
|
)
|
|
|
|
with mock.patch.object(connection.ops.__class__, "field_cast_sql"):
|
|
|
|
with self.assertRaisesMessage(RemovedInDjango60Warning, msg):
|
|
|
|
Exact("name", "book__author__name").as_sql(compiler, connection)
|