Skip to content

Commit da57412

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents c540132 + 2670202 commit da57412

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

fastapi_crudrouter/core/_base.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from abc import ABC, abstractmethod
12
from typing import Any, Callable, Generic, List, Optional, Type, Union
23

34
from fastapi import APIRouter, HTTPException
@@ -9,7 +10,7 @@
910
NOT_FOUND = HTTPException(404, "Item not found")
1011

1112

12-
class CRUDGenerator(Generic[T], APIRouter):
13+
class CRUDGenerator(Generic[T], APIRouter, ABC):
1314
schema: Type[T]
1415
create_schema: Type[T]
1516
update_schema: Type[T]
@@ -176,21 +177,27 @@ def remove_api_route(self, path: str, methods: List[str]) -> None:
176177
):
177178
self.routes.remove(route)
178179

180+
@abstractmethod
179181
def _get_all(self, *args: Any, **kwargs: Any) -> Callable[..., Any]:
180182
raise NotImplementedError
181183

184+
@abstractmethod
182185
def _get_one(self, *args: Any, **kwargs: Any) -> Callable[..., Any]:
183186
raise NotImplementedError
184187

188+
@abstractmethod
185189
def _create(self, *args: Any, **kwargs: Any) -> Callable[..., Any]:
186190
raise NotImplementedError
187191

192+
@abstractmethod
188193
def _update(self, *args: Any, **kwargs: Any) -> Callable[..., Any]:
189194
raise NotImplementedError
190195

196+
@abstractmethod
191197
def _delete_one(self, *args: Any, **kwargs: Any) -> Callable[..., Any]:
192198
raise NotImplementedError
193199

200+
@abstractmethod
194201
def _delete_all(self, *args: Any, **kwargs: Any) -> Callable[..., Any]:
195202
raise NotImplementedError
196203

fastapi_crudrouter/core/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def schema_factory(
3333
}
3434

3535
name = schema_cls.__name__ + name
36-
schema = create_model(__model_name=name, **fields) # type: ignore
36+
schema: Type[T] = create_model(__model_name=name, **fields) # type: ignore
3737
return schema
3838

3939

tests/test_base.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,49 @@
1+
from abc import ABC
2+
from typing import Type
3+
14
import pytest
25
from fastapi import APIRouter, FastAPI
6+
37
from fastapi_crudrouter import (
48
GinoCRUDRouter,
59
MemoryCRUDRouter,
610
OrmarCRUDRouter,
711
SQLAlchemyCRUDRouter,
12+
DatabasesCRUDRouter,
813
)
914

1015
# noinspection PyProtectedMember
1116
from fastapi_crudrouter.core._base import CRUDGenerator
12-
1317
from tests import Potato
1418

1519

16-
def test_router_type():
17-
assert issubclass(CRUDGenerator, APIRouter)
18-
assert issubclass(SQLAlchemyCRUDRouter, APIRouter)
19-
assert issubclass(MemoryCRUDRouter, APIRouter)
20-
assert issubclass(OrmarCRUDRouter, APIRouter)
21-
assert issubclass(GinoCRUDRouter, APIRouter)
20+
@pytest.fixture(
21+
params=[
22+
GinoCRUDRouter,
23+
SQLAlchemyCRUDRouter,
24+
MemoryCRUDRouter,
25+
OrmarCRUDRouter,
26+
GinoCRUDRouter,
27+
DatabasesCRUDRouter,
28+
]
29+
)
30+
def subclass(request) -> Type[CRUDGenerator]:
31+
return request.param
32+
33+
34+
def test_router_is_subclass_of_crud_generator(subclass):
35+
assert issubclass(subclass, CRUDGenerator)
2236

2337

24-
def test_get_one():
38+
def test_router_is_subclass_of_api_router(subclass):
39+
assert issubclass(subclass, APIRouter)
40+
41+
42+
def test_base_class_is_abstract():
43+
assert issubclass(CRUDGenerator, ABC)
44+
45+
46+
def test_raise_not_implemented():
2547
app = FastAPI()
2648

2749
def foo(*args, **kwargs):
@@ -30,14 +52,10 @@ def bar():
3052

3153
return bar
3254

33-
foo()()
34-
3555
methods = CRUDGenerator.get_routes()
3656

3757
for m in methods:
38-
with pytest.raises(NotImplementedError):
58+
with pytest.raises(TypeError):
3959
app.include_router(CRUDGenerator(schema=Potato))
4060

4161
setattr(CRUDGenerator, f"_{m}", foo)
42-
43-
app.include_router(CRUDGenerator(schema=Potato))

0 commit comments

Comments
 (0)