amanuensis/tests/test_lexicon.py

54 lines
1.7 KiB
Python
Raw Normal View History

2021-06-03 04:31:42 +00:00
from amanuensis.db.models import Lexicon
2021-05-28 01:56:29 +00:00
import datetime
import pytest
2021-05-31 17:52:37 +00:00
from amanuensis.db import DbContext
2021-05-28 01:56:29 +00:00
import amanuensis.backend.lexicon as lexiq
from amanuensis.errors import ArgumentError
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)