2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2015-07-26 00:06:49 +08:00
|
|
|
|
2017-05-22 15:01:33 +08:00
|
|
|
describe TopicTimestampChanger do
|
2015-07-26 00:06:49 +08:00
|
|
|
describe "change!" do
|
|
|
|
let(:old_timestamp) { Time.zone.now }
|
|
|
|
let(:new_timestamp) { old_timestamp + 1.day }
|
2017-05-22 16:03:49 +08:00
|
|
|
let(:topic) { Fabricate(:topic, created_at: old_timestamp) }
|
2015-07-26 00:06:49 +08:00
|
|
|
let!(:p1) { Fabricate(:post, topic: topic, created_at: old_timestamp) }
|
|
|
|
let!(:p2) { Fabricate(:post, topic: topic, created_at: old_timestamp + 1.day) }
|
|
|
|
|
2017-05-22 15:01:33 +08:00
|
|
|
context 'new timestamp is in the future' do
|
|
|
|
let(:new_timestamp) { old_timestamp + 2.day }
|
2015-07-26 00:06:49 +08:00
|
|
|
|
2017-05-22 16:03:49 +08:00
|
|
|
it 'should raise the right error' do
|
|
|
|
expect { TopicTimestampChanger.new(topic: topic, timestamp: new_timestamp.to_f).change! }
|
|
|
|
.to raise_error(TopicTimestampChanger::InvalidTimestampError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'new timestamp is in the past' do
|
|
|
|
let(:new_timestamp) { old_timestamp - 2.day }
|
|
|
|
|
2017-05-22 15:01:33 +08:00
|
|
|
it 'changes the timestamp of the topic and opening post' do
|
|
|
|
Timecop.freeze do
|
2017-05-22 16:03:49 +08:00
|
|
|
TopicTimestampChanger.new(topic: topic, timestamp: new_timestamp.to_f).change!
|
2015-07-26 00:06:49 +08:00
|
|
|
|
2017-05-22 15:01:33 +08:00
|
|
|
topic.reload
|
|
|
|
[:created_at, :updated_at, :bumped_at].each do |column|
|
2017-05-22 16:03:49 +08:00
|
|
|
expect(topic.public_send(column)).to be_within(1.second).of(new_timestamp)
|
2017-05-22 15:01:33 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
p1.reload
|
|
|
|
[:created_at, :updated_at].each do |column|
|
2017-05-22 16:03:49 +08:00
|
|
|
expect(p1.public_send(column)).to be_within(1.second).of(new_timestamp)
|
2017-05-22 15:01:33 +08:00
|
|
|
end
|
2015-10-20 10:12:52 +08:00
|
|
|
|
2017-05-22 16:03:49 +08:00
|
|
|
p2.reload
|
|
|
|
[:created_at, :updated_at].each do |column|
|
|
|
|
expect(p2.public_send(column)).to be_within(1.second).of(new_timestamp + 1.day)
|
|
|
|
end
|
2015-07-26 00:06:49 +08:00
|
|
|
|
2017-05-22 16:03:49 +08:00
|
|
|
expect(topic.last_posted_at).to be_within(1.second).of(p2.reload.created_at)
|
2015-07-26 00:06:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-22 16:03:49 +08:00
|
|
|
describe 'when posts have timestamps in the future' do
|
|
|
|
let(:new_timestamp) { Time.zone.now }
|
|
|
|
let(:p3) { Fabricate(:post, topic: topic, created_at: new_timestamp + 3.day) }
|
|
|
|
|
|
|
|
it 'should set the new timestamp as the default timestamp' do
|
|
|
|
Timecop.freeze do
|
|
|
|
p3
|
|
|
|
TopicTimestampChanger.new(topic: topic, timestamp: new_timestamp.to_f).change!
|
2015-07-26 00:06:49 +08:00
|
|
|
|
2017-05-22 16:03:49 +08:00
|
|
|
p3.reload
|
2015-07-26 00:06:49 +08:00
|
|
|
|
2017-05-22 16:03:49 +08:00
|
|
|
[:created_at, :updated_at].each do |column|
|
|
|
|
expect(p3.public_send(column)).to be_within(1.second).of(new_timestamp)
|
|
|
|
end
|
|
|
|
end
|
2015-07-26 00:06:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes the stats cache' do
|
2017-03-21 04:15:23 +08:00
|
|
|
$redis.set AdminDashboardData.stats_cache_key, "X"
|
|
|
|
$redis.set About.stats_cache_key, "X"
|
|
|
|
|
2017-05-22 16:03:49 +08:00
|
|
|
TopicTimestampChanger.new(topic: topic, timestamp: Time.zone.now.to_f).change!
|
2017-03-21 04:15:23 +08:00
|
|
|
|
|
|
|
expect($redis.get(AdminDashboardData.stats_cache_key)).to eq(nil)
|
|
|
|
expect($redis.get(About.stats_cache_key)).to eq(nil)
|
2015-07-26 00:06:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|