From 06fabe491d2814f021e6783a34950191434d9573 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 18 Nov 2019 14:57:44 +0000 Subject: [PATCH 1/2] Add failing shipping_methods spec --- .../customer_totals_report_spec.rb | 58 ++++++++++++++----- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb b/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb index c53398e2c6..ed2db8b767 100644 --- a/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb +++ b/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb @@ -2,14 +2,7 @@ require "spec_helper" RSpec.describe OpenFoodNetwork::OrdersAndFulfillmentsReport::CustomerTotalsReport do let!(:distributor) { create(:distributor_enterprise) } - let!(:customer) { create(:customer, enterprise: distributor) } - - let!(:order) do - create(:completed_order_with_totals, line_items_count: 1, user: customer.user, - customer: customer, distributor: distributor) - end - let(:current_user) { distributor.owner } let(:permissions) { OpenFoodNetwork::Permissions.new(current_user) } @@ -22,18 +15,51 @@ RSpec.describe OpenFoodNetwork::OrdersAndFulfillmentsReport::CustomerTotalsRepor OpenFoodNetwork::OrderGrouper.new(report.rules, report.columns).table(report.table_items) end - it "generates the report" do - expect(report_table.length).to eq(2) + context "viewing the report" do + let!(:order) do + create(:completed_order_with_totals, line_items_count: 1, user: customer.user, + customer: customer, distributor: distributor) + end + + it "generates the report" do + expect(report_table.length).to eq(2) + end + + it "has a line item row" do + distributor_name_field = report_table.first[0] + expect(distributor_name_field).to eq distributor.name + + customer_name_field = report_table.first[1] + expect(customer_name_field).to eq order.bill_address.full_name + + total_field = report_table.last[5] + expect(total_field).to eq I18n.t("admin.reports.total") + end end - it "has a line item row" do - distributor_name_field = report_table.first[0] - expect(distributor_name_field).to eq distributor.name + context "loading shipping methods" do + let!(:shipping_method1) { + create(:shipping_method, distributors: [distributor], name: "First") + } + let!(:shipping_method2) { + create(:shipping_method, distributors: [distributor], name: "Second") + } + let!(:shipping_method3) { + create(:shipping_method, distributors: [distributor], name: "Third") + } + let!(:order) do + create(:completed_order_with_totals, line_items_count: 1, user: customer.user, + customer: customer, distributor: distributor) + end - customer_name_field = report_table.first[1] - expect(customer_name_field).to eq order.bill_address.full_name + before do + order.shipments.each(&:refresh_rates) + order.select_shipping_method(shipping_method2.id) + end - total_field = report_table.last[5] - expect(total_field).to eq I18n.t("admin.reports.total") + xit "displays the correct shipping_method" do + shipping_method_name_field = report_table.first[15] + expect(shipping_method_name_field).to eq shipping_method2.name + end end end From 585135d27edf18a26c02c78b42bbc10287d3a766 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 18 Nov 2019 15:07:31 +0000 Subject: [PATCH 2/2] Fix shipping_method querying in customer totals reports --- .../customer_totals_report.rb | 9 +++++++-- .../customer_totals_report_spec.rb | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report.rb b/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report.rb index 74456d174f..fefaa73b64 100644 --- a/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report.rb +++ b/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report.rb @@ -198,8 +198,13 @@ module OpenFoodNetwork private def shipping_method(line_items) - line_items.first.order.shipments.first. - andand.shipping_rates.andand.first.andand.shipping_method + shipping_rates = line_items.first.order.shipments.first. + andand.shipping_rates + + return unless shipping_rates + + shipping_rate = shipping_rates.find(&:selected) || shipping_rates.first + shipping_rate.try(:shipping_method) end end end diff --git a/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb b/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb index ed2db8b767..261e52a925 100644 --- a/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb +++ b/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb @@ -57,7 +57,7 @@ RSpec.describe OpenFoodNetwork::OrdersAndFulfillmentsReport::CustomerTotalsRepor order.select_shipping_method(shipping_method2.id) end - xit "displays the correct shipping_method" do + it "displays the correct shipping_method" do shipping_method_name_field = report_table.first[15] expect(shipping_method_name_field).to eq shipping_method2.name end