post testing properly creates database context

This commit is contained in:
Nikolai 2021-05-29 18:44:03 -07:00
parent 4af22ad863
commit 592becff2d
1 changed files with 20 additions and 5 deletions

View File

@ -2,26 +2,41 @@ 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': 0,
'user_id': 0,
'lexicon_id': new_lexicon.id,
'user_id': new_user.id,
'body': 'body'
}
# Test ids are integers
# 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'})
# test empty arguments
# empty arguments don't work
with pytest.raises(ArgumentError):
postq.create(db, **{**kwargs, 'lexicon_id': ''})
with pytest.raises(ArgumentError):
@ -29,7 +44,7 @@ def test_create_post(db):
with pytest.raises(ArgumentError):
postq.create(db, **{**kwargs, 'body': ''})
# check whitespace
# post with only whitespace doesn't work
with pytest.raises(ArgumentError):
postq.create(db, **{**kwargs, 'body': ' '})