Add example of third party API
This commit is contained in:
2
app/other_api/whatsit/__init__.py
Normal file
2
app/other_api/whatsit/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .model import Whatsit # noqa
|
||||
from .schema import WhatsitSchema # noqa
|
56
app/other_api/whatsit/controller.py
Normal file
56
app/other_api/whatsit/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 WhatsitSchema
|
||||
from .service import WhatsitService
|
||||
from .model import Whatsit
|
||||
from .interface import WhatsitInterface
|
||||
|
||||
api = Namespace('Whatsit', description='A modular namespace within Other API') # noqa
|
||||
|
||||
|
||||
@api.route('/')
|
||||
class WhatsitResource(Resource):
|
||||
'''Whatsits'''
|
||||
|
||||
@responds(schema=WhatsitSchema, many=True)
|
||||
def get(self) -> List[Whatsit]:
|
||||
'''Get all Whatsits'''
|
||||
|
||||
return WhatsitService.get_all()
|
||||
|
||||
@accepts(schema=WhatsitSchema, api=api)
|
||||
@responds(schema=WhatsitSchema)
|
||||
def post(self) -> Whatsit:
|
||||
'''Create a Single Whatsit'''
|
||||
|
||||
return WhatsitService.create(request.parsed_obj)
|
||||
|
||||
|
||||
@api.route('/<int:whatsitId>')
|
||||
@api.param('whatsitId', 'Whatsit database ID')
|
||||
class WhatsitIdResource(Resource):
|
||||
@responds(schema=WhatsitSchema)
|
||||
def get(self, whatsitId: int) -> Whatsit:
|
||||
'''Get Single Whatsit'''
|
||||
|
||||
return WhatsitService.get_by_id(whatsitId)
|
||||
|
||||
def delete(self, whatsitId: int) -> Response:
|
||||
'''Delete Single Whatsit'''
|
||||
from flask import jsonify
|
||||
|
||||
id = WhatsitService.delete_by_id(whatsitId)
|
||||
return jsonify(dict(status='Success', id=id))
|
||||
|
||||
@accepts(schema=WhatsitSchema, api=api)
|
||||
@responds(schema=WhatsitSchema)
|
||||
def put(self, whatsitId: int) -> Whatsit:
|
||||
'''Update Single Whatsit'''
|
||||
|
||||
changes: WhatsitInterface = request.parsed_obj
|
||||
Whatsit = WhatsitService.get_by_id(whatsitId)
|
||||
return WhatsitService.update(Whatsit, changes)
|
84
app/other_api/whatsit/controller_test.py
Normal file
84
app/other_api/whatsit/controller_test.py
Normal file
@@ -0,0 +1,84 @@
|
||||
|
||||
from unittest.mock import patch
|
||||
from flask.testing import FlaskClient
|
||||
|
||||
from app.test.fixtures import client, app # noqa
|
||||
from .service import WhatsitService
|
||||
from .schema import WhatsitSchema
|
||||
from .model import Whatsit
|
||||
from .interface import WhatsitInterface
|
||||
from .. import BASE_ROUTE
|
||||
|
||||
|
||||
def make_whatsit(id: int = 123, name: str = 'Test whatsit',
|
||||
purpose: str = 'Test purpose') -> Whatsit:
|
||||
return Whatsit(
|
||||
whatsit_id=id, name=name, purpose=purpose
|
||||
)
|
||||
|
||||
|
||||
class TestWhatsitResource:
|
||||
@patch.object(WhatsitService, 'get_all',
|
||||
lambda: [make_whatsit(123, name='Test Whatsit 1'),
|
||||
make_whatsit(456, name='Test Whatsit 2')])
|
||||
def test_get(self, client: FlaskClient): # noqa
|
||||
with client:
|
||||
results = client.get(f'/api/{BASE_ROUTE}/whatsit',
|
||||
follow_redirects=True).get_json()
|
||||
expected = WhatsitSchema(many=True).dump(
|
||||
[make_whatsit(123, name='Test Whatsit 1'),
|
||||
make_whatsit(456, name='Test Whatsit 2')]
|
||||
).data
|
||||
for r in results:
|
||||
assert r in expected
|
||||
|
||||
@patch.object(WhatsitService, 'create',
|
||||
lambda create_request: Whatsit(**create_request))
|
||||
def test_post(self, client: FlaskClient): # noqa
|
||||
with client:
|
||||
|
||||
payload = dict(name='Test whatsit', purpose='Test purpose')
|
||||
result = client.post(f'/api/{BASE_ROUTE}/whatsit/', json=payload).get_json()
|
||||
expected = WhatsitSchema().dump(Whatsit(
|
||||
name=payload['name'],
|
||||
purpose=payload['purpose'],
|
||||
)).data
|
||||
assert result == expected
|
||||
|
||||
|
||||
def fake_update(whatsit: Whatsit, changes: WhatsitInterface) -> Whatsit:
|
||||
# To fake an update, just return a new object
|
||||
updated_Whatsit = Whatsit(whatsit_id=whatsit.whatsit_id,
|
||||
name=changes['name'],
|
||||
purpose=changes['purpose'])
|
||||
return updated_Whatsit
|
||||
|
||||
|
||||
class TestWhatsitIdResource:
|
||||
@patch.object(WhatsitService, 'get_by_id',
|
||||
lambda id: make_whatsit(id=id))
|
||||
def test_get(self, client: FlaskClient): # noqa
|
||||
with client:
|
||||
result = client.get(f'/api/{BASE_ROUTE}/whatsit/123').get_json()
|
||||
expected = Whatsit(whatsit_id=123)
|
||||
assert result['whatsitId'] == expected.whatsit_id
|
||||
|
||||
@patch.object(WhatsitService, 'delete_by_id',
|
||||
lambda id: [id])
|
||||
def test_delete(self, client: FlaskClient): # noqa
|
||||
with client:
|
||||
result = client.delete(f'/api/{BASE_ROUTE}/whatsit/123').get_json()
|
||||
expected = dict(status='Success', id=[123])
|
||||
assert result == expected
|
||||
|
||||
@patch.object(WhatsitService, 'get_by_id',
|
||||
lambda id: make_whatsit(id=id))
|
||||
@patch.object(WhatsitService, 'update', fake_update)
|
||||
def test_put(self, client: FlaskClient): # noqa
|
||||
with client:
|
||||
result = client.put(f'/api/{BASE_ROUTE}/whatsit/123',
|
||||
json={'name': 'New Whatsit',
|
||||
'purpose': 'New purpose'}).get_json()
|
||||
expected = WhatsitSchema().dump(
|
||||
Whatsit(whatsit_id=123, name='New Whatsit', purpose='New purpose')).data
|
||||
assert result == expected
|
7
app/other_api/whatsit/interface.py
Normal file
7
app/other_api/whatsit/interface.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from mypy_extensions import TypedDict
|
||||
|
||||
|
||||
class WhatsitInterface(TypedDict, total=False):
|
||||
whatsit_id: int
|
||||
name: str
|
||||
purpose: str
|
19
app/other_api/whatsit/model.py
Normal file
19
app/other_api/whatsit/model.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from sqlalchemy import Integer, Column, String
|
||||
from app import db # noqa
|
||||
from .interface import WhatsitInterface
|
||||
from typing import Any
|
||||
|
||||
|
||||
class Whatsit(db.Model): # type: ignore
|
||||
'''A snazzy Whatsit'''
|
||||
|
||||
__tablename__ = 'whatsit'
|
||||
|
||||
whatsit_id = Column(Integer(), primary_key=True)
|
||||
name = Column(String(255))
|
||||
purpose = Column(String(255))
|
||||
|
||||
def update(self, changes: WhatsitInterface):
|
||||
for key, val in changes.items():
|
||||
setattr(self, key, val)
|
||||
return self
|
22
app/other_api/whatsit/model_test.py
Normal file
22
app/other_api/whatsit/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 Whatsit
|
||||
|
||||
|
||||
@fixture
|
||||
def whatsit() -> Whatsit:
|
||||
return Whatsit(
|
||||
whatsit_id=1, name='Test whatsit', purpose='Test purpose'
|
||||
)
|
||||
|
||||
|
||||
def test_Whatsit_create(whatsit: Whatsit):
|
||||
assert whatsit
|
||||
|
||||
|
||||
def test_Whatsit_retrieve(whatsit: Whatsit, db: SQLAlchemy): # noqa
|
||||
db.session.add(whatsit)
|
||||
db.session.commit()
|
||||
s = Whatsit.query.first()
|
||||
assert s.__dict__ == whatsit.__dict__
|
9
app/other_api/whatsit/schema.py
Normal file
9
app/other_api/whatsit/schema.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from marshmallow import fields, Schema
|
||||
|
||||
|
||||
class WhatsitSchema(Schema):
|
||||
'''Whatsit schema'''
|
||||
|
||||
whatsitId = fields.Number(attribute='whatsit_id')
|
||||
name = fields.String(attribute='name')
|
||||
purpose = fields.String(attribute='purpose')
|
0
app/other_api/whatsit/schema_test.py
Normal file
0
app/other_api/whatsit/schema_test.py
Normal file
41
app/other_api/whatsit/service.py
Normal file
41
app/other_api/whatsit/service.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from app import db
|
||||
from typing import List
|
||||
from .model import Whatsit
|
||||
from .interface import WhatsitInterface
|
||||
|
||||
|
||||
class WhatsitService():
|
||||
@staticmethod
|
||||
def get_all() -> List[Whatsit]:
|
||||
return Whatsit.query.all()
|
||||
|
||||
@staticmethod
|
||||
def get_by_id(whatsit_id: int) -> Whatsit:
|
||||
return Whatsit.query.get(whatsit_id)
|
||||
|
||||
@staticmethod
|
||||
def update(whatsit: Whatsit, Whatsit_change_updates: WhatsitInterface) -> Whatsit:
|
||||
whatsit.update(Whatsit_change_updates)
|
||||
db.session.commit()
|
||||
return whatsit
|
||||
|
||||
@staticmethod
|
||||
def delete_by_id(whatsit_id: int) -> List[int]:
|
||||
whatsit = Whatsit.query.filter(Whatsit.whatsit_id == whatsit_id).first()
|
||||
if not whatsit:
|
||||
return []
|
||||
db.session.delete(whatsit)
|
||||
db.session.commit()
|
||||
return [whatsit_id]
|
||||
|
||||
@staticmethod
|
||||
def create(new_attrs: WhatsitInterface) -> Whatsit:
|
||||
new_whatsit = Whatsit(
|
||||
name=new_attrs['name'],
|
||||
purpose=new_attrs['purpose']
|
||||
)
|
||||
|
||||
db.session.add(new_whatsit)
|
||||
db.session.commit()
|
||||
|
||||
return new_whatsit
|
60
app/other_api/whatsit/service_test.py
Normal file
60
app/other_api/whatsit/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 Whatsit
|
||||
from .service import WhatsitService # noqa
|
||||
from .interface import WhatsitInterface
|
||||
|
||||
|
||||
def test_get_all(db: SQLAlchemy): # noqa
|
||||
yin: Whatsit = Whatsit(whatsit_id=1, name='Yin', purpose='thing 1')
|
||||
yang: Whatsit = Whatsit(whatsit_id=2, name='Yang', purpose='thing 2')
|
||||
db.session.add(yin)
|
||||
db.session.add(yang)
|
||||
db.session.commit()
|
||||
|
||||
results: List[Whatsit] = WhatsitService.get_all()
|
||||
|
||||
assert len(results) == 2
|
||||
assert yin in results and yang in results
|
||||
|
||||
|
||||
def test_update(db: SQLAlchemy): # noqa
|
||||
yin: Whatsit = Whatsit(whatsit_id=1, name='Yin', purpose='thing 1')
|
||||
|
||||
db.session.add(yin)
|
||||
db.session.commit()
|
||||
updates: WhatsitInterface = dict(name='New Whatsit name')
|
||||
|
||||
WhatsitService.update(yin, updates)
|
||||
|
||||
result: Whatsit = Whatsit.query.get(yin.whatsit_id)
|
||||
assert result.name == 'New Whatsit name'
|
||||
|
||||
|
||||
def test_delete_by_id(db: SQLAlchemy): # noqa
|
||||
yin: Whatsit = Whatsit(whatsit_id=1, name='Yin', purpose='thing 1')
|
||||
yang: Whatsit = Whatsit(whatsit_id=2, name='Yang', purpose='thing 2')
|
||||
db.session.add(yin)
|
||||
db.session.add(yang)
|
||||
db.session.commit()
|
||||
|
||||
WhatsitService.delete_by_id(1)
|
||||
db.session.commit()
|
||||
|
||||
results: List[Whatsit] = Whatsit.query.all()
|
||||
|
||||
assert len(results) == 1
|
||||
assert yin not in results and yang in results
|
||||
|
||||
|
||||
def test_create(db: SQLAlchemy): # noqa
|
||||
|
||||
yin: WhatsitInterface = dict(name='Fancy new whatsit', purpose='Fancy new purpose')
|
||||
WhatsitService.create(yin)
|
||||
results: List[Whatsit] = Whatsit.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