27 lines
985 B
Python
27 lines
985 B
Python
from sqlalchemy import select
|
|
from sqlalchemy.exc import IntegrityError
|
|
import pytest
|
|
from the_works.database import db
|
|
from the_works.models import Genre
|
|
|
|
def test_genre_create(client, _app):
|
|
"""Integrated testing of adding a Genre record."""
|
|
response = client.post("/genre/create", data={"form_Genre": "spam"}, follow_redirects=True)
|
|
|
|
# assert there was exactly 1 redirect
|
|
assert len(response.history) == 1
|
|
# assert the redirect led to the correct page
|
|
assert response.request.path == "/genre/all"
|
|
assert response.status_code == 200
|
|
|
|
# assert record was successfully added to DB
|
|
with _app.app_context():
|
|
genre = db.session.scalars(select(Genre).where(Genre.Genre == "spam")).all()
|
|
assert len(genre) == 1
|
|
assert isinstance(genre[0], Genre)
|
|
|
|
# assert uniqueness of records
|
|
with pytest.raises(IntegrityError) as excinfo:
|
|
response = client.post("/genre/create", data={"form_Genre": "spam"})
|
|
assert "UNIQUE constraint failed" in str(excinfo.value)
|