From f9babd73b9c0ed70f5cea1964913a93bced40117 Mon Sep 17 00:00:00 2001 From: Nikolai Date: Sat, 29 May 2021 17:45:03 -0700 Subject: [PATCH] Added post creation method and unit tests --- amanuensis/backend/lexicon.py | 2 +- amanuensis/backend/post.py | 42 ++++++++++++++++++++++++ tests/test_post.py | 61 +++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 amanuensis/backend/post.py create mode 100644 tests/test_post.py diff --git a/amanuensis/backend/lexicon.py b/amanuensis/backend/lexicon.py index 187d50f..b8391ba 100644 --- a/amanuensis/backend/lexicon.py +++ b/amanuensis/backend/lexicon.py @@ -48,4 +48,4 @@ def create( ) db.session.add(new_lexicon) db.session.commit() - return new_lexicon + return new_lexicon \ No newline at end of file diff --git a/amanuensis/backend/post.py b/amanuensis/backend/post.py new file mode 100644 index 0000000..9ce635f --- /dev/null +++ b/amanuensis/backend/post.py @@ -0,0 +1,42 @@ +""" +Post query interface +""" + +import re + +from sqlalchemy import select, func + +from amanuensis.db import DbContext, Post +from amanuensis.errors import ArgumentError + +def create( + db: DbContext, + lexicon_id: int, + user_id: int, + body: str) -> Post: + """ + Create a new post + """ + + # Verify lexicon id + if not isinstance(lexicon_id, int): + raise ArgumentError('Lexicon id must be an integer.') + + # Verify user_id + 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): + raise ArgumentError('Post body must be a string.') + if not body.strip(): + raise ArgumentError('Post body cannot be empty.') + + new_post = Post( + lexicon_id=lexicon_id, + user_id=user_id, + body=body + ) + db.session.add(new_post) + db.session.commit() + return new_post diff --git a/tests/test_post.py b/tests/test_post.py new file mode 100644 index 0000000..934ddcb --- /dev/null +++ b/tests/test_post.py @@ -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