def test_featuring_one_post_after_another(user_factory, post_factory, context_factory, fake_datetime): db.session.add(post_factory(id=1)) db.session.add(post_factory(id=2)) db.session.commit() assert posts.try_get_featured_post() is None assert not posts.get_post_by_id(1).is_featured assert not posts.get_post_by_id(2).is_featured with patch("szurubooru.func.posts.serialize_post"), patch( "szurubooru.func.snapshots._post_to_webhooks"): with fake_datetime("1997"): api.post_api.set_featured_post( context_factory( params={"id": 1}, user=user_factory(rank=model.User.RANK_REGULAR), )) with fake_datetime("1998"): api.post_api.set_featured_post( context_factory( params={"id": 2}, user=user_factory(rank=model.User.RANK_REGULAR), )) assert posts.try_get_featured_post() is not None assert posts.try_get_featured_post().post_id == 2 assert not posts.get_post_by_id(1).is_featured assert posts.get_post_by_id(2).is_featured
def test_get_post_by_id(post_factory): post = post_factory() db.session.add(post) db.session.flush() assert posts.get_post_by_id(post.post_id) == post with pytest.raises(posts.PostNotFoundError): posts.get_post_by_id(post.post_id + 1)
def test_try_get_post_by_id(post_factory): post = post_factory() db.session.add(post) db.session.flush() assert posts.try_get_post_by_id(post.post_id) == post assert posts.try_get_post_by_id(post.post_id + 1) is None with pytest.raises(posts.InvalidPostIdError): posts.get_post_by_id('-')
def test_get_post_by_id(post_factory): post = post_factory() db.session.add(post) db.session.flush() assert posts.get_post_by_id(post.post_id) == post with pytest.raises(posts.PostNotFoundError): posts.get_post_by_id(post.post_id + 1) with pytest.raises(posts.InvalidPostIdError): posts.get_post_by_id('-')
def merge_posts(ctx, _params=None): source_post_id = ctx.get_param_as_string('remove', required=True) or '' target_post_id = ctx.get_param_as_string('mergeTo', required=True) or '' replace_content = ctx.get_param_as_bool('replaceContent') source_post = posts.get_post_by_id(source_post_id) target_post = posts.get_post_by_id(target_post_id) versions.verify_version(source_post, ctx, 'removeVersion') versions.verify_version(target_post, ctx, 'mergeToVersion') versions.bump_version(target_post) auth.verify_privilege(ctx.user, 'posts:merge') posts.merge_posts(source_post, target_post, replace_content) snapshots.merge(source_post, target_post, ctx.user) ctx.session.commit() return _serialize_post(ctx, target_post)
def merge_posts( ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response: source_post_id = ctx.get_param_as_int('remove') target_post_id = ctx.get_param_as_int('mergeTo') source_post = posts.get_post_by_id(source_post_id) target_post = posts.get_post_by_id(target_post_id) replace_content = ctx.get_param_as_bool('replaceContent') versions.verify_version(source_post, ctx, 'removeVersion') versions.verify_version(target_post, ctx, 'mergeToVersion') versions.bump_version(target_post) auth.verify_privilege(ctx.user, 'posts:merge') posts.merge_posts(source_post, target_post, replace_content) snapshots.merge(source_post, target_post, ctx.user) ctx.session.commit() return _serialize_post(ctx, target_post)
def test_merge_posts_doesnt_duplicate_parent_relations(post_factory): source_post = post_factory() target_post = post_factory() related_post = post_factory() related_post.relations = [source_post, target_post] db.session.add_all([source_post, target_post, related_post]) db.session.commit() assert source_post.relation_count == 1 assert target_post.relation_count == 1 assert related_post.relation_count == 2 posts.merge_posts(source_post, target_post, False) db.session.commit() assert posts.try_get_post_by_id(source_post.post_id) is None assert posts.get_post_by_id(target_post.post_id).relation_count == 1 assert posts.get_post_by_id(related_post.post_id).relation_count == 1
def merge_posts(ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response: source_post_id = ctx.get_param_as_int('remove') target_post_id = ctx.get_param_as_int('mergeTo') source_post = posts.get_post_by_id(source_post_id) target_post = posts.get_post_by_id(target_post_id) replace_content = ctx.get_param_as_bool('replaceContent') versions.verify_version(source_post, ctx, 'removeVersion') versions.verify_version(target_post, ctx, 'mergeToVersion') versions.bump_version(target_post) auth.verify_privilege(ctx.user, 'posts:merge') posts.merge_posts(source_post, target_post, replace_content) snapshots.merge(source_post, target_post, ctx.user) ctx.session.commit() return _serialize_post(ctx, target_post)
def set_post_score(ctx, params): auth.verify_privilege(ctx.user, 'posts:score') post = posts.get_post_by_id(params['post_id']) score = ctx.get_param_as_int('score', required=True) scores.set_score(post, ctx.user, score) ctx.session.commit() return _serialize_post(ctx, post)
def put(self, ctx, post_id): auth.verify_privilege(ctx.user, 'posts:score') post = posts.get_post_by_id(post_id) score = ctx.get_param_as_int('score', required=True) scores.set_score(post, ctx.user, score) ctx.session.commit() return posts.serialize_post_with_details(post, ctx.user)
def put(self, ctx, post_id): post = posts.get_post_by_id(post_id) if ctx.has_file('content'): auth.verify_privilege(ctx.user, 'posts:edit:content') posts.update_post_content(post, ctx.get_file('content')) if ctx.has_param('tags'): auth.verify_privilege(ctx.user, 'posts:edit:tags') posts.update_post_tags(post, ctx.get_param_as_list('tags')) if ctx.has_param('safety'): auth.verify_privilege(ctx.user, 'posts:edit:safety') posts.update_post_safety(post, ctx.get_param_as_string('safety')) if ctx.has_param('source'): auth.verify_privilege(ctx.user, 'posts:edit:source') posts.update_post_source(post, ctx.get_param_as_string('source')) elif ctx.has_param('contentUrl'): posts.update_post_source(post, ctx.get_param_as_string('contentUrl')) if ctx.has_param('relations'): auth.verify_privilege(ctx.user, 'posts:edit:relations') posts.update_post_relations(post, ctx.get_param_as_list('relations')) if ctx.has_param('notes'): auth.verify_privilege(ctx.user, 'posts:edit:notes') posts.update_post_notes(post, ctx.get_param_as_list('notes')) if ctx.has_param('flags'): auth.verify_privilege(ctx.user, 'posts:edit:flags') posts.update_post_flags(post, ctx.get_param_as_list('flags')) if ctx.has_file('thumbnail'): auth.verify_privilege(ctx.user, 'posts:edit:thumbnail') posts.update_post_thumbnail(post, ctx.get_file('thumbnail')) post.last_edit_time = datetime.datetime.now() ctx.session.flush() snapshots.save_entity_modification(post, ctx.user) ctx.session.commit() tags.export_to_json() return posts.serialize_post_with_details(post, ctx.user)
def test_merge_posts_replaces_content( post_factory, config_injector, tmpdir, read_asset): config_injector({ 'data_dir': str(tmpdir.mkdir('data')), 'data_url': 'example.com', 'thumbnails': { 'post_width': 300, 'post_height': 300, }, }) source_post = post_factory() target_post = post_factory() content = read_asset('png.png') db.session.add_all([source_post, target_post]) db.session.commit() posts.update_post_content(source_post, content) db.session.flush() assert os.path.exists(os.path.join(str(tmpdir), 'data/posts/1.png')) assert not os.path.exists(os.path.join(str(tmpdir), 'data/posts/2.dat')) assert not os.path.exists(os.path.join(str(tmpdir), 'data/posts/2.png')) posts.merge_posts(source_post, target_post, True) db.session.flush() assert posts.try_get_post_by_id(source_post.post_id) is None post = posts.get_post_by_id(target_post.post_id) assert post is not None assert os.path.exists(os.path.join(str(tmpdir), 'data/posts/1.png')) assert os.path.exists(os.path.join(str(tmpdir), 'data/posts/2.png'))
def test_merge_posts_replaces_content(post_factory, config_injector, tmpdir, read_asset): config_injector({ 'data_dir': str(tmpdir.mkdir('data')), 'data_url': 'example.com', 'thumbnails': { 'post_width': 300, 'post_height': 300, }, 'secret': 'test', }) source_post = post_factory(id=1) target_post = post_factory(id=2) content = read_asset('png.png') db.session.add_all([source_post, target_post]) db.session.commit() posts.update_post_content(source_post, content) db.session.flush() source_path = (os.path.join( '{}/data/posts/1_244c8840887984c4.png'.format(tmpdir))) target_path1 = (os.path.join( '{}/data/posts/2_49caeb3ec1643406.png'.format(tmpdir))) target_path2 = (os.path.join( '{}/data/posts/2_49caeb3ec1643406.dat'.format(tmpdir))) assert os.path.exists(source_path) assert not os.path.exists(target_path1) assert not os.path.exists(target_path2) posts.merge_posts(source_post, target_post, True) db.session.flush() assert posts.try_get_post_by_id(source_post.post_id) is None post = posts.get_post_by_id(target_post.post_id) assert post is not None assert os.path.exists(source_path) assert os.path.exists(target_path1) assert not os.path.exists(target_path2)
def test_merge_posts_moves_parent_relations(post_factory, config_injector): config_injector({"delete_source_files": False}) source_post = post_factory() target_post = post_factory() related_post = post_factory() related_post.relations = [source_post] db.session.add_all([source_post, target_post, related_post]) db.session.commit() assert source_post.relation_count == 1 assert target_post.relation_count == 0 assert related_post.relation_count == 1 posts.merge_posts(source_post, target_post, False) db.session.commit() assert posts.try_get_post_by_id(source_post.post_id) is None assert posts.get_post_by_id(target_post.post_id).relation_count == 1 assert posts.get_post_by_id(related_post.post_id).relation_count == 1
def delete(self, ctx, post_id): auth.verify_privilege(ctx.user, 'posts:delete') post = posts.get_post_by_id(post_id) snapshots.save_entity_deletion(post, ctx.user) ctx.session.delete(post) ctx.session.commit() tags.export_to_json() return {}
def post(self, ctx): auth.verify_privilege(ctx.user, 'comments:create') text = ctx.get_param_as_string('text', required=True) post_id = ctx.get_param_as_int('postId', required=True) post = posts.get_post_by_id(post_id) comment = comments.create_comment(ctx.user, post, text) ctx.session.add(comment) ctx.session.commit() return comments.serialize_comment_with_details(comment, ctx.user)
def create_comment(ctx, _params=None): auth.verify_privilege(ctx.user, 'comments:create') text = ctx.get_param_as_string('text', required=True) post_id = ctx.get_param_as_int('postId', required=True) post = posts.get_post_by_id(post_id) comment = comments.create_comment(ctx.user, post, text) ctx.session.add(comment) ctx.session.commit() return _serialize(ctx, comment)
def delete_post(ctx, params): auth.verify_privilege(ctx.user, 'posts:delete') post = posts.get_post_by_id(params['post_id']) versions.verify_version(post, ctx) snapshots.delete(post, ctx.user) posts.delete(post) ctx.session.commit() tags.export_to_json() return {}
def test_featuring(user_factory, post_factory, context_factory): auth_user = user_factory(rank=model.User.RANK_REGULAR) post = post_factory(id=1) db.session.add(post) db.session.flush() assert not posts.get_post_by_id(1).is_featured with patch("szurubooru.func.posts.serialize_post"), patch( "szurubooru.func.snapshots.modify"): posts.serialize_post.return_value = "serialized post" result = api.post_api.set_featured_post( context_factory(params={"id": 1}, user=auth_user)) assert result == "serialized post" assert posts.try_get_featured_post() is not None assert posts.try_get_featured_post().post_id == 1 assert posts.get_post_by_id(1).is_featured result = api.post_api.get_featured_post( context_factory(user=user_factory(rank=model.User.RANK_REGULAR))) assert result == "serialized post" snapshots.modify.assert_called_once_with(post, auth_user)
def create_comment( ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response: auth.verify_privilege(ctx.user, 'comments:create') text = ctx.get_param_as_string('text') post_id = ctx.get_param_as_int('postId') post = posts.get_post_by_id(post_id) comment = comments.create_comment(ctx.user, post, text) ctx.session.add(comment) ctx.session.commit() return _serialize(ctx, comment)
def test_merge_posts_deletes_source_post(post_factory): source_post = post_factory() target_post = post_factory() db.session.add_all([source_post, target_post]) db.session.flush() posts.merge_posts(source_post, target_post, False) db.session.flush() assert posts.try_get_post_by_id(source_post.post_id) is None post = posts.get_post_by_id(target_post.post_id) assert post is not None
def create_comment(ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response: auth.verify_privilege(ctx.user, 'comments:create') text = ctx.get_param_as_string('text') post_id = ctx.get_param_as_int('postId') post = posts.get_post_by_id(post_id) comment = comments.create_comment(ctx.user, post, text) ctx.session.add(comment) ctx.session.commit() return _serialize(ctx, comment)
def test_featuring(user_factory, post_factory, context_factory): auth_user = user_factory(rank=db.User.RANK_REGULAR) post = post_factory(id=1) db.session.add(post) db.session.flush() assert not posts.get_post_by_id(1).is_featured with patch('szurubooru.func.posts.serialize_post'), \ patch('szurubooru.func.snapshots.modify'): posts.serialize_post.return_value = 'serialized post' result = api.post_api.set_featured_post( context_factory(params={'id': 1}, user=auth_user)) assert result == 'serialized post' assert posts.try_get_featured_post() is not None assert posts.try_get_featured_post().post_id == 1 assert posts.get_post_by_id(1).is_featured result = api.post_api.get_featured_post( context_factory( user=user_factory(rank=db.User.RANK_REGULAR))) assert result == 'serialized post' snapshots.modify.assert_called_once_with(post, auth_user)
def test_merge_posts_deletes_source_post(post_factory, config_injector): config_injector({"delete_source_files": False}) source_post = post_factory() target_post = post_factory() db.session.add_all([source_post, target_post]) db.session.flush() posts.merge_posts(source_post, target_post, False) db.session.flush() assert posts.try_get_post_by_id(source_post.post_id) is None post = posts.get_post_by_id(target_post.post_id) assert post is not None
def test_merge_posts_moves_scores(post_factory, post_score_factory): source_post = post_factory() target_post = post_factory() score = post_score_factory(post=source_post, score=1) db.session.add_all([source_post, target_post, score]) db.session.commit() assert source_post.score == 1 assert target_post.score == 0 posts.merge_posts(source_post, target_post, False) db.session.commit() assert posts.try_get_post_by_id(source_post.post_id) is None assert posts.get_post_by_id(target_post.post_id).score == 1
def test_merge_posts_moves_comments(post_factory, comment_factory): source_post = post_factory() target_post = post_factory() comment = comment_factory(post=source_post) db.session.add_all([source_post, target_post, comment]) db.session.commit() assert source_post.comment_count == 1 assert target_post.comment_count == 0 posts.merge_posts(source_post, target_post, False) db.session.commit() assert posts.try_get_post_by_id(source_post.post_id) is None assert posts.get_post_by_id(target_post.post_id).comment_count == 1
def test_merge_posts_moves_favorites(post_factory, post_favorite_factory): source_post = post_factory() target_post = post_factory() favorite = post_favorite_factory(post=source_post) db.session.add_all([source_post, target_post, favorite]) db.session.commit() assert source_post.favorite_count == 1 assert target_post.favorite_count == 0 posts.merge_posts(source_post, target_post, False) db.session.commit() assert posts.try_get_post_by_id(source_post.post_id) is None assert posts.get_post_by_id(target_post.post_id).favorite_count == 1
def test_merge_posts_doesnt_create_relation_loop_for_children(post_factory): source_post = post_factory() target_post = post_factory() source_post.relations = [target_post] db.session.add_all([source_post, target_post]) db.session.commit() assert source_post.relation_count == 1 assert target_post.relation_count == 1 posts.merge_posts(source_post, target_post, False) db.session.commit() assert posts.try_get_post_by_id(source_post.post_id) is None assert posts.get_post_by_id(target_post.post_id).relation_count == 0
def set_featured_post(ctx, _params=None): auth.verify_privilege(ctx.user, 'posts:feature') post_id = ctx.get_param_as_int('id', required=True) post = posts.get_post_by_id(post_id) featured_post = posts.try_get_featured_post() if featured_post and featured_post.post_id == post.post_id: raise posts.PostAlreadyFeaturedError( 'Post %r is already featured.' % post_id) posts.feature_post(post, ctx.user) snapshots.modify(post, ctx.user) ctx.session.commit() return _serialize_post(ctx, post)
def test_featuring_one_post_after_another(test_ctx, fake_datetime): db.session.add(test_ctx.post_factory(id=1)) db.session.add(test_ctx.post_factory(id=2)) db.session.commit() assert posts.try_get_featured_post() is None assert not posts.get_post_by_id(1).is_featured assert not posts.get_post_by_id(2).is_featured with fake_datetime('1997'): result = test_ctx.api.post( test_ctx.context_factory( input={'id': 1}, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR))) with fake_datetime('1998'): result = test_ctx.api.post( test_ctx.context_factory( input={'id': 2}, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR))) assert posts.try_get_featured_post() is not None assert posts.try_get_featured_post().post_id == 2 assert not posts.get_post_by_id(1).is_featured assert posts.get_post_by_id(2).is_featured
def set_featured_post(ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response: auth.verify_privilege(ctx.user, 'posts:feature') post_id = ctx.get_param_as_int('id') post = posts.get_post_by_id(post_id) featured_post = posts.try_get_featured_post() if featured_post and featured_post.post_id == post.post_id: raise posts.PostAlreadyFeaturedError('짤 %r 는 이미 대문짤임.' % post_id) posts.feature_post(post, ctx.user) snapshots.modify(post, ctx.user) ctx.session.commit() return _serialize_post(ctx, post)
def test_merge_posts_moves_child_relations(post_factory): source_post = post_factory() target_post = post_factory() related_post = post_factory() source_post.relations = [related_post] db.session.add_all([source_post, target_post, related_post]) db.session.commit() assert source_post.relation_count == 1 assert target_post.relation_count == 0 posts.merge_posts(source_post, target_post, False) db.session.commit() assert posts.try_get_post_by_id(source_post.post_id) is None assert posts.get_post_by_id(target_post.post_id).relation_count == 1
def test_merge_posts_doesnt_duplicate_tags(post_factory, tag_factory): source_post = post_factory() target_post = post_factory() tag = tag_factory() tag.posts = [source_post, target_post] db.session.add_all([source_post, target_post, tag]) db.session.commit() assert source_post.tag_count == 1 assert target_post.tag_count == 1 posts.merge_posts(source_post, target_post, False) db.session.commit() assert posts.try_get_post_by_id(source_post.post_id) is None assert posts.get_post_by_id(target_post.post_id).tag_count == 1
def set_featured_post( ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response: auth.verify_privilege(ctx.user, 'posts:feature') post_id = ctx.get_param_as_int('id') post = posts.get_post_by_id(post_id) featured_post = posts.try_get_featured_post() if featured_post and featured_post.post_id == post.post_id: raise posts.PostAlreadyFeaturedError( 'Post %r is already featured.' % post_id) posts.feature_post(post, ctx.user) snapshots.modify(post, ctx.user) ctx.session.commit() return _serialize_post(ctx, post)
def test_featuring(test_ctx): db.session.add(test_ctx.post_factory(id=1)) db.session.commit() assert not posts.get_post_by_id(1).is_featured result = test_ctx.api.post( test_ctx.context_factory( input={'id': 1}, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR))) assert posts.try_get_featured_post() is not None assert posts.try_get_featured_post().post_id == 1 assert posts.get_post_by_id(1).is_featured assert 'post' in result assert 'id' in result['post'] assert 'snapshots' in result assert 'comments' in result result = test_ctx.api.get( test_ctx.context_factory( user=test_ctx.user_factory(rank=db.User.RANK_REGULAR))) assert 'post' in result assert 'id' in result['post'] assert 'snapshots' in result assert 'comments' in result
def test_merge_posts_moves_scores(post_factory, post_score_factory, config_injector): config_injector({"delete_source_files": False}) source_post = post_factory() target_post = post_factory() score = post_score_factory(post=source_post, score=1) db.session.add_all([source_post, target_post, score]) db.session.commit() assert source_post.score == 1 assert target_post.score == 0 posts.merge_posts(source_post, target_post, False) db.session.commit() assert posts.try_get_post_by_id(source_post.post_id) is None assert posts.get_post_by_id(target_post.post_id).score == 1
def test_merge_posts_moves_comments(post_factory, comment_factory, config_injector): config_injector({"delete_source_files": False}) source_post = post_factory() target_post = post_factory() comment = comment_factory(post=source_post) db.session.add_all([source_post, target_post, comment]) db.session.commit() assert source_post.comment_count == 1 assert target_post.comment_count == 0 posts.merge_posts(source_post, target_post, False) db.session.commit() assert posts.try_get_post_by_id(source_post.post_id) is None assert posts.get_post_by_id(target_post.post_id).comment_count == 1
def test_featuring_one_post_after_another(user_factory, post_factory, context_factory, fake_datetime): db.session.add(post_factory(id=1)) db.session.add(post_factory(id=2)) db.session.commit() assert posts.try_get_featured_post() is None assert not posts.get_post_by_id(1).is_featured assert not posts.get_post_by_id(2).is_featured with patch('szurubooru.func.posts.serialize_post'): with fake_datetime('1997'): api.post_api.set_featured_post( context_factory( params={'id': 1}, user=user_factory(rank=model.User.RANK_REGULAR))) with fake_datetime('1998'): api.post_api.set_featured_post( context_factory( params={'id': 2}, user=user_factory(rank=model.User.RANK_REGULAR))) assert posts.try_get_featured_post() is not None assert posts.try_get_featured_post().post_id == 2 assert not posts.get_post_by_id(1).is_featured assert posts.get_post_by_id(2).is_featured
def test_featuring_one_post_after_another( user_factory, post_factory, context_factory, fake_datetime): db.session.add(post_factory(id=1)) db.session.add(post_factory(id=2)) db.session.commit() assert posts.try_get_featured_post() is None assert not posts.get_post_by_id(1).is_featured assert not posts.get_post_by_id(2).is_featured with patch('szurubooru.func.posts.serialize_post'): with fake_datetime('1997'): api.post_api.set_featured_post( context_factory( params={'id': 1}, user=user_factory(rank=db.User.RANK_REGULAR))) with fake_datetime('1998'): api.post_api.set_featured_post( context_factory( params={'id': 2}, user=user_factory(rank=db.User.RANK_REGULAR))) assert posts.try_get_featured_post() is not None assert posts.try_get_featured_post().post_id == 2 assert not posts.get_post_by_id(1).is_featured assert posts.get_post_by_id(2).is_featured
def test_merge_posts_moves_tags(post_factory, tag_factory, config_injector): config_injector({'delete_source_files': False}) source_post = post_factory() target_post = post_factory() tag = tag_factory() tag.posts = [source_post] db.session.add_all([source_post, target_post, tag]) db.session.commit() assert source_post.tag_count == 1 assert target_post.tag_count == 0 posts.merge_posts(source_post, target_post, False) db.session.commit() assert posts.try_get_post_by_id(source_post.post_id) is None assert posts.get_post_by_id(target_post.post_id).tag_count == 1
def test_merge_posts_doesnt_create_relation_loop_for_parents( post_factory, config_injector): config_injector({'delete_source_files': False}) source_post = post_factory() target_post = post_factory() target_post.relations = [source_post] db.session.add_all([source_post, target_post]) db.session.commit() assert source_post.relation_count == 1 assert target_post.relation_count == 1 posts.merge_posts(source_post, target_post, False) db.session.commit() assert posts.try_get_post_by_id(source_post.post_id) is None assert posts.get_post_by_id(target_post.post_id).relation_count == 0
def post(self, ctx): auth.verify_privilege(ctx.user, 'posts:feature') post_id = ctx.get_param_as_int('id', required=True) post = posts.get_post_by_id(post_id) featured_post = posts.try_get_featured_post() if featured_post and featured_post.post_id == post.post_id: raise posts.PostAlreadyFeaturedError( 'Post %r is already featured.' % post_id) posts.feature_post(post, ctx.user) if featured_post: snapshots.save_entity_modification(featured_post, ctx.user) snapshots.save_entity_modification(post, ctx.user) ctx.session.commit() return posts.serialize_post_with_details(post, ctx.user)
def test_merge_posts_doesnt_duplicate_scores( post_factory, user_factory, post_score_factory): source_post = post_factory() target_post = post_factory() user = user_factory() score1 = post_score_factory(post=source_post, score=1, user=user) score2 = post_score_factory(post=target_post, score=1, user=user) db.session.add_all([source_post, target_post, score1, score2]) db.session.commit() assert source_post.score == 1 assert target_post.score == 1 posts.merge_posts(source_post, target_post, False) db.session.commit() assert posts.try_get_post_by_id(source_post.post_id) is None assert posts.get_post_by_id(target_post.post_id).score == 1
def test_merge_posts_doesnt_duplicate_favorites( post_factory, user_factory, post_favorite_factory): source_post = post_factory() target_post = post_factory() user = user_factory() favorite1 = post_favorite_factory(post=source_post, user=user) favorite2 = post_favorite_factory(post=target_post, user=user) db.session.add_all([source_post, target_post, favorite1, favorite2]) db.session.commit() assert source_post.favorite_count == 1 assert target_post.favorite_count == 1 posts.merge_posts(source_post, target_post, False) db.session.commit() assert posts.try_get_post_by_id(source_post.post_id) is None assert posts.get_post_by_id(target_post.post_id).favorite_count == 1