Convert to black formatting
This commit is contained in:
parent
9bdaa8f161
commit
9b136b1ffa
|
@ -10,13 +10,14 @@ def create_app(env=None):
|
||||||
from app.routes import register_routes
|
from app.routes import register_routes
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config.from_object(config_by_name[env or 'test'])
|
app.config.from_object(config_by_name[env or "test"])
|
||||||
api = Api(app, title='Flaskerific API', version='0.1.0')
|
api = Api(app, title="Flaskerific API", version="0.1.0")
|
||||||
|
|
||||||
register_routes(api, app)
|
register_routes(api, app)
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
|
|
||||||
@app.route('/health')
|
@app.route("/health")
|
||||||
def health():
|
def health():
|
||||||
return jsonify('healthy')
|
return jsonify("healthy")
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
|
@ -7,7 +7,7 @@ def test_app_creates(app): # noqa
|
||||||
|
|
||||||
def test_app_healthy(app, client): # noqa
|
def test_app_healthy(app, client): # noqa
|
||||||
with client:
|
with client:
|
||||||
resp = client.get('/health')
|
resp = client.get("/health")
|
||||||
assert resp.status_code == 200
|
assert resp.status_code == 200
|
||||||
assert resp.is_json
|
assert resp.is_json
|
||||||
assert resp.json == 'healthy'
|
assert resp.json == "healthy"
|
||||||
|
|
|
@ -5,16 +5,17 @@ basedir = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
||||||
|
|
||||||
class BaseConfig:
|
class BaseConfig:
|
||||||
CONFIG_NAME = 'base'
|
CONFIG_NAME = "base"
|
||||||
USE_MOCK_EQUIVALENCY = False
|
USE_MOCK_EQUIVALENCY = False
|
||||||
DEBUG = False
|
DEBUG = False
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
|
|
||||||
|
|
||||||
class DevelopmentConfig(BaseConfig):
|
class DevelopmentConfig(BaseConfig):
|
||||||
CONFIG_NAME = 'dev'
|
CONFIG_NAME = "dev"
|
||||||
SECRET_KEY = os.getenv(
|
SECRET_KEY = os.getenv(
|
||||||
"DEV_SECRET_KEY", "You can't see California without Marlon Widgeto's eyes")
|
"DEV_SECRET_KEY", "You can't see California without Marlon Widgeto's eyes"
|
||||||
|
)
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
TESTING = False
|
TESTING = False
|
||||||
|
@ -22,7 +23,7 @@ class DevelopmentConfig(BaseConfig):
|
||||||
|
|
||||||
|
|
||||||
class TestingConfig(BaseConfig):
|
class TestingConfig(BaseConfig):
|
||||||
CONFIG_NAME = 'test'
|
CONFIG_NAME = "test"
|
||||||
SECRET_KEY = os.getenv("TEST_SECRET_KEY", "Thanos did nothing wrong")
|
SECRET_KEY = os.getenv("TEST_SECRET_KEY", "Thanos did nothing wrong")
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
|
@ -31,7 +32,7 @@ class TestingConfig(BaseConfig):
|
||||||
|
|
||||||
|
|
||||||
class ProductionConfig(BaseConfig):
|
class ProductionConfig(BaseConfig):
|
||||||
CONFIG_NAME = 'prod'
|
CONFIG_NAME = "prod"
|
||||||
SECRET_KEY = os.getenv("PROD_SECRET_KEY", "I'm Ron Burgundy?")
|
SECRET_KEY = os.getenv("PROD_SECRET_KEY", "I'm Ron Burgundy?")
|
||||||
DEBUG = False
|
DEBUG = False
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
|
@ -40,5 +41,8 @@ class ProductionConfig(BaseConfig):
|
||||||
|
|
||||||
|
|
||||||
EXPORT_CONFIGS: List[Type[BaseConfig]] = [
|
EXPORT_CONFIGS: List[Type[BaseConfig]] = [
|
||||||
DevelopmentConfig, TestingConfig, ProductionConfig]
|
DevelopmentConfig,
|
||||||
|
TestingConfig,
|
||||||
|
ProductionConfig,
|
||||||
|
]
|
||||||
config_by_name = {cfg.CONFIG_NAME: cfg for cfg in EXPORT_CONFIGS}
|
config_by_name = {cfg.CONFIG_NAME: cfg for cfg in EXPORT_CONFIGS}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
BASE_ROUTE = 'fizz'
|
BASE_ROUTE = "fizz"
|
||||||
|
|
||||||
|
|
||||||
def register_routes(api, app, root='api'):
|
def register_routes(api, app, root="api"):
|
||||||
from .fizzbar.controller import api as fizzbar_api
|
from .fizzbar.controller import api as fizzbar_api
|
||||||
from .fizzbaz.controller import api as fizzbaz_api
|
from .fizzbaz.controller import api as fizzbaz_api
|
||||||
|
|
||||||
api.add_namespace(fizzbar_api, path=f'/{root}/{BASE_ROUTE}/fizzbar')
|
api.add_namespace(fizzbar_api, path=f"/{root}/{BASE_ROUTE}/fizzbar")
|
||||||
api.add_namespace(fizzbaz_api, path=f'/{root}/{BASE_ROUTE}/fizzbaz')
|
api.add_namespace(fizzbaz_api, path=f"/{root}/{BASE_ROUTE}/fizzbaz")
|
||||||
|
|
|
@ -9,47 +9,48 @@ from .service import FizzbarService
|
||||||
from .model import Fizzbar
|
from .model import Fizzbar
|
||||||
from .interface import FizzbarInterface
|
from .interface import FizzbarInterface
|
||||||
|
|
||||||
api = Namespace('Fizzbar', description='A modular namespace within fizz') # noqa
|
api = Namespace("Fizzbar", description="A modular namespace within fizz") # noqa
|
||||||
|
|
||||||
|
|
||||||
@api.route('/')
|
@api.route("/")
|
||||||
class FizzbarResource(Resource):
|
class FizzbarResource(Resource):
|
||||||
'''Fizzbars'''
|
"""Fizzbars"""
|
||||||
|
|
||||||
@responds(schema=FizzbarSchema, many=True)
|
@responds(schema=FizzbarSchema, many=True)
|
||||||
def get(self) -> List[Fizzbar]:
|
def get(self) -> List[Fizzbar]:
|
||||||
'''Get all Fizzbars'''
|
"""Get all Fizzbars"""
|
||||||
|
|
||||||
return FizzbarService.get_all()
|
return FizzbarService.get_all()
|
||||||
|
|
||||||
@accepts(schema=FizzbarSchema, api=api)
|
@accepts(schema=FizzbarSchema, api=api)
|
||||||
@responds(schema=FizzbarSchema)
|
@responds(schema=FizzbarSchema)
|
||||||
def post(self) -> Fizzbar:
|
def post(self) -> Fizzbar:
|
||||||
'''Create a Single Fizzbar'''
|
"""Create a Single Fizzbar"""
|
||||||
|
|
||||||
return FizzbarService.create(request.parsed_obj)
|
return FizzbarService.create(request.parsed_obj)
|
||||||
|
|
||||||
|
|
||||||
@api.route('/<int:fizzbarId>')
|
@api.route("/<int:fizzbarId>")
|
||||||
@api.param('fizzbarId', 'Fizzbar database ID')
|
@api.param("fizzbarId", "Fizzbar database ID")
|
||||||
class FizzbarIdResource(Resource):
|
class FizzbarIdResource(Resource):
|
||||||
@responds(schema=FizzbarSchema)
|
@responds(schema=FizzbarSchema)
|
||||||
def get(self, fizzbarId: int) -> Fizzbar:
|
def get(self, fizzbarId: int) -> Fizzbar:
|
||||||
'''Get Single Fizzbar'''
|
"""Get Single Fizzbar"""
|
||||||
|
|
||||||
return FizzbarService.get_by_id(fizzbarId)
|
return FizzbarService.get_by_id(fizzbarId)
|
||||||
|
|
||||||
def delete(self, fizzbarId: int) -> Response:
|
def delete(self, fizzbarId: int) -> Response:
|
||||||
'''Delete Single Fizzbar'''
|
"""Delete Single Fizzbar"""
|
||||||
from flask import jsonify
|
from flask import jsonify
|
||||||
print('fizzbarId = ', fizzbarId)
|
|
||||||
|
print("fizzbarId = ", fizzbarId)
|
||||||
id = FizzbarService.delete_by_id(fizzbarId)
|
id = FizzbarService.delete_by_id(fizzbarId)
|
||||||
return jsonify(dict(status='Success', id=id))
|
return jsonify(dict(status="Success", id=id))
|
||||||
|
|
||||||
@accepts(schema=FizzbarSchema, api=api)
|
@accepts(schema=FizzbarSchema, api=api)
|
||||||
@responds(schema=FizzbarSchema)
|
@responds(schema=FizzbarSchema)
|
||||||
def put(self, fizzbarId: int) -> Fizzbar:
|
def put(self, fizzbarId: int) -> Fizzbar:
|
||||||
'''Update Single Fizzbar'''
|
"""Update Single Fizzbar"""
|
||||||
|
|
||||||
changes: FizzbarInterface = request.parsed_obj
|
changes: FizzbarInterface = request.parsed_obj
|
||||||
Fizzbar = FizzbarService.get_by_id(fizzbarId)
|
Fizzbar = FizzbarService.get_by_id(fizzbarId)
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
from flask.testing import FlaskClient
|
from flask.testing import FlaskClient
|
||||||
|
|
||||||
|
@ -10,75 +9,91 @@ from .interface import FizzbarInterface
|
||||||
from .. import BASE_ROUTE
|
from .. import BASE_ROUTE
|
||||||
|
|
||||||
|
|
||||||
def make_fizzbar(id: int = 123, name: str = 'Test fizzbar',
|
def make_fizzbar(
|
||||||
purpose: str = 'Test purpose') -> Fizzbar:
|
id: int = 123, name: str = "Test fizzbar", purpose: str = "Test purpose"
|
||||||
return Fizzbar(
|
) -> Fizzbar:
|
||||||
fizzbar_id=id, name=name, purpose=purpose
|
return Fizzbar(fizzbar_id=id, name=name, purpose=purpose)
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestFizzbarResource:
|
class TestFizzbarResource:
|
||||||
@patch.object(FizzbarService, 'get_all',
|
@patch.object(
|
||||||
lambda: [make_fizzbar(123, name='Test Fizzbar 1'),
|
FizzbarService,
|
||||||
make_fizzbar(456, name='Test Fizzbar 2')])
|
"get_all",
|
||||||
|
lambda: [
|
||||||
|
make_fizzbar(123, name="Test Fizzbar 1"),
|
||||||
|
make_fizzbar(456, name="Test Fizzbar 2"),
|
||||||
|
],
|
||||||
|
)
|
||||||
def test_get(self, client: FlaskClient): # noqa
|
def test_get(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
results = client.get(f'/api/{BASE_ROUTE}/fizzbar',
|
results = client.get(
|
||||||
follow_redirects=True).get_json()
|
f"/api/{BASE_ROUTE}/fizzbar", follow_redirects=True
|
||||||
expected = FizzbarSchema(many=True).dump(
|
).get_json()
|
||||||
[make_fizzbar(123, name='Test Fizzbar 1'),
|
expected = (
|
||||||
make_fizzbar(456, name='Test Fizzbar 2')]
|
FizzbarSchema(many=True)
|
||||||
).data
|
.dump(
|
||||||
|
[
|
||||||
|
make_fizzbar(123, name="Test Fizzbar 1"),
|
||||||
|
make_fizzbar(456, name="Test Fizzbar 2"),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
.data
|
||||||
|
)
|
||||||
for r in results:
|
for r in results:
|
||||||
assert r in expected
|
assert r in expected
|
||||||
|
|
||||||
@patch.object(FizzbarService, 'create',
|
@patch.object(
|
||||||
lambda create_request: Fizzbar(**create_request))
|
FizzbarService, "create", lambda create_request: Fizzbar(**create_request)
|
||||||
|
)
|
||||||
def test_post(self, client: FlaskClient): # noqa
|
def test_post(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
|
|
||||||
payload = dict(name='Test fizzbar', purpose='Test purpose')
|
payload = dict(name="Test fizzbar", purpose="Test purpose")
|
||||||
result = client.post(f'/api/{BASE_ROUTE}/fizzbar/', json=payload).get_json()
|
result = client.post(f"/api/{BASE_ROUTE}/fizzbar/", json=payload).get_json()
|
||||||
expected = FizzbarSchema().dump(Fizzbar(
|
expected = (
|
||||||
name=payload['name'],
|
FizzbarSchema()
|
||||||
purpose=payload['purpose'],
|
.dump(Fizzbar(name=payload["name"], purpose=payload["purpose"]))
|
||||||
)).data
|
.data
|
||||||
|
)
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
|
|
||||||
def fake_update(fizzbar: Fizzbar, changes: FizzbarInterface) -> Fizzbar:
|
def fake_update(fizzbar: Fizzbar, changes: FizzbarInterface) -> Fizzbar:
|
||||||
# To fake an update, just return a new object
|
# To fake an update, just return a new object
|
||||||
updated_Fizzbar = Fizzbar(fizzbar_id=fizzbar.fizzbar_id,
|
updated_Fizzbar = Fizzbar(
|
||||||
name=changes['name'],
|
fizzbar_id=fizzbar.fizzbar_id, name=changes["name"], purpose=changes["purpose"]
|
||||||
purpose=changes['purpose'])
|
)
|
||||||
return updated_Fizzbar
|
return updated_Fizzbar
|
||||||
|
|
||||||
|
|
||||||
class TestFizzbarIdResource:
|
class TestFizzbarIdResource:
|
||||||
@patch.object(FizzbarService, 'get_by_id',
|
@patch.object(FizzbarService, "get_by_id", lambda id: make_fizzbar(id=id))
|
||||||
lambda id: make_fizzbar(id=id))
|
|
||||||
def test_get(self, client: FlaskClient): # noqa
|
def test_get(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
result = client.get(f'/api/{BASE_ROUTE}/fizzbar/123').get_json()
|
result = client.get(f"/api/{BASE_ROUTE}/fizzbar/123").get_json()
|
||||||
expected = Fizzbar(fizzbar_id=123)
|
expected = Fizzbar(fizzbar_id=123)
|
||||||
assert result['fizzbarId'] == expected.fizzbar_id
|
assert result["fizzbarId"] == expected.fizzbar_id
|
||||||
|
|
||||||
@patch.object(FizzbarService, 'delete_by_id',
|
@patch.object(FizzbarService, "delete_by_id", lambda id: [id])
|
||||||
lambda id: [id])
|
|
||||||
def test_delete(self, client: FlaskClient): # noqa
|
def test_delete(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
result = client.delete(f'/api/{BASE_ROUTE}/fizzbar/123').get_json()
|
result = client.delete(f"/api/{BASE_ROUTE}/fizzbar/123").get_json()
|
||||||
expected = dict(status='Success', id=[123])
|
expected = dict(status="Success", id=[123])
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
@patch.object(FizzbarService, 'get_by_id',
|
@patch.object(FizzbarService, "get_by_id", lambda id: make_fizzbar(id=id))
|
||||||
lambda id: make_fizzbar(id=id))
|
@patch.object(FizzbarService, "update", fake_update)
|
||||||
@patch.object(FizzbarService, 'update', fake_update)
|
|
||||||
def test_put(self, client: FlaskClient): # noqa
|
def test_put(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
result = client.put(f'/api/{BASE_ROUTE}/fizzbar/123',
|
result = client.put(
|
||||||
json={'name': 'New Fizzbar',
|
f"/api/{BASE_ROUTE}/fizzbar/123",
|
||||||
'purpose': 'New purpose'}).get_json()
|
json={"name": "New Fizzbar", "purpose": "New purpose"},
|
||||||
expected = FizzbarSchema().dump(
|
).get_json()
|
||||||
Fizzbar(fizzbar_id=123, name='New Fizzbar', purpose='New purpose')).data
|
expected = (
|
||||||
|
FizzbarSchema()
|
||||||
|
.dump(
|
||||||
|
Fizzbar(fizzbar_id=123, name="New Fizzbar", purpose="New purpose")
|
||||||
|
)
|
||||||
|
.data
|
||||||
|
)
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
|
@ -5,9 +5,7 @@ from .interface import FizzbarInterface
|
||||||
|
|
||||||
@fixture
|
@fixture
|
||||||
def interface() -> FizzbarInterface:
|
def interface() -> FizzbarInterface:
|
||||||
return FizzbarInterface(
|
return FizzbarInterface(fizzbar_id=1, name="Test fizzbar", purpose="Test purpose")
|
||||||
fizzbar_id=1, name='Test fizzbar', purpose='Test purpose'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_FizzbarInterface_create(interface: FizzbarInterface):
|
def test_FizzbarInterface_create(interface: FizzbarInterface):
|
||||||
|
|
|
@ -5,9 +5,9 @@ from typing import Any
|
||||||
|
|
||||||
|
|
||||||
class Fizzbar(db.Model): # type: ignore
|
class Fizzbar(db.Model): # type: ignore
|
||||||
'''A snazzy Fizzbar'''
|
"""A snazzy Fizzbar"""
|
||||||
|
|
||||||
__tablename__ = 'fizzbar'
|
__tablename__ = "fizzbar"
|
||||||
|
|
||||||
fizzbar_id = Column(Integer(), primary_key=True)
|
fizzbar_id = Column(Integer(), primary_key=True)
|
||||||
name = Column(String(255))
|
name = Column(String(255))
|
||||||
|
|
|
@ -6,9 +6,7 @@ from .model import Fizzbar
|
||||||
|
|
||||||
@fixture
|
@fixture
|
||||||
def fizzbar() -> Fizzbar:
|
def fizzbar() -> Fizzbar:
|
||||||
return Fizzbar(
|
return Fizzbar(fizzbar_id=1, name="Test fizzbar", purpose="Test purpose")
|
||||||
fizzbar_id=1, name='Test fizzbar', purpose='Test purpose'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_Fizzbar_create(fizzbar: Fizzbar):
|
def test_Fizzbar_create(fizzbar: Fizzbar):
|
||||||
|
|
|
@ -2,8 +2,8 @@ from marshmallow import fields, Schema
|
||||||
|
|
||||||
|
|
||||||
class FizzbarSchema(Schema):
|
class FizzbarSchema(Schema):
|
||||||
'''Fizzbar schema'''
|
"""Fizzbar schema"""
|
||||||
|
|
||||||
fizzbarId = fields.Number(attribute='fizzbar_id')
|
fizzbarId = fields.Number(attribute="fizzbar_id")
|
||||||
name = fields.String(attribute='name')
|
name = fields.String(attribute="name")
|
||||||
purpose = fields.String(attribute='purpose')
|
purpose = fields.String(attribute="purpose")
|
||||||
|
|
|
@ -15,13 +15,11 @@ def test_FizzbarSchema_create(schema: FizzbarSchema):
|
||||||
|
|
||||||
|
|
||||||
def test_FizzbarSchema_works(schema: FizzbarSchema):
|
def test_FizzbarSchema_works(schema: FizzbarSchema):
|
||||||
params: FizzbarInterface = schema.load({
|
params: FizzbarInterface = schema.load(
|
||||||
'fizzbarId': '123',
|
{"fizzbarId": "123", "name": "Test fizzbar", "purpose": "Test purpose"}
|
||||||
'name': 'Test fizzbar',
|
).data
|
||||||
'purpose': 'Test purpose'
|
|
||||||
}).data
|
|
||||||
fizzbar = Fizzbar(**params)
|
fizzbar = Fizzbar(**params)
|
||||||
|
|
||||||
assert fizzbar.fizzbar_id == 123
|
assert fizzbar.fizzbar_id == 123
|
||||||
assert fizzbar.name == 'Test fizzbar'
|
assert fizzbar.name == "Test fizzbar"
|
||||||
assert fizzbar.purpose == 'Test purpose'
|
assert fizzbar.purpose == "Test purpose"
|
||||||
|
|
|
@ -4,7 +4,7 @@ from .model import Fizzbar
|
||||||
from .interface import FizzbarInterface
|
from .interface import FizzbarInterface
|
||||||
|
|
||||||
|
|
||||||
class FizzbarService():
|
class FizzbarService:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_all() -> List[Fizzbar]:
|
def get_all() -> List[Fizzbar]:
|
||||||
return Fizzbar.query.all()
|
return Fizzbar.query.all()
|
||||||
|
@ -30,10 +30,7 @@ class FizzbarService():
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(new_attrs: FizzbarInterface) -> Fizzbar:
|
def create(new_attrs: FizzbarInterface) -> Fizzbar:
|
||||||
new_fizzbar = Fizzbar(
|
new_fizzbar = Fizzbar(name=new_attrs["name"], purpose=new_attrs["purpose"])
|
||||||
name=new_attrs['name'],
|
|
||||||
purpose=new_attrs['purpose']
|
|
||||||
)
|
|
||||||
|
|
||||||
db.session.add(new_fizzbar)
|
db.session.add(new_fizzbar)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
|
@ -7,8 +7,8 @@ from .interface import FizzbarInterface
|
||||||
|
|
||||||
|
|
||||||
def test_get_all(db: SQLAlchemy): # noqa
|
def test_get_all(db: SQLAlchemy): # noqa
|
||||||
yin: Fizzbar = Fizzbar(fizzbar_id=1, name='Yin', purpose='thing 1')
|
yin: Fizzbar = Fizzbar(fizzbar_id=1, name="Yin", purpose="thing 1")
|
||||||
yang: Fizzbar = Fizzbar(fizzbar_id=2, name='Yang', purpose='thing 2')
|
yang: Fizzbar = Fizzbar(fizzbar_id=2, name="Yang", purpose="thing 2")
|
||||||
db.session.add(yin)
|
db.session.add(yin)
|
||||||
db.session.add(yang)
|
db.session.add(yang)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -20,21 +20,21 @@ def test_get_all(db: SQLAlchemy): # noqa
|
||||||
|
|
||||||
|
|
||||||
def test_update(db: SQLAlchemy): # noqa
|
def test_update(db: SQLAlchemy): # noqa
|
||||||
yin: Fizzbar = Fizzbar(fizzbar_id=1, name='Yin', purpose='thing 1')
|
yin: Fizzbar = Fizzbar(fizzbar_id=1, name="Yin", purpose="thing 1")
|
||||||
|
|
||||||
db.session.add(yin)
|
db.session.add(yin)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
updates: FizzbarInterface = dict(name='New Fizzbar name')
|
updates: FizzbarInterface = dict(name="New Fizzbar name")
|
||||||
|
|
||||||
FizzbarService.update(yin, updates)
|
FizzbarService.update(yin, updates)
|
||||||
|
|
||||||
result: Fizzbar = Fizzbar.query.get(yin.fizzbar_id)
|
result: Fizzbar = Fizzbar.query.get(yin.fizzbar_id)
|
||||||
assert result.name == 'New Fizzbar name'
|
assert result.name == "New Fizzbar name"
|
||||||
|
|
||||||
|
|
||||||
def test_delete_by_id(db: SQLAlchemy): # noqa
|
def test_delete_by_id(db: SQLAlchemy): # noqa
|
||||||
yin: Fizzbar = Fizzbar(fizzbar_id=1, name='Yin', purpose='thing 1')
|
yin: Fizzbar = Fizzbar(fizzbar_id=1, name="Yin", purpose="thing 1")
|
||||||
yang: Fizzbar = Fizzbar(fizzbar_id=2, name='Yang', purpose='thing 2')
|
yang: Fizzbar = Fizzbar(fizzbar_id=2, name="Yang", purpose="thing 2")
|
||||||
db.session.add(yin)
|
db.session.add(yin)
|
||||||
db.session.add(yang)
|
db.session.add(yang)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -50,7 +50,7 @@ def test_delete_by_id(db: SQLAlchemy): # noqa
|
||||||
|
|
||||||
def test_create(db: SQLAlchemy): # noqa
|
def test_create(db: SQLAlchemy): # noqa
|
||||||
|
|
||||||
yin: FizzbarInterface = dict(name='Fancy new fizzbar', purpose='Fancy new purpose')
|
yin: FizzbarInterface = dict(name="Fancy new fizzbar", purpose="Fancy new purpose")
|
||||||
FizzbarService.create(yin)
|
FizzbarService.create(yin)
|
||||||
results: List[Fizzbar] = Fizzbar.query.all()
|
results: List[Fizzbar] = Fizzbar.query.all()
|
||||||
|
|
||||||
|
|
|
@ -9,47 +9,47 @@ from .service import FizzbazService
|
||||||
from .model import Fizzbaz
|
from .model import Fizzbaz
|
||||||
from .interface import FizzbazInterface
|
from .interface import FizzbazInterface
|
||||||
|
|
||||||
api = Namespace('Fizzbaz', description='A modular namespace within fizz') # noqa
|
api = Namespace("Fizzbaz", description="A modular namespace within fizz") # noqa
|
||||||
|
|
||||||
|
|
||||||
@api.route('/')
|
@api.route("/")
|
||||||
class FizzbazResource(Resource):
|
class FizzbazResource(Resource):
|
||||||
'''Fizzbazs'''
|
"""Fizzbaz"""
|
||||||
|
|
||||||
@responds(schema=FizzbazSchema, many=True)
|
@responds(schema=FizzbazSchema, many=True)
|
||||||
def get(self) -> List[Fizzbaz]:
|
def get(self) -> List[Fizzbaz]:
|
||||||
'''Get all Fizzbazs'''
|
"""Get all Fizzbaz"""
|
||||||
|
|
||||||
return FizzbazService.get_all()
|
return FizzbazService.get_all()
|
||||||
|
|
||||||
@accepts(schema=FizzbazSchema, api=api)
|
@accepts(schema=FizzbazSchema, api=api)
|
||||||
@responds(schema=FizzbazSchema)
|
@responds(schema=FizzbazSchema)
|
||||||
def post(self) -> Fizzbaz:
|
def post(self) -> Fizzbaz:
|
||||||
'''Create a Single Fizzbaz'''
|
"""Create a Single Fizzbaz"""
|
||||||
|
|
||||||
return FizzbazService.create(request.parsed_obj)
|
return FizzbazService.create(request.parsed_obj)
|
||||||
|
|
||||||
|
|
||||||
@api.route('/<int:fizzbazId>')
|
@api.route("/<int:fizzbazId>")
|
||||||
@api.param('fizzbazId', 'Fizzbaz database ID')
|
@api.param("fizzbazId", "Fizzbaz database ID")
|
||||||
class FizzbazIdResource(Resource):
|
class FizzbazIdResource(Resource):
|
||||||
@responds(schema=FizzbazSchema)
|
@responds(schema=FizzbazSchema)
|
||||||
def get(self, fizzbazId: int) -> Fizzbaz:
|
def get(self, fizzbazId: int) -> Fizzbaz:
|
||||||
'''Get Single Fizzbaz'''
|
"""Get Single Fizzbaz"""
|
||||||
|
|
||||||
return FizzbazService.get_by_id(fizzbazId)
|
return FizzbazService.get_by_id(fizzbazId)
|
||||||
|
|
||||||
def delete(self, fizzbazId: int) -> Response:
|
def delete(self, fizzbazId: int) -> Response:
|
||||||
'''Delete Single Fizzbaz'''
|
"""Delete Single Fizzbaz"""
|
||||||
from flask import jsonify
|
from flask import jsonify
|
||||||
|
|
||||||
id = FizzbazService.delete_by_id(fizzbazId)
|
id = FizzbazService.delete_by_id(fizzbazId)
|
||||||
return jsonify(dict(status='Success', id=id))
|
return jsonify(dict(status="Success", id=id))
|
||||||
|
|
||||||
@accepts(schema=FizzbazSchema, api=api)
|
@accepts(schema=FizzbazSchema, api=api)
|
||||||
@responds(schema=FizzbazSchema)
|
@responds(schema=FizzbazSchema)
|
||||||
def put(self, fizzbazId: int) -> Fizzbaz:
|
def put(self, fizzbazId: int) -> Fizzbaz:
|
||||||
'''Update Single Fizzbaz'''
|
"""Update Single Fizzbaz"""
|
||||||
|
|
||||||
changes: FizzbazInterface = request.parsed_obj
|
changes: FizzbazInterface = request.parsed_obj
|
||||||
Fizzbaz = FizzbazService.get_by_id(fizzbazId)
|
Fizzbaz = FizzbazService.get_by_id(fizzbazId)
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
from flask.testing import FlaskClient
|
from flask.testing import FlaskClient
|
||||||
|
|
||||||
|
@ -10,75 +9,91 @@ from .interface import FizzbazInterface
|
||||||
from .. import BASE_ROUTE
|
from .. import BASE_ROUTE
|
||||||
|
|
||||||
|
|
||||||
def make_fizzbaz(id: int = 123, name: str = 'Test fizzbaz',
|
def make_fizzbaz(
|
||||||
purpose: str = 'Test purpose') -> Fizzbaz:
|
id: int = 123, name: str = "Test fizzbaz", purpose: str = "Test purpose"
|
||||||
return Fizzbaz(
|
) -> Fizzbaz:
|
||||||
fizzbaz_id=id, name=name, purpose=purpose
|
return Fizzbaz(fizzbaz_id=id, name=name, purpose=purpose)
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestFizzbazResource:
|
class TestFizzbazResource:
|
||||||
@patch.object(FizzbazService, 'get_all',
|
@patch.object(
|
||||||
lambda: [make_fizzbaz(123, name='Test Fizzbaz 1'),
|
FizzbazService,
|
||||||
make_fizzbaz(456, name='Test Fizzbaz 2')])
|
"get_all",
|
||||||
|
lambda: [
|
||||||
|
make_fizzbaz(123, name="Test Fizzbaz 1"),
|
||||||
|
make_fizzbaz(456, name="Test Fizzbaz 2"),
|
||||||
|
],
|
||||||
|
)
|
||||||
def test_get(self, client: FlaskClient): # noqa
|
def test_get(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
results = client.get(f'/api/{BASE_ROUTE}/fizzbaz',
|
results = client.get(
|
||||||
follow_redirects=True).get_json()
|
f"/api/{BASE_ROUTE}/fizzbaz", follow_redirects=True
|
||||||
expected = FizzbazSchema(many=True).dump(
|
).get_json()
|
||||||
[make_fizzbaz(123, name='Test Fizzbaz 1'),
|
expected = (
|
||||||
make_fizzbaz(456, name='Test Fizzbaz 2')]
|
FizzbazSchema(many=True)
|
||||||
).data
|
.dump(
|
||||||
|
[
|
||||||
|
make_fizzbaz(123, name="Test Fizzbaz 1"),
|
||||||
|
make_fizzbaz(456, name="Test Fizzbaz 2"),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
.data
|
||||||
|
)
|
||||||
for r in results:
|
for r in results:
|
||||||
assert r in expected
|
assert r in expected
|
||||||
|
|
||||||
@patch.object(FizzbazService, 'create',
|
@patch.object(
|
||||||
lambda create_request: Fizzbaz(**create_request))
|
FizzbazService, "create", lambda create_request: Fizzbaz(**create_request)
|
||||||
|
)
|
||||||
def test_post(self, client: FlaskClient): # noqa
|
def test_post(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
|
|
||||||
payload = dict(name='Test fizzbaz', purpose='Test purpose')
|
payload = dict(name="Test fizzbaz", purpose="Test purpose")
|
||||||
result = client.post(f'/api/{BASE_ROUTE}/fizzbaz/', json=payload).get_json()
|
result = client.post(f"/api/{BASE_ROUTE}/fizzbaz/", json=payload).get_json()
|
||||||
expected = FizzbazSchema().dump(Fizzbaz(
|
expected = (
|
||||||
name=payload['name'],
|
FizzbazSchema()
|
||||||
purpose=payload['purpose'],
|
.dump(Fizzbaz(name=payload["name"], purpose=payload["purpose"]))
|
||||||
)).data
|
.data
|
||||||
|
)
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
|
|
||||||
def fake_update(fizzbaz: Fizzbaz, changes: FizzbazInterface) -> Fizzbaz:
|
def fake_update(fizzbaz: Fizzbaz, changes: FizzbazInterface) -> Fizzbaz:
|
||||||
# To fake an update, just return a new object
|
# To fake an update, just return a new object
|
||||||
updated_Fizzbaz = Fizzbaz(fizzbaz_id=fizzbaz.fizzbaz_id,
|
updated_Fizzbaz = Fizzbaz(
|
||||||
name=changes['name'],
|
fizzbaz_id=fizzbaz.fizzbaz_id, name=changes["name"], purpose=changes["purpose"]
|
||||||
purpose=changes['purpose'])
|
)
|
||||||
return updated_Fizzbaz
|
return updated_Fizzbaz
|
||||||
|
|
||||||
|
|
||||||
class TestFizzbazIdResource:
|
class TestFizzbazIdResource:
|
||||||
@patch.object(FizzbazService, 'get_by_id',
|
@patch.object(FizzbazService, "get_by_id", lambda id: make_fizzbaz(id=id))
|
||||||
lambda id: make_fizzbaz(id=id))
|
|
||||||
def test_get(self, client: FlaskClient): # noqa
|
def test_get(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
result = client.get(f'/api/{BASE_ROUTE}/fizzbaz/123').get_json()
|
result = client.get(f"/api/{BASE_ROUTE}/fizzbaz/123").get_json()
|
||||||
expected = Fizzbaz(fizzbaz_id=123)
|
expected = Fizzbaz(fizzbaz_id=123)
|
||||||
assert result['fizzbazId'] == expected.fizzbaz_id
|
assert result["fizzbazId"] == expected.fizzbaz_id
|
||||||
|
|
||||||
@patch.object(FizzbazService, 'delete_by_id',
|
@patch.object(FizzbazService, "delete_by_id", lambda id: [id])
|
||||||
lambda id: [id])
|
|
||||||
def test_delete(self, client: FlaskClient): # noqa
|
def test_delete(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
result = client.delete(f'/api/{BASE_ROUTE}/fizzbaz/123').get_json()
|
result = client.delete(f"/api/{BASE_ROUTE}/fizzbaz/123").get_json()
|
||||||
expected = dict(status='Success', id=[123])
|
expected = dict(status="Success", id=[123])
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
@patch.object(FizzbazService, 'get_by_id',
|
@patch.object(FizzbazService, "get_by_id", lambda id: make_fizzbaz(id=id))
|
||||||
lambda id: make_fizzbaz(id=id))
|
@patch.object(FizzbazService, "update", fake_update)
|
||||||
@patch.object(FizzbazService, 'update', fake_update)
|
|
||||||
def test_put(self, client: FlaskClient): # noqa
|
def test_put(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
result = client.put(f'/api/{BASE_ROUTE}/fizzbaz/123',
|
result = client.put(
|
||||||
json={'name': 'New Fizzbaz',
|
f"/api/{BASE_ROUTE}/fizzbaz/123",
|
||||||
'purpose': 'New purpose'}).get_json()
|
json={"name": "New Fizzbaz", "purpose": "New purpose"},
|
||||||
expected = FizzbazSchema().dump(
|
).get_json()
|
||||||
Fizzbaz(fizzbaz_id=123, name='New Fizzbaz', purpose='New purpose')).data
|
expected = (
|
||||||
|
FizzbazSchema()
|
||||||
|
.dump(
|
||||||
|
Fizzbaz(fizzbaz_id=123, name="New Fizzbaz", purpose="New purpose")
|
||||||
|
)
|
||||||
|
.data
|
||||||
|
)
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
|
@ -5,9 +5,7 @@ from .interface import FizzbazInterface
|
||||||
|
|
||||||
@fixture
|
@fixture
|
||||||
def interface() -> FizzbazInterface:
|
def interface() -> FizzbazInterface:
|
||||||
return FizzbazInterface(
|
return FizzbazInterface(fizzbaz_id=1, name="Test fizzbaz", purpose="Test purpose")
|
||||||
fizzbaz_id=1, name='Test fizzbaz', purpose='Test purpose'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_FizzbazInterface_create(interface: FizzbazInterface):
|
def test_FizzbazInterface_create(interface: FizzbazInterface):
|
||||||
|
|
|
@ -5,9 +5,9 @@ from typing import Any
|
||||||
|
|
||||||
|
|
||||||
class Fizzbaz(db.Model): # type: ignore
|
class Fizzbaz(db.Model): # type: ignore
|
||||||
'''A snazzy Fizzbaz'''
|
"""A snazzy Fizzbaz"""
|
||||||
|
|
||||||
__tablename__ = 'fizzbaz'
|
__tablename__ = "fizzbaz"
|
||||||
|
|
||||||
fizzbaz_id = Column(Integer(), primary_key=True)
|
fizzbaz_id = Column(Integer(), primary_key=True)
|
||||||
name = Column(String(255))
|
name = Column(String(255))
|
||||||
|
|
|
@ -6,9 +6,7 @@ from .model import Fizzbaz
|
||||||
|
|
||||||
@fixture
|
@fixture
|
||||||
def fizzbaz() -> Fizzbaz:
|
def fizzbaz() -> Fizzbaz:
|
||||||
return Fizzbaz(
|
return Fizzbaz(fizzbaz_id=1, name="Test fizzbaz", purpose="Test purpose")
|
||||||
fizzbaz_id=1, name='Test fizzbaz', purpose='Test purpose'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_Fizzbaz_create(fizzbaz: Fizzbaz):
|
def test_Fizzbaz_create(fizzbaz: Fizzbaz):
|
||||||
|
|
|
@ -2,8 +2,8 @@ from marshmallow import fields, Schema
|
||||||
|
|
||||||
|
|
||||||
class FizzbazSchema(Schema):
|
class FizzbazSchema(Schema):
|
||||||
'''Fizzbaz schema'''
|
"""Fizzbaz schema"""
|
||||||
|
|
||||||
fizzbazId = fields.Number(attribute='fizzbaz_id')
|
fizzbazId = fields.Number(attribute="fizzbaz_id")
|
||||||
name = fields.String(attribute='name')
|
name = fields.String(attribute="name")
|
||||||
purpose = fields.String(attribute='purpose')
|
purpose = fields.String(attribute="purpose")
|
||||||
|
|
|
@ -15,13 +15,11 @@ def test_FizzbazSchema_create(schema: FizzbazSchema):
|
||||||
|
|
||||||
|
|
||||||
def test_FizzbazSchema_works(schema: FizzbazSchema):
|
def test_FizzbazSchema_works(schema: FizzbazSchema):
|
||||||
params: FizzbazInterface = schema.load({
|
params: FizzbazInterface = schema.load(
|
||||||
'fizzbazId': '123',
|
{"fizzbazId": "123", "name": "Test fizzbaz", "purpose": "Test purpose"}
|
||||||
'name': 'Test fizzbaz',
|
).data
|
||||||
'purpose': 'Test purpose'
|
|
||||||
}).data
|
|
||||||
fizzbaz = Fizzbaz(**params)
|
fizzbaz = Fizzbaz(**params)
|
||||||
|
|
||||||
assert fizzbaz.fizzbaz_id == 123
|
assert fizzbaz.fizzbaz_id == 123
|
||||||
assert fizzbaz.name == 'Test fizzbaz'
|
assert fizzbaz.name == "Test fizzbaz"
|
||||||
assert fizzbaz.purpose == 'Test purpose'
|
assert fizzbaz.purpose == "Test purpose"
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
BASE_ROUTE = 'other_api'
|
BASE_ROUTE = "other_api"
|
||||||
|
|
||||||
|
|
||||||
def register_routes(api, app, root='api'):
|
def register_routes(api, app, root="api"):
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
from flask_restplus import Api
|
from flask_restplus import Api
|
||||||
|
|
||||||
bp = Blueprint('other_api', __name__)
|
bp = Blueprint("other_api", __name__)
|
||||||
api = Api(bp, title='Another API with separate Swagger docs', version='0.1.0')
|
api = Api(bp, title="Another API with separate Swagger docs", version="0.1.0")
|
||||||
|
|
||||||
from .doodad.controller import api as doodad_api
|
from .doodad.controller import api as doodad_api
|
||||||
from .whatsit.controller import api as whatsit_api
|
from .whatsit.controller import api as whatsit_api
|
||||||
|
|
||||||
api.add_namespace(doodad_api, path=f'/doodad')
|
api.add_namespace(doodad_api, path=f"/doodad")
|
||||||
api.add_namespace(whatsit_api, path=f'/whatsit')
|
api.add_namespace(whatsit_api, path=f"/whatsit")
|
||||||
app.register_blueprint(bp, url_prefix=f'/{root}/{BASE_ROUTE}')
|
app.register_blueprint(bp, url_prefix=f"/{root}/{BASE_ROUTE}")
|
||||||
|
|
|
@ -9,47 +9,48 @@ from .service import DoodadService
|
||||||
from .model import Doodad
|
from .model import Doodad
|
||||||
from .interface import DoodadInterface
|
from .interface import DoodadInterface
|
||||||
|
|
||||||
api = Namespace('Doodad', description='A modular namespace within Other API') # noqa
|
api = Namespace("Doodad", description="A modular namespace within Other API") # noqa
|
||||||
|
|
||||||
|
|
||||||
@api.route('/')
|
@api.route("/")
|
||||||
class DoodadResource(Resource):
|
class DoodadResource(Resource):
|
||||||
'''Doodads'''
|
"""Doodads"""
|
||||||
|
|
||||||
@responds(schema=DoodadSchema, many=True)
|
@responds(schema=DoodadSchema, many=True)
|
||||||
def get(self) -> List[Doodad]:
|
def get(self) -> List[Doodad]:
|
||||||
'''Get all Doodads'''
|
"""Get all Doodads"""
|
||||||
|
|
||||||
return DoodadService.get_all()
|
return DoodadService.get_all()
|
||||||
|
|
||||||
@accepts(schema=DoodadSchema, api=api)
|
@accepts(schema=DoodadSchema, api=api)
|
||||||
@responds(schema=DoodadSchema)
|
@responds(schema=DoodadSchema)
|
||||||
def post(self) -> Doodad:
|
def post(self) -> Doodad:
|
||||||
'''Create a Single Doodad'''
|
"""Create a Single Doodad"""
|
||||||
|
|
||||||
return DoodadService.create(request.parsed_obj)
|
return DoodadService.create(request.parsed_obj)
|
||||||
|
|
||||||
|
|
||||||
@api.route('/<int:doodadId>')
|
@api.route("/<int:doodadId>")
|
||||||
@api.param('doodadId', 'Doodad database ID')
|
@api.param("doodadId", "Doodad database ID")
|
||||||
class DoodadIdResource(Resource):
|
class DoodadIdResource(Resource):
|
||||||
@responds(schema=DoodadSchema)
|
@responds(schema=DoodadSchema)
|
||||||
def get(self, doodadId: int) -> Doodad:
|
def get(self, doodadId: int) -> Doodad:
|
||||||
'''Get Single Doodad'''
|
"""Get Single Doodad"""
|
||||||
|
|
||||||
return DoodadService.get_by_id(doodadId)
|
return DoodadService.get_by_id(doodadId)
|
||||||
|
|
||||||
def delete(self, doodadId: int) -> Response:
|
def delete(self, doodadId: int) -> Response:
|
||||||
'''Delete Single Doodad'''
|
"""Delete Single Doodad"""
|
||||||
from flask import jsonify
|
from flask import jsonify
|
||||||
print('doodadId = ', doodadId)
|
|
||||||
|
print("doodadId = ", doodadId)
|
||||||
id = DoodadService.delete_by_id(doodadId)
|
id = DoodadService.delete_by_id(doodadId)
|
||||||
return jsonify(dict(status='Success', id=id))
|
return jsonify(dict(status="Success", id=id))
|
||||||
|
|
||||||
@accepts(schema=DoodadSchema, api=api)
|
@accepts(schema=DoodadSchema, api=api)
|
||||||
@responds(schema=DoodadSchema)
|
@responds(schema=DoodadSchema)
|
||||||
def put(self, doodadId: int) -> Doodad:
|
def put(self, doodadId: int) -> Doodad:
|
||||||
'''Update Single Doodad'''
|
"""Update Single Doodad"""
|
||||||
|
|
||||||
changes: DoodadInterface = request.parsed_obj
|
changes: DoodadInterface = request.parsed_obj
|
||||||
Doodad = DoodadService.get_by_id(doodadId)
|
Doodad = DoodadService.get_by_id(doodadId)
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
from flask.testing import FlaskClient
|
from flask.testing import FlaskClient
|
||||||
|
|
||||||
|
@ -10,75 +9,89 @@ from .interface import DoodadInterface
|
||||||
from .. import BASE_ROUTE
|
from .. import BASE_ROUTE
|
||||||
|
|
||||||
|
|
||||||
def make_doodad(id: int = 123, name: str = 'Test doodad',
|
def make_doodad(
|
||||||
purpose: str = 'Test purpose') -> Doodad:
|
id: int = 123, name: str = "Test doodad", purpose: str = "Test purpose"
|
||||||
return Doodad(
|
) -> Doodad:
|
||||||
doodad_id=id, name=name, purpose=purpose
|
return Doodad(doodad_id=id, name=name, purpose=purpose)
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestDoodadResource:
|
class TestDoodadResource:
|
||||||
@patch.object(DoodadService, 'get_all',
|
@patch.object(
|
||||||
lambda: [make_doodad(123, name='Test Doodad 1'),
|
DoodadService,
|
||||||
make_doodad(456, name='Test Doodad 2')])
|
"get_all",
|
||||||
|
lambda: [
|
||||||
|
make_doodad(123, name="Test Doodad 1"),
|
||||||
|
make_doodad(456, name="Test Doodad 2"),
|
||||||
|
],
|
||||||
|
)
|
||||||
def test_get(self, client: FlaskClient): # noqa
|
def test_get(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
results = client.get(f'/api/{BASE_ROUTE}/doodad',
|
results = client.get(
|
||||||
follow_redirects=True).get_json()
|
f"/api/{BASE_ROUTE}/doodad", follow_redirects=True
|
||||||
expected = DoodadSchema(many=True).dump(
|
).get_json()
|
||||||
[make_doodad(123, name='Test Doodad 1'),
|
expected = (
|
||||||
make_doodad(456, name='Test Doodad 2')]
|
DoodadSchema(many=True)
|
||||||
).data
|
.dump(
|
||||||
|
[
|
||||||
|
make_doodad(123, name="Test Doodad 1"),
|
||||||
|
make_doodad(456, name="Test Doodad 2"),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
.data
|
||||||
|
)
|
||||||
for r in results:
|
for r in results:
|
||||||
assert r in expected
|
assert r in expected
|
||||||
|
|
||||||
@patch.object(DoodadService, 'create',
|
@patch.object(
|
||||||
lambda create_request: Doodad(**create_request))
|
DoodadService, "create", lambda create_request: Doodad(**create_request)
|
||||||
|
)
|
||||||
def test_post(self, client: FlaskClient): # noqa
|
def test_post(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
|
|
||||||
payload = dict(name='Test doodad', purpose='Test purpose')
|
payload = dict(name="Test doodad", purpose="Test purpose")
|
||||||
result = client.post(f'/api/{BASE_ROUTE}/doodad/', json=payload).get_json()
|
result = client.post(f"/api/{BASE_ROUTE}/doodad/", json=payload).get_json()
|
||||||
expected = DoodadSchema().dump(Doodad(
|
expected = (
|
||||||
name=payload['name'],
|
DoodadSchema()
|
||||||
purpose=payload['purpose'],
|
.dump(Doodad(name=payload["name"], purpose=payload["purpose"]))
|
||||||
)).data
|
.data
|
||||||
|
)
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
|
|
||||||
def fake_update(doodad: Doodad, changes: DoodadInterface) -> Doodad:
|
def fake_update(doodad: Doodad, changes: DoodadInterface) -> Doodad:
|
||||||
# To fake an update, just return a new object
|
# To fake an update, just return a new object
|
||||||
updated_Doodad = Doodad(doodad_id=doodad.doodad_id,
|
updated_Doodad = Doodad(
|
||||||
name=changes['name'],
|
doodad_id=doodad.doodad_id, name=changes["name"], purpose=changes["purpose"]
|
||||||
purpose=changes['purpose'])
|
)
|
||||||
return updated_Doodad
|
return updated_Doodad
|
||||||
|
|
||||||
|
|
||||||
class TestDoodadIdResource:
|
class TestDoodadIdResource:
|
||||||
@patch.object(DoodadService, 'get_by_id',
|
@patch.object(DoodadService, "get_by_id", lambda id: make_doodad(id=id))
|
||||||
lambda id: make_doodad(id=id))
|
|
||||||
def test_get(self, client: FlaskClient): # noqa
|
def test_get(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
result = client.get(f'/api/{BASE_ROUTE}/doodad/123').get_json()
|
result = client.get(f"/api/{BASE_ROUTE}/doodad/123").get_json()
|
||||||
expected = Doodad(doodad_id=123)
|
expected = Doodad(doodad_id=123)
|
||||||
assert result['doodadId'] == expected.doodad_id
|
assert result["doodadId"] == expected.doodad_id
|
||||||
|
|
||||||
@patch.object(DoodadService, 'delete_by_id',
|
@patch.object(DoodadService, "delete_by_id", lambda id: [id])
|
||||||
lambda id: [id])
|
|
||||||
def test_delete(self, client: FlaskClient): # noqa
|
def test_delete(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
result = client.delete(f'/api/{BASE_ROUTE}/doodad/123').get_json()
|
result = client.delete(f"/api/{BASE_ROUTE}/doodad/123").get_json()
|
||||||
expected = dict(status='Success', id=[123])
|
expected = dict(status="Success", id=[123])
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
@patch.object(DoodadService, 'get_by_id',
|
@patch.object(DoodadService, "get_by_id", lambda id: make_doodad(id=id))
|
||||||
lambda id: make_doodad(id=id))
|
@patch.object(DoodadService, "update", fake_update)
|
||||||
@patch.object(DoodadService, 'update', fake_update)
|
|
||||||
def test_put(self, client: FlaskClient): # noqa
|
def test_put(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
result = client.put(f'/api/{BASE_ROUTE}/doodad/123',
|
result = client.put(
|
||||||
json={'name': 'New Doodad',
|
f"/api/{BASE_ROUTE}/doodad/123",
|
||||||
'purpose': 'New purpose'}).get_json()
|
json={"name": "New Doodad", "purpose": "New purpose"},
|
||||||
expected = DoodadSchema().dump(
|
).get_json()
|
||||||
Doodad(doodad_id=123, name='New Doodad', purpose='New purpose')).data
|
expected = (
|
||||||
|
DoodadSchema()
|
||||||
|
.dump(Doodad(doodad_id=123, name="New Doodad", purpose="New purpose"))
|
||||||
|
.data
|
||||||
|
)
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
|
@ -5,9 +5,7 @@ from .interface import DoodadInterface
|
||||||
|
|
||||||
@fixture
|
@fixture
|
||||||
def interface() -> DoodadInterface:
|
def interface() -> DoodadInterface:
|
||||||
return DoodadInterface(
|
return DoodadInterface(doodad_id=1, name="Test doodad", purpose="Test purpose")
|
||||||
doodad_id=1, name='Test doodad', purpose='Test purpose'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_DoodadInterface_create(interface: DoodadInterface):
|
def test_DoodadInterface_create(interface: DoodadInterface):
|
||||||
|
|
|
@ -5,9 +5,9 @@ from typing import Any
|
||||||
|
|
||||||
|
|
||||||
class Doodad(db.Model): # type: ignore
|
class Doodad(db.Model): # type: ignore
|
||||||
'''A snazzy Doodad'''
|
"""A snazzy Doodad"""
|
||||||
|
|
||||||
__tablename__ = 'doodad'
|
__tablename__ = "doodad"
|
||||||
|
|
||||||
doodad_id = Column(Integer(), primary_key=True)
|
doodad_id = Column(Integer(), primary_key=True)
|
||||||
name = Column(String(255))
|
name = Column(String(255))
|
||||||
|
|
|
@ -6,9 +6,7 @@ from .model import Doodad
|
||||||
|
|
||||||
@fixture
|
@fixture
|
||||||
def doodad() -> Doodad:
|
def doodad() -> Doodad:
|
||||||
return Doodad(
|
return Doodad(doodad_id=1, name="Test doodad", purpose="Test purpose")
|
||||||
doodad_id=1, name='Test doodad', purpose='Test purpose'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_Doodad_create(doodad: Doodad):
|
def test_Doodad_create(doodad: Doodad):
|
||||||
|
|
|
@ -2,8 +2,8 @@ from marshmallow import fields, Schema
|
||||||
|
|
||||||
|
|
||||||
class DoodadSchema(Schema):
|
class DoodadSchema(Schema):
|
||||||
'''Doodad schema'''
|
"""Doodad schema"""
|
||||||
|
|
||||||
doodadId = fields.Number(attribute='doodad_id')
|
doodadId = fields.Number(attribute="doodad_id")
|
||||||
name = fields.String(attribute='name')
|
name = fields.String(attribute="name")
|
||||||
purpose = fields.String(attribute='purpose')
|
purpose = fields.String(attribute="purpose")
|
||||||
|
|
|
@ -15,13 +15,11 @@ def test_DoodadSchema_create(schema: DoodadSchema):
|
||||||
|
|
||||||
|
|
||||||
def test_DoodadSchema_works(schema: DoodadSchema):
|
def test_DoodadSchema_works(schema: DoodadSchema):
|
||||||
params: DoodadInterface = schema.load({
|
params: DoodadInterface = schema.load(
|
||||||
'doodadId': '123',
|
{"doodadId": "123", "name": "Test doodad", "purpose": "Test purpose"}
|
||||||
'name': 'Test doodad',
|
).data
|
||||||
'purpose': 'Test purpose'
|
|
||||||
}).data
|
|
||||||
doodad = Doodad(**params)
|
doodad = Doodad(**params)
|
||||||
|
|
||||||
assert doodad.doodad_id == 123
|
assert doodad.doodad_id == 123
|
||||||
assert doodad.name == 'Test doodad'
|
assert doodad.name == "Test doodad"
|
||||||
assert doodad.purpose == 'Test purpose'
|
assert doodad.purpose == "Test purpose"
|
||||||
|
|
|
@ -4,7 +4,7 @@ from .model import Doodad
|
||||||
from .interface import DoodadInterface
|
from .interface import DoodadInterface
|
||||||
|
|
||||||
|
|
||||||
class DoodadService():
|
class DoodadService:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_all() -> List[Doodad]:
|
def get_all() -> List[Doodad]:
|
||||||
return Doodad.query.all()
|
return Doodad.query.all()
|
||||||
|
@ -30,10 +30,7 @@ class DoodadService():
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(new_attrs: DoodadInterface) -> Doodad:
|
def create(new_attrs: DoodadInterface) -> Doodad:
|
||||||
new_doodad = Doodad(
|
new_doodad = Doodad(name=new_attrs["name"], purpose=new_attrs["purpose"])
|
||||||
name=new_attrs['name'],
|
|
||||||
purpose=new_attrs['purpose']
|
|
||||||
)
|
|
||||||
|
|
||||||
db.session.add(new_doodad)
|
db.session.add(new_doodad)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
|
@ -7,8 +7,8 @@ from .interface import DoodadInterface
|
||||||
|
|
||||||
|
|
||||||
def test_get_all(db: SQLAlchemy): # noqa
|
def test_get_all(db: SQLAlchemy): # noqa
|
||||||
yin: Doodad = Doodad(doodad_id=1, name='Yin', purpose='thing 1')
|
yin: Doodad = Doodad(doodad_id=1, name="Yin", purpose="thing 1")
|
||||||
yang: Doodad = Doodad(doodad_id=2, name='Yang', purpose='thing 2')
|
yang: Doodad = Doodad(doodad_id=2, name="Yang", purpose="thing 2")
|
||||||
db.session.add(yin)
|
db.session.add(yin)
|
||||||
db.session.add(yang)
|
db.session.add(yang)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -20,21 +20,21 @@ def test_get_all(db: SQLAlchemy): # noqa
|
||||||
|
|
||||||
|
|
||||||
def test_update(db: SQLAlchemy): # noqa
|
def test_update(db: SQLAlchemy): # noqa
|
||||||
yin: Doodad = Doodad(doodad_id=1, name='Yin', purpose='thing 1')
|
yin: Doodad = Doodad(doodad_id=1, name="Yin", purpose="thing 1")
|
||||||
|
|
||||||
db.session.add(yin)
|
db.session.add(yin)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
updates: DoodadInterface = dict(name='New Doodad name')
|
updates: DoodadInterface = dict(name="New Doodad name")
|
||||||
|
|
||||||
DoodadService.update(yin, updates)
|
DoodadService.update(yin, updates)
|
||||||
|
|
||||||
result: Doodad = Doodad.query.get(yin.doodad_id)
|
result: Doodad = Doodad.query.get(yin.doodad_id)
|
||||||
assert result.name == 'New Doodad name'
|
assert result.name == "New Doodad name"
|
||||||
|
|
||||||
|
|
||||||
def test_delete_by_id(db: SQLAlchemy): # noqa
|
def test_delete_by_id(db: SQLAlchemy): # noqa
|
||||||
yin: Doodad = Doodad(doodad_id=1, name='Yin', purpose='thing 1')
|
yin: Doodad = Doodad(doodad_id=1, name="Yin", purpose="thing 1")
|
||||||
yang: Doodad = Doodad(doodad_id=2, name='Yang', purpose='thing 2')
|
yang: Doodad = Doodad(doodad_id=2, name="Yang", purpose="thing 2")
|
||||||
db.session.add(yin)
|
db.session.add(yin)
|
||||||
db.session.add(yang)
|
db.session.add(yang)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -50,7 +50,7 @@ def test_delete_by_id(db: SQLAlchemy): # noqa
|
||||||
|
|
||||||
def test_create(db: SQLAlchemy): # noqa
|
def test_create(db: SQLAlchemy): # noqa
|
||||||
|
|
||||||
yin: DoodadInterface = dict(name='Fancy new doodad', purpose='Fancy new purpose')
|
yin: DoodadInterface = dict(name="Fancy new doodad", purpose="Fancy new purpose")
|
||||||
DoodadService.create(yin)
|
DoodadService.create(yin)
|
||||||
results: List[Doodad] = Doodad.query.all()
|
results: List[Doodad] = Doodad.query.all()
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
from flask.testing import FlaskClient
|
from flask.testing import FlaskClient
|
||||||
|
|
||||||
|
@ -10,75 +9,91 @@ from .interface import WhatsitInterface
|
||||||
from .. import BASE_ROUTE
|
from .. import BASE_ROUTE
|
||||||
|
|
||||||
|
|
||||||
def make_whatsit(id: int = 123, name: str = 'Test whatsit',
|
def make_whatsit(
|
||||||
purpose: str = 'Test purpose') -> Whatsit:
|
id: int = 123, name: str = "Test whatsit", purpose: str = "Test purpose"
|
||||||
return Whatsit(
|
) -> Whatsit:
|
||||||
whatsit_id=id, name=name, purpose=purpose
|
return Whatsit(whatsit_id=id, name=name, purpose=purpose)
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestWhatsitResource:
|
class TestWhatsitResource:
|
||||||
@patch.object(WhatsitService, 'get_all',
|
@patch.object(
|
||||||
lambda: [make_whatsit(123, name='Test Whatsit 1'),
|
WhatsitService,
|
||||||
make_whatsit(456, name='Test Whatsit 2')])
|
"get_all",
|
||||||
|
lambda: [
|
||||||
|
make_whatsit(123, name="Test Whatsit 1"),
|
||||||
|
make_whatsit(456, name="Test Whatsit 2"),
|
||||||
|
],
|
||||||
|
)
|
||||||
def test_get(self, client: FlaskClient): # noqa
|
def test_get(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
results = client.get(f'/api/{BASE_ROUTE}/whatsit',
|
results = client.get(
|
||||||
follow_redirects=True).get_json()
|
f"/api/{BASE_ROUTE}/whatsit", follow_redirects=True
|
||||||
expected = WhatsitSchema(many=True).dump(
|
).get_json()
|
||||||
[make_whatsit(123, name='Test Whatsit 1'),
|
expected = (
|
||||||
make_whatsit(456, name='Test Whatsit 2')]
|
WhatsitSchema(many=True)
|
||||||
).data
|
.dump(
|
||||||
|
[
|
||||||
|
make_whatsit(123, name="Test Whatsit 1"),
|
||||||
|
make_whatsit(456, name="Test Whatsit 2"),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
.data
|
||||||
|
)
|
||||||
for r in results:
|
for r in results:
|
||||||
assert r in expected
|
assert r in expected
|
||||||
|
|
||||||
@patch.object(WhatsitService, 'create',
|
@patch.object(
|
||||||
lambda create_request: Whatsit(**create_request))
|
WhatsitService, "create", lambda create_request: Whatsit(**create_request)
|
||||||
|
)
|
||||||
def test_post(self, client: FlaskClient): # noqa
|
def test_post(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
|
|
||||||
payload = dict(name='Test whatsit', purpose='Test purpose')
|
payload = dict(name="Test whatsit", purpose="Test purpose")
|
||||||
result = client.post(f'/api/{BASE_ROUTE}/whatsit/', json=payload).get_json()
|
result = client.post(f"/api/{BASE_ROUTE}/whatsit/", json=payload).get_json()
|
||||||
expected = WhatsitSchema().dump(Whatsit(
|
expected = (
|
||||||
name=payload['name'],
|
WhatsitSchema()
|
||||||
purpose=payload['purpose'],
|
.dump(Whatsit(name=payload["name"], purpose=payload["purpose"]))
|
||||||
)).data
|
.data
|
||||||
|
)
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
|
|
||||||
def fake_update(whatsit: Whatsit, changes: WhatsitInterface) -> Whatsit:
|
def fake_update(whatsit: Whatsit, changes: WhatsitInterface) -> Whatsit:
|
||||||
# To fake an update, just return a new object
|
# To fake an update, just return a new object
|
||||||
updated_Whatsit = Whatsit(whatsit_id=whatsit.whatsit_id,
|
updated_Whatsit = Whatsit(
|
||||||
name=changes['name'],
|
whatsit_id=whatsit.whatsit_id, name=changes["name"], purpose=changes["purpose"]
|
||||||
purpose=changes['purpose'])
|
)
|
||||||
return updated_Whatsit
|
return updated_Whatsit
|
||||||
|
|
||||||
|
|
||||||
class TestWhatsitIdResource:
|
class TestWhatsitIdResource:
|
||||||
@patch.object(WhatsitService, 'get_by_id',
|
@patch.object(WhatsitService, "get_by_id", lambda id: make_whatsit(id=id))
|
||||||
lambda id: make_whatsit(id=id))
|
|
||||||
def test_get(self, client: FlaskClient): # noqa
|
def test_get(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
result = client.get(f'/api/{BASE_ROUTE}/whatsit/123').get_json()
|
result = client.get(f"/api/{BASE_ROUTE}/whatsit/123").get_json()
|
||||||
expected = Whatsit(whatsit_id=123)
|
expected = Whatsit(whatsit_id=123)
|
||||||
assert result['whatsitId'] == expected.whatsit_id
|
assert result["whatsitId"] == expected.whatsit_id
|
||||||
|
|
||||||
@patch.object(WhatsitService, 'delete_by_id',
|
@patch.object(WhatsitService, "delete_by_id", lambda id: [id])
|
||||||
lambda id: [id])
|
|
||||||
def test_delete(self, client: FlaskClient): # noqa
|
def test_delete(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
result = client.delete(f'/api/{BASE_ROUTE}/whatsit/123').get_json()
|
result = client.delete(f"/api/{BASE_ROUTE}/whatsit/123").get_json()
|
||||||
expected = dict(status='Success', id=[123])
|
expected = dict(status="Success", id=[123])
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
@patch.object(WhatsitService, 'get_by_id',
|
@patch.object(WhatsitService, "get_by_id", lambda id: make_whatsit(id=id))
|
||||||
lambda id: make_whatsit(id=id))
|
@patch.object(WhatsitService, "update", fake_update)
|
||||||
@patch.object(WhatsitService, 'update', fake_update)
|
|
||||||
def test_put(self, client: FlaskClient): # noqa
|
def test_put(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
result = client.put(f'/api/{BASE_ROUTE}/whatsit/123',
|
result = client.put(
|
||||||
json={'name': 'New Whatsit',
|
f"/api/{BASE_ROUTE}/whatsit/123",
|
||||||
'purpose': 'New purpose'}).get_json()
|
json={"name": "New Whatsit", "purpose": "New purpose"},
|
||||||
expected = WhatsitSchema().dump(
|
).get_json()
|
||||||
Whatsit(whatsit_id=123, name='New Whatsit', purpose='New purpose')).data
|
expected = (
|
||||||
|
WhatsitSchema()
|
||||||
|
.dump(
|
||||||
|
Whatsit(whatsit_id=123, name="New Whatsit", purpose="New purpose")
|
||||||
|
)
|
||||||
|
.data
|
||||||
|
)
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
|
@ -5,9 +5,7 @@ from .interface import WhatsitInterface
|
||||||
|
|
||||||
@fixture
|
@fixture
|
||||||
def interface() -> WhatsitInterface:
|
def interface() -> WhatsitInterface:
|
||||||
return WhatsitInterface(
|
return WhatsitInterface(whatsit_id=1, name="Test whatsit", purpose="Test purpose")
|
||||||
whatsit_id=1, name='Test whatsit', purpose='Test purpose'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_WhatsitInterface_create(interface: WhatsitInterface):
|
def test_WhatsitInterface_create(interface: WhatsitInterface):
|
||||||
|
|
|
@ -5,9 +5,9 @@ from typing import Any
|
||||||
|
|
||||||
|
|
||||||
class Whatsit(db.Model): # type: ignore
|
class Whatsit(db.Model): # type: ignore
|
||||||
'''A snazzy Whatsit'''
|
"""A snazzy Whatsit"""
|
||||||
|
|
||||||
__tablename__ = 'whatsit'
|
__tablename__ = "whatsit"
|
||||||
|
|
||||||
whatsit_id = Column(Integer(), primary_key=True)
|
whatsit_id = Column(Integer(), primary_key=True)
|
||||||
name = Column(String(255))
|
name = Column(String(255))
|
||||||
|
|
|
@ -6,9 +6,7 @@ from .model import Whatsit
|
||||||
|
|
||||||
@fixture
|
@fixture
|
||||||
def whatsit() -> Whatsit:
|
def whatsit() -> Whatsit:
|
||||||
return Whatsit(
|
return Whatsit(whatsit_id=1, name="Test whatsit", purpose="Test purpose")
|
||||||
whatsit_id=1, name='Test whatsit', purpose='Test purpose'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_Whatsit_create(whatsit: Whatsit):
|
def test_Whatsit_create(whatsit: Whatsit):
|
||||||
|
|
|
@ -2,8 +2,8 @@ from marshmallow import fields, Schema
|
||||||
|
|
||||||
|
|
||||||
class WhatsitSchema(Schema):
|
class WhatsitSchema(Schema):
|
||||||
'''Whatsit schema'''
|
"""Whatsit schema"""
|
||||||
|
|
||||||
whatsitId = fields.Number(attribute='whatsit_id')
|
whatsitId = fields.Number(attribute="whatsit_id")
|
||||||
name = fields.String(attribute='name')
|
name = fields.String(attribute="name")
|
||||||
purpose = fields.String(attribute='purpose')
|
purpose = fields.String(attribute="purpose")
|
||||||
|
|
|
@ -15,13 +15,11 @@ def test_WhatsitSchema_create(schema: WhatsitSchema):
|
||||||
|
|
||||||
|
|
||||||
def test_WhatsitSchema_works(schema: WhatsitSchema):
|
def test_WhatsitSchema_works(schema: WhatsitSchema):
|
||||||
params: WhatsitInterface = schema.load({
|
params: WhatsitInterface = schema.load(
|
||||||
'whatsitId': '123',
|
{"whatsitId": "123", "name": "Test whatsit", "purpose": "Test purpose"}
|
||||||
'name': 'Test whatsit',
|
).data
|
||||||
'purpose': 'Test purpose'
|
|
||||||
}).data
|
|
||||||
whatsit = Whatsit(**params)
|
whatsit = Whatsit(**params)
|
||||||
|
|
||||||
assert whatsit.whatsit_id == 123
|
assert whatsit.whatsit_id == 123
|
||||||
assert whatsit.name == 'Test whatsit'
|
assert whatsit.name == "Test whatsit"
|
||||||
assert whatsit.purpose == 'Test purpose'
|
assert whatsit.purpose == "Test purpose"
|
||||||
|
|
|
@ -4,7 +4,7 @@ from .model import Whatsit
|
||||||
from .interface import WhatsitInterface
|
from .interface import WhatsitInterface
|
||||||
|
|
||||||
|
|
||||||
class WhatsitService():
|
class WhatsitService:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_all() -> List[Whatsit]:
|
def get_all() -> List[Whatsit]:
|
||||||
return Whatsit.query.all()
|
return Whatsit.query.all()
|
||||||
|
@ -30,10 +30,7 @@ class WhatsitService():
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(new_attrs: WhatsitInterface) -> Whatsit:
|
def create(new_attrs: WhatsitInterface) -> Whatsit:
|
||||||
new_whatsit = Whatsit(
|
new_whatsit = Whatsit(name=new_attrs["name"], purpose=new_attrs["purpose"])
|
||||||
name=new_attrs['name'],
|
|
||||||
purpose=new_attrs['purpose']
|
|
||||||
)
|
|
||||||
|
|
||||||
db.session.add(new_whatsit)
|
db.session.add(new_whatsit)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
|
@ -7,8 +7,8 @@ from .interface import WhatsitInterface
|
||||||
|
|
||||||
|
|
||||||
def test_get_all(db: SQLAlchemy): # noqa
|
def test_get_all(db: SQLAlchemy): # noqa
|
||||||
yin: Whatsit = Whatsit(whatsit_id=1, name='Yin', purpose='thing 1')
|
yin: Whatsit = Whatsit(whatsit_id=1, name="Yin", purpose="thing 1")
|
||||||
yang: Whatsit = Whatsit(whatsit_id=2, name='Yang', purpose='thing 2')
|
yang: Whatsit = Whatsit(whatsit_id=2, name="Yang", purpose="thing 2")
|
||||||
db.session.add(yin)
|
db.session.add(yin)
|
||||||
db.session.add(yang)
|
db.session.add(yang)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -20,21 +20,21 @@ def test_get_all(db: SQLAlchemy): # noqa
|
||||||
|
|
||||||
|
|
||||||
def test_update(db: SQLAlchemy): # noqa
|
def test_update(db: SQLAlchemy): # noqa
|
||||||
yin: Whatsit = Whatsit(whatsit_id=1, name='Yin', purpose='thing 1')
|
yin: Whatsit = Whatsit(whatsit_id=1, name="Yin", purpose="thing 1")
|
||||||
|
|
||||||
db.session.add(yin)
|
db.session.add(yin)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
updates: WhatsitInterface = dict(name='New Whatsit name')
|
updates: WhatsitInterface = dict(name="New Whatsit name")
|
||||||
|
|
||||||
WhatsitService.update(yin, updates)
|
WhatsitService.update(yin, updates)
|
||||||
|
|
||||||
result: Whatsit = Whatsit.query.get(yin.whatsit_id)
|
result: Whatsit = Whatsit.query.get(yin.whatsit_id)
|
||||||
assert result.name == 'New Whatsit name'
|
assert result.name == "New Whatsit name"
|
||||||
|
|
||||||
|
|
||||||
def test_delete_by_id(db: SQLAlchemy): # noqa
|
def test_delete_by_id(db: SQLAlchemy): # noqa
|
||||||
yin: Whatsit = Whatsit(whatsit_id=1, name='Yin', purpose='thing 1')
|
yin: Whatsit = Whatsit(whatsit_id=1, name="Yin", purpose="thing 1")
|
||||||
yang: Whatsit = Whatsit(whatsit_id=2, name='Yang', purpose='thing 2')
|
yang: Whatsit = Whatsit(whatsit_id=2, name="Yang", purpose="thing 2")
|
||||||
db.session.add(yin)
|
db.session.add(yin)
|
||||||
db.session.add(yang)
|
db.session.add(yang)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -50,7 +50,7 @@ def test_delete_by_id(db: SQLAlchemy): # noqa
|
||||||
|
|
||||||
def test_create(db: SQLAlchemy): # noqa
|
def test_create(db: SQLAlchemy): # noqa
|
||||||
|
|
||||||
yin: WhatsitInterface = dict(name='Fancy new whatsit', purpose='Fancy new purpose')
|
yin: WhatsitInterface = dict(name="Fancy new whatsit", purpose="Fancy new purpose")
|
||||||
WhatsitService.create(yin)
|
WhatsitService.create(yin)
|
||||||
results: List[Whatsit] = Whatsit.query.all()
|
results: List[Whatsit] = Whatsit.query.all()
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
def register_routes(api, app, root='api'):
|
def register_routes(api, app, root="api"):
|
||||||
from app.widget import register_routes as attach_widget
|
from app.widget import register_routes as attach_widget
|
||||||
from app.fizz import register_routes as attach_fizz
|
from app.fizz import register_routes as attach_fizz
|
||||||
from app.other_api import register_routes as attach_other_api
|
from app.other_api import register_routes as attach_other_api
|
||||||
|
@ -8,4 +8,4 @@ def register_routes(api, app, root='api'):
|
||||||
attach_widget(api, app)
|
attach_widget(api, app)
|
||||||
attach_fizz(api, app)
|
attach_fizz(api, app)
|
||||||
attach_other_api(api, app)
|
attach_other_api(api, app)
|
||||||
app.register_blueprint(create_bp(), url_prefix='/third_party')
|
app.register_blueprint(create_bp(), url_prefix="/third_party")
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
class QueryService:
|
class QueryService:
|
||||||
'''An example of a service that is shared'''
|
"""An example of a service that is shared"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def execute(query):
|
def execute(query):
|
||||||
return 'Success'
|
return "Success"
|
||||||
|
|
|
@ -2,6 +2,6 @@ from .service import QueryService
|
||||||
|
|
||||||
|
|
||||||
def test_execute():
|
def test_execute():
|
||||||
result = QueryService.execute('a complicated query')
|
result = QueryService.execute("a complicated query")
|
||||||
|
|
||||||
assert result == 'Success'
|
assert result == "Success"
|
||||||
|
|
|
@ -5,7 +5,7 @@ from app import create_app
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def app():
|
def app():
|
||||||
return create_app('test')
|
return create_app("test")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
@ -16,6 +16,7 @@ def client(app):
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def db(app):
|
def db(app):
|
||||||
from app import db
|
from app import db
|
||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.create_all()
|
db.create_all()
|
||||||
yield db
|
yield db
|
||||||
|
|
12
app/third_party/app/__init__.py
vendored
12
app/third_party/app/__init__.py
vendored
|
@ -3,12 +3,14 @@ def create_bp(env=None):
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from flask_restplus import Api, Resource, Namespace
|
from flask_restplus import Api, Resource, Namespace
|
||||||
|
|
||||||
bp = Blueprint('Example third party API', __name__)
|
bp = Blueprint("Example third party API", __name__)
|
||||||
api = Api(bp, title='Flaskerific API', version='0.1.0')
|
api = Api(bp, title="Flaskerific API", version="0.1.0")
|
||||||
ns = Namespace('Third party hello world API')
|
ns = Namespace("Third party hello world API")
|
||||||
@ns.route('/')
|
|
||||||
|
@ns.route("/")
|
||||||
class ExampleResource(Resource):
|
class ExampleResource(Resource):
|
||||||
def get(self):
|
def get(self):
|
||||||
return "I'm a third party API!"
|
return "I'm a third party API!"
|
||||||
api.add_namespace(ns, path='/hello_world')
|
|
||||||
|
api.add_namespace(ns, path="/hello_world")
|
||||||
return bp
|
return bp
|
||||||
|
|
16
app/third_party/app/config.py
vendored
16
app/third_party/app/config.py
vendored
|
@ -5,16 +5,17 @@ basedir = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
||||||
|
|
||||||
class BaseConfig:
|
class BaseConfig:
|
||||||
CONFIG_NAME = 'base'
|
CONFIG_NAME = "base"
|
||||||
USE_MOCK_EQUIVALENCY = False
|
USE_MOCK_EQUIVALENCY = False
|
||||||
DEBUG = False
|
DEBUG = False
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
|
|
||||||
|
|
||||||
class DevelopmentConfig(BaseConfig):
|
class DevelopmentConfig(BaseConfig):
|
||||||
CONFIG_NAME = 'dev'
|
CONFIG_NAME = "dev"
|
||||||
SECRET_KEY = os.getenv(
|
SECRET_KEY = os.getenv(
|
||||||
"DEV_SECRET_KEY", "You can't see California without Marlon Widgeto's eyes")
|
"DEV_SECRET_KEY", "You can't see California without Marlon Widgeto's eyes"
|
||||||
|
)
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
TESTING = False
|
TESTING = False
|
||||||
|
@ -22,7 +23,7 @@ class DevelopmentConfig(BaseConfig):
|
||||||
|
|
||||||
|
|
||||||
class TestingConfig(BaseConfig):
|
class TestingConfig(BaseConfig):
|
||||||
CONFIG_NAME = 'test'
|
CONFIG_NAME = "test"
|
||||||
SECRET_KEY = os.getenv("TEST_SECRET_KEY", "Thanos did nothing wrong")
|
SECRET_KEY = os.getenv("TEST_SECRET_KEY", "Thanos did nothing wrong")
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
|
@ -31,7 +32,7 @@ class TestingConfig(BaseConfig):
|
||||||
|
|
||||||
|
|
||||||
class ProductionConfig(BaseConfig):
|
class ProductionConfig(BaseConfig):
|
||||||
CONFIG_NAME = 'prod'
|
CONFIG_NAME = "prod"
|
||||||
SECRET_KEY = os.getenv("PROD_SECRET_KEY", "I'm Ron Burgundy?")
|
SECRET_KEY = os.getenv("PROD_SECRET_KEY", "I'm Ron Burgundy?")
|
||||||
DEBUG = False
|
DEBUG = False
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
|
@ -40,5 +41,8 @@ class ProductionConfig(BaseConfig):
|
||||||
|
|
||||||
|
|
||||||
EXPORT_CONFIGS: List[Type[BaseConfig]] = [
|
EXPORT_CONFIGS: List[Type[BaseConfig]] = [
|
||||||
DevelopmentConfig, TestingConfig, ProductionConfig]
|
DevelopmentConfig,
|
||||||
|
TestingConfig,
|
||||||
|
ProductionConfig,
|
||||||
|
]
|
||||||
config_by_name = {cfg.CONFIG_NAME: cfg for cfg in EXPORT_CONFIGS}
|
config_by_name = {cfg.CONFIG_NAME: cfg for cfg in EXPORT_CONFIGS}
|
||||||
|
|
2
app/third_party/app/routes.py
vendored
2
app/third_party/app/routes.py
vendored
|
@ -1,4 +1,4 @@
|
||||||
def register_routes(api, app, root='api'):
|
def register_routes(api, app, root="api"):
|
||||||
from app.widget import register_routes as attach_widget
|
from app.widget import register_routes as attach_widget
|
||||||
from app.fizz import register_routes as attach_fizz
|
from app.fizz import register_routes as attach_fizz
|
||||||
from app.other_api import register_routes as attach_other_api
|
from app.other_api import register_routes as attach_other_api
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
from .model import Widget # noqa
|
from .model import Widget # noqa
|
||||||
from .schema import WidgetSchema # noqa
|
from .schema import WidgetSchema # noqa
|
||||||
BASE_ROUTE = 'widget'
|
|
||||||
|
BASE_ROUTE = "widget"
|
||||||
|
|
||||||
|
|
||||||
def register_routes(api, app, root='api'):
|
def register_routes(api, app, root="api"):
|
||||||
from .controller import api as widget_api
|
from .controller import api as widget_api
|
||||||
|
|
||||||
api.add_namespace(widget_api, path=f'/{root}/{BASE_ROUTE}')
|
api.add_namespace(widget_api, path=f"/{root}/{BASE_ROUTE}")
|
||||||
|
|
|
@ -9,47 +9,47 @@ from .service import WidgetService
|
||||||
from .model import Widget
|
from .model import Widget
|
||||||
from .interface import WidgetInterface
|
from .interface import WidgetInterface
|
||||||
|
|
||||||
api = Namespace('Widget', description='Single namespace, single entity') # noqa
|
api = Namespace("Widget", description="Single namespace, single entity") # noqa
|
||||||
|
|
||||||
|
|
||||||
@api.route('/')
|
@api.route("/")
|
||||||
class WidgetResource(Resource):
|
class WidgetResource(Resource):
|
||||||
'''Widgets'''
|
"""Widgets"""
|
||||||
|
|
||||||
@responds(schema=WidgetSchema, many=True)
|
@responds(schema=WidgetSchema, many=True)
|
||||||
def get(self) -> List[Widget]:
|
def get(self) -> List[Widget]:
|
||||||
'''Get all Widgets'''
|
"""Get all Widgets"""
|
||||||
|
|
||||||
return WidgetService.get_all()
|
return WidgetService.get_all()
|
||||||
|
|
||||||
@accepts(schema=WidgetSchema, api=api)
|
@accepts(schema=WidgetSchema, api=api)
|
||||||
@responds(schema=WidgetSchema)
|
@responds(schema=WidgetSchema)
|
||||||
def post(self) -> Widget:
|
def post(self) -> Widget:
|
||||||
'''Create a Single Widget'''
|
"""Create a Single Widget"""
|
||||||
|
|
||||||
return WidgetService.create(request.parsed_obj)
|
return WidgetService.create(request.parsed_obj)
|
||||||
|
|
||||||
|
|
||||||
@api.route('/<int:widgetId>')
|
@api.route("/<int:widgetId>")
|
||||||
@api.param('widgetId', 'Widget database ID')
|
@api.param("widgetId", "Widget database ID")
|
||||||
class WidgetIdResource(Resource):
|
class WidgetIdResource(Resource):
|
||||||
@responds(schema=WidgetSchema)
|
@responds(schema=WidgetSchema)
|
||||||
def get(self, widgetId: int) -> Widget:
|
def get(self, widgetId: int) -> Widget:
|
||||||
'''Get Single Widget'''
|
"""Get Single Widget"""
|
||||||
|
|
||||||
return WidgetService.get_by_id(widgetId)
|
return WidgetService.get_by_id(widgetId)
|
||||||
|
|
||||||
def delete(self, widgetId: int) -> Response:
|
def delete(self, widgetId: int) -> Response:
|
||||||
'''Delete Single Widget'''
|
"""Delete Single Widget"""
|
||||||
from flask import jsonify
|
from flask import jsonify
|
||||||
|
|
||||||
id = WidgetService.delete_by_id(widgetId)
|
id = WidgetService.delete_by_id(widgetId)
|
||||||
return jsonify(dict(status='Success', id=id))
|
return jsonify(dict(status="Success", id=id))
|
||||||
|
|
||||||
@accepts(schema=WidgetSchema, api=api)
|
@accepts(schema=WidgetSchema, api=api)
|
||||||
@responds(schema=WidgetSchema)
|
@responds(schema=WidgetSchema)
|
||||||
def put(self, widgetId: int) -> Widget:
|
def put(self, widgetId: int) -> Widget:
|
||||||
'''Update Single Widget'''
|
"""Update Single Widget"""
|
||||||
|
|
||||||
changes: WidgetInterface = request.parsed_obj
|
changes: WidgetInterface = request.parsed_obj
|
||||||
Widget = WidgetService.get_by_id(widgetId)
|
Widget = WidgetService.get_by_id(widgetId)
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
from flask.testing import FlaskClient
|
from flask.testing import FlaskClient
|
||||||
|
|
||||||
|
@ -10,76 +9,88 @@ from .interface import WidgetInterface
|
||||||
from . import BASE_ROUTE
|
from . import BASE_ROUTE
|
||||||
|
|
||||||
|
|
||||||
def make_widget(id: int = 123, name: str = 'Test widget',
|
def make_widget(
|
||||||
purpose: str = 'Test purpose') -> Widget:
|
id: int = 123, name: str = "Test widget", purpose: str = "Test purpose"
|
||||||
return Widget(
|
) -> Widget:
|
||||||
widget_id=id, name=name, purpose=purpose
|
return Widget(widget_id=id, name=name, purpose=purpose)
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestWidgetResource:
|
class TestWidgetResource:
|
||||||
@patch.object(WidgetService, 'get_all',
|
@patch.object(
|
||||||
lambda: [make_widget(123, name='Test Widget 1'),
|
WidgetService,
|
||||||
make_widget(456, name='Test Widget 2')])
|
"get_all",
|
||||||
|
lambda: [
|
||||||
|
make_widget(123, name="Test Widget 1"),
|
||||||
|
make_widget(456, name="Test Widget 2"),
|
||||||
|
],
|
||||||
|
)
|
||||||
def test_get(self, client: FlaskClient): # noqa
|
def test_get(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
results = client.get(f'/api/{BASE_ROUTE}',
|
results = client.get(f"/api/{BASE_ROUTE}", follow_redirects=True).get_json()
|
||||||
follow_redirects=True).get_json()
|
expected = (
|
||||||
expected = WidgetSchema(many=True).dump(
|
WidgetSchema(many=True)
|
||||||
[make_widget(123, name='Test Widget 1'),
|
.dump(
|
||||||
make_widget(456, name='Test Widget 2')]
|
[
|
||||||
).data
|
make_widget(123, name="Test Widget 1"),
|
||||||
|
make_widget(456, name="Test Widget 2"),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
.data
|
||||||
|
)
|
||||||
for r in results:
|
for r in results:
|
||||||
assert r in expected
|
assert r in expected
|
||||||
|
|
||||||
@patch.object(WidgetService, 'create',
|
@patch.object(
|
||||||
lambda create_request: Widget(**create_request))
|
WidgetService, "create", lambda create_request: Widget(**create_request)
|
||||||
|
)
|
||||||
def test_post(self, client: FlaskClient): # noqa
|
def test_post(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
|
|
||||||
payload = dict(name='Test widget', purpose='Test purpose')
|
payload = dict(name="Test widget", purpose="Test purpose")
|
||||||
result = client.post(f'/api/{BASE_ROUTE}/', json=payload).get_json()
|
result = client.post(f"/api/{BASE_ROUTE}/", json=payload).get_json()
|
||||||
expected = WidgetSchema().dump(Widget(
|
expected = (
|
||||||
name=payload['name'],
|
WidgetSchema()
|
||||||
purpose=payload['purpose'],
|
.dump(Widget(name=payload["name"], purpose=payload["purpose"]))
|
||||||
)).data
|
.data
|
||||||
|
)
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
|
|
||||||
def fake_update(widget: Widget, changes: WidgetInterface) -> Widget:
|
def fake_update(widget: Widget, changes: WidgetInterface) -> Widget:
|
||||||
# To fake an update, just return a new object
|
# To fake an update, just return a new object
|
||||||
updated_Widget = Widget(widget_id=widget.widget_id,
|
updated_Widget = Widget(
|
||||||
name=changes['name'],
|
widget_id=widget.widget_id, name=changes["name"], purpose=changes["purpose"]
|
||||||
purpose=changes['purpose'])
|
)
|
||||||
return updated_Widget
|
return updated_Widget
|
||||||
|
|
||||||
|
|
||||||
class TestWidgetIdResource:
|
class TestWidgetIdResource:
|
||||||
@patch.object(WidgetService, 'get_by_id',
|
@patch.object(WidgetService, "get_by_id", lambda id: make_widget(id=id))
|
||||||
lambda id: make_widget(id=id))
|
|
||||||
def test_get(self, client: FlaskClient): # noqa
|
def test_get(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
result = client.get(f'/api/{BASE_ROUTE}/123').get_json()
|
result = client.get(f"/api/{BASE_ROUTE}/123").get_json()
|
||||||
expected = make_widget(id=123)
|
expected = make_widget(id=123)
|
||||||
print(f'result = ', result)
|
print(f"result = ", result)
|
||||||
assert result['widgetId'] == expected.widget_id
|
assert result["widgetId"] == expected.widget_id
|
||||||
|
|
||||||
@patch.object(WidgetService, 'delete_by_id',
|
@patch.object(WidgetService, "delete_by_id", lambda id: id)
|
||||||
lambda id: id)
|
|
||||||
def test_delete(self, client: FlaskClient): # noqa
|
def test_delete(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
result = client.delete(f'/api/{BASE_ROUTE}/123').get_json()
|
result = client.delete(f"/api/{BASE_ROUTE}/123").get_json()
|
||||||
expected = dict(status='Success', id=123)
|
expected = dict(status="Success", id=123)
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
@patch.object(WidgetService, 'get_by_id',
|
@patch.object(WidgetService, "get_by_id", lambda id: make_widget(id=id))
|
||||||
lambda id: make_widget(id=id))
|
@patch.object(WidgetService, "update", fake_update)
|
||||||
@patch.object(WidgetService, 'update', fake_update)
|
|
||||||
def test_put(self, client: FlaskClient): # noqa
|
def test_put(self, client: FlaskClient): # noqa
|
||||||
with client:
|
with client:
|
||||||
result = client.put(f'/api/{BASE_ROUTE}/123',
|
result = client.put(
|
||||||
json={'name': 'New Widget',
|
f"/api/{BASE_ROUTE}/123",
|
||||||
'purpose': 'New purpose'}).get_json()
|
json={"name": "New Widget", "purpose": "New purpose"},
|
||||||
expected = WidgetSchema().dump(
|
).get_json()
|
||||||
Widget(widget_id=123, name='New Widget', purpose='New purpose')).data
|
expected = (
|
||||||
|
WidgetSchema()
|
||||||
|
.dump(Widget(widget_id=123, name="New Widget", purpose="New purpose"))
|
||||||
|
.data
|
||||||
|
)
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
|
@ -5,9 +5,7 @@ from .interface import WidgetInterface
|
||||||
|
|
||||||
@fixture
|
@fixture
|
||||||
def interface() -> WidgetInterface:
|
def interface() -> WidgetInterface:
|
||||||
return WidgetInterface(
|
return WidgetInterface(widget_id=1, name="Test widget", purpose="Test purpose")
|
||||||
widget_id=1, name='Test widget', purpose='Test purpose'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_WidgetInterface_create(interface: WidgetInterface):
|
def test_WidgetInterface_create(interface: WidgetInterface):
|
||||||
|
|
|
@ -4,9 +4,9 @@ from .interface import WidgetInterface
|
||||||
|
|
||||||
|
|
||||||
class Widget(db.Model): # type: ignore
|
class Widget(db.Model): # type: ignore
|
||||||
'''A snazzy Widget'''
|
"""A snazzy Widget"""
|
||||||
|
|
||||||
__tablename__ = 'widget'
|
__tablename__ = "widget"
|
||||||
|
|
||||||
widget_id = Column(Integer(), primary_key=True)
|
widget_id = Column(Integer(), primary_key=True)
|
||||||
name = Column(String(255))
|
name = Column(String(255))
|
||||||
|
|
|
@ -6,9 +6,7 @@ from .model import Widget
|
||||||
|
|
||||||
@fixture
|
@fixture
|
||||||
def widget() -> Widget:
|
def widget() -> Widget:
|
||||||
return Widget(
|
return Widget(widget_id=1, name="Test widget", purpose="Test purpose")
|
||||||
widget_id=1, name='Test widget', purpose='Test purpose'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_Widget_create(widget: Widget):
|
def test_Widget_create(widget: Widget):
|
||||||
|
|
|
@ -2,8 +2,8 @@ from marshmallow import fields, Schema
|
||||||
|
|
||||||
|
|
||||||
class WidgetSchema(Schema):
|
class WidgetSchema(Schema):
|
||||||
'''Widget schema'''
|
"""Widget schema"""
|
||||||
|
|
||||||
widgetId = fields.Number(attribute='widget_id')
|
widgetId = fields.Number(attribute="widget_id")
|
||||||
name = fields.String(attribute='name')
|
name = fields.String(attribute="name")
|
||||||
purpose = fields.String(attribute='purpose')
|
purpose = fields.String(attribute="purpose")
|
||||||
|
|
|
@ -15,13 +15,11 @@ def test_WidgetSchema_create(schema: WidgetSchema):
|
||||||
|
|
||||||
|
|
||||||
def test_WidgetSchema_works(schema: WidgetSchema):
|
def test_WidgetSchema_works(schema: WidgetSchema):
|
||||||
params: WidgetInterface = schema.load({
|
params: WidgetInterface = schema.load(
|
||||||
'widgetId': '123',
|
{"widgetId": "123", "name": "Test widget", "purpose": "Test purpose"}
|
||||||
'name': 'Test widget',
|
).data
|
||||||
'purpose': 'Test purpose'
|
|
||||||
}).data
|
|
||||||
widget = Widget(**params)
|
widget = Widget(**params)
|
||||||
|
|
||||||
assert widget.widget_id == 123
|
assert widget.widget_id == 123
|
||||||
assert widget.name == 'Test widget'
|
assert widget.name == "Test widget"
|
||||||
assert widget.purpose == 'Test purpose'
|
assert widget.purpose == "Test purpose"
|
||||||
|
|
|
@ -4,7 +4,7 @@ from .model import Widget
|
||||||
from .interface import WidgetInterface
|
from .interface import WidgetInterface
|
||||||
|
|
||||||
|
|
||||||
class WidgetService():
|
class WidgetService:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_all() -> List[Widget]:
|
def get_all() -> List[Widget]:
|
||||||
return Widget.query.all()
|
return Widget.query.all()
|
||||||
|
@ -30,10 +30,7 @@ class WidgetService():
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(new_attrs: WidgetInterface) -> Widget:
|
def create(new_attrs: WidgetInterface) -> Widget:
|
||||||
new_widget = Widget(
|
new_widget = Widget(name=new_attrs["name"], purpose=new_attrs["purpose"])
|
||||||
name=new_attrs['name'],
|
|
||||||
purpose=new_attrs['purpose']
|
|
||||||
)
|
|
||||||
|
|
||||||
db.session.add(new_widget)
|
db.session.add(new_widget)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
|
@ -7,8 +7,8 @@ from .interface import WidgetInterface
|
||||||
|
|
||||||
|
|
||||||
def test_get_all(db: SQLAlchemy): # noqa
|
def test_get_all(db: SQLAlchemy): # noqa
|
||||||
yin: Widget = Widget(widget_id=1, name='Yin', purpose='thing 1')
|
yin: Widget = Widget(widget_id=1, name="Yin", purpose="thing 1")
|
||||||
yang: Widget = Widget(widget_id=2, name='Yang', purpose='thing 2')
|
yang: Widget = Widget(widget_id=2, name="Yang", purpose="thing 2")
|
||||||
db.session.add(yin)
|
db.session.add(yin)
|
||||||
db.session.add(yang)
|
db.session.add(yang)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -20,21 +20,21 @@ def test_get_all(db: SQLAlchemy): # noqa
|
||||||
|
|
||||||
|
|
||||||
def test_update(db: SQLAlchemy): # noqa
|
def test_update(db: SQLAlchemy): # noqa
|
||||||
yin: Widget = Widget(widget_id=1, name='Yin', purpose='thing 1')
|
yin: Widget = Widget(widget_id=1, name="Yin", purpose="thing 1")
|
||||||
|
|
||||||
db.session.add(yin)
|
db.session.add(yin)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
updates: WidgetInterface = dict(name='New Widget name')
|
updates: WidgetInterface = dict(name="New Widget name")
|
||||||
|
|
||||||
WidgetService.update(yin, updates)
|
WidgetService.update(yin, updates)
|
||||||
|
|
||||||
result: Widget = Widget.query.get(yin.widget_id)
|
result: Widget = Widget.query.get(yin.widget_id)
|
||||||
assert result.name == 'New Widget name'
|
assert result.name == "New Widget name"
|
||||||
|
|
||||||
|
|
||||||
def test_delete_by_id(db: SQLAlchemy): # noqa
|
def test_delete_by_id(db: SQLAlchemy): # noqa
|
||||||
yin: Widget = Widget(widget_id=1, name='Yin', purpose='thing 1')
|
yin: Widget = Widget(widget_id=1, name="Yin", purpose="thing 1")
|
||||||
yang: Widget = Widget(widget_id=2, name='Yang', purpose='thing 2')
|
yang: Widget = Widget(widget_id=2, name="Yang", purpose="thing 2")
|
||||||
db.session.add(yin)
|
db.session.add(yin)
|
||||||
db.session.add(yang)
|
db.session.add(yang)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -50,7 +50,7 @@ def test_delete_by_id(db: SQLAlchemy): # noqa
|
||||||
|
|
||||||
def test_create(db: SQLAlchemy): # noqa
|
def test_create(db: SQLAlchemy): # noqa
|
||||||
|
|
||||||
yin: WidgetInterface = dict(name='Fancy new widget', purpose='Fancy new purpose')
|
yin: WidgetInterface = dict(name="Fancy new widget", purpose="Fancy new purpose")
|
||||||
WidgetService.create(yin)
|
WidgetService.create(yin)
|
||||||
results: List[Widget] = Widget.query.all()
|
results: List[Widget] = Widget.query.all()
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
from .seed_command import SeedCommand
|
from .seed_command import SeedCommand
|
||||||
|
|
|
@ -19,30 +19,26 @@ def seed_things():
|
||||||
|
|
||||||
def seed_thing(cls):
|
def seed_thing(cls):
|
||||||
things = [
|
things = [
|
||||||
{
|
{"name": "Pizza Slicer", "purpose": "Cut delicious pizza"},
|
||||||
'name': 'Pizza Slicer',
|
{"name": "Rolling Pin", "purpose": "Roll delicious pizza"},
|
||||||
'purpose': 'Cut delicious pizza',
|
{"name": "Pizza Oven", "purpose": "Bake delicious pizza"},
|
||||||
},
|
|
||||||
{
|
|
||||||
'name': 'Rolling Pin',
|
|
||||||
'purpose': 'Roll delicious pizza',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
'name': 'Pizza Oven',
|
|
||||||
'purpose': 'Bake delicious pizza',
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
db.session.bulk_insert_mappings(cls, things)
|
db.session.bulk_insert_mappings(cls, things)
|
||||||
|
|
||||||
|
|
||||||
class SeedCommand(Command):
|
class SeedCommand(Command):
|
||||||
""" Seed the DB."""
|
""" Seed the DB."""
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
if input('Are you sure you want to drop all tables and recreate? (y/N)\n'
|
if (
|
||||||
).lower() == 'y':
|
input(
|
||||||
print('Dropping tables...')
|
"Are you sure you want to drop all tables and recreate? (y/N)\n"
|
||||||
|
).lower()
|
||||||
|
== "y"
|
||||||
|
):
|
||||||
|
print("Dropping tables...")
|
||||||
db.drop_all()
|
db.drop_all()
|
||||||
db.create_all()
|
db.create_all()
|
||||||
seed_things()
|
seed_things()
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
print('DB successfully seeded.')
|
print("DB successfully seeded.")
|
||||||
|
|
14
manage.py
14
manage.py
|
@ -4,13 +4,13 @@ from flask_script import Manager
|
||||||
from app import create_app, db
|
from app import create_app, db
|
||||||
from commands.seed_command import SeedCommand
|
from commands.seed_command import SeedCommand
|
||||||
|
|
||||||
env = os.getenv('FLASK_ENV') or 'test'
|
env = os.getenv("FLASK_ENV") or "test"
|
||||||
print(f'Active environment: * {env} *')
|
print(f"Active environment: * {env} *")
|
||||||
app = create_app(env)
|
app = create_app(env)
|
||||||
|
|
||||||
manager = Manager(app)
|
manager = Manager(app)
|
||||||
app.app_context().push()
|
app.app_context().push()
|
||||||
manager.add_command('seed_db', SeedCommand)
|
manager.add_command("seed_db", SeedCommand)
|
||||||
|
|
||||||
|
|
||||||
@manager.command
|
@manager.command
|
||||||
|
@ -20,16 +20,16 @@ def run():
|
||||||
|
|
||||||
@manager.command
|
@manager.command
|
||||||
def init_db():
|
def init_db():
|
||||||
print('Creating all resources.')
|
print("Creating all resources.")
|
||||||
db.create_all()
|
db.create_all()
|
||||||
|
|
||||||
|
|
||||||
@manager.command
|
@manager.command
|
||||||
def drop_all():
|
def drop_all():
|
||||||
if input('Are you sure you want to drop all tables? (y/N)\n').lower() == 'y':
|
if input("Are you sure you want to drop all tables? (y/N)\n").lower() == "y":
|
||||||
print('Dropping tables...')
|
print("Dropping tables...")
|
||||||
db.drop_all()
|
db.drop_all()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
manager.run()
|
manager.run()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user