amanuensis/tests/backend/test_post.py

55 lines
1.6 KiB
Python
Raw Permalink Normal View History

import pytest
2021-06-29 04:13:14 +00:00
from amanuensis.backend import postq
2021-05-31 17:52:37 +00:00
from amanuensis.db import DbContext
from amanuensis.errors import ArgumentError, BackendArgumentTypeError
2021-05-31 17:52:37 +00:00
def test_create_post(db: DbContext, lexicon_with_editor):
"""Test new post creation"""
2021-05-31 17:52:37 +00:00
lexicon, editor = lexicon_with_editor
# argument dictionary for post object
2021-06-03 04:31:42 +00:00
defaults: dict = {
"db": db,
"lexicon_id": lexicon.id,
"user_id": editor.id,
"body": "body",
}
kwargs: dict
# ids are integers
with pytest.raises(BackendArgumentTypeError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "user_id": "zero"}
postq.create(**kwargs)
with pytest.raises(BackendArgumentTypeError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "lexicon_id": "zero"}
postq.create(**kwargs)
# empty arguments don't work
with pytest.raises(BackendArgumentTypeError):
kwargs = {**defaults, "lexicon_id": None}
2021-06-03 04:31:42 +00:00
postq.create(**kwargs)
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "body": ""}
postq.create(**kwargs)
# post with only whitespace doesn't work
with pytest.raises(ArgumentError):
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "body": " "}
postq.create(**kwargs)
# post creation works and populates fields
2021-06-03 04:31:42 +00:00
new_post = postq.create(**defaults)
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
2021-06-03 04:31:42 +00:00
kwargs = {**defaults, "user_id": None}
new_post = postq.create(**kwargs)
assert new_post
assert new_post.user_id is None