2019-05-18 19:47:47 +02:00
|
|
|
from unittest.mock import patch
|
|
|
|
from flask.testing import FlaskClient
|
|
|
|
|
|
|
|
from app.test.fixtures import client, app # noqa
|
|
|
|
from .service import WhatsitService
|
|
|
|
from .schema import WhatsitSchema
|
|
|
|
from .model import Whatsit
|
|
|
|
from .interface import WhatsitInterface
|
|
|
|
from .. import BASE_ROUTE
|
|
|
|
|
|
|
|
|
2019-08-03 18:55:38 +02:00
|
|
|
def make_whatsit(
|
|
|
|
id: int = 123, name: str = "Test whatsit", purpose: str = "Test purpose"
|
|
|
|
) -> Whatsit:
|
|
|
|
return Whatsit(whatsit_id=id, name=name, purpose=purpose)
|
2019-05-18 19:47:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestWhatsitResource:
|
2019-08-03 18:55:38 +02:00
|
|
|
@patch.object(
|
|
|
|
WhatsitService,
|
|
|
|
"get_all",
|
|
|
|
lambda: [
|
|
|
|
make_whatsit(123, name="Test Whatsit 1"),
|
|
|
|
make_whatsit(456, name="Test Whatsit 2"),
|
|
|
|
],
|
|
|
|
)
|
2019-05-18 19:47:47 +02:00
|
|
|
def test_get(self, client: FlaskClient): # noqa
|
|
|
|
with client:
|
2019-08-03 18:55:38 +02:00
|
|
|
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
|
|
|
|
)
|
2019-05-18 19:47:47 +02:00
|
|
|
for r in results:
|
|
|
|
assert r in expected
|
|
|
|
|
2019-08-03 18:55:38 +02:00
|
|
|
@patch.object(
|
|
|
|
WhatsitService, "create", lambda create_request: Whatsit(**create_request)
|
|
|
|
)
|
2019-05-18 19:47:47 +02:00
|
|
|
def test_post(self, client: FlaskClient): # noqa
|
|
|
|
with client:
|
|
|
|
|
2019-08-03 18:55:38 +02:00
|
|
|
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
|
|
|
|
)
|
2019-05-18 19:47:47 +02:00
|
|
|
assert result == expected
|
|
|
|
|
|
|
|
|
|
|
|
def fake_update(whatsit: Whatsit, changes: WhatsitInterface) -> Whatsit:
|
|
|
|
# To fake an update, just return a new object
|
2019-08-03 18:55:38 +02:00
|
|
|
updated_Whatsit = Whatsit(
|
|
|
|
whatsit_id=whatsit.whatsit_id, name=changes["name"], purpose=changes["purpose"]
|
|
|
|
)
|
2019-05-18 19:47:47 +02:00
|
|
|
return updated_Whatsit
|
|
|
|
|
|
|
|
|
|
|
|
class TestWhatsitIdResource:
|
2019-08-03 18:55:38 +02:00
|
|
|
@patch.object(WhatsitService, "get_by_id", lambda id: make_whatsit(id=id))
|
2019-05-18 19:47:47 +02:00
|
|
|
def test_get(self, client: FlaskClient): # noqa
|
|
|
|
with client:
|
2019-08-03 18:55:38 +02:00
|
|
|
result = client.get(f"/api/{BASE_ROUTE}/whatsit/123").get_json()
|
2019-05-18 19:47:47 +02:00
|
|
|
expected = Whatsit(whatsit_id=123)
|
2019-08-03 18:55:38 +02:00
|
|
|
assert result["whatsitId"] == expected.whatsit_id
|
2019-05-18 19:47:47 +02:00
|
|
|
|
2019-08-03 18:55:38 +02:00
|
|
|
@patch.object(WhatsitService, "delete_by_id", lambda id: [id])
|
2019-05-18 19:47:47 +02:00
|
|
|
def test_delete(self, client: FlaskClient): # noqa
|
|
|
|
with client:
|
2019-08-03 18:55:38 +02:00
|
|
|
result = client.delete(f"/api/{BASE_ROUTE}/whatsit/123").get_json()
|
|
|
|
expected = dict(status="Success", id=[123])
|
2019-05-18 19:47:47 +02:00
|
|
|
assert result == expected
|
|
|
|
|
2019-08-03 18:55:38 +02:00
|
|
|
@patch.object(WhatsitService, "get_by_id", lambda id: make_whatsit(id=id))
|
|
|
|
@patch.object(WhatsitService, "update", fake_update)
|
2019-05-18 19:47:47 +02:00
|
|
|
def test_put(self, client: FlaskClient): # noqa
|
|
|
|
with client:
|
2019-08-03 18:55:38 +02:00
|
|
|
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
|
|
|
|
)
|
2019-05-18 19:47:47 +02:00
|
|
|
assert result == expected
|