CICD/app/widget/controller_test.py

97 lines
3.2 KiB
Python
Raw Normal View History

2019-05-18 19:00:13 +02:00
from unittest.mock import patch
from flask.testing import FlaskClient
from app.test.fixtures import client, app # noqa
from .service import WidgetService
from .schema import WidgetSchema
from .model import Widget
from .interface import WidgetInterface
from . import BASE_ROUTE
2019-08-03 18:55:38 +02:00
def make_widget(
id: int = 123, name: str = "Test widget", purpose: str = "Test purpose"
) -> Widget:
return Widget(widget_id=id, name=name, purpose=purpose)
2019-05-18 19:00:13 +02:00
class TestWidgetResource:
2019-08-03 18:55:38 +02:00
@patch.object(
WidgetService,
"get_all",
lambda: [
make_widget(123, name="Test Widget 1"),
make_widget(456, name="Test Widget 2"),
],
)
2019-05-18 19:00:13 +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}", follow_redirects=True).get_json()
expected = (
WidgetSchema(many=True)
.dump(
[
make_widget(123, name="Test Widget 1"),
make_widget(456, name="Test Widget 2"),
]
)
2019-08-03 18:55:38 +02:00
)
2019-05-18 19:00:13 +02:00
for r in results:
assert r in expected
2019-08-03 18:55:38 +02:00
@patch.object(
WidgetService, "create", lambda create_request: Widget(**create_request)
)
2019-05-18 19:00:13 +02:00
def test_post(self, client: FlaskClient): # noqa
with client:
2019-08-03 18:55:38 +02:00
payload = dict(name="Test widget", purpose="Test purpose")
result = client.post(f"/api/{BASE_ROUTE}/", json=payload).get_json()
expected = (
WidgetSchema()
.dump(Widget(name=payload["name"], purpose=payload["purpose"]))
2019-08-03 18:55:38 +02:00
)
2019-05-18 19:00:13 +02:00
assert result == expected
def fake_update(widget: Widget, changes: WidgetInterface) -> Widget:
# To fake an update, just return a new object
2019-08-03 18:55:38 +02:00
updated_Widget = Widget(
widget_id=widget.widget_id, name=changes["name"], purpose=changes["purpose"]
)
2019-05-18 19:00:13 +02:00
return updated_Widget
class TestWidgetIdResource:
2019-08-03 18:55:38 +02:00
@patch.object(WidgetService, "get_by_id", lambda id: make_widget(id=id))
2019-05-18 19:00:13 +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}/123").get_json()
2019-05-18 19:00:13 +02:00
expected = make_widget(id=123)
2019-08-03 18:55:38 +02:00
print(f"result = ", result)
assert result["widgetId"] == expected.widget_id
2019-05-18 19:00:13 +02:00
2019-08-03 18:55:38 +02:00
@patch.object(WidgetService, "delete_by_id", lambda id: id)
2019-05-18 19:00:13 +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}/123").get_json()
expected = dict(status="Success", id=123)
2019-05-18 19:00:13 +02:00
assert result == expected
2019-08-03 18:55:38 +02:00
@patch.object(WidgetService, "get_by_id", lambda id: make_widget(id=id))
@patch.object(WidgetService, "update", fake_update)
2019-05-18 19:00:13 +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}/123",
json={"name": "New Widget", "purpose": "New purpose"},
).get_json()
expected = (
WidgetSchema()
.dump(Widget(widget_id=123, name="New Widget", purpose="New purpose"))
2019-08-03 18:55:38 +02:00
)
2019-05-18 19:00:13 +02:00
assert result == expected