Compare commits
5 Commits
1e9405bad0
...
ea5b3e3b66
Author | SHA1 | Date |
---|---|---|
Nikolai | ea5b3e3b66 | |
Nikolai | 592becff2d | |
Nikolai | 4af22ad863 | |
Nikolai | 2f568fb385 | |
Tim Van Baak | 76f4bcc181 |
|
@ -0,0 +1,32 @@
|
||||||
|
"""
|
||||||
|
Membership query interface
|
||||||
|
"""
|
||||||
|
|
||||||
|
from amanuensis.db import DbContext, Membership
|
||||||
|
from amanuensis.errors import ArgumentError
|
||||||
|
|
||||||
|
|
||||||
|
def create(
|
||||||
|
db: DbContext,
|
||||||
|
user_id: int,
|
||||||
|
lexicon_id: int,
|
||||||
|
is_editor: bool) -> Membership:
|
||||||
|
"""
|
||||||
|
Create a new user membership in a lexicon.
|
||||||
|
"""
|
||||||
|
# Quick argument verification
|
||||||
|
if not isinstance(user_id, int):
|
||||||
|
raise ArgumentError('user_id')
|
||||||
|
if not isinstance(lexicon_id, int):
|
||||||
|
raise ArgumentError('lexicon_id')
|
||||||
|
if not isinstance(is_editor, bool):
|
||||||
|
raise ArgumentError('is_editor')
|
||||||
|
|
||||||
|
new_membership = Membership(
|
||||||
|
user_id=user_id,
|
||||||
|
lexicon_id=lexicon_id,
|
||||||
|
is_editor=is_editor,
|
||||||
|
)
|
||||||
|
db.session.add(new_membership)
|
||||||
|
db.session.commit()
|
||||||
|
return new_membership
|
|
@ -23,8 +23,8 @@ def create(
|
||||||
raise ArgumentError('Lexicon id must be an integer.')
|
raise ArgumentError('Lexicon id must be an integer.')
|
||||||
|
|
||||||
# Verify user_id
|
# Verify user_id
|
||||||
if not isinstance(user_id, int):
|
if not (isinstance(user_id, int) or user_id is None):
|
||||||
raise ArgumentError('Lexicon id must ne an integer.')
|
raise ArgumentError('User id must be an integer.')
|
||||||
|
|
||||||
# Verify body
|
# Verify body
|
||||||
if not isinstance(body, str):
|
if not isinstance(body, str):
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
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(db: DbContext):
|
||||||
|
"""Test joining a game."""
|
||||||
|
# Set up a user and a 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'
|
||||||
|
|
||||||
|
# Add the user to the lexicon as an editor
|
||||||
|
mem = memq.create(db, new_user.id, new_lexicon.id, True)
|
||||||
|
assert mem, 'Failed to create membership'
|
||||||
|
|
||||||
|
# Check that the user and lexicon are mutually visible in the ORM relationships
|
||||||
|
assert new_user.memberships, 'User memberships not updated'
|
||||||
|
assert new_lexicon.memberships, 'Lexicon memberships not updated'
|
||||||
|
assert new_user.memberships[0].lexicon_id == new_lexicon.id
|
||||||
|
assert new_lexicon.memberships[0].user_id == new_user.id
|
||||||
|
|
||||||
|
# Check that the editor flag was set properly
|
||||||
|
assert new_lexicon.memberships
|
|
@ -0,0 +1,61 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from amanuensis.db import *
|
||||||
|
import amanuensis.backend.lexicon as lexiq
|
||||||
|
import amanuensis.backend.user as userq
|
||||||
|
import amanuensis.backend.post as postq
|
||||||
|
import amanuensis.backend.membership as memq
|
||||||
|
|
||||||
|
from amanuensis.errors import ArgumentError
|
||||||
|
|
||||||
|
from .test_db import db
|
||||||
|
|
||||||
|
def test_create_post(db):
|
||||||
|
"""Test new post creation"""
|
||||||
|
|
||||||
|
# Make 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'
|
||||||
|
|
||||||
|
# Add the user to the lexicon as an editor
|
||||||
|
mem = memq.create(db, new_user.id, new_lexicon.id, True)
|
||||||
|
assert mem, 'Failed to create membership'
|
||||||
|
|
||||||
|
# argument dictionary for post object
|
||||||
|
kwargs = {
|
||||||
|
'lexicon_id': new_lexicon.id,
|
||||||
|
'user_id': new_user.id,
|
||||||
|
'body': 'body'
|
||||||
|
}
|
||||||
|
|
||||||
|
# ids are integers
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
postq.create(db, **{**kwargs, 'user_id': 'zero'})
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
postq.create(db, **{**kwargs, 'lexicon_id': 'zero'})
|
||||||
|
|
||||||
|
# empty arguments don't work
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
postq.create(db, **{**kwargs, 'lexicon_id': ''})
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
postq.create(db, **{**kwargs, 'user_id': ''})
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
postq.create(db, **{**kwargs, 'body': ''})
|
||||||
|
|
||||||
|
# post with only whitespace doesn't work
|
||||||
|
with pytest.raises(ArgumentError):
|
||||||
|
postq.create(db, **{**kwargs, 'body': ' '})
|
||||||
|
|
||||||
|
# post creation works and populates fields
|
||||||
|
new_post = postq.create(db, **kwargs)
|
||||||
|
assert new_post
|
||||||
|
assert new_post.lexicon_id is not None
|
||||||
|
assert new_post.user_id is not None
|
||||||
|
assert new_post.body is not None
|
||||||
|
|
||||||
|
# post creation works when user is None
|
||||||
|
new_post = postq.create(db, **{**kwargs, 'user_id': None})
|
||||||
|
assert new_post
|
||||||
|
assert new_post.user_id is None
|
Loading…
Reference in New Issue