test_operations.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import decimal
  2. from django.core.management.color import no_style
  3. from django.db import NotSupportedError, connection, transaction
  4. from django.db.backends.base.operations import BaseDatabaseOperations
  5. from django.db.models import DurationField, Value
  6. from django.test import (
  7. SimpleTestCase, TestCase, TransactionTestCase, override_settings,
  8. skipIfDBFeature,
  9. )
  10. from django.utils import timezone
  11. from ..models import Author, Book
  12. class SimpleDatabaseOperationTests(SimpleTestCase):
  13. may_require_msg = 'subclasses of BaseDatabaseOperations may require a %s() method'
  14. def setUp(self):
  15. self.ops = BaseDatabaseOperations(connection=connection)
  16. def test_deferrable_sql(self):
  17. self.assertEqual(self.ops.deferrable_sql(), '')
  18. def test_end_transaction_rollback(self):
  19. self.assertEqual(self.ops.end_transaction_sql(success=False), 'ROLLBACK;')
  20. def test_no_limit_value(self):
  21. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'no_limit_value'):
  22. self.ops.no_limit_value()
  23. def test_quote_name(self):
  24. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'quote_name'):
  25. self.ops.quote_name('a')
  26. def test_regex_lookup(self):
  27. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'regex_lookup'):
  28. self.ops.regex_lookup(lookup_type='regex')
  29. def test_set_time_zone_sql(self):
  30. self.assertEqual(self.ops.set_time_zone_sql(), '')
  31. def test_sql_flush(self):
  32. msg = 'subclasses of BaseDatabaseOperations must provide an sql_flush() method'
  33. with self.assertRaisesMessage(NotImplementedError, msg):
  34. self.ops.sql_flush(None, None)
  35. def test_pk_default_value(self):
  36. self.assertEqual(self.ops.pk_default_value(), 'DEFAULT')
  37. def test_tablespace_sql(self):
  38. self.assertEqual(self.ops.tablespace_sql(None), '')
  39. def test_sequence_reset_by_name_sql(self):
  40. self.assertEqual(self.ops.sequence_reset_by_name_sql(None, []), [])
  41. def test_adapt_unknown_value_decimal(self):
  42. value = decimal.Decimal('3.14')
  43. self.assertEqual(
  44. self.ops.adapt_unknown_value(value),
  45. self.ops.adapt_decimalfield_value(value)
  46. )
  47. def test_adapt_unknown_value_date(self):
  48. value = timezone.now().date()
  49. self.assertEqual(self.ops.adapt_unknown_value(value), self.ops.adapt_datefield_value(value))
  50. def test_adapt_unknown_value_time(self):
  51. value = timezone.now().time()
  52. self.assertEqual(self.ops.adapt_unknown_value(value), self.ops.adapt_timefield_value(value))
  53. def test_adapt_timefield_value_none(self):
  54. self.assertIsNone(self.ops.adapt_timefield_value(None))
  55. def test_adapt_timefield_value_expression(self):
  56. value = Value(timezone.now().time())
  57. self.assertEqual(self.ops.adapt_timefield_value(value), value)
  58. def test_adapt_datetimefield_value_none(self):
  59. self.assertIsNone(self.ops.adapt_datetimefield_value(None))
  60. def test_adapt_datetimefield_value_expression(self):
  61. value = Value(timezone.now())
  62. self.assertEqual(self.ops.adapt_datetimefield_value(value), value)
  63. def test_adapt_timefield_value(self):
  64. msg = 'Django does not support timezone-aware times.'
  65. with self.assertRaisesMessage(ValueError, msg):
  66. self.ops.adapt_timefield_value(timezone.make_aware(timezone.now()))
  67. @override_settings(USE_TZ=False)
  68. def test_adapt_timefield_value_unaware(self):
  69. now = timezone.now()
  70. self.assertEqual(self.ops.adapt_timefield_value(now), str(now))
  71. def test_format_for_duration_arithmetic(self):
  72. msg = self.may_require_msg % 'format_for_duration_arithmetic'
  73. with self.assertRaisesMessage(NotImplementedError, msg):
  74. self.ops.format_for_duration_arithmetic(None)
  75. def test_date_extract_sql(self):
  76. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'date_extract_sql'):
  77. self.ops.date_extract_sql(None, None)
  78. def test_time_extract_sql(self):
  79. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'date_extract_sql'):
  80. self.ops.time_extract_sql(None, None)
  81. def test_date_trunc_sql(self):
  82. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'date_trunc_sql'):
  83. self.ops.date_trunc_sql(None, None)
  84. def test_time_trunc_sql(self):
  85. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'time_trunc_sql'):
  86. self.ops.time_trunc_sql(None, None)
  87. def test_datetime_trunc_sql(self):
  88. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'datetime_trunc_sql'):
  89. self.ops.datetime_trunc_sql(None, None, None)
  90. def test_datetime_cast_date_sql(self):
  91. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'datetime_cast_date_sql'):
  92. self.ops.datetime_cast_date_sql(None, None)
  93. def test_datetime_cast_time_sql(self):
  94. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'datetime_cast_time_sql'):
  95. self.ops.datetime_cast_time_sql(None, None)
  96. def test_datetime_extract_sql(self):
  97. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'datetime_extract_sql'):
  98. self.ops.datetime_extract_sql(None, None, None)
  99. class DatabaseOperationTests(TestCase):
  100. def setUp(self):
  101. self.ops = BaseDatabaseOperations(connection=connection)
  102. @skipIfDBFeature('supports_over_clause')
  103. def test_window_frame_raise_not_supported_error(self):
  104. msg = 'This backend does not support window expressions.'
  105. with self.assertRaisesMessage(NotSupportedError, msg):
  106. self.ops.window_frame_rows_start_end()
  107. @skipIfDBFeature('can_distinct_on_fields')
  108. def test_distinct_on_fields(self):
  109. msg = 'DISTINCT ON fields is not supported by this database backend'
  110. with self.assertRaisesMessage(NotSupportedError, msg):
  111. self.ops.distinct_sql(['a', 'b'], None)
  112. @skipIfDBFeature('supports_temporal_subtraction')
  113. def test_subtract_temporals(self):
  114. duration_field = DurationField()
  115. duration_field_internal_type = duration_field.get_internal_type()
  116. msg = (
  117. 'This backend does not support %s subtraction.' %
  118. duration_field_internal_type
  119. )
  120. with self.assertRaisesMessage(NotSupportedError, msg):
  121. self.ops.subtract_temporals(duration_field_internal_type, None, None)
  122. class SqlFlushTests(TransactionTestCase):
  123. available_apps = ['backends']
  124. def test_sql_flush_no_tables(self):
  125. self.assertEqual(connection.ops.sql_flush(no_style(), []), [])
  126. def test_execute_sql_flush_statements(self):
  127. with transaction.atomic():
  128. author = Author.objects.create(name='George Orwell')
  129. Book.objects.create(author=author)
  130. author = Author.objects.create(name='Harper Lee')
  131. Book.objects.create(author=author)
  132. Book.objects.create(author=author)
  133. self.assertIs(Author.objects.exists(), True)
  134. self.assertIs(Book.objects.exists(), True)
  135. sql_list = connection.ops.sql_flush(
  136. no_style(),
  137. [Author._meta.db_table, Book._meta.db_table],
  138. reset_sequences=True,
  139. allow_cascade=True,
  140. )
  141. connection.ops.execute_sql_flush(sql_list)
  142. with transaction.atomic():
  143. self.assertIs(Author.objects.exists(), False)
  144. self.assertIs(Book.objects.exists(), False)
  145. if connection.features.supports_sequence_reset:
  146. author = Author.objects.create(name='F. Scott Fitzgerald')
  147. self.assertEqual(author.pk, 1)
  148. book = Book.objects.create(author=author)
  149. self.assertEqual(book.pk, 1)