2013-02-06 03:16:51 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe PostTiming do
|
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
it { is_expected.to validate_presence_of :post_number }
|
|
|
|
it { is_expected.to validate_presence_of :msecs }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-07-04 09:47:12 +08:00
|
|
|
describe 'pretend_read' do
|
|
|
|
let!(:p1) { Fabricate(:post) }
|
|
|
|
let!(:p2) { Fabricate(:post, topic: p1.topic, user: p1.user) }
|
|
|
|
let!(:p3) { Fabricate(:post, topic: p1.topic, user: p1.user) }
|
|
|
|
|
|
|
|
let :topic_id do
|
|
|
|
p1.topic_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def timing(user_id, post_number)
|
|
|
|
PostTiming.create!(topic_id: topic_id, user_id: user_id, post_number: post_number, msecs: 0)
|
|
|
|
end
|
|
|
|
|
2014-10-31 06:40:35 +08:00
|
|
|
def topic_user(user_id, last_read_post_number, highest_seen_post_number)
|
2013-07-04 09:47:12 +08:00
|
|
|
TopicUser.create!(
|
|
|
|
topic_id: topic_id,
|
|
|
|
user_id: user_id,
|
|
|
|
last_read_post_number: last_read_post_number,
|
2014-10-31 06:40:35 +08:00
|
|
|
highest_seen_post_number: highest_seen_post_number
|
2013-07-04 09:47:12 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'works correctly' do
|
|
|
|
timing(1,1)
|
|
|
|
timing(2,1)
|
|
|
|
timing(2,2)
|
|
|
|
timing(3,1)
|
|
|
|
timing(3,2)
|
|
|
|
timing(3,3)
|
|
|
|
|
2014-10-31 06:40:35 +08:00
|
|
|
_tu_one = topic_user(1,1,1)
|
|
|
|
_tu_two = topic_user(2,2,2)
|
|
|
|
_tu_three = topic_user(3,3,3)
|
2013-07-04 09:47:12 +08:00
|
|
|
|
|
|
|
PostTiming.pretend_read(topic_id, 2, 3)
|
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(PostTiming.where(topic_id: topic_id, user_id: 1, post_number: 3).count).to eq(0)
|
|
|
|
expect(PostTiming.where(topic_id: topic_id, user_id: 2, post_number: 3).count).to eq(1)
|
|
|
|
expect(PostTiming.where(topic_id: topic_id, user_id: 3, post_number: 3).count).to eq(1)
|
2013-07-04 09:47:12 +08:00
|
|
|
|
2014-05-06 21:41:59 +08:00
|
|
|
tu = TopicUser.find_by(topic_id: topic_id, user_id: 1)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tu.last_read_post_number).to eq(1)
|
|
|
|
expect(tu.highest_seen_post_number).to eq(1)
|
2013-07-04 09:47:12 +08:00
|
|
|
|
2014-05-06 21:41:59 +08:00
|
|
|
tu = TopicUser.find_by(topic_id: topic_id, user_id: 2)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tu.last_read_post_number).to eq(3)
|
|
|
|
expect(tu.highest_seen_post_number).to eq(3)
|
2013-07-04 09:47:12 +08:00
|
|
|
|
2014-05-06 21:41:59 +08:00
|
|
|
tu = TopicUser.find_by(topic_id: topic_id, user_id: 3)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tu.last_read_post_number).to eq(3)
|
|
|
|
expect(tu.highest_seen_post_number).to eq(3)
|
2013-07-04 09:47:12 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-14 23:49:44 +08:00
|
|
|
describe 'safeguard' do
|
|
|
|
it "doesn't store timings that are larger than the account lifetime" do
|
|
|
|
user = Fabricate(:user, created_at: 3.minutes.ago)
|
|
|
|
post = Fabricate(:post)
|
|
|
|
|
|
|
|
PostTiming.process_timings(user, post.topic_id, 1, [[post.post_number, 123]])
|
|
|
|
msecs = PostTiming.where(post_number: post.post_number, user_id: user.id).pluck(:msecs)[0]
|
|
|
|
expect(msecs).to eq(123)
|
|
|
|
|
|
|
|
PostTiming.process_timings(user, post.topic_id, 1, [[post.post_number, 10.minutes.to_i * 1000]])
|
|
|
|
msecs = PostTiming.where(post_number: post.post_number, user_id: user.id).pluck(:msecs)[0]
|
|
|
|
expect(msecs).to eq(123)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
describe 'process_timings' do
|
|
|
|
|
2013-02-25 15:42:42 +08:00
|
|
|
# integration test
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it 'processes timings correctly' do
|
2013-05-14 09:59:55 +08:00
|
|
|
|
|
|
|
ActiveRecord::Base.observers.enable :all
|
|
|
|
|
2013-02-25 15:42:42 +08:00
|
|
|
post = Fabricate(:post)
|
|
|
|
user2 = Fabricate(:coding_horror)
|
|
|
|
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(user2, post, PostActionType.types[:like])
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(post.user.unread_notifications).to eq(1)
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-04-08 09:12:52 +08:00
|
|
|
PostTiming.process_timings(post.user, post.topic_id, 1, [[post.post_number, 100]])
|
2013-02-25 15:42:42 +08:00
|
|
|
|
|
|
|
post.user.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(post.user.unread_notifications).to eq(0)
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-25 15:42:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe 'recording' do
|
|
|
|
before do
|
|
|
|
@post = Fabricate(:post)
|
|
|
|
@topic = @post.topic
|
|
|
|
@coding_horror = Fabricate(:coding_horror)
|
|
|
|
@timing_attrs = {msecs: 1234, topic_id: @post.topic_id, user_id: @coding_horror.id, post_number: @post.post_number}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds a view to the post' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect {
|
2013-02-06 03:16:51 +08:00
|
|
|
PostTiming.record_timing(@timing_attrs)
|
|
|
|
@post.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
}.to change(@post, :reads).by(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'multiple calls' do
|
2014-08-07 12:20:42 +08:00
|
|
|
it 'correctly works' do
|
2013-02-06 03:16:51 +08:00
|
|
|
PostTiming.record_timing(@timing_attrs)
|
|
|
|
PostTiming.record_timing(@timing_attrs)
|
2014-08-07 12:20:42 +08:00
|
|
|
timing = PostTiming.find_by(topic_id: @post.topic_id, user_id: @coding_horror.id, post_number: @post.post_number)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(timing).to be_present
|
|
|
|
expect(timing.msecs).to eq(2468)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@coding_horror.user_stat.posts_read_count).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-08-07 12:20:42 +08:00
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
describe 'avg times' do
|
|
|
|
|
|
|
|
describe 'posts' do
|
|
|
|
it 'has no avg_time by default' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@post.avg_time).to be_blank
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it "doesn't change when we calculate the avg time for the post because there's no timings" do
|
|
|
|
Post.calculate_avg_time
|
|
|
|
@post.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@post.avg_time).to be_blank
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'topics' do
|
|
|
|
it 'has no avg_time by default' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@topic.avg_time).to be_blank
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it "doesn't change when we calculate the avg time for the post because there's no timings" do
|
|
|
|
Topic.calculate_avg_time
|
|
|
|
@topic.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@topic.avg_time).to be_blank
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "it doesn't create an avg time for the same user" do
|
|
|
|
it 'something' do
|
|
|
|
PostTiming.record_timing(@timing_attrs.merge(user_id: @post.user_id))
|
|
|
|
Post.calculate_avg_time
|
|
|
|
@post.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@post.avg_time).to be_blank
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with a timing for another user' do
|
|
|
|
before do
|
|
|
|
PostTiming.record_timing(@timing_attrs)
|
|
|
|
Post.calculate_avg_time
|
|
|
|
@post.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a post avg_time from the timing' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@post.avg_time).to be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'forum topics' do
|
|
|
|
before do
|
|
|
|
Topic.calculate_avg_time
|
|
|
|
@topic.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has an avg_time from the timing' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(@topic.avg_time).to be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|