mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-29 21:17:17 +00:00
Paranoia doesn't support unique validation including deleted records: https://github.com/rubysherpas/paranoia/pull/333 We use a custom validator, ScopedUniquenessValidator to avoid the issue
55 lines
1.4 KiB
Ruby
55 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'spec_helper'
|
|
|
|
RSpec.describe Vouchers::FlatRate do
|
|
describe 'validations' do
|
|
subject { build(:voucher_flat_rate) }
|
|
|
|
it { is_expected.to validate_presence_of(:amount) }
|
|
it { is_expected.to validate_numericality_of(:amount).is_greater_than(0) }
|
|
it_behaves_like 'has a unique code per enterprise', "voucher_flat_rate"
|
|
end
|
|
|
|
describe '#compute_amount' do
|
|
let(:order) { create(:order_with_totals) }
|
|
|
|
before do
|
|
order.update_columns(item_total: 15)
|
|
end
|
|
|
|
context 'when order total is more than the voucher' do
|
|
subject { create(:voucher_flat_rate, amount: 5) }
|
|
|
|
it 'uses the voucher total' do
|
|
expect(subject.compute_amount(order).to_f).to eq(-5)
|
|
end
|
|
end
|
|
|
|
context 'when order total is less than the voucher' do
|
|
subject { create(:voucher_flat_rate, amount: 20) }
|
|
|
|
it 'matches the order total' do
|
|
expect(subject.compute_amount(order).to_f).to eq(-15)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "#rate" do
|
|
subject do
|
|
create(:voucher_flat_rate, code: 'new_code', amount: 5)
|
|
end
|
|
let(:order) { create(:order_with_totals) }
|
|
|
|
before do
|
|
order.update_columns(item_total: 10)
|
|
end
|
|
|
|
it "returns the voucher rate" do
|
|
# rate = -voucher_amount / order.pre_discount_total
|
|
# -5 / 10 = -0.5
|
|
expect(subject.rate(order).to_f).to eq(-0.5)
|
|
end
|
|
end
|
|
end
|