Skip to content

Commit

Permalink
Merge pull request #137 from awtkns/abc
Browse files Browse the repository at this point in the history
Add Abstract annotation to base router
  • Loading branch information
awtkns authored Jan 27, 2022
2 parents 920ab61 + 2c18c90 commit 2670202
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
9 changes: 8 additions & 1 deletion fastapi_crudrouter/core/_base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from abc import ABC, abstractmethod
from typing import Any, Callable, Generic, List, Optional, Type, Union

from fastapi import APIRouter, HTTPException
Expand All @@ -9,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 Expand Up @@ -176,21 +177,27 @@ def remove_api_route(self, path: str, methods: List[str]) -> None:
):
self.routes.remove(route)

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

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

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

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

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

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

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
44 changes: 31 additions & 13 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
from abc import ABC
from typing import Type

import pytest
from fastapi import APIRouter, FastAPI

from fastapi_crudrouter import (
GinoCRUDRouter,
MemoryCRUDRouter,
OrmarCRUDRouter,
SQLAlchemyCRUDRouter,
DatabasesCRUDRouter,
)

# noinspection PyProtectedMember
from fastapi_crudrouter.core._base import CRUDGenerator

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)
@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_get_one():
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():
app = FastAPI()

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

return bar

foo()()

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))

1 comment on commit 2670202

@vercel
Copy link

@vercel vercel bot commented on 2670202 Jan 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.