demarches-normaliennes/spec/controllers/stats_controller_spec.rb

94 lines
3.1 KiB
Ruby
Raw Normal View History

describe StatsController, type: :controller do
describe "#last_four_months_hash" do
context "while a regular user is logged in" do
before do
create(:procedure, created_at: 6.months.ago, updated_at: 6.months.ago)
create(:procedure, created_at: 2.months.ago, updated_at: 62.days.ago)
create(:procedure, created_at: 2.months.ago, updated_at: 62.days.ago)
create(:procedure, created_at: 2.months.ago, updated_at: 31.days.ago)
create(:procedure, created_at: 2.months.ago, updated_at: Time.zone.now)
@controller = StatsController.new
allow(@controller).to receive(:super_admin_signed_in?).and_return(false)
end
let(:association) { Procedure.all }
subject { @controller.send(:last_four_months_hash, association, :updated_at) }
2018-01-15 18:54:57 +01:00
it do
expect(subject).to match_array([
[I18n.l(62.days.ago.beginning_of_month, format: "%B %Y"), 2],
[I18n.l(31.days.ago.beginning_of_month, format: "%B %Y"), 1]
])
2018-01-15 18:54:57 +01:00
end
end
context "while a super admin is logged in" do
before do
create(:procedure, updated_at: 6.months.ago)
create(:procedure, updated_at: 45.days.ago)
create(:procedure, updated_at: 1.day.ago)
create(:procedure, updated_at: 1.day.ago)
@controller = StatsController.new
allow(@controller).to receive(:super_admin_signed_in?).and_return(true)
end
let (:association) { Procedure.all }
subject { @controller.send(:last_four_months_hash, association, :updated_at) }
2018-01-15 18:54:57 +01:00
it do
expect(subject).to eq([
[I18n.l(45.days.ago.beginning_of_month, format: "%B %Y"), 1],
2018-03-06 13:44:29 +01:00
[I18n.l(1.day.ago.beginning_of_month, format: "%B %Y"), 2]
2017-06-12 14:10:49 +02:00
])
2018-01-15 18:54:57 +01:00
end
end
end
describe '#cumulative_hash' do
before do
2018-10-25 15:21:06 +02:00
Timecop.freeze(Time.zone.local(2016, 10, 2))
create(:procedure, created_at: 55.days.ago, updated_at: 43.days.ago)
create(:procedure, created_at: 45.days.ago, updated_at: 40.days.ago)
create(:procedure, created_at: 45.days.ago, updated_at: 20.days.ago)
create(:procedure, created_at: 15.days.ago, updated_at: 20.days.ago)
create(:procedure, created_at: 15.days.ago, updated_at: 1.hour.ago)
end
after { Timecop.return }
let (:association) { Procedure.all }
context "while a super admin is logged in" do
before { allow(@controller).to receive(:super_admin_signed_in?).and_return(true) }
subject { @controller.send(:cumulative_hash, association, :updated_at) }
2018-01-15 19:02:12 +01:00
it do
expect(subject).to eq({
Time.utc(2016, 8, 1) => 2,
Time.utc(2016, 9, 1) => 4,
Time.utc(2016, 10, 1) => 5
})
2018-01-15 19:02:12 +01:00
end
end
context "while a super admin is not logged in" do
before { allow(@controller).to receive(:super_admin_signed_in?).and_return(false) }
subject { @controller.send(:cumulative_hash, association, :updated_at) }
2018-01-15 19:02:12 +01:00
it do
expect(subject).to eq({
Time.utc(2016, 8, 1) => 2,
Time.utc(2016, 9, 1) => 4
})
2018-01-15 19:02:12 +01:00
end
end
end
end