This commit is contained in:
Tim Van Baak 2021-05-31 12:35:11 -07:00
parent e47711356c
commit f788e11077
2 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,40 @@
"""
Membership query interface
"""
from amanuensis.db import DbContext, Character
from amanuensis.errors import ArgumentError
def create(
db: DbContext,
lexicon_id: int,
user_id: int,
name: str,
signature: str) -> Character:
"""
Create a new character for a user.
"""
# Quick argument verification
if not isinstance(lexicon_id, int):
raise ArgumentError('lexicon_id')
if not isinstance(user_id, int):
raise ArgumentError('user_id')
# Verify character information
if not isinstance(name, str):
raise ArgumentError('Character name must be a string')
if not name.strip():
raise ArgumentError('Character name cannot be blank')
if signature is not None and not isinstance(signature, str):
raise ArgumentError('Signature must be a string')
new_character = Character(
lexicon_id=lexicon_id,
user_id=user_id,
name=name,
signature=signature,
)
db.session.add(new_character)
db.session.commit()
return new_character

19
tests/test_character.py Normal file
View File

@ -0,0 +1,19 @@
from amanuensis.db import *
import amanuensis.backend.lexicon as lexiq
import amanuensis.backend.membership as memq
import amanuensis.backend.user as userq
from .test_db import db
def test_create_character(db: DbContext):
"""Test creating a character."""
# Set up a user and lexicon
new_user = userq.create(db, 'username', 'password', 'user', 'a@b.c', False)
assert new_user.id, 'Failed to create user'
new_lexicon = lexiq.create(db, 'Test', None, 'prompt')
assert new_lexicon.id, 'Failed to create lexicon'
mem = memq.create(db, new_user.id, new_lexicon.id, True)
assert mem, 'Failed to create membership'
#