|
4 | 4 | from django.contrib.admin.options import VERTICAL, ModelAdmin, TabularInline
|
5 | 5 | from django.contrib.admin.sites import AdminSite
|
6 | 6 | from django.core.checks import Error
|
| 7 | +from django.db import models |
7 | 8 | from django.db.models import CASCADE, F, Field, ForeignKey, ManyToManyField, Model
|
8 | 9 | from django.db.models.functions import Upper
|
9 | 10 | from django.forms.models import BaseModelFormSet
|
10 |
| -from django.test import SimpleTestCase |
| 11 | +from django.test import TestCase, skipUnlessDBFeature |
11 | 12 | from django.test.utils import isolate_apps
|
12 | 13 |
|
13 | 14 | from .models import Band, Song, User, ValidationTestInlineModel, ValidationTestModel
|
14 | 15 |
|
15 | 16 |
|
16 |
| -class CheckTestCase(SimpleTestCase): |
| 17 | +class CheckTestCase(TestCase): |
17 | 18 | def assertIsInvalid(
|
18 | 19 | self,
|
19 | 20 | model_admin,
|
@@ -97,6 +98,29 @@ class TestModelAdmin(ModelAdmin):
|
97 | 98 |
|
98 | 99 | self.assertIsValid(TestModelAdmin, ValidationTestModel)
|
99 | 100 |
|
| 101 | + @isolate_apps("modeladmin") |
| 102 | + def assertGeneratedDateTimeFieldIsValid(self, *, db_persist): |
| 103 | + class TestModel(Model): |
| 104 | + date = models.DateTimeField() |
| 105 | + date_copy = models.GeneratedField( |
| 106 | + expression=F("date"), |
| 107 | + output_field=models.DateTimeField(), |
| 108 | + db_persist=db_persist, |
| 109 | + ) |
| 110 | + |
| 111 | + class TestModelAdmin(ModelAdmin): |
| 112 | + date_hierarchy = "date_copy" |
| 113 | + |
| 114 | + self.assertIsValid(TestModelAdmin, TestModel) |
| 115 | + |
| 116 | + @skipUnlessDBFeature("supports_stored_generated_columns") |
| 117 | + def test_valid_case_stored_generated_field(self): |
| 118 | + self.assertGeneratedDateTimeFieldIsValid(db_persist=True) |
| 119 | + |
| 120 | + @skipUnlessDBFeature("supports_virtual_generated_columns") |
| 121 | + def test_valid_case_virtual_generated_field(self): |
| 122 | + self.assertGeneratedDateTimeFieldIsValid(db_persist=False) |
| 123 | + |
100 | 124 | def test_field_attname(self):
|
101 | 125 | class TestModelAdmin(ModelAdmin):
|
102 | 126 | raw_id_fields = ["band_id"]
|
@@ -1016,6 +1040,33 @@ class TestModelAdmin(ModelAdmin):
|
1016 | 1040 | "admin.E128",
|
1017 | 1041 | )
|
1018 | 1042 |
|
| 1043 | + @isolate_apps("modeladmin") |
| 1044 | + def assertGeneratedIntegerFieldIsInvalid(self, *, db_persist): |
| 1045 | + class TestModel(Model): |
| 1046 | + generated = models.GeneratedField( |
| 1047 | + expression=models.Value(1), |
| 1048 | + output_field=models.IntegerField(), |
| 1049 | + db_persist=db_persist, |
| 1050 | + ) |
| 1051 | + |
| 1052 | + class TestModelAdmin(ModelAdmin): |
| 1053 | + date_hierarchy = "generated" |
| 1054 | + |
| 1055 | + self.assertIsInvalid( |
| 1056 | + TestModelAdmin, |
| 1057 | + TestModel, |
| 1058 | + "The value of 'date_hierarchy' must be a DateField or DateTimeField.", |
| 1059 | + "admin.E128", |
| 1060 | + ) |
| 1061 | + |
| 1062 | + @skipUnlessDBFeature("supports_stored_generated_columns") |
| 1063 | + def test_related_invalid_field_type_stored_generated_field(self): |
| 1064 | + self.assertGeneratedIntegerFieldIsInvalid(db_persist=True) |
| 1065 | + |
| 1066 | + @skipUnlessDBFeature("supports_virtual_generated_columns") |
| 1067 | + def test_related_invalid_field_type_virtual_generated_field(self): |
| 1068 | + self.assertGeneratedIntegerFieldIsInvalid(db_persist=False) |
| 1069 | + |
1019 | 1070 | def test_valid_case(self):
|
1020 | 1071 | class TestModelAdmin(ModelAdmin):
|
1021 | 1072 | date_hierarchy = "pub_date"
|
|
0 commit comments