amanuensis/tests/conftest.py

125 lines
3.3 KiB
Python
Raw Normal View History

2021-05-31 17:52:37 +00:00
"""
pytest test fixtures
"""
import pytest
from amanuensis.db import DbContext
2021-06-02 01:46:19 +00:00
import amanuensis.backend.character as charq
2021-05-31 17:52:37 +00:00
import amanuensis.backend.lexicon as lexiq
import amanuensis.backend.membership as memq
import amanuensis.backend.user as userq
@pytest.fixture
2021-06-03 04:31:42 +00:00
def db() -> DbContext:
2021-05-31 17:52:37 +00:00
"""Provides an initialized database in memory."""
2021-06-03 03:10:34 +00:00
db = DbContext("sqlite:///:memory:", debug=False)
2021-05-31 17:52:37 +00:00
db.create_all()
return db
@pytest.fixture
def make_user(db: DbContext):
"""Provides a factory function for creating users, with valid default values."""
2021-06-03 03:05:33 +00:00
2021-06-03 03:10:34 +00:00
def user_factory(state={"nonce": 1}, **kwargs):
2021-05-31 17:52:37 +00:00
default_kwargs = {
2021-06-03 03:10:34 +00:00
"username": f'test_user_{state["nonce"]}',
"password": "password",
"display_name": None,
"email": "user@example.com",
"is_site_admin": False,
2021-05-31 17:52:37 +00:00
}
2021-06-03 03:10:34 +00:00
state["nonce"] += 1
2021-05-31 17:52:37 +00:00
updated_kwargs = {**default_kwargs, **kwargs}
return userq.create(db, **updated_kwargs)
2021-06-03 03:05:33 +00:00
2021-05-31 17:52:37 +00:00
return user_factory
@pytest.fixture
def make_lexicon(db: DbContext):
"""Provides a factory function for creating lexicons, with valid default values."""
2021-06-03 03:05:33 +00:00
2021-06-03 03:10:34 +00:00
def lexicon_factory(state={"nonce": 1}, **kwargs):
2021-05-31 17:52:37 +00:00
default_kwargs = {
2021-06-03 03:10:34 +00:00
"name": f'Test_{state["nonce"]}',
"title": None,
"prompt": f'Test Lexicon game {state["nonce"]}',
2021-05-31 17:52:37 +00:00
}
2021-06-03 03:10:34 +00:00
state["nonce"] += 1
2021-05-31 17:52:37 +00:00
updated_kwargs = {**default_kwargs, **kwargs}
lex = lexiq.create(db, **updated_kwargs)
lex.joinable = True
db.session.commit()
return lex
2021-06-03 03:05:33 +00:00
2021-05-31 17:52:37 +00:00
return lexicon_factory
@pytest.fixture
def make_membership(db: DbContext):
"""Provides a factory function for creating memberships, with valid default values."""
2021-06-03 03:05:33 +00:00
2021-05-31 17:52:37 +00:00
def membership_factory(**kwargs):
default_kwargs = {
2021-06-03 03:10:34 +00:00
"is_editor": False,
2021-05-31 17:52:37 +00:00
}
updated_kwargs = {**default_kwargs, **kwargs}
return memq.create(db, **updated_kwargs)
2021-06-03 03:05:33 +00:00
2021-05-31 17:52:37 +00:00
return membership_factory
@pytest.fixture
2021-06-02 01:46:19 +00:00
def make_character(db: DbContext):
"""Provides a factory function for creating characters, with valid default values."""
2021-06-03 03:05:33 +00:00
2021-06-03 03:10:34 +00:00
def character_factory(state={"nonce": 1}, **kwargs):
2021-06-02 01:46:19 +00:00
default_kwargs = {
2021-06-03 03:10:34 +00:00
"name": f'Character {state["nonce"]}',
"signature": None,
2021-06-02 01:46:19 +00:00
}
2021-06-03 03:10:34 +00:00
state["nonce"] += 1
2021-06-02 01:46:19 +00:00
updated_kwargs = {**default_kwargs, **kwargs}
return charq.create(db, **updated_kwargs)
2021-06-03 03:05:33 +00:00
2021-06-02 01:46:19 +00:00
return character_factory
class TestFactory:
def __init__(self, db, **factories):
self.db = db
self.factories = factories
def __getattr__(self, name):
return self.factories[name]
@pytest.fixture
def make(
2021-06-03 03:05:33 +00:00
db: DbContext, make_user, make_lexicon, make_membership, make_character
) -> TestFactory:
2021-06-02 01:46:19 +00:00
"""Fixture that groups all factory fixtures together."""
return TestFactory(
db,
user=make_user,
lexicon=make_lexicon,
membership=make_membership,
character=make_character,
)
@pytest.fixture
def lexicon_with_editor(make):
2021-05-31 17:52:37 +00:00
"""Shortcut setup for a lexicon game with an editor."""
2021-06-02 01:46:19 +00:00
editor = make.user()
2021-05-31 17:52:37 +00:00
assert editor
2021-06-02 01:46:19 +00:00
lexicon = make.lexicon()
2021-05-31 17:52:37 +00:00
assert lexicon
2021-06-03 03:05:33 +00:00
membership = make.membership(
user_id=editor.id, lexicon_id=lexicon.id, is_editor=True
)
2021-05-31 17:52:37 +00:00
assert membership
return (lexicon, editor)