2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2015-03-16 15:14:33 -04:00
|
|
|
|
|
|
|
describe DirectoryItem do
|
2016-01-08 05:53:52 -05:00
|
|
|
|
|
|
|
describe '#period_types' do
|
|
|
|
context "verify enum sequence" do
|
|
|
|
before do
|
|
|
|
@period_types = DirectoryItem.period_types
|
|
|
|
end
|
|
|
|
|
|
|
|
it "'all' should be at 1st position" do
|
|
|
|
expect(@period_types[:all]).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "'quarterly' should be at 6th position" do
|
|
|
|
expect(@period_types[:quarterly]).to eq(6)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-18 20:53:59 -05:00
|
|
|
context 'inactive and silenced users' do
|
|
|
|
it 'removes silenced users correctly' do
|
|
|
|
post = create_post
|
|
|
|
DirectoryItem.refresh_period!(:daily)
|
|
|
|
|
|
|
|
count = DirectoryItem.where(user_id: post.user_id).count
|
|
|
|
expect(count).to eq(1)
|
|
|
|
|
|
|
|
post.user.update_columns(active: false)
|
|
|
|
DirectoryItem.refresh_period!(:daily)
|
|
|
|
|
|
|
|
count = DirectoryItem.where(user_id: post.user_id).count
|
|
|
|
expect(count).to eq(0)
|
|
|
|
|
|
|
|
post.user.update_columns(active: true)
|
|
|
|
DirectoryItem.refresh_period!(:daily)
|
|
|
|
|
|
|
|
count = DirectoryItem.where(user_id: post.user_id).count
|
|
|
|
expect(count).to eq(1)
|
|
|
|
|
|
|
|
post.user.update_columns(silenced_till: 1.year.from_now)
|
|
|
|
DirectoryItem.refresh_period!(:daily)
|
|
|
|
|
|
|
|
count = DirectoryItem.where(user_id: post.user_id).count
|
|
|
|
expect(count).to eq(0)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-16 15:14:33 -04:00
|
|
|
context 'refresh' do
|
2016-03-02 02:23:29 -05:00
|
|
|
before do
|
2016-12-21 23:03:40 -05:00
|
|
|
UserActionCreator.enable
|
2016-03-02 02:23:29 -05:00
|
|
|
end
|
|
|
|
|
2017-11-20 00:43:53 -05:00
|
|
|
it "creates the record for the user and handles likes" do
|
2017-08-09 15:57:35 -04:00
|
|
|
post = create_post
|
2017-11-20 00:43:53 -05:00
|
|
|
_post2 = create_post(topic_id: post.topic_id, user: post.user)
|
|
|
|
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
|
|
|
|
PostAction.act(user2, post, PostActionType.types[:like])
|
|
|
|
|
2015-03-16 15:14:33 -04:00
|
|
|
DirectoryItem.refresh!
|
2017-11-20 00:43:53 -05:00
|
|
|
|
|
|
|
item1 = DirectoryItem.find_by(period_type: DirectoryItem.period_types[:all], user_id: post.user_id)
|
|
|
|
item2 = DirectoryItem.find_by(period_type: DirectoryItem.period_types[:all], user_id: user2.id)
|
|
|
|
|
|
|
|
expect(item1.topic_count).to eq(1)
|
|
|
|
expect(item1.likes_received).to eq(1)
|
|
|
|
expect(item1.post_count).to eq(1)
|
|
|
|
|
|
|
|
expect(item2.likes_given).to eq(1)
|
|
|
|
|
|
|
|
post.topic.trash!
|
|
|
|
|
|
|
|
DirectoryItem.refresh!
|
|
|
|
|
|
|
|
item1 = DirectoryItem.find_by(period_type: DirectoryItem.period_types[:all], user_id: post.user_id)
|
|
|
|
item2 = DirectoryItem.find_by(period_type: DirectoryItem.period_types[:all], user_id: user2.id)
|
|
|
|
|
|
|
|
expect(item1.likes_given).to eq(0)
|
|
|
|
expect(item1.likes_received).to eq(0)
|
|
|
|
expect(item1.post_count).to eq(0)
|
|
|
|
expect(item1.topic_count).to eq(0)
|
2015-03-16 15:14:33 -04:00
|
|
|
end
|
|
|
|
|
2017-08-09 15:57:35 -04:00
|
|
|
it "handles users with no activity" do
|
|
|
|
post = nil
|
|
|
|
|
|
|
|
freeze_time(2.years.ago) do
|
|
|
|
post = create_post
|
|
|
|
# Create records for that activity
|
|
|
|
DirectoryItem.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
DirectoryItem.refresh!
|
|
|
|
[:yearly, :monthly, :weekly, :daily, :quarterly].each do |period|
|
|
|
|
directory_item = DirectoryItem
|
|
|
|
.where(period_type: DirectoryItem.period_types[period])
|
|
|
|
.where(user_id: post.user.id)
|
|
|
|
.first
|
|
|
|
expect(directory_item.topic_count).to eq(0)
|
|
|
|
expect(directory_item.post_count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
directory_item = DirectoryItem
|
|
|
|
.where(period_type: DirectoryItem.period_types[:all])
|
|
|
|
.where(user_id: post.user.id)
|
|
|
|
.first
|
|
|
|
expect(directory_item.topic_count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2015-03-16 15:14:33 -04:00
|
|
|
end
|
|
|
|
end
|