Compare commits
1 Commits
243df40ac7
...
f788e11077
Author | SHA1 | Date |
---|---|---|
Tim Van Baak | f788e11077 |
|
@ -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
|
|
@ -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'
|
||||
|
||||
#
|
Loading…
Reference in New Issue