Skip to content

Commit

Permalink
Refs django#470 -- Fixed field_defaults test failures due to year-end…
Browse files Browse the repository at this point in the history
… boundary conditions.
  • Loading branch information
jacobtylerwalls authored Jan 23, 2025
1 parent f133285 commit 352d860
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions tests/field_defaults/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
)
from django.db.models.functions import Collate
from django.db.models.lookups import GreaterThan
from django.test import SimpleTestCase, TestCase, skipIfDBFeature, skipUnlessDBFeature
from django.test import (
SimpleTestCase,
TestCase,
override_settings,
skipIfDBFeature,
skipUnlessDBFeature,
)
from django.utils import timezone

from .models import (
Article,
Expand Down Expand Up @@ -69,12 +76,13 @@ def test_null_db_default(self):
self.assertIsNone(obj2.null)

@skipUnlessDBFeature("supports_expression_defaults")
@override_settings(USE_TZ=True)
def test_db_default_function(self):
m = DBDefaultsFunction.objects.create()
if not connection.features.can_return_columns_from_insert:
m.refresh_from_db()
self.assertAlmostEqual(m.number, pi)
self.assertEqual(m.year, datetime.now().year)
self.assertEqual(m.year, timezone.now().year)
self.assertAlmostEqual(m.added, pi + 4.5)
self.assertEqual(m.multiple_subfunctions, 4.5)

Expand Down Expand Up @@ -163,12 +171,13 @@ def test_bulk_create_mixed_db_defaults(self):
self.assertCountEqual(headlines, ["Default headline", "Something else"])

@skipUnlessDBFeature("supports_expression_defaults")
@override_settings(USE_TZ=True)
def test_bulk_create_mixed_db_defaults_function(self):
instances = [DBDefaultsFunction(), DBDefaultsFunction(year=2000)]
DBDefaultsFunction.objects.bulk_create(instances)

years = DBDefaultsFunction.objects.values_list("year", flat=True)
self.assertCountEqual(years, [2000, datetime.now().year])
self.assertCountEqual(years, [2000, timezone.now().year])

def test_full_clean(self):
obj = DBArticle()
Expand Down

0 comments on commit 352d860

Please sign in to comment.