the_works/tests/integration/test_int_genre.py

28 lines
1002 B
Python

from sqlalchemy import select
from the_works.database import db
from the_works.models import Genre
from sqlalchemy.exc import IntegrityError
import pytest
def test_genre_create(client, app):
"""Integrated testing of adding a Genre record."""
response = client.post("/genre/create", data={"form_Genre": "Test-Genre"}, 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 == "Test-Genre")).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": "Test-Genre"})
assert "UNIQUE constraint failed" in str(excinfo.value)