amanuensis/tests/backend/test_lexicon.py

112 lines
3.5 KiB
Python
Raw Normal View History

2021-05-28 01:56:29 +00:00
import datetime
import time
2021-05-28 01:56:29 +00:00
import pytest
import amanuensis.backend.lexicon as lexiq
from amanuensis.db import DbContext, Lexicon, User
2021-05-28 01:56:29 +00:00
from amanuensis.errors import ArgumentError
from tests.conftest import ObjectFactory
2021-05-28 01:56:29 +00:00
2021-05-31 17:52:37 +00:00
def test_create_lexicon(db: DbContext):
2021-05-28 01:56:29 +00:00
"""Test new game creation."""
2021-06-03 04:31:42 +00:00
defaults: dict = {
"db": db,
"name": "Test",
"title": None,
"prompt": "A test Lexicon game",
}
kwargs: dict
2021-05-28 01:56:29 +00:00
# Test name constraints
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "name": None}
lexiq.create(**kwargs)
2021-05-28 01:56:29 +00:00
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "name": ""}
lexiq.create(**kwargs)
2021-05-28 01:56:29 +00:00
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "name": " "}
lexiq.create(**kwargs)
2021-05-28 01:56:29 +00:00
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "name": ".."}
lexiq.create(**kwargs)
2021-05-28 01:56:29 +00:00
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "name": "\x00"}
lexiq.create(**kwargs)
2021-05-28 01:56:29 +00:00
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "name": "space in name"}
lexiq.create(**kwargs)
2021-05-28 01:56:29 +00:00
# Validate that creation populates fields, including timestamps
before = datetime.datetime.utcnow() - datetime.timedelta(seconds=1)
2021-06-03 04:31:42 +00:00
new_lexicon: Lexicon = lexiq.create(**defaults)
2021-05-28 01:56:29 +00:00
after = datetime.datetime.utcnow() + datetime.timedelta(seconds=1)
assert new_lexicon
assert new_lexicon.id is not None
assert new_lexicon.created is not None
assert before < new_lexicon.created
assert new_lexicon.created < after
# No duplicate lexicon names
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
lexiq.create(**defaults)
def test_lexicon_from(db: DbContext, make: ObjectFactory):
"""Test lexiq.from_*."""
lexicon1: Lexicon = make.lexicon()
lexicon2: Lexicon = make.lexicon()
2021-06-29 03:56:40 +00:00
assert lexiq.try_from_name(db, lexicon1.name) == lexicon1
assert lexiq.try_from_name(db, lexicon2.name) == lexicon2
def test_get_lexicon(db: DbContext, make: ObjectFactory):
"""Test the various scoped get functions."""
user: User = make.user()
public_joined: Lexicon = make.lexicon()
public_joined.public = True
make.membership(user_id=user.id, lexicon_id=public_joined.id)
private_joined: Lexicon = make.lexicon()
private_joined.public = False
make.membership(user_id=user.id, lexicon_id=private_joined.id)
public_open: Lexicon = make.lexicon()
public_open.public = True
db.session.commit()
private_open: Lexicon = make.lexicon()
private_open.public = False
db.session.commit()
get_all = list(lexiq.get_all(db))
assert public_joined in get_all
assert private_joined in get_all
assert public_open in get_all
assert private_open in get_all
get_joined = list(lexiq.get_joined(db, user.id))
assert public_joined in get_joined
assert private_joined in get_joined
assert public_open not in get_joined
assert private_open not in get_joined
get_public = list(lexiq.get_public(db))
assert public_joined in get_public
assert private_joined not in get_public
assert public_open in get_public
assert private_open not in get_public
def test_lexicon_update_ts(db: DbContext, make: ObjectFactory):
"""Test the update timestamp."""
lexicon: Lexicon = make.lexicon()
assert lexicon.created == lexicon.last_updated
time.sleep(1) # The update timestamp has only second-level precision
lexicon.prompt = "New prompt, who dis"
db.session.commit()
assert lexicon.created != lexicon.last_updated