2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-09-11 14:50:26 -04:00
|
|
|
|
|
|
|
describe UserStat do
|
|
|
|
|
|
|
|
it "is created automatically when a user is created" do
|
|
|
|
user = Fabricate(:evil_trout)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.user_stat).to be_present
|
2014-03-03 14:31:29 -05:00
|
|
|
|
|
|
|
# It populates the `new_since` field by default
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.user_stat.new_since).to be_present
|
2013-09-11 14:50:26 -04:00
|
|
|
end
|
|
|
|
|
2013-10-03 23:28:49 -04:00
|
|
|
context '#update_view_counts' do
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:stat) { user.user_stat }
|
|
|
|
|
|
|
|
context 'topics_entered' do
|
|
|
|
context 'without any views' do
|
|
|
|
it "doesn't increase the user's topics_entered" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect { UserStat.update_view_counts; stat.reload }.not_to change(stat, :topics_entered)
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a view' do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
2014-08-04 05:07:55 -04:00
|
|
|
let!(:view) { TopicViewItem.add(topic.id, '127.0.0.1', user.id) }
|
2013-10-03 23:28:49 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
user.update_column :last_seen_at, 1.second.ago
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds one to the topics entered" do
|
|
|
|
UserStat.update_view_counts
|
|
|
|
stat.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(stat.topics_entered).to eq(1)
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "won't record a second view as a different topic" do
|
2014-08-04 05:07:55 -04:00
|
|
|
TopicViewItem.add(topic.id, '127.0.0.1', user.id)
|
2013-10-03 23:28:49 -04:00
|
|
|
UserStat.update_view_counts
|
|
|
|
stat.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(stat.topics_entered).to eq(1)
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'posts_read_count' do
|
|
|
|
context 'without any post timings' do
|
|
|
|
it "doesn't increase the user's posts_read_count" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect { UserStat.update_view_counts; stat.reload }.not_to change(stat, :posts_read_count)
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a post timing' do
|
|
|
|
let!(:post) { Fabricate(:post) }
|
|
|
|
let!(:post_timings) do
|
|
|
|
PostTiming.record_timing(msecs: 1234, topic_id: post.topic_id, user_id: user.id, post_number: post.post_number)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
user.update_column :last_seen_at, 1.second.ago
|
|
|
|
end
|
|
|
|
|
|
|
|
it "increases posts_read_count" do
|
|
|
|
UserStat.update_view_counts
|
|
|
|
stat.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(stat.posts_read_count).to eq(1)
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
describe 'update_time_read!' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:stat) { user.user_stat }
|
|
|
|
|
|
|
|
it 'makes no changes if nothing is cached' do
|
2014-01-06 00:50:55 -05:00
|
|
|
stat.expects(:last_seen_cached).returns(nil)
|
2013-10-03 23:28:49 -04:00
|
|
|
stat.update_time_read!
|
|
|
|
stat.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(stat.time_read).to eq(0)
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'makes a change if time read is below threshold' do
|
2014-01-06 00:50:55 -05:00
|
|
|
stat.expects(:last_seen_cached).returns(Time.now - 10)
|
2013-10-03 23:28:49 -04:00
|
|
|
stat.update_time_read!
|
|
|
|
stat.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(stat.time_read).to eq(10)
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'makes no change if time read is above threshold' do
|
|
|
|
t = Time.now - 1 - UserStat::MAX_TIME_READ_DIFF
|
2014-01-06 00:50:55 -05:00
|
|
|
stat.expects(:last_seen_cached).returns(t)
|
2013-10-03 23:28:49 -04:00
|
|
|
stat.update_time_read!
|
|
|
|
stat.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(stat.time_read).to eq(0)
|
2013-10-03 23:28:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|