From 5f879cd062cfb66aead511741f72f49257df1c65 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Mon, 31 May 2021 10:52:37 -0700 Subject: [PATCH] Refactor fixtures and clean up tests --- tests/conftest.py | 73 ++++++++++++++++++++++++++++++++++++++++ tests/test_db.py | 10 +----- tests/test_lexicon.py | 7 ++-- tests/test_membership.py | 12 +++---- tests/test_post.py | 23 +++---------- tests/test_user.py | 14 ++------ 6 files changed, 88 insertions(+), 51 deletions(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..3b6ae46 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,73 @@ +""" +pytest test fixtures +""" +import pytest + +from amanuensis.db import DbContext +import amanuensis.backend.lexicon as lexiq +import amanuensis.backend.membership as memq +import amanuensis.backend.user as userq + + +@pytest.fixture +def db(): + """Provides an initialized database in memory.""" + db = DbContext('sqlite:///:memory:', debug=True) + db.create_all() + return db + + +@pytest.fixture +def make_user(db: DbContext): + """Provides a factory function for creating users, with valid default values.""" + def user_factory(state={'nonce': 1}, **kwargs): + default_kwargs = { + 'username': f'test_user_{state["nonce"]}', + 'password': 'password', + 'display_name': None, + 'email': 'user@example.com', + 'is_site_admin': False, + } + state['nonce'] += 1 + updated_kwargs = {**default_kwargs, **kwargs} + return userq.create(db, **updated_kwargs) + return user_factory + + +@pytest.fixture +def make_lexicon(db: DbContext): + """Provides a factory function for creating lexicons, with valid default values.""" + def lexicon_factory(state={'nonce': 1}, **kwargs): + default_kwargs = { + 'name': f'Test_{state["nonce"]}', + 'title': None, + 'prompt': f'Test Lexicon game {state["nonce"]}' + } + state['nonce'] += 1 + updated_kwargs = {**default_kwargs, **kwargs} + return lexiq.create(db, **updated_kwargs) + return lexicon_factory + + +@pytest.fixture +def make_membership(db: DbContext): + """Provides a factory function for creating memberships, with valid default values.""" + def membership_factory(**kwargs): + default_kwargs = { + 'is_editor': False, + } + updated_kwargs = {**default_kwargs, **kwargs} + return memq.create(db, **updated_kwargs) + return membership_factory + + +@pytest.fixture +def lexicon_with_editor(make_user, make_lexicon, make_membership): + """Shortcut setup for a lexicon game with an editor.""" + editor = make_user() + assert editor + lexicon = make_lexicon() + assert lexicon + membership = make_membership(user_id=editor.id, lexicon_id=lexicon.id, is_editor=True) + assert membership + return (lexicon, editor) diff --git a/tests/test_db.py b/tests/test_db.py index 17460d7..5d4a4e8 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -1,17 +1,9 @@ -import pytest from sqlalchemy import func from amanuensis.db import * -@pytest.fixture -def db(): - db = DbContext('sqlite:///:memory:', debug=True) - db.create_all() - return db - - -def test_create(db): +def test_create_db(db: DbContext): """Simple test that the database creates fine from scratch.""" assert db.session.query(func.count(User.id)).scalar() == 0 assert db.session.query(func.count(Lexicon.id)).scalar() == 0 diff --git a/tests/test_lexicon.py b/tests/test_lexicon.py index 963c6c5..b0dfe9b 100644 --- a/tests/test_lexicon.py +++ b/tests/test_lexicon.py @@ -2,14 +2,12 @@ import datetime import pytest -from amanuensis.db import * +from amanuensis.db import DbContext import amanuensis.backend.lexicon as lexiq from amanuensis.errors import ArgumentError -from .test_db import db - -def test_create_lexicon(db): +def test_create_lexicon(db: DbContext): """Test new game creation.""" kwargs = { 'name': 'Test', @@ -43,3 +41,4 @@ def test_create_lexicon(db): # No duplicate lexicon names with pytest.raises(ArgumentError): duplicate = lexiq.create(db, **kwargs) + assert duplicate diff --git a/tests/test_membership.py b/tests/test_membership.py index 3d78547..13e64a5 100644 --- a/tests/test_membership.py +++ b/tests/test_membership.py @@ -1,17 +1,13 @@ -from amanuensis.db import * -import amanuensis.backend.lexicon as lexiq +from amanuensis.db import DbContext import amanuensis.backend.membership as memq -import amanuensis.backend.user as userq - -from .test_db import db -def test_create(db: DbContext): +def test_create_membership(db: DbContext, make_user, make_lexicon): """Test joining a game.""" # Set up a user and a lexicon - new_user = userq.create(db, 'username', 'password', 'user', 'a@b.c', False) + new_user = make_user() assert new_user.id, 'Failed to create user' - new_lexicon = lexiq.create(db, 'Test', None, 'prompt') + new_lexicon = make_lexicon() assert new_lexicon.id, 'Failed to create lexicon' # Add the user to the lexicon as an editor diff --git a/tests/test_post.py b/tests/test_post.py index 934ddcb..0e14cdd 100644 --- a/tests/test_post.py +++ b/tests/test_post.py @@ -1,32 +1,19 @@ import pytest -from amanuensis.db import * -import amanuensis.backend.lexicon as lexiq -import amanuensis.backend.user as userq +from amanuensis.db import DbContext import amanuensis.backend.post as postq -import amanuensis.backend.membership as memq from amanuensis.errors import ArgumentError -from .test_db import db -def test_create_post(db): +def test_create_post(db: DbContext, lexicon_with_editor): """Test new post creation""" - - # Make user and lexicon - new_user = userq.create(db, 'username', 'password', 'user', 'a@b.c', False) - assert new_user.id, 'Failed to create user' - new_lexicon = lexiq.create(db, 'Test', None, 'prompt') - assert new_lexicon.id, 'Failed to create lexicon' - - # Add the user to the lexicon as an editor - mem = memq.create(db, new_user.id, new_lexicon.id, True) - assert mem, 'Failed to create membership' + lexicon, editor = lexicon_with_editor # argument dictionary for post object kwargs = { - 'lexicon_id': new_lexicon.id, - 'user_id': new_user.id, + 'lexicon_id': lexicon.id, + 'user_id': editor.id, 'body': 'body' } diff --git a/tests/test_user.py b/tests/test_user.py index dc51173..099f871 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -1,21 +1,11 @@ import pytest -from amanuensis.db import * -import amanuensis.backend.lexicon as lexiq +from amanuensis.db import DbContext import amanuensis.backend.user as userq from amanuensis.errors import ArgumentError -from .test_db import db - -@pytest.fixture -def db(): - db = DbContext('sqlite:///:memory:', debug=True) - db.create_all() - return db - - -def test_create_user(db): +def test_create_user(db: DbContext): """Test new user creation.""" kwargs = { 'username': 'username',