rename topic_status_update to topic_timer

This commit is contained in:
Neil Lalonde
2017-05-11 18:23:18 -04:00
parent 92d63b59a7
commit 55b61e9bea
39 changed files with 297 additions and 287 deletions

View File

@@ -496,11 +496,11 @@ describe PostAction do
expect(topic.reload.closed).to eq(true)
topic_status_update = TopicStatusUpdate.last
topic_status_update = TopicTimer.last
expect(topic_status_update.topic).to eq(topic)
expect(topic_status_update.execute_at).to be_within(1.second).of(1.hour.from_now)
expect(topic_status_update.status_type).to eq(TopicStatusUpdate.types[:open])
expect(topic_status_update.status_type).to eq(TopicTimer.types[:open])
end
end

View File

@@ -746,7 +746,7 @@ describe Topic do
expect(@topic).to be_closed
expect(@topic.bumped_at.to_f).to eq(@original_bumped_at)
expect(@topic.moderator_posts_count).to eq(1)
expect(@topic.topic_status_updates.first).to eq(nil)
expect(@topic.topic_timers.first).to eq(nil)
end
end
end
@@ -777,7 +777,7 @@ describe Topic do
freeze_time(2.days.ago)
@topic.set_or_create_status_update(TopicStatusUpdate.types[:close], 48)
@topic.set_or_create_timer(TopicTimer.types[:close], 48)
@topic.save!
freeze_time(2.days.from_now)
@@ -1100,12 +1100,12 @@ describe Topic do
end
end
describe '#set_or_create_status_update' do
describe '#set_or_create_timer' do
let(:topic) { Fabricate.build(:topic) }
let(:closing_topic) do
Fabricate(:topic,
topic_status_updates: [Fabricate(:topic_status_update, execute_at: 5.hours.from_now)]
topic_timers: [Fabricate(:topic_timer, execute_at: 5.hours.from_now)]
)
end
@@ -1116,146 +1116,146 @@ describe Topic do
it 'can take a number of hours as an integer' do
Timecop.freeze(now) do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], 72, by_user: admin)
expect(topic.topic_status_updates.first.execute_at).to eq(3.days.from_now)
topic.set_or_create_timer(TopicTimer.types[:close], 72, by_user: admin)
expect(topic.topic_timers.first.execute_at).to eq(3.days.from_now)
end
end
it 'can take a number of hours as an integer, with timezone offset' do
Timecop.freeze(now) do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], 72, {by_user: admin, timezone_offset: 240})
expect(topic.topic_status_updates.first.execute_at).to eq(3.days.from_now)
topic.set_or_create_timer(TopicTimer.types[:close], 72, {by_user: admin, timezone_offset: 240})
expect(topic.topic_timers.first.execute_at).to eq(3.days.from_now)
end
end
it 'can take a number of hours as a string' do
Timecop.freeze(now) do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], '18', by_user: admin)
expect(topic.topic_status_updates.first.execute_at).to eq(18.hours.from_now)
topic.set_or_create_timer(TopicTimer.types[:close], '18', by_user: admin)
expect(topic.topic_timers.first.execute_at).to eq(18.hours.from_now)
end
end
it 'can take a number of hours as a string, with timezone offset' do
Timecop.freeze(now) do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], '18', {by_user: admin, timezone_offset: 240})
expect(topic.topic_status_updates.first.execute_at).to eq(18.hours.from_now)
topic.set_or_create_timer(TopicTimer.types[:close], '18', {by_user: admin, timezone_offset: 240})
expect(topic.topic_timers.first.execute_at).to eq(18.hours.from_now)
end
end
it 'can take a number of hours as a string and can handle based on last post' do
Timecop.freeze(now) do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], '18', {by_user: admin, based_on_last_post: true})
expect(topic.topic_status_updates.first.execute_at).to eq(18.hours.from_now)
topic.set_or_create_timer(TopicTimer.types[:close], '18', {by_user: admin, based_on_last_post: true})
expect(topic.topic_timers.first.execute_at).to eq(18.hours.from_now)
end
end
it "can take a time later in the day" do
Timecop.freeze(now) do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], '13:00', {by_user: admin})
expect(topic.topic_status_updates.first.execute_at).to eq(Time.zone.local(2013,11,20,13,0))
topic.set_or_create_timer(TopicTimer.types[:close], '13:00', {by_user: admin})
expect(topic.topic_timers.first.execute_at).to eq(Time.zone.local(2013,11,20,13,0))
end
end
it "can take a time later in the day, with timezone offset" do
Timecop.freeze(now) do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], '13:00', {by_user: admin, timezone_offset: 240})
expect(topic.topic_status_updates.first.execute_at).to eq(Time.zone.local(2013,11,20,17,0))
topic.set_or_create_timer(TopicTimer.types[:close], '13:00', {by_user: admin, timezone_offset: 240})
expect(topic.topic_timers.first.execute_at).to eq(Time.zone.local(2013,11,20,17,0))
end
end
it "can take a time for the next day" do
Timecop.freeze(now) do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], '5:00', {by_user: admin})
expect(topic.topic_status_updates.first.execute_at).to eq(Time.zone.local(2013,11,21,5,0))
topic.set_or_create_timer(TopicTimer.types[:close], '5:00', {by_user: admin})
expect(topic.topic_timers.first.execute_at).to eq(Time.zone.local(2013,11,21,5,0))
end
end
it "can take a time for the next day, with timezone offset" do
Timecop.freeze(now) do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], '1:00', {by_user: admin, timezone_offset: 240})
expect(topic.topic_status_updates.first.execute_at).to eq(Time.zone.local(2013,11,21,5,0))
topic.set_or_create_timer(TopicTimer.types[:close], '1:00', {by_user: admin, timezone_offset: 240})
expect(topic.topic_timers.first.execute_at).to eq(Time.zone.local(2013,11,21,5,0))
end
end
it "can take a timestamp for a future time" do
Timecop.freeze(now) do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], '2013-11-22 5:00', {by_user: admin})
expect(topic.topic_status_updates.first.execute_at).to eq(Time.zone.local(2013,11,22,5,0))
topic.set_or_create_timer(TopicTimer.types[:close], '2013-11-22 5:00', {by_user: admin})
expect(topic.topic_timers.first.execute_at).to eq(Time.zone.local(2013,11,22,5,0))
end
end
it "can take a timestamp for a future time, with timezone offset" do
Timecop.freeze(now) do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], '2013-11-22 5:00', {by_user: admin, timezone_offset: 240})
expect(topic.topic_status_updates.first.execute_at).to eq(Time.zone.local(2013,11,22,9,0))
topic.set_or_create_timer(TopicTimer.types[:close], '2013-11-22 5:00', {by_user: admin, timezone_offset: 240})
expect(topic.topic_timers.first.execute_at).to eq(Time.zone.local(2013,11,22,9,0))
end
end
it "sets a validation error when given a timestamp in the past" do
Timecop.freeze(now) do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], '2013-11-19 5:00', {by_user: admin})
topic.set_or_create_timer(TopicTimer.types[:close], '2013-11-19 5:00', {by_user: admin})
expect(topic.topic_status_updates.first.execute_at).to eq(Time.zone.local(2013,11,19,5,0))
expect(topic.topic_status_updates.first.errors[:execute_at]).to be_present
expect(topic.topic_timers.first.execute_at).to eq(Time.zone.local(2013,11,19,5,0))
expect(topic.topic_timers.first.errors[:execute_at]).to be_present
end
end
it "can take a timestamp with timezone" do
Timecop.freeze(now) do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], '2013-11-25T01:35:00-08:00', {by_user: admin})
expect(topic.topic_status_updates.first.execute_at).to eq(Time.utc(2013,11,25,9,35))
topic.set_or_create_timer(TopicTimer.types[:close], '2013-11-25T01:35:00-08:00', {by_user: admin})
expect(topic.topic_timers.first.execute_at).to eq(Time.utc(2013,11,25,9,35))
end
end
it 'sets topic status update user to given user if it is a staff or TL4 user' do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], 3, {by_user: admin})
expect(topic.topic_status_updates.first.user).to eq(admin)
topic.set_or_create_timer(TopicTimer.types[:close], 3, {by_user: admin})
expect(topic.topic_timers.first.user).to eq(admin)
end
it 'sets topic status update user to given user if it is a TL4 user' do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], 3, {by_user: trust_level_4})
expect(topic.topic_status_updates.first.user).to eq(trust_level_4)
topic.set_or_create_timer(TopicTimer.types[:close], 3, {by_user: trust_level_4})
expect(topic.topic_timers.first.user).to eq(trust_level_4)
end
it 'sets topic status update user to system user if given user is not staff or a TL4 user' do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], 3, {by_user: Fabricate.build(:user, id: 444)})
expect(topic.topic_status_updates.first.user).to eq(admin)
topic.set_or_create_timer(TopicTimer.types[:close], 3, {by_user: Fabricate.build(:user, id: 444)})
expect(topic.topic_timers.first.user).to eq(admin)
end
it 'sets topic status update user to system user if user is not given and topic creator is not staff nor TL4 user' do
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], 3)
expect(topic.topic_status_updates.first.user).to eq(admin)
topic.set_or_create_timer(TopicTimer.types[:close], 3)
expect(topic.topic_timers.first.user).to eq(admin)
end
it 'sets topic status update user to topic creator if it is a staff user' do
staff_topic = Fabricate.build(:topic, user: Fabricate.build(:admin, id: 999))
staff_topic.set_or_create_status_update(TopicStatusUpdate.types[:close], 3)
expect(staff_topic.topic_status_updates.first.user_id).to eq(999)
staff_topic.set_or_create_timer(TopicTimer.types[:close], 3)
expect(staff_topic.topic_timers.first.user_id).to eq(999)
end
it 'sets topic status update user to topic creator if it is a TL4 user' do
tl4_topic = Fabricate.build(:topic, user: Fabricate.build(:trust_level_4, id: 998))
tl4_topic.set_or_create_status_update(TopicStatusUpdate.types[:close], 3)
expect(tl4_topic.topic_status_updates.first.user_id).to eq(998)
tl4_topic.set_or_create_timer(TopicTimer.types[:close], 3)
expect(tl4_topic.topic_timers.first.user_id).to eq(998)
end
it 'removes close topic status update if arg is nil' do
closing_topic.set_or_create_status_update(TopicStatusUpdate.types[:close], nil)
closing_topic.set_or_create_timer(TopicTimer.types[:close], nil)
closing_topic.reload
expect(closing_topic.topic_status_updates.first).to be_nil
expect(closing_topic.topic_timers.first).to be_nil
end
it 'updates topic status update execute_at if it was already set to close' do
Timecop.freeze(now) do
closing_topic.set_or_create_status_update(TopicStatusUpdate.types[:close], 48)
closing_topic.set_or_create_timer(TopicTimer.types[:close], 48)
expect(closing_topic.reload.topic_status_update.execute_at).to eq(2.days.from_now)
end
end
it "does not update topic's topic status created_at it was already set to close" do
expect{
closing_topic.set_or_create_status_update(TopicStatusUpdate.types[:close], 14)
}.to_not change { closing_topic.topic_status_updates.first.created_at }
closing_topic.set_or_create_timer(TopicTimer.types[:close], 14)
}.to_not change { closing_topic.topic_timers.first.created_at }
end
describe "when category's default auto close is set" do
@@ -1263,14 +1263,14 @@ describe Topic do
let(:topic) { Fabricate(:topic, category: category) }
it "should be able to override category's default auto close" do
expect(topic.topic_status_updates.first.duration).to eq(4)
expect(topic.topic_timers.first.duration).to eq(4)
topic.set_or_create_status_update(TopicStatusUpdate.types[:close], 2, by_user: admin)
topic.set_or_create_timer(TopicTimer.types[:close], 2, by_user: admin)
expect(topic.reload.closed).to eq(false)
Timecop.travel(3.hours.from_now) do
TopicStatusUpdate.ensure_consistency!
TopicTimer.ensure_consistency!
expect(topic.reload.closed).to eq(true)
end
end

View File

@@ -1,7 +1,7 @@
require 'rails_helper'
RSpec.describe TopicStatusUpdate, type: :model do
let(:topic_status_update) { Fabricate(:topic_status_update) }
RSpec.describe TopicTimer, type: :model do
let(:topic_timer) { Fabricate(:topic_timer) }
let(:topic) { Fabricate(:topic) }
before do
@@ -11,10 +11,10 @@ RSpec.describe TopicStatusUpdate, type: :model do
context "validations" do
describe '#status_type' do
it 'should ensure that only one active topic status update exists' do
topic_status_update.update!(topic: topic)
Fabricate(:topic_status_update, deleted_at: Time.zone.now, topic: topic)
topic_timer.update!(topic: topic)
Fabricate(:topic_timer, deleted_at: Time.zone.now, topic: topic)
expect { Fabricate(:topic_status_update, topic: topic) }
expect { Fabricate(:topic_timer, topic: topic) }
.to raise_error(ActiveRecord::RecordInvalid)
end
end
@@ -22,26 +22,26 @@ RSpec.describe TopicStatusUpdate, type: :model do
describe '#execute_at' do
describe 'when #execute_at is greater than #created_at' do
it 'should be valid' do
topic_status_update = Fabricate.build(:topic_status_update,
topic_timer = Fabricate.build(:topic_timer,
execute_at: Time.zone.now + 1.hour,
user: Fabricate(:user),
topic: Fabricate(:topic)
)
expect(topic_status_update).to be_valid
expect(topic_timer).to be_valid
end
end
describe 'when #execute_at is smaller than #created_at' do
it 'should not be valid' do
topic_status_update = Fabricate.build(:topic_status_update,
topic_timer = Fabricate.build(:topic_timer,
execute_at: Time.zone.now - 1.hour,
created_at: Time.zone.now,
user: Fabricate(:user),
topic: Fabricate(:topic)
)
expect(topic_status_update).to_not be_valid
expect(topic_timer).to_not be_valid
end
end
end
@@ -50,25 +50,25 @@ RSpec.describe TopicStatusUpdate, type: :model do
describe 'when #status_type is publish_to_category' do
describe 'when #category_id is not present' do
it 'should not be valid' do
topic_status_update = Fabricate.build(:topic_status_update,
status_type: TopicStatusUpdate.types[:publish_to_category]
topic_timer = Fabricate.build(:topic_timer,
status_type: TopicTimer.types[:publish_to_category]
)
expect(topic_status_update).to_not be_valid
expect(topic_status_update.errors.keys).to include(:category_id)
expect(topic_timer).to_not be_valid
expect(topic_timer.errors.keys).to include(:category_id)
end
end
describe 'when #category_id is present' do
it 'should be valid' do
topic_status_update = Fabricate.build(:topic_status_update,
status_type: TopicStatusUpdate.types[:publish_to_category],
topic_timer = Fabricate.build(:topic_timer,
status_type: TopicTimer.types[:publish_to_category],
category_id: Fabricate(:category).id,
user: Fabricate(:user),
topic: Fabricate(:topic)
)
expect(topic_status_update).to be_valid
expect(topic_timer).to be_valid
end
end
end
@@ -79,51 +79,51 @@ RSpec.describe TopicStatusUpdate, type: :model do
describe 'when #execute_at and #user_id are not changed' do
it 'should not schedule another to update topic' do
Jobs.expects(:enqueue_at).with(
topic_status_update.execute_at,
topic_timer.execute_at,
:toggle_topic_closed,
topic_status_update_id: topic_status_update.id,
topic_timer_id: topic_timer.id,
state: true
).once
topic_status_update
topic_timer
Jobs.expects(:cancel_scheduled_job).never
topic_status_update.update!(topic: Fabricate(:topic))
topic_timer.update!(topic: Fabricate(:topic))
end
end
describe 'when #execute_at value is changed' do
it 'reschedules the job' do
Timecop.freeze do
topic_status_update
topic_timer
Jobs.expects(:cancel_scheduled_job).with(
:toggle_topic_closed, topic_status_update_id: topic_status_update.id
:toggle_topic_closed, topic_timer_id: topic_timer.id
)
Jobs.expects(:enqueue_at).with(
3.days.from_now, :toggle_topic_closed,
topic_status_update_id: topic_status_update.id,
topic_timer_id: topic_timer.id,
state: true
)
topic_status_update.update!(execute_at: 3.days.from_now, created_at: Time.zone.now)
topic_timer.update!(execute_at: 3.days.from_now, created_at: Time.zone.now)
end
end
describe 'when execute_at is smaller than the current time' do
it 'should enqueue the job immediately' do
Timecop.freeze do
topic_status_update
topic_timer
Jobs.expects(:enqueue_at).with(
Time.zone.now, :toggle_topic_closed,
topic_status_update_id: topic_status_update.id,
topic_timer_id: topic_timer.id,
state: true
)
topic_status_update.update!(
topic_timer.update!(
execute_at: Time.zone.now - 1.hour,
created_at: Time.zone.now - 2.hour
)
@@ -135,22 +135,22 @@ RSpec.describe TopicStatusUpdate, type: :model do
describe 'when user is changed' do
it 'should update the job' do
Timecop.freeze do
topic_status_update
topic_timer
Jobs.expects(:cancel_scheduled_job).with(
:toggle_topic_closed, topic_status_update_id: topic_status_update.id
:toggle_topic_closed, topic_timer_id: topic_timer.id
)
admin = Fabricate(:admin)
Jobs.expects(:enqueue_at).with(
topic_status_update.execute_at,
topic_timer.execute_at,
:toggle_topic_closed,
topic_status_update_id: topic_status_update.id,
topic_timer_id: topic_timer.id,
state: true
)
topic_status_update.update!(user: admin)
topic_timer.update!(user: admin)
end
end
end
@@ -158,22 +158,22 @@ RSpec.describe TopicStatusUpdate, type: :model do
describe 'when a open topic status update is created for an open topic' do
let(:topic) { Fabricate(:topic, closed: false) }
let(:topic_status_update) do
Fabricate(:topic_status_update,
let(:topic_timer) do
Fabricate(:topic_timer,
status_type: described_class.types[:open],
topic: topic
)
end
it 'should close the topic' do
topic_status_update
topic_timer
expect(topic.reload.closed).to eq(true)
end
describe 'when topic has been deleted' do
it 'should not queue the job' do
topic.trash!
topic_status_update
topic_timer
expect(Jobs::ToggleTopicClosed.jobs).to eq([])
end
@@ -183,22 +183,22 @@ RSpec.describe TopicStatusUpdate, type: :model do
describe 'when a close topic status update is created for a closed topic' do
let(:topic) { Fabricate(:topic, closed: true) }
let(:topic_status_update) do
Fabricate(:topic_status_update,
let(:topic_timer) do
Fabricate(:topic_timer,
status_type: described_class.types[:close],
topic: topic
)
end
it 'should open the topic' do
topic_status_update
topic_timer
expect(topic.reload.closed).to eq(false)
end
describe 'when topic has been deleted' do
it 'should not queue the job' do
topic.trash!
topic_status_update
topic_timer
expect(Jobs::ToggleTopicClosed.jobs).to eq([])
end
@@ -213,20 +213,20 @@ RSpec.describe TopicStatusUpdate, type: :model do
end
it 'should enqueue jobs that have been missed' do
close_topic_status_update = Fabricate(:topic_status_update,
close_topic_timer = Fabricate(:topic_timer,
execute_at: Time.zone.now - 1.hour,
created_at: Time.zone.now - 2.hour
)
open_topic_status_update = Fabricate(:topic_status_update,
open_topic_timer = Fabricate(:topic_timer,
status_type: described_class.types[:open],
execute_at: Time.zone.now - 1.hour,
created_at: Time.zone.now - 2.hour
)
Fabricate(:topic_status_update)
Fabricate(:topic_timer)
Fabricate(:topic_status_update,
Fabricate(:topic_timer,
execute_at: Time.zone.now - 1.hour,
created_at: Time.zone.now - 2.hour
).topic.trash!
@@ -236,12 +236,12 @@ RSpec.describe TopicStatusUpdate, type: :model do
job_args = Jobs::ToggleTopicClosed.jobs.first["args"].first
expect(job_args["topic_status_update_id"]).to eq(close_topic_status_update.id)
expect(job_args["topic_timer_id"]).to eq(close_topic_timer.id)
expect(job_args["state"]).to eq(true)
job_args = Jobs::ToggleTopicClosed.jobs.last["args"].first
expect(job_args["topic_status_update_id"]).to eq(open_topic_status_update.id)
expect(job_args["topic_timer_id"]).to eq(open_topic_timer.id)
expect(job_args["state"]).to eq(false)
end
end