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_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_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_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 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_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_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_doesnt_create_relation_loop_for_parents(post_factory): 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 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_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 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_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_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_merge_posts_doesnt_create_relation_loop_for_children( post_factory, config_injector): config_injector({"delete_source_files": False}) 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 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
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_child_relations(post_factory, config_injector): config_injector({'delete_source_files': False}) source_post = post_factory() target_post = post_factory() related_post = post_factory() source_post.relations = [related_post] target_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 == 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
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 test_merge_posts_replaces_content( post_factory, config_injector, tmpdir, read_asset ): config_injector( { "data_dir": str(tmpdir.mkdir("data")), "data_url": "example.com", "delete_source_files": False, "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_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