FIX: Use a logging table for daily likes given. Use it for badges.

This commit is contained in:
Robin Ward
2016-03-17 14:41:00 -04:00
parent f15d463eb8
commit 1fba835d4f
9 changed files with 165 additions and 60 deletions

View File

@ -0,0 +1,50 @@
require 'rails_helper'
describe GivenDailyLike do
it 'no errors without a user' do
expect(-> { GivenDailyLike.increment_for(nil) }).not_to raise_error
expect(-> { GivenDailyLike.decrement_for(nil) }).not_to raise_error
end
context 'with a user' do
let(:user) { Fabricate(:user) }
def value_for(user_id, date)
GivenDailyLike.find_for(user_id, date).pluck(:likes_given)[0] || 0
end
def limit_reached_for(user_id, date)
GivenDailyLike.find_for(user_id, date).pluck(:limit_reached)[0] || false
end
it 'can be incremented and decremented' do
SiteSetting.max_likes_per_day = 2
Timecop.freeze(Date.today) do
dt = Date.today
expect(value_for(user.id, dt)).to eq(0)
expect(limit_reached_for(user.id, dt)).to eq(false)
GivenDailyLike.increment_for(user.id)
expect(value_for(user.id, dt)).to eq(1)
expect(limit_reached_for(user.id, dt)).to eq(false)
GivenDailyLike.increment_for(user.id)
expect(value_for(user.id, dt)).to eq(2)
expect(limit_reached_for(user.id, dt)).to eq(true)
GivenDailyLike.decrement_for(user.id)
expect(value_for(user.id, dt)).to eq(1)
expect(limit_reached_for(user.id, dt)).to eq(false)
GivenDailyLike.decrement_for(user.id)
expect(value_for(user.id, dt)).to eq(0)
expect(limit_reached_for(user.id, dt)).to eq(false)
end
end
end
end

View File

@ -168,19 +168,24 @@ describe PostAction do
describe "update_counters" do
it "properly updates topic counters" do
# we need this to test it
TopicUser.change(codinghorror, post.topic, posted: true)
Timecop.freeze(Date.today) do
# we need this to test it
TopicUser.change(codinghorror, post.topic, posted: true)
PostAction.act(moderator, post, PostActionType.types[:like])
PostAction.act(codinghorror, second_post, PostActionType.types[:like])
expect(GivenDailyLike.value_for(moderator.id, Date.today)).to eq(0)
post.topic.reload
expect(post.topic.like_count).to eq(2)
PostAction.act(moderator, post, PostActionType.types[:like])
PostAction.act(codinghorror, second_post, PostActionType.types[:like])
tu = TopicUser.get(post.topic, codinghorror)
expect(tu.liked).to be true
expect(tu.bookmarked).to be false
post.topic.reload
expect(post.topic.like_count).to eq(2)
expect(GivenDailyLike.value_for(moderator.id, Date.today)).to eq(1)
tu = TopicUser.get(post.topic, codinghorror)
expect(tu.liked).to be true
expect(tu.bookmarked).to be false
end
end
end
@ -239,29 +244,33 @@ describe PostAction do
end
it 'should increase the `like_count` and `like_score` when a user likes something' do
PostAction.act(codinghorror, post, PostActionType.types[:like])
post.reload
expect(post.like_count).to eq(1)
expect(post.like_score).to eq(1)
post.topic.reload
expect(post.topic.like_count).to eq(1)
Timecop.freeze(Date.today) do
PostAction.act(codinghorror, post, PostActionType.types[:like])
post.reload
expect(post.like_count).to eq(1)
expect(post.like_score).to eq(1)
post.topic.reload
expect(post.topic.like_count).to eq(1)
expect(GivenDailyLike.value_for(codinghorror.id, Date.today)).to eq(1)
# When a staff member likes it
PostAction.act(moderator, post, PostActionType.types[:like])
post.reload
expect(post.like_count).to eq(2)
expect(post.like_score).to eq(4)
# When a staff member likes it
PostAction.act(moderator, post, PostActionType.types[:like])
post.reload
expect(post.like_count).to eq(2)
expect(post.like_score).to eq(4)
# Removing likes
PostAction.remove_act(codinghorror, post, PostActionType.types[:like])
post.reload
expect(post.like_count).to eq(1)
expect(post.like_score).to eq(3)
# Removing likes
PostAction.remove_act(codinghorror, post, PostActionType.types[:like])
post.reload
expect(post.like_count).to eq(1)
expect(post.like_score).to eq(3)
expect(GivenDailyLike.value_for(codinghorror.id, Date.today)).to eq(0)
PostAction.remove_act(moderator, post, PostActionType.types[:like])
post.reload
expect(post.like_count).to eq(0)
expect(post.like_score).to eq(0)
PostAction.remove_act(moderator, post, PostActionType.types[:like])
post.reload
expect(post.like_count).to eq(0)
expect(post.like_score).to eq(0)
end
end
end