Skip to content

Commit 2c18c90

Browse files
committed
✅ Make CRUDGenerator subclass ABC
1 parent 8aaa71f commit 2c18c90

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

fastapi_crudrouter/core/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from abc import abstractmethod
1+
from abc import ABC, abstractmethod
22
from typing import Any, Callable, Generic, List, Optional, Type, Union
33

44
from fastapi import APIRouter, HTTPException
@@ -10,7 +10,7 @@
1010
NOT_FOUND = HTTPException(404, "Item not found")
1111

1212

13-
class CRUDGenerator(Generic[T], APIRouter):
13+
class CRUDGenerator(Generic[T], APIRouter, ABC):
1414
schema: Type[T]
1515
create_schema: Type[T]
1616
update_schema: Type[T]

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: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from abc import ABC
2+
from typing import Type
3+
14
import pytest
25
from fastapi import APIRouter, FastAPI
36

@@ -14,13 +17,30 @@
1417
from tests import Potato
1518

1619

17-
def test_router_type():
18-
assert issubclass(CRUDGenerator, APIRouter)
19-
assert issubclass(SQLAlchemyCRUDRouter, APIRouter)
20-
assert issubclass(MemoryCRUDRouter, APIRouter)
21-
assert issubclass(OrmarCRUDRouter, APIRouter)
22-
assert issubclass(GinoCRUDRouter, APIRouter)
23-
assert issubclass(DatabasesCRUDRouter, 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)
36+
37+
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)
2444

2545

2646
def test_raise_not_implemented():
@@ -35,9 +55,7 @@ def bar():
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)