Skip to content

Commit

Permalink
Test OutstandingBalance in all order states
Browse files Browse the repository at this point in the history
This duplicates the scenarios tested for CustomersWithBalance.
  • Loading branch information
sauloperez committed Jan 19, 2021
1 parent 7ce53ef commit 9eb1110
Showing 1 changed file with 166 additions and 4 deletions.
170 changes: 166 additions & 4 deletions spec/queries/outstanding_balance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,174 @@ def normalize(sql)
end

describe '#query' do
before { create(:order) }
let(:relation) { Spree::Order.all }
let(:total) { 200.00 }
let(:order_total) { 100.00 }

it 'computes a new balance_value column in the specified relation' do
orders_with_balance = outstanding_balance.query
expect(orders_with_balance.first[:balance_value]).to eq(0.0)
context 'when orders are in cart state' do
before do
create(:order, total: order_total, payment_total: 0, state: 'cart')
create(:order, total: order_total, payment_total: 0, state: 'cart')
end

it 'returns the order balance' do
order = outstanding_balance.query.first
expect(order.balance_value).to eq(-order_total)
end
end

context 'when orders are in address state' do
before do
create(:order, total: order_total, payment_total: 0, state: 'address')
create(:order, total: order_total, payment_total: 50, state: 'address')
end

it 'returns the order balance' do
order = outstanding_balance.query.first
expect(order.balance_value).to eq(-order_total)
end
end

context 'when orders are in delivery state' do
before do
create(:order, total: order_total, payment_total: 0, state: 'delivery')
create(:order, total: order_total, payment_total: 50, state: 'delivery')
end

it 'returns the order balance' do
order = outstanding_balance.query.first
expect(order.balance_value).to eq(-order_total)
end
end

context 'when orders are in payment state' do
before do
create(:order, total: order_total, payment_total: 0, state: 'payment')
create(:order, total: order_total, payment_total: 50, state: 'payment')
end

it 'returns the order balance' do
order = outstanding_balance.query.first
expect(order.balance_value).to eq(-order_total)
end
end

context 'when no orders where paid' do
before do
order = create(:order, total: order_total, payment_total: 0)
order.update_attribute(:state, 'checkout')
order = create(:order, total: order_total, payment_total: 0)
order.update_attribute(:state, 'checkout')
end

it 'returns the customer balance' do
order = outstanding_balance.query.first
expect(order.balance_value).to eq(-order_total)
end
end

context 'when an order was paid' do
let(:payment_total) { order_total }

before do
order = create(:order, total: order_total, payment_total: 0)
order.update_attribute(:state, 'checkout')
order = create(:order, total: order_total, payment_total: payment_total)
order.update_attribute(:state, 'checkout')
end

it 'returns the customer balance' do
order = outstanding_balance.query.first
expect(order.balance_value).to eq(payment_total - 200.0)
end
end

context 'when an order is canceled' do
let(:payment_total) { order_total }
let(:non_canceled_orders_total) { order_total }

before do
create(:order, total: order_total, payment_total: order_total, state: 'canceled')
order = create(:order, total: order_total, payment_total: 0)
order.update_attribute(:state, 'checkout')
end

it 'returns the customer balance' do
order = outstanding_balance.query.first
expect(order.balance_value).to eq(payment_total)
end
end

context 'when an order is resumed' do
let(:payment_total) { order_total }

before do
order = create(:order, total: order_total, payment_total: 0)
order.update_attribute(:state, 'checkout')
order = create(:order, total: order_total, payment_total: payment_total)
order.update_attribute(:state, 'resumed')
end

it 'returns the customer balance' do
order = outstanding_balance.query.first
expect(order.balance_value).to eq(payment_total - 200.0)
end
end

context 'when an order is in payment' do
let(:payment_total) { order_total }

before do
order = create(:order, total: order_total, payment_total: 0)
order.update_attribute(:state, 'checkout')
order = create(:order, total: order_total, payment_total: payment_total)
order.update_attribute(:state, 'payment')
end

it 'returns the customer balance' do
order = outstanding_balance.query.first
expect(order.balance_value).to eq(payment_total - 200.0)
end
end

context 'when an order is awaiting_return' do
let(:payment_total) { order_total }

before do
order = create(:order, total: order_total, payment_total: 0)
order.update_attribute(:state, 'checkout')
order = create(:order, total: order_total, payment_total: payment_total)
order.update_attribute(:state, 'awaiting_return')
end

it 'returns the customer balance' do
order = outstanding_balance.query.first
expect(order.balance_value).to eq(payment_total - 200.0)
end
end

context 'when an order is returned' do
let(:payment_total) { order_total }
let(:non_returned_orders_total) { order_total }

before do
order = create(:order, total: order_total, payment_total: payment_total)
order.update_attribute(:state, 'returned')
order = create(:order, total: order_total, payment_total: 0)
order.update_attribute(:state, 'checkout')
end

it 'returns the customer balance' do
order = outstanding_balance.query.first
expect(order.balance_value).to eq(payment_total)
end
end

context 'when there are no orders' do
it 'returns the order balance' do
orders = outstanding_balance.query
expect(orders).to be_empty
end
end
end
end

0 comments on commit 9eb1110

Please sign in to comment.