From 4af22ad863ab7d41fa9692b2007d2b855075f461 Mon Sep 17 00:00:00 2001 From: Nikolai Date: Sat, 29 May 2021 18:31:31 -0700 Subject: [PATCH] added unit test for post --- amanuensis/backend/post.py | 4 ++-- tests/test_post.py | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/amanuensis/backend/post.py b/amanuensis/backend/post.py index 9b88138..ea5e219 100644 --- a/amanuensis/backend/post.py +++ b/amanuensis/backend/post.py @@ -23,8 +23,8 @@ def create( raise ArgumentError('Lexicon id must be an integer.') # Verify user_id - if not isinstance(user_id, int): - raise ArgumentError('Lexicon id must ne an integer.') + if not (isinstance(user_id, int) or user_id is None): + raise ArgumentError('User id must be an integer.') # Verify body if not isinstance(body, str): diff --git a/tests/test_post.py b/tests/test_post.py index e69de29..2d039dc 100644 --- a/tests/test_post.py +++ b/tests/test_post.py @@ -0,0 +1,46 @@ +import pytest + +from amanuensis.db import * +import amanuensis.backend.lexicon as lexiq +import amanuensis.backend.post as postq +from amanuensis.errors import ArgumentError + +from .test_db import db + +def test_create_post(db): + """Test new post creation""" + kwargs = { + 'lexicon_id': 0, + 'user_id': 0, + 'body': 'body' + } + + # Test 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 + 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': ''}) + + # check whitespace + 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 \ No newline at end of file