amanuensis/tests/test_character.py

89 lines
2.8 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."""
2021-06-03 04:31:42 +00:00
lexicon: Lexicon
user: User
2021-05-31 22:09:26 +00:00
lexicon, user = lexicon_with_editor
2021-06-03 04:31:42 +00:00
defaults: dict = {
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
}
2021-06-03 04:31:42 +00:00
kwargs: dict
2021-05-31 22:09:26 +00:00
# Bad argument types
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "name": b"bytestring"}
charq.create(**kwargs)
2021-05-31 22:09:26 +00:00
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "name": None}
charq.create(**kwargs)
2021-05-31 22:09:26 +00:00
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "signature": b"bytestring"}
charq.create(**kwargs)
2021-05-31 22:09:26 +00:00
# Bad character name
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "name": " "}
charq.create(**kwargs)
2021-05-31 22:09:26 +00:00
# Signature is auto-populated
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "signature": None}
char = charq.create(**kwargs)
2021-05-31 22:09:26 +00:00
assert char.signature is not None
# User must be in lexicon
2021-06-03 04:31:42 +00:00
new_user: User = make.user()
2021-05-31 22:09:26 +00:00
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "user_id": new_user.id}
charq.create(**kwargs)
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 04:31:42 +00:00
char1: Character = charq.create(
db, lexicon.id, user.id, "Test Character 1", signature=None
)
2021-06-03 03:10:34 +00:00
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 04:31:42 +00:00
char2: Character = 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 04:31:42 +00:00
char3: Character = 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"