Skip to content

Commit 17e08c1

Browse files
authored
Merge pull request #88 from eadwinCode/restore_test_coverage
restored test coverage to 100%
2 parents 9dd27c1 + 5867b2b commit 17e08c1

File tree

5 files changed

+108
-31
lines changed

5 files changed

+108
-31
lines changed

ninja_extra/controllers/model/builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def _register_list_endpoint(self) -> None:
145145
pagination_class=self._config.pagination.klass,
146146
pagination_response_schema=self._config.pagination.pagination_schema,
147147
)
148-
if self._config.pagination.paginator_kwargs:
148+
if self._config.pagination.paginator_kwargs: # pragma: no cover
149149
paginate_kwargs.update(self._config.pagination.paginator_kwargs)
150150

151151
list_items = ModelEndpointFactory.list(

ninja_extra/controllers/model/endpoints.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def update_item(
287287
else self.service.get_one(pk=pk, **kwargs)
288288
)
289289

290-
if not obj:
290+
if not obj: # pragma: no cover
291291
raise NotFound()
292292

293293
self.check_object_permissions(obj)
@@ -362,7 +362,7 @@ def patch_item(
362362
if object_getter
363363
else self.service.get_one(pk=pk, **kwargs)
364364
)
365-
if not obj:
365+
if not obj: # pragma: no cover
366366
raise NotFound()
367367
self.check_object_permissions(obj)
368368
instance = (
@@ -430,7 +430,7 @@ def get_item(self: "ModelControllerBase", **kwargs: t.Any) -> t.Any:
430430
if object_getter
431431
else self.service.get_one(pk=pk, **kwargs)
432432
)
433-
if not obj:
433+
if not obj: # pragma: no cover
434434
raise NotFound()
435435
self.check_object_permissions(obj)
436436
return obj
@@ -579,7 +579,7 @@ def delete_item(self: "ModelControllerBase", **kwargs: t.Any) -> t.Any:
579579
if object_getter
580580
else self.service.get_one(pk=pk, **kwargs)
581581
)
582-
if not obj:
582+
if not obj: # pragma: no cover
583583
raise NotFound()
584584
self.check_object_permissions(obj)
585585
custom_handler(

ninja_extra/controllers/model/schemas.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ class ModelPagination(PydanticModel):
3232
paginator_kwargs: Optional[dict] = None
3333
pagination_schema: Type[PydanticModel] = PaginatedResponseSchema
3434

35-
@validator("klass", allow_reuse=True)
36-
def validate_klass(cls, value: Any) -> Any:
37-
if isinstance(value, type) and issubclass(value, PaginationBase):
38-
return value
39-
raise ValueError(f"{value} is not of type `PaginationBase`")
40-
4135
@validator("pagination_schema", allow_reuse=True)
4236
def validate_schema(cls, value: Any) -> Any:
4337
if (
@@ -106,24 +100,6 @@ def validate_allow_routes(cls, value: List[Any]) -> Any:
106100
raise ValueError(f"'{item}' action is not recognized in [{defaults}]")
107101
return value
108102

109-
@validator("model", allow_reuse=True)
110-
def validate_model(cls, value: Any) -> Any:
111-
if value and hasattr(value, "objects"):
112-
return value
113-
raise ValueError(f"{value} is not a valid Django model.")
114-
115-
@validator(
116-
"create_schema",
117-
"retrieve_schema",
118-
"update_schema",
119-
"patch_schema",
120-
allow_reuse=True,
121-
)
122-
def validate_schemas(cls, value: Any) -> Any:
123-
if value and not issubclass(value, PydanticModel):
124-
raise ValueError(f"{value} is not a valid pydantic type.")
125-
return value
126-
127103
def __init__(self, **kwargs: Any) -> None:
128104
super().__init__(**kwargs)
129105
self.generate_all_schema()
@@ -162,7 +138,7 @@ def generate_all_schema(self) -> None:
162138
if self.schema_config.include == "__all__":
163139
working_fields = set(all_fields)
164140

165-
elif self.schema_config.include and self.schema_config.include != "__all__":
141+
if self.schema_config.include and self.schema_config.include != "__all__":
166142
include_fields = set(self.schema_config.include)
167143
working_fields = include_fields
168144

tests/test_model_controller/test_model_controller_configurations.py

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
2+
from ninja_schema.errors import ConfigError
23

3-
from ninja_extra import ModelConfig, ModelPagination
4+
from ninja_extra import ModelConfig, ModelPagination, ModelSchemaConfig
45
from ninja_extra.pagination import PageNumberPaginationExtra
56
from ninja_extra.schemas import PaginatedResponseSchema, RouteParameter
67

@@ -42,6 +43,98 @@ def test_default_model_config():
4243
}
4344

4445

46+
def test_include_gen_schema():
47+
model_config = ModelConfig(
48+
model=Event,
49+
allowed_routes=["list", "find_one"],
50+
schema_config=ModelSchemaConfig(include=["title", "start_date", "end_date"]),
51+
)
52+
assert model_config.create_schema is None
53+
assert model_config.patch_schema is None
54+
assert model_config.update_schema is None
55+
56+
assert model_config.retrieve_schema.schema() == {
57+
"title": "EventSchema",
58+
"type": "object",
59+
"properties": {
60+
"id": {"title": "Id", "type": "integer"},
61+
"title": {"title": "Title", "maxLength": 100, "type": "string"},
62+
"start_date": {"title": "Start Date", "type": "string", "format": "date"},
63+
"end_date": {"title": "End Date", "type": "string", "format": "date"},
64+
},
65+
"required": ["id", "title", "start_date", "end_date"],
66+
}
67+
68+
69+
def test_exclude_gen_schema():
70+
model_config = ModelConfig(
71+
model=Event,
72+
allowed_routes=["list", "find_one"],
73+
schema_config=ModelSchemaConfig(exclude=["start_date", "end_date"]),
74+
)
75+
assert model_config.create_schema is None
76+
assert model_config.patch_schema is None
77+
assert model_config.update_schema is None
78+
79+
assert model_config.retrieve_schema.schema() == {
80+
"properties": {
81+
"category_id": {"title": "Category", "type": "integer"},
82+
"id": {"title": "Id", "type": "integer"},
83+
"title": {"maxLength": 100, "title": "Title", "type": "string"},
84+
},
85+
"required": ["id", "title"],
86+
"title": "EventSchema",
87+
"type": "object",
88+
}
89+
90+
91+
def test_update_schema_created_in_absence_of_create_schema():
92+
model_config = ModelConfig(
93+
model=Event,
94+
allowed_routes=["update", "patch"],
95+
schema_config=ModelSchemaConfig(include=["title", "start_date", "end_date"]),
96+
)
97+
assert model_config.create_schema is None
98+
assert model_config.retrieve_schema is not None
99+
assert model_config.patch_schema is not None
100+
assert model_config.update_schema is not None
101+
102+
103+
def test_create_schema_invalid_key():
104+
model_config = ModelConfig(
105+
model=Event,
106+
allowed_routes=["create"],
107+
schema_config=ModelSchemaConfig(include=["title", "start_date", "end_date"]),
108+
)
109+
assert model_config.create_schema is not None
110+
assert model_config.retrieve_schema is not None
111+
112+
assert model_config.update_schema is None
113+
assert model_config.patch_schema is None
114+
115+
with pytest.raises(ConfigError):
116+
ModelConfig(
117+
model=Event,
118+
allowed_routes=["update", "patch"],
119+
schema_config=ModelSchemaConfig(
120+
include=["title", "start_date", "end_date"],
121+
write_only_fields=["invalid"],
122+
),
123+
)
124+
125+
126+
def test_retrieve_schema_invalid_key():
127+
with pytest.raises(ConfigError):
128+
ModelConfig(
129+
model=Event,
130+
allowed_routes=["update", "patch"],
131+
schema_config=ModelSchemaConfig(
132+
include=["title", "start_date", "end_date"],
133+
read_only_fields=["invalid"],
134+
),
135+
)
136+
137+
45138
def test_invalid_django_model():
46139
with pytest.raises(ValueError):
47140
ModelConfig(model=InvalidTypeORSchema)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pytest
2+
3+
from ninja_extra import ModelEndpointFactory
4+
5+
6+
def test_path_parameter_duplicate():
7+
with pytest.raises(ValueError):
8+
ModelEndpointFactory.delete(path="/{int:ex}/somewhere/{ex}", lookup_param="any")

0 commit comments

Comments
 (0)