the_works/tests/integration/test_int_genre.py

84 lines
3.1 KiB
Python

from sqlalchemy import select
from sqlalchemy.exc import IntegrityError
import pytest
from the_works.models import Genre
class TestIntGenreRead:
"""Integrated tests for reading all records."""
def test_records_read(self, client, db):
"""test reading records"""
response = client.get("/genre/all", follow_redirects=True)
# good status code
assert response.status_code == 200
# two records returned
assert response.data.count(b'<tr id="genre-') == 2
# assert records were correctly read from DB
assert b'spam' in response.data
assert b'eggs' in response.data
class TestIntGenreCreate:
"""Integrated tests for adding a Genre record."""
def test_record_added(self, client, db):
"""test record creation"""
client.post("/genre/create", data={"form_Genre": "spam & eggs"}, follow_redirects=True)
genres = db.session.scalars(select(Genre).where(Genre.Genre == "spam & eggs")).all()
assert len(genres) == 1
assert isinstance(genres[0], Genre) and genres[0].Genre == "spam & eggs"
def test_uniqueness_constraint(self, client, db):
"""assert uniqueness constraint when adding records"""
with pytest.raises(IntegrityError) as excinfo:
client.post("/genre/create", data={"form_Genre": "spam"})
assert "UNIQUE constraint failed" in str(excinfo.value)
def test_notempty_constraint(self, client):
"""assert non-empty constraint when adding record"""
with pytest.raises(IntegrityError) as excinfo:
client.post("/genre/create", data={"form_Genre": ""})
assert "CHECK constraint failed" in str(excinfo.value)
class TestIntGenreUpdate:
"""Integrated tests for updating a Genre record."""
def test_record_updated(self, client, db):
"""test record updating"""
# get test record id
g = db.session.scalar(select(Genre).where(Genre.Genre == "spam"))
# update record
client.post(f"/genre/update/{g.ID}", data={"form_Genre": "spam & eggs"})
# get updated record
g = db.session.get(Genre, g.ID)
assert g.Genre == "spam & eggs"
def test_uniqueness_constraint(self, client, db):
"""assert uniqueness constraint when updating records"""
g = db.session.scalar(select(Genre).where(Genre.Genre == "spam"))
with pytest.raises(IntegrityError) as excinfo:
client.post(f"/genre/update/{g.ID}", data={"form_Genre": "eggs"})
assert "UNIQUE constraint failed" in str(excinfo.value)
def test_notempty_constraint(self, client, db):
"""assert non-empty constraint when updating record"""
g = db.session.scalar(select(Genre).where(Genre.Genre == "spam"))
with pytest.raises(IntegrityError) as excinfo:
client.post(f"/genre/update/{g.ID}", data={"form_Genre": ""})
assert "CHECK constraint failed" in str(excinfo.value)
class TestIntGenreDelete:
"""Integrated tests for deleting a Genre record."""
def test_record_deleted(self, client, db):
"""test record deletion"""
# get test record id
g = db.session.scalar(select(Genre).where(Genre.Genre == "spam"))
# delete record
client.get(f"/genre/delete/{g.ID}")
g = db.session.scalars(select(Genre).where(Genre.Genre == "spam")).all()
assert g == []