This commit is contained in:
Feruz Oripov
2024-02-27 01:01:22 +05:00
parent 3cf75fce72
commit c2d8bdd414
5 changed files with 13 additions and 9 deletions

View File

@@ -3,8 +3,8 @@
module Api
module Admin
# This serializer relies on `object` to respond to `#balance_value`. That's done in
# `CustomersWithBalanceQuery` due to the fact that ActiveRecord maps the DB result set's columns to
# instance methods. This way, the `balance_value` alias on that class ends up being
# `CustomersWithBalanceQuery` due to the fact that ActiveRecord maps the DB result set's
# columns to instance methods. This way, the `balance_value` alias on that class ends up being
# `object.balance_value` here.
class CustomerWithBalanceSerializer < CustomerSerializer
attributes :balance, :balance_status

View File

@@ -9,8 +9,8 @@ module Api
has_many :payments, serializer: Api::PaymentSerializer
# This method relies on `balance_value` as a computed DB column. See `CompleteOrdersWithBalanceQuery`
# for reference.
# This method relies on `balance_value` as a computed DB column.
# See `CompleteOrdersWithBalanceQuery` for reference.
def outstanding_balance
-object.balance_value
end

View File

@@ -18,7 +18,8 @@ module Reporting
describe "fetching orders" do
it 'calls the OutstandingBalanceQuery query object' do
outstanding_balance = instance_double(OutstandingBalanceQuery, call: Spree::Order.none)
outstanding_balance = instance_double(OutstandingBalanceQuery,
call: Spree::Order.none)
expect(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance)
subject.orders

View File

@@ -26,9 +26,11 @@ describe CompleteOrdersWithBalanceQuery do
it 'calls OutstandingBalanceQuery#call' do
allow(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance)
expect(outstanding_balance).to receive(:call)
allow(outstanding_balance).to receive(:call)
result
expect(outstanding_balance).to have_received(:call)
end
it 'returns complete orders including their balance' do
@@ -46,9 +48,11 @@ describe CompleteOrdersWithBalanceQuery do
it 'calls OutstandingBalanceQuery' do
allow(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance)
expect(outstanding_balance).to receive(:call)
allow(outstanding_balance).to receive(:call)
result
expect(outstanding_balance).to have_received(:call)
end
it 'returns an empty array' do

View File

@@ -9,10 +9,9 @@ describe OutstandingBalanceQuery do
describe '#statement' do
let(:relation) { Spree::Order.none }
let(:normalized_sql_statement) { normalize(query.statement) }
it 'returns the CASE statement necessary to compute the order balance' do
normalized_sql_statement = normalize(query.statement)
expect(normalized_sql_statement).to eq(normalize(<<-SQL.squish))
CASE WHEN "spree_orders"."state" IN ('canceled', 'returned') THEN "spree_orders"."payment_total"
WHEN "spree_orders"."state" IS NOT NULL THEN "spree_orders"."payment_total" - "spree_orders"."total"