amanuensis/tests/test_character.py

78 lines
2.5 KiB
Python
Raw Normal View History

2021-05-31 22:09:26 +00:00
import pytest
from amanuensis.db import *
import amanuensis.backend.character as charq
from amanuensis.errors import ArgumentError
2021-06-02 01:46:19 +00:00
def test_create_character(db: DbContext, lexicon_with_editor, make):
2021-05-31 22:09:26 +00:00
"""Test creating a character."""
lexicon, user = lexicon_with_editor
kwargs = {
2021-06-03 03:10:34 +00:00
"db": db,
"user_id": user.id,
"lexicon_id": lexicon.id,
"name": "Character Name",
"signature": "Signature",
2021-05-31 22:09:26 +00:00
}
# Bad argument types
with pytest.raises(ArgumentError):
2021-06-03 03:10:34 +00:00
charq.create(**{**kwargs, "name": b"bytestring"})
2021-05-31 22:09:26 +00:00
with pytest.raises(ArgumentError):
2021-06-03 03:10:34 +00:00
charq.create(**{**kwargs, "name": None})
2021-05-31 22:09:26 +00:00
with pytest.raises(ArgumentError):
2021-06-03 03:10:34 +00:00
charq.create(**{**kwargs, "signature": b"bytestring"})
2021-05-31 22:09:26 +00:00
# Bad character name
with pytest.raises(ArgumentError):
2021-06-03 03:10:34 +00:00
charq.create(**{**kwargs, "name": " "})
2021-05-31 22:09:26 +00:00
# Signature is auto-populated
2021-06-03 03:10:34 +00:00
char = charq.create(**{**kwargs, "signature": None})
2021-05-31 22:09:26 +00:00
assert char.signature is not None
# User must be in lexicon
2021-06-02 01:46:19 +00:00
new_user = make.user()
2021-05-31 22:09:26 +00:00
with pytest.raises(ArgumentError):
2021-06-03 03:10:34 +00:00
charq.create(**{**kwargs, "user_id": new_user.id})
2021-05-31 22:09:26 +00:00
def test_character_limits(db: DbContext, lexicon_with_editor):
"""Test lexicon settings limiting character creation."""
lexicon: Lexicon
user: User
lexicon, user = lexicon_with_editor
# Set character limit to one and create a character
lexicon.character_limit = 1
db.session.commit()
2021-06-03 03:10:34 +00:00
char1 = charq.create(db, lexicon.id, user.id, "Test Character 1", signature=None)
assert char1.id, "Failed to create character 1"
2021-05-31 22:09:26 +00:00
# Creating a second character should fail
with pytest.raises(ArgumentError):
2021-06-03 03:05:33 +00:00
char2 = charq.create(
2021-06-03 03:10:34 +00:00
db, lexicon.id, user.id, "Test Character 2", signature=None
2021-06-03 03:05:33 +00:00
)
2021-05-31 22:09:26 +00:00
assert char2
# Raising the limit to 2 should allow a second character
lexicon.character_limit = 2
db.session.commit()
2021-06-03 03:10:34 +00:00
char2 = charq.create(db, lexicon.id, user.id, "Test Character 2", signature=None)
assert char2.id, "Failed to create character 2"
2021-05-31 22:09:26 +00:00
# Creating a third character should fail
with pytest.raises(ArgumentError):
2021-06-03 03:05:33 +00:00
char3 = charq.create(
2021-06-03 03:10:34 +00:00
db, lexicon.id, user.id, "Test Character 3", signature=None
2021-06-03 03:05:33 +00:00
)
2021-05-31 22:09:26 +00:00
assert char3
# Setting the limit to null should allow a third character
lexicon.character_limit = None
db.session.commit()
2021-06-03 03:10:34 +00:00
char3 = charq.create(db, lexicon.id, user.id, "Test Character 3", signature=None)
assert char3.id, "Failed to create character 3"