refactoring
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from typing import Any
|
||||
|
||||
from aiohttp_pydantic import PydanticView
|
||||
from aiohttp.web import View
|
||||
|
||||
|
||||
def count_wrappers(obj: Any) -> int:
|
||||
@@ -16,43 +17,58 @@ def count_wrappers(obj: Any) -> int:
|
||||
raise RuntimeError("Too many wrappers")
|
||||
|
||||
|
||||
class ViewParent(PydanticView):
|
||||
class AiohttpViewParent(View):
|
||||
async def put(self):
|
||||
pass
|
||||
|
||||
async def delete(self):
|
||||
|
||||
class PydanticViewParent(PydanticView):
|
||||
async def get(self, id: int, /):
|
||||
pass
|
||||
|
||||
|
||||
class ViewParentNonPydantic:
|
||||
async def post(self):
|
||||
pass
|
||||
|
||||
|
||||
class ViewChild(ViewParent, ViewParentNonPydantic):
|
||||
async def get(self):
|
||||
pass
|
||||
|
||||
async def delete(self):
|
||||
pass
|
||||
|
||||
async def not_allowed(self):
|
||||
pass
|
||||
|
||||
|
||||
def test_allowed_methods_are_set_correctly():
|
||||
assert ViewParent.allowed_methods == {"PUT", "DELETE"}
|
||||
assert ViewChild.allowed_methods == {"GET", "POST", "PUT", "DELETE"}
|
||||
|
||||
|
||||
def test_allowed_methods_get_decorated_exactly_once():
|
||||
assert count_wrappers(ViewParent.put) == 1
|
||||
assert count_wrappers(ViewParent.delete) == 1
|
||||
assert count_wrappers(ViewChild.get) == 1
|
||||
assert count_wrappers(ViewChild.post) == 1
|
||||
assert count_wrappers(ViewChild.put) == 1
|
||||
assert count_wrappers(ViewChild.post) == 1
|
||||
assert count_wrappers(ViewChild.put) == 1
|
||||
|
||||
assert count_wrappers(ViewChild.not_allowed) == 0
|
||||
assert count_wrappers(ViewParentNonPydantic.post) == 0
|
||||
class ChildView(PydanticViewParent):
|
||||
async def post(self, id: int, /):
|
||||
pass
|
||||
|
||||
class SubChildView(ChildView):
|
||||
async def get(self, id: int, /):
|
||||
return super().get(id)
|
||||
|
||||
assert count_wrappers(ChildView.post) == 1
|
||||
assert count_wrappers(ChildView.get) == 1
|
||||
assert count_wrappers(SubChildView.post) == 1
|
||||
assert count_wrappers(SubChildView.get) == 1
|
||||
|
||||
|
||||
def test_methods_inherited_from_aiohttp_view_should_not_be_decorated():
|
||||
|
||||
class ChildView(AiohttpViewParent, PydanticView):
|
||||
async def post(self, id: int, /):
|
||||
pass
|
||||
|
||||
assert count_wrappers(ChildView.put) == 0
|
||||
assert count_wrappers(ChildView.post) == 1
|
||||
|
||||
|
||||
def test_allowed_methods_are_set_correctly():
|
||||
|
||||
class ChildView(AiohttpViewParent, PydanticView):
|
||||
async def post(self, id: int, /):
|
||||
pass
|
||||
|
||||
assert ChildView.allowed_methods == {"POST", "PUT"}
|
||||
|
||||
class ChildView(PydanticViewParent):
|
||||
async def post(self, id: int, /):
|
||||
pass
|
||||
|
||||
assert ChildView.allowed_methods == {"POST", "GET"}
|
||||
|
||||
class ChildView(AiohttpViewParent, PydanticViewParent):
|
||||
async def post(self, id: int, /):
|
||||
pass
|
||||
|
||||
assert ChildView.allowed_methods == {"POST", "PUT", "GET"}
|
||||
|
||||
Reference in New Issue
Block a user