| 1 |
import pytest |
| 2 |
from django.db import models |
| 3 |
|
| 4 |
from ninja import ModelSchema |
| 5 |
from ninja.errors import ConfigError |
| 6 |
|
| 7 |
|
| 8 |
def test_simple(): |
| 9 |
class User(models.Model): |
| 10 |
firstname = models.CharField() |
| 11 |
lastname = models.CharField(blank=True, null=True) |
| 12 |
|
| 13 |
class Meta: |
| 14 |
app_label = "tests" |
| 15 |
|
| 16 |
class SampleSchema(ModelSchema): |
| 17 |
class Meta: |
| 18 |
model = User |
| 19 |
fields = ["firstname", "lastname"] |
| 20 |
|
| 21 |
def hello(self): |
| 22 |
return f"Hello({self.firstname})" |
| 23 |
|
| 24 |
assert SampleSchema.json_schema() == { |
| 25 |
"title": "SampleSchema", |
| 26 |
"type": "object", |
| 27 |
"properties": { |
| 28 |
"firstname": {"title": "Firstname", "type": "string"}, |
| 29 |
"lastname": { |
| 30 |
"anyOf": [{"type": "string"}, {"type": "null"}], |
| 31 |
"title": "Lastname", |
| 32 |
}, |
| 33 |
}, |
| 34 |
"required": ["firstname"], |
| 35 |
} |
| 36 |
|
| 37 |
assert SampleSchema(firstname="ninja", lastname="Django").hello() == "Hello(ninja)" |
| 38 |
|
| 39 |
|
| 40 |
class SampleSchema2(ModelSchema): |
| 41 |
class Meta: |
| 42 |
model = User |
| 43 |
exclude = ["lastname"] |
| 44 |
|
| 45 |
assert SampleSchema2.json_schema() == { |
| 46 |
"title": "SampleSchema2", |
| 47 |
"type": "object", |
| 48 |
"properties": { |
| 49 |
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"}, |
| 50 |
"firstname": {"title": "Firstname", "type": "string"}, |
| 51 |
}, |
| 52 |
"required": ["firstname"], |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
def test_custom(): |
| 57 |
class CustomModel(models.Model): |
| 58 |
f1 = models.CharField() |
| 59 |
f2 = models.CharField(blank=True, null=True) |
| 60 |
|
| 61 |
class Meta: |
| 62 |
app_label = "tests" |
| 63 |
|
| 64 |
class CustomSchema(ModelSchema): |
| 65 |
f3: int |
| 66 |
f4: int = 1 |
| 67 |
_private: str = "<secret>" |
| 68 |
|
| 69 |
class Meta: |
| 70 |
model = CustomModel |
| 71 |
fields = ["f1", "f2"] |
| 72 |
|
| 73 |
assert CustomSchema.json_schema() == { |
| 74 |
"title": "CustomSchema", |
| 75 |
"type": "object", |
| 76 |
"properties": { |
| 77 |
"f1": {"title": "F1", "type": "string"}, |
| 78 |
"f2": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "F2"}, |
| 79 |
"f3": {"title": "F3", "type": "integer"}, |
| 80 |
"f4": {"title": "F4", "default": 1, "type": "integer"}, |
| 81 |
}, |
| 82 |
"required": ["f3", "f1"], |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
def test_config(): |
| 87 |
class Category(models.Model): |
| 88 |
title = models.CharField() |
| 89 |
|
| 90 |
class Meta: |
| 91 |
app_label = "tests" |
| 92 |
|
| 93 |
with pytest.raises(ConfigError): |
| 94 |
|
| 95 |
class CategorySchema(ModelSchema): |
| 96 |
class Meta: |
| 97 |
model = Category |
| 98 |
|
| 99 |
|
| 100 |
def test_optional(): |
| 101 |
class OptModel(models.Model): |
| 102 |
title = models.CharField() |
| 103 |
other = models.CharField(null=True) |
| 104 |
extra = models.IntegerField() |
| 105 |
count = models.IntegerField(default=0, null=True) |
| 106 |
|
| 107 |
class Meta: |
| 108 |
app_label = "tests" |
| 109 |
|
| 110 |
class OptSchema(ModelSchema): |
| 111 |
class Meta: |
| 112 |
model = OptModel |
| 113 |
fields = "__all__" |
| 114 |
fields_optional = ["title"] |
| 115 |
|
| 116 |
class OptSchema2(ModelSchema): |
| 117 |
class Meta: |
| 118 |
model = OptModel |
| 119 |
fields = "__all__" |
| 120 |
fields_optional = "__all__" |
| 121 |
|
| 122 |
assert OptSchema.json_schema().get("required") == ["extra"] |
| 123 |
assert OptSchema.json_schema()["properties"] == { |
| 124 |
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"}, |
| 125 |
"title": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Title"}, |
| 126 |
"other": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Other"}, |
| 127 |
"extra": {"title": "Extra", "type": "integer"}, |
| 128 |
"count": { |
| 129 |
"anyOf": [{"type": "integer"}, {"type": "null"}], |
| 130 |
"default": 0, |
| 131 |
"title": "Count", |
| 132 |
}, |
| 133 |
} |
| 134 |
|
| 135 |
assert OptSchema2.json_schema().get("required") is None |
| 136 |
assert OptSchema2.json_schema()["properties"] == { |
| 137 |
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"}, |
| 138 |
"title": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Title"}, |
| 139 |
"other": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Other"}, |
| 140 |
"extra": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "Extra"}, |
| 141 |
"count": { |
| 142 |
"anyOf": [{"type": "integer"}, {"type": "null"}], |
| 143 |
"default": 0, |
| 144 |
"title": "Count", |
| 145 |
}, |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
def test_fields_all(): |
| 150 |
class SomeModel(models.Model): |
| 151 |
field1 = models.CharField() |
| 152 |
field2 = models.CharField(blank=True, null=True) |
| 153 |
|
| 154 |
class Meta: |
| 155 |
app_label = "tests" |
| 156 |
|
| 157 |
class SomeSchema(ModelSchema): |
| 158 |
class Meta: |
| 159 |
model = SomeModel |
| 160 |
fields = "__all__" |
| 161 |
|
| 162 |
print(SomeSchema.json_schema()) |
| 163 |
assert SomeSchema.json_schema() == { |
| 164 |
"title": "SomeSchema", |
| 165 |
"type": "object", |
| 166 |
"properties": { |
| 167 |
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"}, |
| 168 |
"field1": {"title": "Field1", "type": "string"}, |
| 169 |
"field2": { |
| 170 |
"anyOf": [{"type": "string"}, {"type": "null"}], |
| 171 |
"title": "Field2", |
| 172 |
}, |
| 173 |
}, |
| 174 |
"required": ["field1"], |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
def test_model_as_string_valid(): |
| 179 |
class SomeModel(models.Model): |
| 180 |
field1 = models.CharField() |
| 181 |
|
| 182 |
class Meta: |
| 183 |
app_label = "someapp" |
| 184 |
|
| 185 |
class SomeStringModelSchema(ModelSchema): |
| 186 |
class Meta: |
| 187 |
model = "someapp.SomeModel" |
| 188 |
fields = ["field1"] |
| 189 |
|
| 190 |
assert SomeStringModelSchema.json_schema() == { |
| 191 |
"title": "SomeStringModelSchema", |
| 192 |
"type": "object", |
| 193 |
"properties": {"field1": {"title": "Field1", "type": "string"}}, |
| 194 |
"required": ["field1"], |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
def test_model_as_string_invalid_format(): |
| 199 |
with pytest.raises( |
| 200 |
ValueError, |
| 201 |
match="Model string must be in format 'app_label.ModelName', got: SomeModel", |
| 202 |
): |
| 203 |
|
| 204 |
class SomeSchema(ModelSchema): |
| 205 |
class Meta: |
| 206 |
model = "SomeModel" |
| 207 |
fields = ["field1"] |
| 208 |
|
| 209 |
|
| 210 |
def test_model_as_string_invalid_format2(): |
| 211 |
with pytest.raises( |
| 212 |
ValueError, |
| 213 |
match="Model string must be in format 'app_label.ModelName', got: someapp.models.SomeModel", |
| 214 |
): |
| 215 |
|
| 216 |
class SomeSchema(ModelSchema): |
| 217 |
class Meta: |
| 218 |
model = "someapp.models.SomeModel" |
| 219 |
fields = ["field1"] |
| 220 |
|
| 221 |
|
| 222 |
def test_model_as_string_nonexistent_app(): |
| 223 |
with pytest.raises( |
| 224 |
LookupError, |
| 225 |
match="No installed app with label 'nonexistentapp'.", |
| 226 |
): |
| 227 |
|
| 228 |
class SomeSchema(ModelSchema): |
| 229 |
class Meta: |
| 230 |
model = "nonexistentapp.NonExistentModel" |
| 231 |
fields = ["field1"] |
| 232 |
|
| 233 |
|
| 234 |
def test_model_as_string_nonexistent_model_in_app(): |
| 235 |
with pytest.raises( |
| 236 |
LookupError, |
| 237 |
match="App 'someapp' doesn't have a 'NonExistentModel' model.", |
| 238 |
): |
| 239 |
|
| 240 |
class SomeSchema(ModelSchema): |
| 241 |
class Meta: |
| 242 |
model = "someapp.NonExistentModel" |
| 243 |
fields = ["field1"] |
| 244 |
|
| 245 |
|
| 246 |
def test_model_schema_without_meta(): |
| 247 |
with pytest.raises( |
| 248 |
ConfigError, |
| 249 |
match=r"ModelSchema class 'NoMetaSchema' requires a 'Meta' subclass", |
| 250 |
): |
| 251 |
|
| 252 |
class NoMetaSchema(ModelSchema): |
| 253 |
x: int |
| 254 |
|
| 255 |
|
| 256 |
def test_model_schema_with_old_config(): |
| 257 |
with pytest.raises( |
| 258 |
ConfigError, |
| 259 |
match=r"The use of `Config` class is removed for ModelSchema, use 'Meta' instead", |
| 260 |
): |
| 261 |
|
| 262 |
class OldConfigSchema(ModelSchema): |
| 263 |
x: int |
| 264 |
|
| 265 |
class Config: |
| 266 |
model = "User" |
| 267 |
model_fields = ["x"] |