@@ -93,12 +93,49 @@ class Category(models.Model):
93
93
class Meta :
94
94
app_label = "tests"
95
95
96
- with pytest .raises (ConfigError , match = "Specify either `exclude` or `fields`" ):
96
+ with pytest .raises (ValidationError , match = "Specify either `exclude` or `fields`" ):
97
97
98
- class CategorySchema (ModelSchema ):
98
+ class CategorySchema1 (ModelSchema ):
99
99
class Meta :
100
100
model = Category
101
101
102
+ with pytest .raises (ValidationError , match = "Specify either `exclude` or `fields`" ):
103
+
104
+ class CategorySchema2 (ModelSchema ):
105
+ class Meta :
106
+ model = Category
107
+ exclude = ["title" ]
108
+ fields = ["title" ]
109
+
110
+ with pytest .raises (
111
+ ValidationError ,
112
+ match = "Use only `optional_fields`, `fields_optional` is deprecated." ,
113
+ ):
114
+
115
+ class CategorySchema3 (ModelSchema ):
116
+ class Meta :
117
+ model = Category
118
+ fields = "__all__"
119
+ fields_optional = ["title" ]
120
+ optional_fields = ["title" ]
121
+
122
+ with pytest .raises (
123
+ ConfigError ,
124
+ match = "'title' is defined in class body and in Meta.fields or implicitly in Meta.excluded" ,
125
+ ):
126
+
127
+ class CategorySchema4 (ModelSchema ):
128
+ title : str
129
+
130
+ class Meta :
131
+ model = Category
132
+ fields = "__all__"
133
+
134
+ class CategorySchema5 (ModelSchema ):
135
+ class Config :
136
+ model = Category
137
+ fields = "__all__"
138
+
102
139
103
140
def test_optional ():
104
141
class OptModel (models .Model ):
@@ -179,15 +216,14 @@ class Meta:
179
216
180
217
181
218
def test_model_schema_without_config ():
219
+ # do not raise on creation of class
220
+ class NoConfigSchema (ModelSchema ):
221
+ x : int
222
+
182
223
with pytest .raises (
183
224
ConfigError ,
184
225
match = r"No model set for class 'NoConfigSchema'" ,
185
226
):
186
- # do not raise on creation of class
187
- class NoConfigSchema (ModelSchema ):
188
- x : int
189
-
190
- # instead raise in instantiation
191
227
NoConfigSchema (x = 1 )
192
228
193
229
0 commit comments