From ff6830f95486646b7f0280486c5b015e0f6d16aa Mon Sep 17 00:00:00 2001 From: Feruz Oripov Date: Mon, 26 Feb 2024 23:37:11 +0500 Subject: [PATCH] Update OutstandingBalanceQuery --- .../complete_orders_with_balance_query.rb | 2 +- app/queries/customers_with_balance_query.rb | 2 +- ...alance.rb => outstanding_balance_query.rb} | 4 +-- .../reports/order_cycle_management/base.rb | 2 +- .../spree/users_controller_spec.rb | 8 ++--- .../order_cycle_management_report_spec.rb | 6 ++-- ...complete_orders_with_balance_query_spec.rb | 14 ++++---- ...b => customers_with_balance_query_spec.rb} | 6 ++-- ...c.rb => outstanding_balance_query_spec.rb} | 33 ++++++++++--------- 9 files changed, 39 insertions(+), 38 deletions(-) rename app/queries/{outstanding_balance.rb => outstanding_balance_query.rb} (97%) rename spec/queries/{customers_with_balance_spec.rb => customers_with_balance_query_spec.rb} (97%) rename spec/queries/{outstanding_balance_spec.rb => outstanding_balance_query_spec.rb} (88%) diff --git a/app/queries/complete_orders_with_balance_query.rb b/app/queries/complete_orders_with_balance_query.rb index 665e2d2838..c6507f45bb 100644 --- a/app/queries/complete_orders_with_balance_query.rb +++ b/app/queries/complete_orders_with_balance_query.rb @@ -7,7 +7,7 @@ class CompleteOrdersWithBalanceQuery end def call - OutstandingBalance.new(sorted_finalized_orders).query + OutstandingBalanceQuery.new(sorted_finalized_orders).call end private diff --git a/app/queries/customers_with_balance_query.rb b/app/queries/customers_with_balance_query.rb index aa02f37e65..a33b109540 100644 --- a/app/queries/customers_with_balance_query.rb +++ b/app/queries/customers_with_balance_query.rb @@ -32,6 +32,6 @@ class CustomersWithBalanceQuery end def outstanding_balance_sum - "SUM(#{OutstandingBalance.new.statement})::float" + "SUM(#{OutstandingBalanceQuery.new.statement})::float" end end diff --git a/app/queries/outstanding_balance.rb b/app/queries/outstanding_balance_query.rb similarity index 97% rename from app/queries/outstanding_balance.rb rename to app/queries/outstanding_balance_query.rb index 023c23a928..a0685800fa 100644 --- a/app/queries/outstanding_balance.rb +++ b/app/queries/outstanding_balance_query.rb @@ -11,7 +11,7 @@ # # Note this query object and `app/models/concerns/balance.rb` should implement the same behavior # until we find a better way. If you change one, please, change the other too. -class OutstandingBalance +class OutstandingBalanceQuery # All the states of a finished order but that shouldn't count towards the balance (the customer # didn't get the order for whatever reason). Note it does not include complete FINALIZED_NON_SUCCESSFUL_STATES = %w(canceled returned).freeze @@ -22,7 +22,7 @@ class OutstandingBalance @relation = relation end - def query + def call relation.select("#{statement} AS balance_value") end diff --git a/lib/reporting/reports/order_cycle_management/base.rb b/lib/reporting/reports/order_cycle_management/base.rb index e20e13ab25..d62cd3de33 100644 --- a/lib/reporting/reports/order_cycle_management/base.rb +++ b/lib/reporting/reports/order_cycle_management/base.rb @@ -29,7 +29,7 @@ module Reporting def orders search_result = search.result.order(:completed_at) - orders = OutstandingBalance.new(search_result).query.select('spree_orders.*') + orders = OutstandingBalanceQuery.new(search_result).call.select('spree_orders.*') filter(orders) end diff --git a/spec/controllers/spree/users_controller_spec.rb b/spec/controllers/spree/users_controller_spec.rb index 8f6455055c..c61d64d886 100644 --- a/spec/controllers/spree/users_controller_spec.rb +++ b/spec/controllers/spree/users_controller_spec.rb @@ -23,7 +23,7 @@ describe Spree::UsersController, type: :controller do let(:orders) { assigns(:orders) } let(:shops) { Enterprise.where(id: orders.pluck(:distributor_id)) } - let(:outstanding_balance) { instance_double(OutstandingBalance) } + let(:outstanding_balance_query) { instance_double(OutstandingBalanceQuery) } before do allow(controller).to receive(:spree_current_user) { u1 } @@ -47,9 +47,9 @@ describe Spree::UsersController, type: :controller do expect(orders).not_to include d1o3 end - it 'calls OutstandingBalance' do - allow(OutstandingBalance).to receive(:new).and_return(outstanding_balance) - expect(outstanding_balance).to receive(:query) { Spree::Order.none } + it 'calls OutstandingBalanceQuery' do + allow(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance_query) + expect(outstanding_balance_query).to receive(:call) { Spree::Order.none } spree_get :show end diff --git a/spec/lib/reports/order_cycle_management_report_spec.rb b/spec/lib/reports/order_cycle_management_report_spec.rb index 47c5c1fbdc..72d6c44831 100644 --- a/spec/lib/reports/order_cycle_management_report_spec.rb +++ b/spec/lib/reports/order_cycle_management_report_spec.rb @@ -17,9 +17,9 @@ module Reporting end describe "fetching orders" do - it 'calls the OutstandingBalance query object' do - outstanding_balance = instance_double(OutstandingBalance, query: Spree::Order.none) - expect(OutstandingBalance).to receive(:new).and_return(outstanding_balance) + it 'calls the OutstandingBalanceQuery query object' do + outstanding_balance = instance_double(OutstandingBalanceQuery, call: Spree::Order.none) + expect(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance) subject.orders end diff --git a/spec/queries/complete_orders_with_balance_query_spec.rb b/spec/queries/complete_orders_with_balance_query_spec.rb index f0988bc532..ba9fb6e6a2 100644 --- a/spec/queries/complete_orders_with_balance_query_spec.rb +++ b/spec/queries/complete_orders_with_balance_query_spec.rb @@ -7,7 +7,7 @@ describe CompleteOrdersWithBalanceQuery do describe '#call' do let(:user) { order.user } - let(:outstanding_balance) { instance_double(OutstandingBalance) } + let(:outstanding_balance) { instance_double(OutstandingBalanceQuery) } context 'when the user has complete orders' do let(:order) do @@ -24,9 +24,9 @@ describe CompleteOrdersWithBalanceQuery do ) end - it 'calls OutstandingBalance#query' do - allow(OutstandingBalance).to receive(:new).and_return(outstanding_balance) - expect(outstanding_balance).to receive(:query) + it 'calls OutstandingBalanceQuery#call' do + allow(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance) + expect(outstanding_balance).to receive(:call) result end @@ -44,9 +44,9 @@ describe CompleteOrdersWithBalanceQuery do context 'when the user has no complete orders' do let(:order) { create(:order) } - it 'calls OutstandingBalance' do - allow(OutstandingBalance).to receive(:new).and_return(outstanding_balance) - expect(outstanding_balance).to receive(:query) + it 'calls OutstandingBalanceQuery' do + allow(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance) + expect(outstanding_balance).to receive(:call) result end diff --git a/spec/queries/customers_with_balance_spec.rb b/spec/queries/customers_with_balance_query_spec.rb similarity index 97% rename from spec/queries/customers_with_balance_spec.rb rename to spec/queries/customers_with_balance_query_spec.rb index e886350e5f..76208eb32a 100644 --- a/spec/queries/customers_with_balance_spec.rb +++ b/spec/queries/customers_with_balance_query_spec.rb @@ -9,10 +9,10 @@ describe CustomersWithBalanceQuery do let(:customers) { create(:customer) } let(:total) { 200.00 } let(:order_total) { 100.00 } - let(:outstanding_balance) { instance_double(OutstandingBalance) } + let(:outstanding_balance) { instance_double(OutstandingBalanceQuery) } - it 'calls OutstandingBalance#statement' do - allow(OutstandingBalance).to receive(:new).and_return(outstanding_balance) + it 'calls OutstandingBalanceQuery#statement' do + allow(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance) expect(outstanding_balance).to receive(:statement) result diff --git a/spec/queries/outstanding_balance_spec.rb b/spec/queries/outstanding_balance_query_spec.rb similarity index 88% rename from spec/queries/outstanding_balance_spec.rb rename to spec/queries/outstanding_balance_query_spec.rb index 91b80f4c82..2af9a2c611 100644 --- a/spec/queries/outstanding_balance_spec.rb +++ b/spec/queries/outstanding_balance_query_spec.rb @@ -2,14 +2,15 @@ require 'spec_helper' -describe OutstandingBalance do - subject(:outstanding_balance) { described_class.new(relation) } +describe OutstandingBalanceQuery do + subject { described_class.new(relation) } + let(:result) { subject.call } describe '#statement' do let(:relation) { Spree::Order.none } it 'returns the CASE statement necessary to compute the order balance' do - normalized_sql_statement = normalize(outstanding_balance.statement) + normalized_sql_statement = normalize(subject.statement) expect(normalized_sql_statement).to eq(normalize(<<-SQL)) CASE WHEN "spree_orders"."state" IN ('canceled', 'returned') THEN "spree_orders"."payment_total" @@ -23,7 +24,7 @@ describe OutstandingBalance do end end - describe '#query' do + describe '#call' do let(:relation) { Spree::Order.all } let(:total) { 200.00 } let(:order_total) { 100.00 } @@ -35,7 +36,7 @@ describe OutstandingBalance do end it 'returns the order balance' do - order = outstanding_balance.query.first + order = result.first expect(order.balance_value).to eq(-order_total) end end @@ -47,7 +48,7 @@ describe OutstandingBalance do end it 'returns the order balance' do - order = outstanding_balance.query.first + order = result.first expect(order.balance_value).to eq(-order_total) end end @@ -59,7 +60,7 @@ describe OutstandingBalance do end it 'returns the order balance' do - order = outstanding_balance.query.first + order = result.first expect(order.balance_value).to eq(-order_total) end end @@ -71,7 +72,7 @@ describe OutstandingBalance do end it 'returns the order balance' do - order = outstanding_balance.query.first + order = result.first expect(order.balance_value).to eq(-order_total) end end @@ -85,7 +86,7 @@ describe OutstandingBalance do end it 'returns the customer balance' do - order = outstanding_balance.query.first + order = result.first expect(order.balance_value).to eq(-order_total) end end @@ -101,7 +102,7 @@ describe OutstandingBalance do end it 'returns the customer balance' do - order = outstanding_balance.query.first + order = result.first expect(order.balance_value).to eq(payment_total - 200.0) end end @@ -117,7 +118,7 @@ describe OutstandingBalance do end it 'returns the customer balance' do - order = outstanding_balance.query.first + order = result.first expect(order.balance_value).to eq(payment_total) end end @@ -133,7 +134,7 @@ describe OutstandingBalance do end it 'returns the customer balance' do - order = outstanding_balance.query.first + order = result.first expect(order.balance_value).to eq(payment_total - 200.0) end end @@ -149,7 +150,7 @@ describe OutstandingBalance do end it 'returns the customer balance' do - order = outstanding_balance.query.first + order = result.first expect(order.balance_value).to eq(payment_total - 200.0) end end @@ -165,7 +166,7 @@ describe OutstandingBalance do end it 'returns the customer balance' do - order = outstanding_balance.query.first + order = result.first expect(order.balance_value).to eq(payment_total - 200.0) end end @@ -182,14 +183,14 @@ describe OutstandingBalance do end it 'returns the customer balance' do - order = outstanding_balance.query.first + order = result.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 + orders = result expect(orders).to be_empty end end