test_operations.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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
  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_datetimefield_value(self):
  56. self.assertIsNone(self.ops.adapt_datetimefield_value(None))
  57. def test_adapt_timefield_value(self):
  58. msg = 'Django does not support timezone-aware times.'
  59. with self.assertRaisesMessage(ValueError, msg):
  60. self.ops.adapt_timefield_value(timezone.make_aware(timezone.now()))
  61. @override_settings(USE_TZ=False)
  62. def test_adapt_timefield_value_unaware(self):
  63. now = timezone.now()
  64. self.assertEqual(self.ops.adapt_timefield_value(now), str(now))
  65. def test_date_extract_sql(self):
  66. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'date_extract_sql'):
  67. self.ops.date_extract_sql(None, None)
  68. def test_time_extract_sql(self):
  69. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'date_extract_sql'):
  70. self.ops.time_extract_sql(None, None)
  71. def test_date_interval_sql(self):
  72. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'date_interval_sql'):
  73. self.ops.date_interval_sql(None)
  74. def test_date_trunc_sql(self):
  75. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'date_trunc_sql'):
  76. self.ops.date_trunc_sql(None, None)
  77. def test_time_trunc_sql(self):
  78. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'time_trunc_sql'):
  79. self.ops.time_trunc_sql(None, None)
  80. def test_datetime_trunc_sql(self):
  81. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'datetime_trunc_sql'):
  82. self.ops.datetime_trunc_sql(None, None, None)
  83. def test_datetime_cast_date_sql(self):
  84. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'datetime_cast_date_sql'):
  85. self.ops.datetime_cast_date_sql(None, None)
  86. def test_datetime_cast_time_sql(self):
  87. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'datetime_cast_time_sql'):
  88. self.ops.datetime_cast_time_sql(None, None)
  89. def test_datetime_extract_sql(self):
  90. with self.assertRaisesMessage(NotImplementedError, self.may_require_msg % 'datetime_extract_sql'):
  91. self.ops.datetime_extract_sql(None, None, None)
  92. class DatabaseOperationTests(TestCase):
  93. def setUp(self):
  94. self.ops = BaseDatabaseOperations(connection=connection)
  95. @skipIfDBFeature('supports_over_clause')
  96. def test_window_frame_raise_not_supported_error(self):
  97. msg = 'This backend does not support window expressions.'
  98. with self.assertRaisesMessage(NotSupportedError, msg):
  99. self.ops.window_frame_rows_start_end()
  100. @skipIfDBFeature('can_distinct_on_fields')
  101. def test_distinct_on_fields(self):
  102. msg = 'DISTINCT ON fields is not supported by this database backend'
  103. with self.assertRaisesMessage(NotSupportedError, msg):
  104. self.ops.distinct_sql(['a', 'b'], None)
  105. @skipIfDBFeature('supports_temporal_subtraction')
  106. def test_subtract_temporals(self):
  107. duration_field = DurationField()
  108. duration_field_internal_type = duration_field.get_internal_type()
  109. msg = (
  110. 'This backend does not support %s subtraction.' %
  111. duration_field_internal_type
  112. )
  113. with self.assertRaisesMessage(NotSupportedError, msg):
  114. self.ops.subtract_temporals(duration_field_internal_type, None, None)
  115. class SqlFlushTests(TransactionTestCase):
  116. available_apps = ['backends']
  117. def test_sql_flush_no_tables(self):
  118. self.assertEqual(connection.ops.sql_flush(no_style(), []), [])
  119. def test_execute_sql_flush_statements(self):
  120. with transaction.atomic():
  121. author = Author.objects.create(name='George Orwell')
  122. Book.objects.create(author=author)
  123. author = Author.objects.create(name='Harper Lee')
  124. Book.objects.create(author=author)
  125. Book.objects.create(author=author)
  126. self.assertIs(Author.objects.exists(), True)
  127. self.assertIs(Book.objects.exists(), True)
  128. sql_list = connection.ops.sql_flush(
  129. no_style(),
  130. [Author._meta.db_table, Book._meta.db_table],
  131. reset_sequences=True,
  132. allow_cascade=True,
  133. )
  134. connection.ops.execute_sql_flush(sql_list)
  135. with transaction.atomic():
  136. self.assertIs(Author.objects.exists(), False)
  137. self.assertIs(Book.objects.exists(), False)
  138. if connection.features.supports_sequence_reset:
  139. author = Author.objects.create(name='F. Scott Fitzgerald')
  140. self.assertEqual(author.pk, 1)
  141. book = Book.objects.create(author=author)
  142. self.assertEqual(book.pk, 1)