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 DoodadService
|
|
|
|
from .schema import DoodadSchema
|
|
|
|
from .model import Doodad
|
|
|
|
from .interface import DoodadInterface
|
|
|
|
from .. import BASE_ROUTE
|
|
|
|
|
|
|
|
|
2019-08-03 18:55:38 +02:00
|
|
|
def make_doodad(
|
|
|
|
id: int = 123, name: str = "Test doodad", purpose: str = "Test purpose"
|
|
|
|
) -> Doodad:
|
|
|
|
return Doodad(doodad_id=id, name=name, purpose=purpose)
|
2019-05-18 19:47:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestDoodadResource:
|
2019-08-03 18:55:38 +02:00
|
|
|
@patch.object(
|
|
|
|
DoodadService,
|
|
|
|
"get_all",
|
|
|
|
lambda: [
|
|
|
|
make_doodad(123, name="Test Doodad 1"),
|
|
|
|
make_doodad(456, name="Test Doodad 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}/doodad", follow_redirects=True
|
|
|
|
).get_json()
|
|
|
|
expected = (
|
|
|
|
DoodadSchema(many=True)
|
|
|
|
.dump(
|
|
|
|
[
|
|
|
|
make_doodad(123, name="Test Doodad 1"),
|
|
|
|
make_doodad(456, name="Test Doodad 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(
|
|
|
|
DoodadService, "create", lambda create_request: Doodad(**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 doodad", purpose="Test purpose")
|
|
|
|
result = client.post(f"/api/{BASE_ROUTE}/doodad/", json=payload).get_json()
|
|
|
|
expected = (
|
|
|
|
DoodadSchema()
|
|
|
|
.dump(Doodad(name=payload["name"], purpose=payload["purpose"]))
|
|
|
|
.data
|
|
|
|
)
|
2019-05-18 19:47:47 +02:00
|
|
|
assert result == expected
|
|
|
|
|
|
|
|
|
|
|
|
def fake_update(doodad: Doodad, changes: DoodadInterface) -> Doodad:
|
|
|
|
# To fake an update, just return a new object
|
2019-08-03 18:55:38 +02:00
|
|
|
updated_Doodad = Doodad(
|
|
|
|
doodad_id=doodad.doodad_id, name=changes["name"], purpose=changes["purpose"]
|
|
|
|
)
|
2019-05-18 19:47:47 +02:00
|
|
|
return updated_Doodad
|
|
|
|
|
|
|
|
|
|
|
|
class TestDoodadIdResource:
|
2019-08-03 18:55:38 +02:00
|
|
|
@patch.object(DoodadService, "get_by_id", lambda id: make_doodad(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}/doodad/123").get_json()
|
2019-05-18 19:47:47 +02:00
|
|
|
expected = Doodad(doodad_id=123)
|
2019-08-03 18:55:38 +02:00
|
|
|
assert result["doodadId"] == expected.doodad_id
|
2019-05-18 19:47:47 +02:00
|
|
|
|
2019-08-03 18:55:38 +02:00
|
|
|
@patch.object(DoodadService, "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}/doodad/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(DoodadService, "get_by_id", lambda id: make_doodad(id=id))
|
|
|
|
@patch.object(DoodadService, "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}/doodad/123",
|
|
|
|
json={"name": "New Doodad", "purpose": "New purpose"},
|
|
|
|
).get_json()
|
|
|
|
expected = (
|
|
|
|
DoodadSchema()
|
|
|
|
.dump(Doodad(doodad_id=123, name="New Doodad", purpose="New purpose"))
|
|
|
|
.data
|
|
|
|
)
|
2019-05-18 19:47:47 +02:00
|
|
|
assert result == expected
|