Refactor fixtures and clean up tests

This commit is contained in:
Tim Van Baak 2021-05-31 10:52:37 -07:00
parent 0303544dfb
commit 5f879cd062
6 changed files with 88 additions and 51 deletions

73
tests/conftest.py Normal file
View File

@ -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)

View File

@ -1,17 +1,9 @@
import pytest
from sqlalchemy import func from sqlalchemy import func
from amanuensis.db import * from amanuensis.db import *
@pytest.fixture def test_create_db(db: DbContext):
def db():
db = DbContext('sqlite:///:memory:', debug=True)
db.create_all()
return db
def test_create(db):
"""Simple test that the database creates fine from scratch.""" """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(User.id)).scalar() == 0
assert db.session.query(func.count(Lexicon.id)).scalar() == 0 assert db.session.query(func.count(Lexicon.id)).scalar() == 0

View File

@ -2,14 +2,12 @@ import datetime
import pytest import pytest
from amanuensis.db import * from amanuensis.db import DbContext
import amanuensis.backend.lexicon as lexiq import amanuensis.backend.lexicon as lexiq
from amanuensis.errors import ArgumentError from amanuensis.errors import ArgumentError
from .test_db import db
def test_create_lexicon(db: DbContext):
def test_create_lexicon(db):
"""Test new game creation.""" """Test new game creation."""
kwargs = { kwargs = {
'name': 'Test', 'name': 'Test',
@ -43,3 +41,4 @@ def test_create_lexicon(db):
# No duplicate lexicon names # No duplicate lexicon names
with pytest.raises(ArgumentError): with pytest.raises(ArgumentError):
duplicate = lexiq.create(db, **kwargs) duplicate = lexiq.create(db, **kwargs)
assert duplicate

View File

@ -1,17 +1,13 @@
from amanuensis.db import * from amanuensis.db import DbContext
import amanuensis.backend.lexicon as lexiq
import amanuensis.backend.membership as memq 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.""" """Test joining a game."""
# Set up a user and a lexicon # 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' 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' assert new_lexicon.id, 'Failed to create lexicon'
# Add the user to the lexicon as an editor # Add the user to the lexicon as an editor

View File

@ -1,32 +1,19 @@
import pytest import pytest
from amanuensis.db import * from amanuensis.db import DbContext
import amanuensis.backend.lexicon as lexiq
import amanuensis.backend.user as userq
import amanuensis.backend.post as postq import amanuensis.backend.post as postq
import amanuensis.backend.membership as memq
from amanuensis.errors import ArgumentError 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""" """Test new post creation"""
lexicon, editor = lexicon_with_editor
# 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'
# argument dictionary for post object # argument dictionary for post object
kwargs = { kwargs = {
'lexicon_id': new_lexicon.id, 'lexicon_id': lexicon.id,
'user_id': new_user.id, 'user_id': editor.id,
'body': 'body' 'body': 'body'
} }

View File

@ -1,21 +1,11 @@
import pytest import pytest
from amanuensis.db import * from amanuensis.db import DbContext
import amanuensis.backend.lexicon as lexiq
import amanuensis.backend.user as userq import amanuensis.backend.user as userq
from amanuensis.errors import ArgumentError from amanuensis.errors import ArgumentError
from .test_db import db
def test_create_user(db: DbContext):
@pytest.fixture
def db():
db = DbContext('sqlite:///:memory:', debug=True)
db.create_all()
return db
def test_create_user(db):
"""Test new user creation.""" """Test new user creation."""
kwargs = { kwargs = {
'username': 'username', 'username': 'username',