2021-05-28 01:56:29 +00:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from amanuensis.db import *
|
|
|
|
import amanuensis.backend.lexicon as lexiq
|
|
|
|
from amanuensis.errors import ArgumentError
|
|
|
|
|
|
|
|
from .test_db import db
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_lexicon(db):
|
|
|
|
"""Test new game creation."""
|
|
|
|
kwargs = {
|
|
|
|
'name': 'Test',
|
|
|
|
'title': None,
|
|
|
|
'prompt': 'A test Lexicon game'
|
|
|
|
}
|
|
|
|
# Test name constraints
|
|
|
|
with pytest.raises(ArgumentError):
|
2021-05-29 23:53:27 +00:00
|
|
|
lexiq.create(db, **{**kwargs, 'name': None})
|
2021-05-28 01:56:29 +00:00
|
|
|
with pytest.raises(ArgumentError):
|
2021-05-29 23:53:27 +00:00
|
|
|
lexiq.create(db, **{**kwargs, 'name': ''})
|
2021-05-28 01:56:29 +00:00
|
|
|
with pytest.raises(ArgumentError):
|
2021-05-29 23:53:27 +00:00
|
|
|
lexiq.create(db, **{**kwargs, 'name': ' '})
|
2021-05-28 01:56:29 +00:00
|
|
|
with pytest.raises(ArgumentError):
|
2021-05-29 23:53:27 +00:00
|
|
|
lexiq.create(db, **{**kwargs, 'name': '..'})
|
2021-05-28 01:56:29 +00:00
|
|
|
with pytest.raises(ArgumentError):
|
2021-05-29 23:53:27 +00:00
|
|
|
lexiq.create(db, **{**kwargs, 'name': '\x00'})
|
2021-05-28 01:56:29 +00:00
|
|
|
with pytest.raises(ArgumentError):
|
2021-05-29 23:53:27 +00:00
|
|
|
lexiq.create(db, **{**kwargs, 'name': 'space in name'})
|
2021-05-28 01:56:29 +00:00
|
|
|
|
|
|
|
# Validate that creation populates fields, including timestamps
|
|
|
|
before = datetime.datetime.utcnow() - datetime.timedelta(seconds=1)
|
2021-05-29 23:53:27 +00:00
|
|
|
new_lexicon = lexiq.create(db, **kwargs)
|
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-05-29 23:53:27 +00:00
|
|
|
duplicate = lexiq.create(db, **kwargs)
|