Files
openfoodnetwork/spec/lib/tasks/data/truncate_data_spec.rb
Pau Perez 0f1d57db73 Delete LogEntries older than a month
They are useful for troubleshooting but a month data seems enough.
2020-04-29 16:49:43 +02:00

105 lines
3.0 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)
end
context 'when months_to_keep is not specified' do
it 'truncates order cycles closed earlier than 3 months ago' do
order_cycle = create(
:order_cycle, orders_open_at: 4.months.ago, orders_close_at: 4.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
end
context 'when months_to_keep is nil' do
it 'truncates order cycles closed earlier than 3 months ago' do
order_cycle = create(
:order_cycle, orders_open_at: 4.months.ago, orders_close_at: 4.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
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
end
end
end