CICD/app/fizz/fizzbar/controller_test.py

100 lines
3.4 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 FizzbarService
from .schema import FizzbarSchema
from .model import Fizzbar
from .interface import FizzbarInterface
from .. import BASE_ROUTE
2019-08-03 18:55:38 +02:00
def make_fizzbar(
id: int = 123, name: str = "Test fizzbar", purpose: str = "Test purpose"
) -> Fizzbar:
return Fizzbar(fizzbar_id=id, name=name, purpose=purpose)
2019-05-18 19:00:13 +02:00
class TestFizzbarResource:
2019-08-03 18:55:38 +02:00
@patch.object(
FizzbarService,
"get_all",
lambda: [
make_fizzbar(123, name="Test Fizzbar 1"),
make_fizzbar(456, name="Test Fizzbar 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}/fizzbar", follow_redirects=True
).get_json()
expected = (
FizzbarSchema(many=True)
.dump(
[
make_fizzbar(123, name="Test Fizzbar 1"),
make_fizzbar(456, name="Test Fizzbar 2"),
]
)
.data
)
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(
FizzbarService, "create", lambda create_request: Fizzbar(**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 fizzbar", purpose="Test purpose")
result = client.post(f"/api/{BASE_ROUTE}/fizzbar/", json=payload).get_json()
expected = (
FizzbarSchema()
.dump(Fizzbar(name=payload["name"], purpose=payload["purpose"]))
.data
)
2019-05-18 19:00:13 +02:00
assert result == expected
def fake_update(fizzbar: Fizzbar, changes: FizzbarInterface) -> Fizzbar:
# To fake an update, just return a new object
2019-08-03 18:55:38 +02:00
updated_Fizzbar = Fizzbar(
fizzbar_id=fizzbar.fizzbar_id, name=changes["name"], purpose=changes["purpose"]
)
2019-05-18 19:00:13 +02:00
return updated_Fizzbar
class TestFizzbarIdResource:
2019-08-03 18:55:38 +02:00
@patch.object(FizzbarService, "get_by_id", lambda id: make_fizzbar(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}/fizzbar/123").get_json()
2019-05-18 19:00:13 +02:00
expected = Fizzbar(fizzbar_id=123)
2019-08-03 18:55:38 +02:00
assert result["fizzbarId"] == expected.fizzbar_id
2019-05-18 19:00:13 +02:00
2019-08-03 18:55:38 +02:00
@patch.object(FizzbarService, "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}/fizzbar/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(FizzbarService, "get_by_id", lambda id: make_fizzbar(id=id))
@patch.object(FizzbarService, "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}/fizzbar/123",
json={"name": "New Fizzbar", "purpose": "New purpose"},
).get_json()
expected = (
FizzbarSchema()
.dump(
Fizzbar(fizzbar_id=123, name="New Fizzbar", purpose="New purpose")
)
.data
)
2019-05-18 19:00:13 +02:00
assert result == expected