Files
danbooru/test/unit/favorite_test.rb
evazion 1653392361 posts: stop updating fav_string attribute.
Stop updating the fav_string attribute on posts. The column still exists
on the table, but is no longer used or updated.

Like the pool_string in 7d503f08, the fav_string was used in the past to
facilitate `fav:X` searches. Posts had a hidden fav_string column that
contained a list of every user who favorited the post. These were
treated like fake hidden tags on the post so that a search for `fav:X`
was treated like a tag search.

The fav_string attribute has been unused for search purposes for a while
now. It was only kept because of technicalities that required
departitioning the favorites table first (340e1008e) before it could be
removed. Basically, removing favorites with `@favorite.destroy` was
slow because Rails always deletes object by ID, but we didn't have an
index on favorites.id, and we couldn't easily add one until the
favorites table was departitioned.

Fixes #4652. See https://github.com/danbooru/danbooru/issues/4652#issuecomment-754993802
for more discussion of issues caused by the fav_string (in short: write
amplification, post table bloat, and favorite inconsistency problems).
2021-10-09 22:36:26 -05:00

97 lines
3.2 KiB
Ruby

require 'test_helper'
class FavoriteTest < ActiveSupport::TestCase
setup do
@user1 = create(:user)
@user2 = create(:user)
@p1 = create(:post)
@p2 = create(:post)
end
context "Favorites: " do
context "removing a favorite" do
should "update the post and user favorite counts" do
fav = Favorite.create!(post: @p1, user: @user1)
assert_equal(1, @user1.reload.favorite_count)
assert_equal(1, @p1.reload.fav_count)
Favorite.destroy_by(post: @p1, user: @user1)
assert_equal(0, @user1.reload.favorite_count)
assert_equal(0, @p1.reload.fav_count)
end
should "remove the upvote if the user could vote" do
@user = create(:gold_user)
@vote = create(:post_vote, post: @p1, user: @user, score: 1)
fav = Favorite.create!(post: @p1, user: @user)
assert_equal(1, @user.reload.favorite_count)
assert_equal(1, @p1.reload.fav_count)
assert_equal(1, @p1.reload.score)
assert(PostVote.positive.exists?(post: @p1, user: @user))
Favorite.destroy_by(post: @p1, user: @user)
assert_equal(0, @user.reload.favorite_count)
assert_equal(0, @p1.reload.fav_count)
assert_equal(0, @p1.reload.score)
refute(PostVote.positive.exists?(post: @p1, user: @user))
end
end
context "adding a favorite" do
should "update the post and user favorite counts" do
Favorite.create!(post: @p1, user: @user1)
assert_equal(1, @user1.reload.favorite_count)
assert_equal(1, @p1.reload.fav_count)
end
should "not upvote the post if the user can't vote" do
Favorite.create!(post: @p1, user: @user1)
assert_equal(1, @user1.reload.favorite_count)
assert_equal(1, @p1.reload.fav_count)
assert_equal(0, @p1.reload.score)
refute(PostVote.positive.exists?(post: @p1, user: @user1))
end
should "upvote the post if the user can vote" do
@user = create(:gold_user)
Favorite.create!(post: @p1, user: @user)
assert_equal(1, @user.reload.favorite_count)
assert_equal(1, @p1.reload.fav_count)
assert_equal(1, @p1.reload.score)
assert(PostVote.positive.exists?(post: @p1, user: @user))
end
should "convert a downvote into an upvote if the post was downvoted" do
@user = create(:gold_user)
@vote = create(:post_vote, post: @p1, user: @user, score: -1)
assert_equal(-1, @p1.reload.score)
Favorite.create!(post: @p1, user: @user)
assert_equal(1, @user.reload.favorite_count)
assert_equal(1, @p1.reload.fav_count)
assert_equal(1, @p1.reload.score)
assert(PostVote.positive.exists?(post: @p1, user: @user))
refute(PostVote.negative.exists?(post: @p1, user: @user))
end
should "not allow duplicate favorites" do
@f1 = Favorite.create(post: @p1, user: @user1)
@f2 = Favorite.create(post: @p1, user: @user1)
assert_equal(["You have already favorited this post"], @f2.errors.full_messages)
assert_equal(1, @user1.reload.favorite_count)
assert_equal(1, @p1.reload.fav_count)
assert_equal(0, @p1.reload.score)
end
end
end
end