Files
openfoodnetwork/spec/serializers/api/order_serializer_spec.rb
Pau Perez 20abaaa950 Reuse outstanding balance statement across queries
Instead of relying on Spree::Order#outstanding_balance we make us of the
result set `balance_value` computed column. So, we ask PostgreSQL to
compute it instead of Ruby and then serialize it from that computed
column. That's a bit faster to compute that way and let's reuse logic.

We hide this new implementation under this features' toggle so it's only
used when enabled. We want hit the old behaviour by default.
2021-01-20 18:23:22 +01:00

53 lines
1.7 KiB
Ruby

# frozen_string_literal: true
require 'spec_helper'
describe Api::OrderSerializer do
let(:serializer) { Api::OrderSerializer.new order }
let(:order) { create(:completed_order_with_totals) }
let!(:completed_payment) { create(:payment, order: order, state: 'completed', amount: order.total - 1) }
let!(:payment) { create(:payment, order: order, state: 'checkout', amount: 123.45) }
it "serializes an order" do
expect(serializer.to_json).to match order.number.to_s
end
it "convert the state attributes to translatable keys" do
# byebug if serializer.to_json =~ /balance_due/
expect(serializer.to_json).to match "complete"
expect(serializer.to_json).to match "balance_due"
end
it "only serializes completed payments" do
expect(serializer.to_json).to match completed_payment.amount.to_s
expect(serializer.to_json).to_not match payment.amount.to_s
end
describe '#outstanding_balance' do
context 'when the customer_balance feature is enabled' do
before do
allow(OpenFoodNetwork::FeatureToggle)
.to receive(:enabled?).with(:customer_balance, order.user) { true }
allow(order).to receive(:balance_value).and_return(-1.23)
end
it "returns the object's balance_value from the users perspective" do
expect(serializer.serializable_hash[:outstanding_balance]).to eq(1.23)
end
end
context 'when the customer_balance is not enabled' do
before do
allow(OpenFoodNetwork::FeatureToggle)
.to receive(:enabled?).with(:customer_balance, order.user) { false }
end
it 'calls #outstanding_balance on the object' do
expect(serializer.serializable_hash[:outstanding_balance]).to eq(1.0)
end
end
end
end