server/tests: add func.comments tests
This commit is contained in:
parent
65efc309a8
commit
03c74cb5a3
|
@ -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'
|
Loading…
Reference in New Issue