Add skeleton app with namespaces
This commit is contained in:
9
app/widget/__init__.py
Normal file
9
app/widget/__init__.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from .model import Widget # noqa
|
||||
from .schema import WidgetSchema # noqa
|
||||
BASE_ROUTE = 'widget'
|
||||
|
||||
|
||||
def register_routes(root_api, root='api'):
|
||||
from .controller import api as widget_api
|
||||
root_api.add_namespace(widget_api, path=f'/{root}/{BASE_ROUTE}')
|
||||
return root_api
|
56
app/widget/controller.py
Normal file
56
app/widget/controller.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from flask import request
|
||||
from flask_accepts import accepts, responds
|
||||
from flask_restplus import Namespace, Resource
|
||||
from flask.wrappers import Response
|
||||
from typing import List
|
||||
|
||||
from .schema import WidgetSchema
|
||||
from .service import WidgetService
|
||||
from .model import Widget
|
||||
from .interface import WidgetInterface
|
||||
|
||||
api = Namespace('Widget', description='Single namespace, single entity') # noqa
|
||||
|
||||
|
||||
@api.route('/')
|
||||
class WidgetResource(Resource):
|
||||
'''Widgets'''
|
||||
|
||||
@responds(schema=WidgetSchema, many=True)
|
||||
def get(self) -> List[Widget]:
|
||||
'''Get all Widgets'''
|
||||
|
||||
return WidgetService.get_all()
|
||||
|
||||
@accepts(schema=WidgetSchema, api=api)
|
||||
@responds(schema=WidgetSchema)
|
||||
def post(self) -> Widget:
|
||||
'''Create a Single Widget'''
|
||||
|
||||
return WidgetService.create(request.parsed_obj)
|
||||
|
||||
|
||||
@api.route('/<int:widgetId>')
|
||||
@api.param('widgetId', 'Widget database ID')
|
||||
class WidgetIdResource(Resource):
|
||||
@responds(schema=WidgetSchema)
|
||||
def get(self, widgetId: int) -> Widget:
|
||||
'''Get Single Widget'''
|
||||
|
||||
return WidgetService.get_by_id(widgetId)
|
||||
|
||||
def delete(self, widgetId: int) -> Response:
|
||||
'''Delete Single Widget'''
|
||||
from flask import jsonify
|
||||
|
||||
id = WidgetService.delete_by_id(widgetId)
|
||||
return jsonify(dict(status='Success', id=id))
|
||||
|
||||
@accepts(schema=WidgetSchema, api=api)
|
||||
@responds(schema=WidgetSchema)
|
||||
def put(self, widgetId: int) -> Widget:
|
||||
'''Update Single Widget'''
|
||||
|
||||
changes: WidgetInterface = request.parsed_obj
|
||||
Widget = WidgetService.get_by_id(widgetId)
|
||||
return WidgetService.update(Widget, changes)
|
85
app/widget/controller_test.py
Normal file
85
app/widget/controller_test.py
Normal file
@@ -0,0 +1,85 @@
|
||||
|
||||
from unittest.mock import patch
|
||||
from flask.testing import FlaskClient
|
||||
|
||||
from app.test.fixtures import client, app # noqa
|
||||
from .service import WidgetService
|
||||
from .schema import WidgetSchema
|
||||
from .model import Widget
|
||||
from .interface import WidgetInterface
|
||||
from . import BASE_ROUTE
|
||||
|
||||
|
||||
def make_widget(id: int = 123, name: str = 'Test widget',
|
||||
purpose: str = 'Test purpose') -> Widget:
|
||||
return Widget(
|
||||
widget_id=id, name=name, purpose=purpose
|
||||
)
|
||||
|
||||
|
||||
class TestWidgetResource:
|
||||
@patch.object(WidgetService, 'get_all',
|
||||
lambda: [make_widget(123, name='Test Widget 1'),
|
||||
make_widget(456, name='Test Widget 2')])
|
||||
def test_get(self, client: FlaskClient): # noqa
|
||||
with client:
|
||||
results = client.get(f'/api/{BASE_ROUTE}',
|
||||
follow_redirects=True).get_json()
|
||||
expected = WidgetSchema(many=True).dump(
|
||||
[make_widget(123, name='Test Widget 1'),
|
||||
make_widget(456, name='Test Widget 2')]
|
||||
).data
|
||||
for r in results:
|
||||
assert r in expected
|
||||
|
||||
@patch.object(WidgetService, 'create',
|
||||
lambda create_request: Widget(**create_request))
|
||||
def test_post(self, client: FlaskClient): # noqa
|
||||
with client:
|
||||
|
||||
payload = dict(name='Test widget', purpose='Test purpose')
|
||||
result = client.post(f'/api/{BASE_ROUTE}/', json=payload).get_json()
|
||||
expected = WidgetSchema().dump(Widget(
|
||||
name=payload['name'],
|
||||
purpose=payload['purpose'],
|
||||
)).data
|
||||
assert result == expected
|
||||
|
||||
|
||||
def fake_update(widget: Widget, changes: WidgetInterface) -> Widget:
|
||||
# To fake an update, just return a new object
|
||||
updated_Widget = Widget(widget_id=widget.widget_id,
|
||||
name=changes['name'],
|
||||
purpose=changes['purpose'])
|
||||
return updated_Widget
|
||||
|
||||
|
||||
class TestWidgetIdResource:
|
||||
@patch.object(WidgetService, 'get_by_id',
|
||||
lambda id: make_widget(id=id))
|
||||
def test_get(self, client: FlaskClient): # noqa
|
||||
with client:
|
||||
result = client.get(f'/api/{BASE_ROUTE}/123').get_json()
|
||||
expected = make_widget(id=123)
|
||||
print(f'result = ', result)
|
||||
assert result['widgetId'] == expected.widget_id
|
||||
|
||||
@patch.object(WidgetService, 'delete_by_id',
|
||||
lambda id: id)
|
||||
def test_delete(self, client: FlaskClient): # noqa
|
||||
with client:
|
||||
result = client.delete(f'/api/{BASE_ROUTE}/123').get_json()
|
||||
expected = dict(status='Success', id=123)
|
||||
assert result == expected
|
||||
|
||||
@patch.object(WidgetService, 'get_by_id',
|
||||
lambda id: make_widget(id=id))
|
||||
@patch.object(WidgetService, 'update', fake_update)
|
||||
def test_put(self, client: FlaskClient): # noqa
|
||||
with client:
|
||||
result = client.put(f'/api/{BASE_ROUTE}/123',
|
||||
json={'name': 'New Widget',
|
||||
'purpose': 'New purpose'}).get_json()
|
||||
expected = WidgetSchema().dump(
|
||||
Widget(widget_id=123, name='New Widget', purpose='New purpose')).data
|
||||
assert result == expected
|
7
app/widget/interface.py
Normal file
7
app/widget/interface.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from mypy_extensions import TypedDict
|
||||
|
||||
|
||||
class WidgetInterface(TypedDict, total=False):
|
||||
widget_id: int
|
||||
name: str
|
||||
purpose: str
|
19
app/widget/model.py
Normal file
19
app/widget/model.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from sqlalchemy import Integer, Column, String
|
||||
from app import db # noqa
|
||||
from .interface import WidgetInterface
|
||||
from typing import Any
|
||||
|
||||
|
||||
class Widget(db.Model): # type: ignore
|
||||
'''A snazzy Widget'''
|
||||
|
||||
__tablename__ = 'widget'
|
||||
|
||||
widget_id = Column(Integer(), primary_key=True)
|
||||
name = Column(String(255))
|
||||
purpose = Column(String(255))
|
||||
|
||||
def update(self, changes: WidgetInterface):
|
||||
for key, val in changes.items():
|
||||
setattr(self, key, val)
|
||||
return self
|
22
app/widget/model_test.py
Normal file
22
app/widget/model_test.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from pytest import fixture
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from app.test.fixtures import app, db # noqa
|
||||
from .model import Widget
|
||||
|
||||
|
||||
@fixture
|
||||
def widget() -> Widget:
|
||||
return Widget(
|
||||
widget_id=1, name='Test widget', purpose='Test purpose'
|
||||
)
|
||||
|
||||
|
||||
def test_Widget_create(widget: Widget):
|
||||
assert widget
|
||||
|
||||
|
||||
def test_Widget_retrieve(widget: Widget, db: SQLAlchemy): # noqa
|
||||
db.session.add(widget)
|
||||
db.session.commit()
|
||||
s = Widget.query.first()
|
||||
assert s.__dict__ == widget.__dict__
|
9
app/widget/schema.py
Normal file
9
app/widget/schema.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from marshmallow import fields, Schema
|
||||
|
||||
|
||||
class WidgetSchema(Schema):
|
||||
'''Widget schema'''
|
||||
|
||||
widgetId = fields.Number(attribute='widget_id')
|
||||
name = fields.String(attribute='name')
|
||||
purpose = fields.String(attribute='purpose')
|
0
app/widget/schema_test.py
Normal file
0
app/widget/schema_test.py
Normal file
41
app/widget/service.py
Normal file
41
app/widget/service.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from app import db
|
||||
from typing import List
|
||||
from .model import Widget
|
||||
from .interface import WidgetInterface
|
||||
|
||||
|
||||
class WidgetService():
|
||||
@staticmethod
|
||||
def get_all() -> List[Widget]:
|
||||
return Widget.query.all()
|
||||
|
||||
@staticmethod
|
||||
def get_by_id(widget_id: int) -> Widget:
|
||||
return Widget.query.get(widget_id)
|
||||
|
||||
@staticmethod
|
||||
def update(widget: Widget, Widget_change_updates: WidgetInterface) -> Widget:
|
||||
widget.update(Widget_change_updates)
|
||||
db.session.commit()
|
||||
return widget
|
||||
|
||||
@staticmethod
|
||||
def delete_by_id(widget_id: int) -> List[int]:
|
||||
widget = Widget.query.filter(Widget.widget_id == widget_id).first()
|
||||
if not widget:
|
||||
return []
|
||||
db.session.delete(widget)
|
||||
db.session.commit()
|
||||
return [widget_id]
|
||||
|
||||
@staticmethod
|
||||
def create(new_attrs: WidgetInterface) -> Widget:
|
||||
new_widget = Widget(
|
||||
name=new_attrs['name'],
|
||||
purpose=new_attrs['purpose']
|
||||
)
|
||||
|
||||
db.session.add(new_widget)
|
||||
db.session.commit()
|
||||
|
||||
return new_widget
|
60
app/widget/service_test.py
Normal file
60
app/widget/service_test.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from typing import List
|
||||
from app.test.fixtures import app, db # noqa
|
||||
from .model import Widget
|
||||
from .service import WidgetService # noqa
|
||||
from .interface import WidgetInterface
|
||||
|
||||
|
||||
def test_get_all(db: SQLAlchemy): # noqa
|
||||
yin: Widget = Widget(widget_id=1, name='Yin', purpose='thing 1')
|
||||
yang: Widget = Widget(widget_id=2, name='Yang', purpose='thing 2')
|
||||
db.session.add(yin)
|
||||
db.session.add(yang)
|
||||
db.session.commit()
|
||||
|
||||
results: List[Widget] = WidgetService.get_all()
|
||||
|
||||
assert len(results) == 2
|
||||
assert yin in results and yang in results
|
||||
|
||||
|
||||
def test_update(db: SQLAlchemy): # noqa
|
||||
yin: Widget = Widget(widget_id=1, name='Yin', purpose='thing 1')
|
||||
|
||||
db.session.add(yin)
|
||||
db.session.commit()
|
||||
updates: WidgetInterface = dict(name='New Widget name')
|
||||
|
||||
WidgetService.update(yin, updates)
|
||||
|
||||
result: Widget = Widget.query.get(yin.widget_id)
|
||||
assert result.name == 'New Widget name'
|
||||
|
||||
|
||||
def test_delete_by_id(db: SQLAlchemy): # noqa
|
||||
yin: Widget = Widget(widget_id=1, name='Yin', purpose='thing 1')
|
||||
yang: Widget = Widget(widget_id=2, name='Yang', purpose='thing 2')
|
||||
db.session.add(yin)
|
||||
db.session.add(yang)
|
||||
db.session.commit()
|
||||
|
||||
WidgetService.delete_by_id(1)
|
||||
db.session.commit()
|
||||
|
||||
results: List[Widget] = Widget.query.all()
|
||||
|
||||
assert len(results) == 1
|
||||
assert yin not in results and yang in results
|
||||
|
||||
|
||||
def test_create(db: SQLAlchemy): # noqa
|
||||
|
||||
yin: WidgetInterface = dict(name='Fancy new widget', purpose='Fancy new purpose')
|
||||
WidgetService.create(yin)
|
||||
results: List[Widget] = Widget.query.all()
|
||||
|
||||
assert len(results) == 1
|
||||
|
||||
for k in yin.keys():
|
||||
assert getattr(results[0], k) == yin[k]
|
Reference in New Issue
Block a user