minor improvements

This commit is contained in:
eclipse 2025-07-25 09:19:20 +02:00
parent b8edb958d3
commit 1adafc1fc7
2 changed files with 13 additions and 12 deletions

View File

@ -12,28 +12,28 @@ def app():
"SECRET_KEY": "This is my very secret key", "SECRET_KEY": "This is my very secret key",
"TESTING": True "TESTING": True
} }
app = create_app(test_config) _app = create_app(test_config)
# other setup can go here # other setup can go here
yield app yield _app
# clean up / reset resources here # clean up / reset resources here
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
def db(app): def db(_app):
with app.app_context(): with _app.app_context():
yield _db yield _db
_db.drop_all() _db.drop_all()
_db.create_all() _db.create_all()
@pytest.fixture() @pytest.fixture()
def client(app): def client(_app):
return app.test_client() return _app.test_client()
@pytest.fixture() @pytest.fixture()
def runner(app): def runner(_app):
return app.test_cli_runner() return _app.test_cli_runner()

View File

@ -2,13 +2,14 @@ from the_works.models import Genre
import pytest import pytest
def test_genre_all(client, db, mocker): def test_genre_all(client, mocker):
"""Test view all() from genre.py.""" """Test view all() from genre.py."""
# mock database function # mock database function
# Note: The original method returns an sqlalchemy.engine.Result.ScalarResult, not a list, but the template code # Note: The original method returns an sqlalchemy.engine.Result.ScalarResult, not a list, but the template code
# uses the return value in a way that works for both ScalarResult and list # uses the return value in a way that works for both ScalarResult and list
mocker.patch("the_works.database.db.session.scalars", return_value=[ # mocker.patch("the_works.database.db.session.scalars", return_value=[
mocker.patch("flask_sqlalchemy.SQLAlchemy.session.scalars", return_value=[
Genre(ID=4, Genre="bla"), Genre(ID=4, Genre="bla"),
Genre(ID=26, Genre="blubb") Genre(ID=26, Genre="blubb")
]) ])
@ -23,11 +24,11 @@ def test_genre_all(client, db, mocker):
assert response.status_code == 405 assert response.status_code == 405
def test_genre_create(client, db, mocker): def test_genre_create(client, mocker):
"""Test view create() from genre.py.""" """Test view create() from genre.py."""
# mock database function # mock database function
mocker.patch("the_works.database.db.session.add") mocker.patch("flask_sqlalchemy.SQLAlchemy.session.add")
# test a POST request with good data # test a POST request with good data
response = client.post("/genre/create", data={"form_Genre": "Testname"}, follow_redirects=True) response = client.post("/genre/create", data={"form_Genre": "Testname"}, follow_redirects=True)