-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from KiraPC/dev
Dev
- Loading branch information
Showing
9 changed files
with
335 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
"""FastAPI Router Contoller, FastAPI utility to allow Controller Class usage""" | ||
|
||
__version__ = "0.1.0" | ||
|
||
from fastapi_router_controller.lib.controller import Controller as Controller | ||
from fastapi_router_controller.lib.controller import OPEN_API_TAGS as ControllersTags | ||
from fastapi_router_controller.lib.controller_loader import ControllerLoader as ControllerLoader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class MultipleRouterException(Exception): | ||
def __init__(self): | ||
super().__init__('Router already used by another Controller') | ||
|
||
class MultipleResourceException(Exception): | ||
def __init__(self): | ||
super().__init__('Controller already used by another Resource') |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import unittest | ||
from fastapi import Depends, FastAPI, HTTPException, APIRouter | ||
from fastapi_router_controller import Controller | ||
from fastapi.testclient import TestClient | ||
|
||
|
||
router = APIRouter() | ||
controller = Controller(router, openapi_tag={"name": "sample_controller"}) | ||
|
||
|
||
def user_exists(user_id: int): | ||
if user_id <= 5: | ||
raise HTTPException(status_code=400, detail="No User") | ||
|
||
|
||
def user_is_id(user_id: int): | ||
if user_id == 6: | ||
raise HTTPException(status_code=400, detail="Not exact user") | ||
|
||
|
||
@controller.resource() | ||
class User: | ||
dependencies = [Depends(user_exists)] | ||
|
||
@controller.route.get("/users/{user_id}", dependencies=[Depends(user_is_id)]) | ||
def read_users(self, user_id: int): | ||
return {"user_id": user_id} | ||
|
||
|
||
def create_app(): | ||
app = FastAPI() | ||
|
||
app.include_router(User.router()) | ||
return app | ||
|
||
|
||
class TestRoutes(unittest.TestCase): | ||
def setUp(self): | ||
app = create_app() | ||
self.client = TestClient(app) | ||
|
||
def test_class_dep(self): | ||
response = self.client.get('/users/1') | ||
self.assertEqual(response.status_code, 400) | ||
self.assertEqual(response.json(), {'detail': 'No User'}) | ||
|
||
def test_func_dep(self): | ||
response = self.client.get('/users/6') | ||
self.assertEqual(response.status_code, 400) | ||
self.assertEqual(response.json(), {'detail': 'Not exact user'}) | ||
|
||
def test_pass(self): | ||
response = self.client.get('/users/7') | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.json(), {'user_id': 7}) |
Oops, something went wrong.