Skip to content

Commit

Permalink
✅ Make CRUDGenerator subclass ABC
Browse files Browse the repository at this point in the history
  • Loading branch information
awtkns committed Jan 27, 2022
1 parent 8aaa71f commit 2c18c90
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
4 changes: 2 additions & 2 deletions fastapi_crudrouter/core/_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from abc import abstractmethod
from abc import ABC, abstractmethod
from typing import Any, Callable, Generic, List, Optional, Type, Union

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


class CRUDGenerator(Generic[T], APIRouter):
class CRUDGenerator(Generic[T], APIRouter, ABC):
schema: Type[T]
create_schema: Type[T]
update_schema: Type[T]
Expand Down
2 changes: 1 addition & 1 deletion fastapi_crudrouter/core/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def schema_factory(
}

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


Expand Down
38 changes: 28 additions & 10 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from abc import ABC
from typing import Type

import pytest
from fastapi import APIRouter, FastAPI

Expand All @@ -14,13 +17,30 @@
from tests import Potato


def test_router_type():
assert issubclass(CRUDGenerator, APIRouter)
assert issubclass(SQLAlchemyCRUDRouter, APIRouter)
assert issubclass(MemoryCRUDRouter, APIRouter)
assert issubclass(OrmarCRUDRouter, APIRouter)
assert issubclass(GinoCRUDRouter, APIRouter)
assert issubclass(DatabasesCRUDRouter, APIRouter)
@pytest.fixture(
params=[
GinoCRUDRouter,
SQLAlchemyCRUDRouter,
MemoryCRUDRouter,
OrmarCRUDRouter,
GinoCRUDRouter,
DatabasesCRUDRouter,
]
)
def subclass(request) -> Type[CRUDGenerator]:
return request.param


def test_router_is_subclass_of_crud_generator(subclass):
assert issubclass(subclass, CRUDGenerator)


def test_router_is_subclass_of_api_router(subclass):
assert issubclass(subclass, APIRouter)


def test_base_class_is_abstract():
assert issubclass(CRUDGenerator, ABC)


def test_raise_not_implemented():
Expand All @@ -35,9 +55,7 @@ def bar():
methods = CRUDGenerator.get_routes()

for m in methods:
with pytest.raises(NotImplementedError):
with pytest.raises(TypeError):
app.include_router(CRUDGenerator(schema=Potato))

setattr(CRUDGenerator, f"_{m}", foo)

app.include_router(CRUDGenerator(schema=Potato))

0 comments on commit 2c18c90

Please sign in to comment.