Files
openfoodnetwork/spec/lib/tasks/data/truncate_data_spec.rb
Pau Perez e0228f66af Default to archiving data older than 2 years.
This is safer than the current 3 months.
2020-04-29 16:49:43 +02:00

131 lines
3.8 KiB
Ruby

require 'spec_helper'
require 'tasks/data/truncate_data'
describe TruncateData do
describe '#call' do
before do
allow(Spree::ReturnAuthorization).to receive(:delete_all)
allow(Spree::StateChange).to receive(:delete_all)
allow(Spree::LogEntry).to receive(:delete_all)
allow(TruncateData::Session).to receive(:delete_all)
allow(Rails.logger).to receive(:info)
end
context 'when months_to_keep is not specified' do
it 'truncates order cycles closed earlier than 2 years ago' do
order_cycle = create(
:order_cycle, orders_open_at: 25.months.ago, orders_close_at: 25.months.ago + 1.day
)
create(:order, order_cycle: order_cycle)
TruncateData.new.call
expect(OrderCycle.all).to be_empty
end
it 'deletes state changes older than a month' do
TruncateData.new.call
expect(Spree::StateChange)
.to have_received(:delete_all)
.with("created_at < '#{1.month.ago.to_date}'")
end
it 'deletes log entries older than a month' do
TruncateData.new.call
expect(Spree::LogEntry)
.to have_received(:delete_all)
.with("created_at < '#{1.month.ago.to_date}'")
end
it 'deletes sessions older than two weeks' do
TruncateData.new.call
expect(TruncateData::Session)
.to have_received(:delete_all)
.with("created_at < '#{2.weeks.ago.to_date}'")
end
end
context 'when months_to_keep is nil' do
it 'truncates order cycles closed earlier than 2 years ago' do
order_cycle = create(
:order_cycle, orders_open_at: 25.months.ago, orders_close_at: 25.months.ago + 1.day
)
create(:order, order_cycle: order_cycle)
TruncateData.new(nil).call
expect(OrderCycle.all).to be_empty
end
it 'deletes state changes older than a month' do
TruncateData.new.call
expect(Spree::StateChange)
.to have_received(:delete_all)
.with("created_at < '#{1.month.ago.to_date}'")
end
it 'deletes log entries older than a month' do
TruncateData.new.call
expect(Spree::LogEntry)
.to have_received(:delete_all)
.with("created_at < '#{1.month.ago.to_date}'")
end
it 'deletes sessions older than two weeks' do
TruncateData.new.call
expect(TruncateData::Session)
.to have_received(:delete_all)
.with("created_at < '#{2.weeks.ago.to_date}'")
end
end
context 'when months_to_keep is specified' do
it 'truncates order cycles closed earlier than months_to_keep months ago' do
old_order_cycle = create(
:order_cycle, orders_open_at: 7.months.ago, orders_close_at: 7.months.ago + 1.day
)
create(:order, order_cycle: old_order_cycle)
recent_order_cycle = create(
:order_cycle, orders_open_at: 1.months.ago, orders_close_at: 1.months.ago + 1.day
)
create(:order, order_cycle: recent_order_cycle)
TruncateData.new(6).call
expect(OrderCycle.all).to contain_exactly(recent_order_cycle)
end
it 'deletes state changes older than a month' do
TruncateData.new.call
expect(Spree::StateChange)
.to have_received(:delete_all)
.with("created_at < '#{1.month.ago.to_date}'")
end
it 'deletes log entries older than a month' do
TruncateData.new.call
expect(Spree::LogEntry)
.to have_received(:delete_all)
.with("created_at < '#{1.month.ago.to_date}'")
end
it 'deletes sessions older than two weeks' do
TruncateData.new.call
expect(TruncateData::Session)
.to have_received(:delete_all)
.with("created_at < '#{2.weeks.ago.to_date}'")
end
end
end
end