Convert to black formatting
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
|
||||
from unittest.mock import patch
|
||||
from flask.testing import FlaskClient
|
||||
|
||||
@@ -10,75 +9,91 @@ 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
|
||||
)
|
||||
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')])
|
||||
@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
|
||||
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))
|
||||
@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
|
||||
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'])
|
||||
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))
|
||||
@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()
|
||||
result = client.get(f"/api/{BASE_ROUTE}/whatsit/123").get_json()
|
||||
expected = Whatsit(whatsit_id=123)
|
||||
assert result['whatsitId'] == expected.whatsit_id
|
||||
assert result["whatsitId"] == expected.whatsit_id
|
||||
|
||||
@patch.object(WhatsitService, 'delete_by_id',
|
||||
lambda id: [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])
|
||||
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)
|
||||
@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
|
||||
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
|
||||
|
@@ -5,9 +5,7 @@ from .interface import WhatsitInterface
|
||||
|
||||
@fixture
|
||||
def interface() -> WhatsitInterface:
|
||||
return WhatsitInterface(
|
||||
whatsit_id=1, name='Test whatsit', purpose='Test purpose'
|
||||
)
|
||||
return WhatsitInterface(whatsit_id=1, name="Test whatsit", purpose="Test purpose")
|
||||
|
||||
|
||||
def test_WhatsitInterface_create(interface: WhatsitInterface):
|
||||
|
@@ -5,9 +5,9 @@ from typing import Any
|
||||
|
||||
|
||||
class Whatsit(db.Model): # type: ignore
|
||||
'''A snazzy Whatsit'''
|
||||
"""A snazzy Whatsit"""
|
||||
|
||||
__tablename__ = 'whatsit'
|
||||
__tablename__ = "whatsit"
|
||||
|
||||
whatsit_id = Column(Integer(), primary_key=True)
|
||||
name = Column(String(255))
|
||||
|
@@ -6,9 +6,7 @@ from .model import Whatsit
|
||||
|
||||
@fixture
|
||||
def whatsit() -> Whatsit:
|
||||
return Whatsit(
|
||||
whatsit_id=1, name='Test whatsit', purpose='Test purpose'
|
||||
)
|
||||
return Whatsit(whatsit_id=1, name="Test whatsit", purpose="Test purpose")
|
||||
|
||||
|
||||
def test_Whatsit_create(whatsit: Whatsit):
|
||||
|
@@ -2,8 +2,8 @@ from marshmallow import fields, Schema
|
||||
|
||||
|
||||
class WhatsitSchema(Schema):
|
||||
'''Whatsit schema'''
|
||||
"""Whatsit schema"""
|
||||
|
||||
whatsitId = fields.Number(attribute='whatsit_id')
|
||||
name = fields.String(attribute='name')
|
||||
purpose = fields.String(attribute='purpose')
|
||||
whatsitId = fields.Number(attribute="whatsit_id")
|
||||
name = fields.String(attribute="name")
|
||||
purpose = fields.String(attribute="purpose")
|
||||
|
@@ -15,13 +15,11 @@ def test_WhatsitSchema_create(schema: WhatsitSchema):
|
||||
|
||||
|
||||
def test_WhatsitSchema_works(schema: WhatsitSchema):
|
||||
params: WhatsitInterface = schema.load({
|
||||
'whatsitId': '123',
|
||||
'name': 'Test whatsit',
|
||||
'purpose': 'Test purpose'
|
||||
}).data
|
||||
params: WhatsitInterface = schema.load(
|
||||
{"whatsitId": "123", "name": "Test whatsit", "purpose": "Test purpose"}
|
||||
).data
|
||||
whatsit = Whatsit(**params)
|
||||
|
||||
assert whatsit.whatsit_id == 123
|
||||
assert whatsit.name == 'Test whatsit'
|
||||
assert whatsit.purpose == 'Test purpose'
|
||||
assert whatsit.name == "Test whatsit"
|
||||
assert whatsit.purpose == "Test purpose"
|
||||
|
@@ -4,7 +4,7 @@ from .model import Whatsit
|
||||
from .interface import WhatsitInterface
|
||||
|
||||
|
||||
class WhatsitService():
|
||||
class WhatsitService:
|
||||
@staticmethod
|
||||
def get_all() -> List[Whatsit]:
|
||||
return Whatsit.query.all()
|
||||
@@ -30,10 +30,7 @@ class WhatsitService():
|
||||
|
||||
@staticmethod
|
||||
def create(new_attrs: WhatsitInterface) -> Whatsit:
|
||||
new_whatsit = Whatsit(
|
||||
name=new_attrs['name'],
|
||||
purpose=new_attrs['purpose']
|
||||
)
|
||||
new_whatsit = Whatsit(name=new_attrs["name"], purpose=new_attrs["purpose"])
|
||||
|
||||
db.session.add(new_whatsit)
|
||||
db.session.commit()
|
||||
|
@@ -7,8 +7,8 @@ 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')
|
||||
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()
|
||||
@@ -20,21 +20,21 @@ def test_get_all(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.commit()
|
||||
updates: WhatsitInterface = dict(name='New Whatsit name')
|
||||
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'
|
||||
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')
|
||||
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()
|
||||
@@ -50,7 +50,7 @@ def test_delete_by_id(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)
|
||||
results: List[Whatsit] = Whatsit.query.all()
|
||||
|
||||
|
Reference in New Issue
Block a user