From 03c74cb5a31afe5b89c4cb751b7a147bb3b48cd2 Mon Sep 17 00:00:00 2001 From: rr- Date: Sun, 14 Aug 2016 11:50:40 +0200 Subject: [PATCH] server/tests: add func.comments tests --- server/szurubooru/tests/func/test_comments.py | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 server/szurubooru/tests/func/test_comments.py diff --git a/server/szurubooru/tests/func/test_comments.py b/server/szurubooru/tests/func/test_comments.py new file mode 100644 index 00000000..78412f23 --- /dev/null +++ b/server/szurubooru/tests/func/test_comments.py @@ -0,0 +1,73 @@ +import unittest.mock +import pytest +from datetime import datetime +from szurubooru import db +from szurubooru.func import comments, users + +def test_serialize_user(user_factory, comment_factory): + with unittest.mock.patch('szurubooru.func.users.get_avatar_url'): + users.get_avatar_url.return_value = 'https://example.com/avatar.png' + comment = comment_factory(user=user_factory(name='dummy')) + comment.comment_id = 77 + comment.creation_time = datetime(1997, 1, 1) + comment.last_edit_time = datetime(1998, 1, 1) + comment.text = 'text' + + db.session.add(comment) + db.session.flush() + + auth_user = user_factory() + + assert comments.serialize_comment(comment, auth_user) == { + 'id': comment.comment_id, + 'postId': comment.post.post_id, + 'creationTime': datetime(1997, 1, 1, 0, 0), + 'lastEditTime': datetime(1998, 1, 1, 0, 0), + 'score': 0, + 'ownScore': 0, + 'text': 'text', + 'user': { + 'name': 'dummy', + 'avatarUrl': 'https://example.com/avatar.png', + }, + 'version': 1, + } + +def test_try_get_comment(comment_factory): + comment = comment_factory() + db.session.add(comment) + assert comments.try_get_comment_by_id(999) is None + assert comments.try_get_comment_by_id(1) is comment + with pytest.raises(comments.InvalidCommentIdError): + comments.try_get_comment_by_id('-') + +def test_get_comment(comment_factory): + comment = comment_factory() + db.session.add(comment) + with pytest.raises(comments.CommentNotFoundError): + comments.get_comment_by_id(999) + assert comments.get_comment_by_id(1) is comment + with pytest.raises(comments.InvalidCommentIdError): + comments.get_comment_by_id('-') + +def test_create_comment(user_factory, post_factory, fake_datetime): + user = user_factory() + post = post_factory() + db.session.add_all([user, post]) + with unittest.mock.patch('szurubooru.func.comments.update_comment_text'), \ + fake_datetime('1997-01-01'): + comment = comments.create_comment(user, post, 'text') + assert comment.creation_time == datetime(1997, 1, 1) + assert comment.user == user + assert comment.post == post + comments.update_comment_text.assert_called_once_with(comment, 'text') + +def test_update_comment_text_with_emptry_string(comment_factory): + comment = comment_factory() + with pytest.raises(comments.EmptyCommentTextError): + comments.update_comment_text(comment, None) + +def test_update_comment_text(comment_factory): + comment = comment_factory() + comments.update_comment_text(comment, 'text') + assert comment.text == 'text'