From 753a643bf1771eccefd57d6dd1ae36c8fc68f6e8 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 18 Jul 2022 10:54:45 +0200 Subject: [PATCH] Simplify main method by creating two private methods Include some unit tests --- .../concerns/address_transformation.rb | 31 +++--- .../concerns/address_transformation_spec.rb | 97 +++++++++++++++++++ 2 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 spec/controllers/concerns/address_transformation_spec.rb diff --git a/app/controllers/concerns/address_transformation.rb b/app/controllers/concerns/address_transformation.rb index dd55f5b529..e38ed1a60d 100644 --- a/app/controllers/concerns/address_transformation.rb +++ b/app/controllers/concerns/address_transformation.rb @@ -26,22 +26,23 @@ module AddressTransformation }.with_indifferent_access[key] end - if address[:state].present? - address[:state] = Spree::State.find_by( - "LOWER(abbr) = ? OR LOWER(name) = ?", - address.dig(:state, :code)&.downcase, - address.dig(:state, :name)&.downcase, - ) - end - - if address[:country].present? - address[:country] = Spree::Country.find_by( - "LOWER(iso) = ? OR LOWER(name) = ?", - address.dig(:country, :code)&.downcase, - address.dig(:country, :name)&.downcase, - ) - end + address[:state] = find_state(address) if address[:state].present? + address[:country] = find_country(address) if address[:country].present? attributes["#{to}_attributes"] = address end + + private + + def find_state(address) + Spree::State.find_by("LOWER(abbr) = ? OR LOWER(name) = ?", + address.dig(:state, :code)&.downcase, + address.dig(:state, :name)&.downcase) + end + + def find_country(address) + Spree::Country.find_by("LOWER(iso) = ? OR LOWER(name) = ?", + address.dig(:country, :code)&.downcase, + address.dig(:country, :name)&.downcase) + end end diff --git a/spec/controllers/concerns/address_transformation_spec.rb b/spec/controllers/concerns/address_transformation_spec.rb new file mode 100644 index 0000000000..d8f9cf74c2 --- /dev/null +++ b/spec/controllers/concerns/address_transformation_spec.rb @@ -0,0 +1,97 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe AddressTransformation do + include AddressTransformation + + describe "#transform_address!" do + describe "default cases" do + let(:attributes) do + { shipping_address: CustomerSchema.address_example, + billing_address: CustomerSchema.address_example } + end + + let(:wanted_address) do + CustomerSchema.address_example. + except( :first_name, :last_name, :locality, + :postal_code, :region, :street_address_1, :street_address_2). + merge( + firstname: "Alice", + lastname: "Springs", + address1: "1 Flinders Street", + address2: "", + zipcode: "1234", + city: "Melbourne", + state: Spree::State.find_by(name: "Victoria"), + country: Spree::Country.find_by(name: "Australia"), + ) + end + + it "transforms the shipping_address and the billing_address" do + transformed_address = transform_address!(attributes, :shipping_address, :ship_address) + expect(transformed_address).to eq(wanted_address) + expect(attributes["ship_address_attributes"]).to eq(wanted_address) + + transformed_address = transform_address!(attributes, :billing_address, :bill_address) + expect(transformed_address).to eq( wanted_address) + expect(attributes["bill_address_attributes"]).to eq(wanted_address) + end + end + + describe "when the address attributes are nil" do + let(:attributes) do + { shipping_address: nil, + billing_address: nil } + end + + it "returns nil" do + transformed_address = transform_address!(attributes, :shipping_address, :ship_address) + expect(transformed_address).to be_nil + expect(attributes["ship_address_attributes"]).to be_nil + + transformed_address = transform_address!(attributes, :billing_address, :bill_address) + expect(transformed_address).to be_nil + expect(attributes["bill_address_attributes"]).to be_nil + end + end + + describe "when attributes doesn't contains the searched key" do + let(:attributes) do + { + address: CustomerSchema.address_example, + } + end + + it "returns nil" do + transformed_address = transform_address!(attributes, :shipping_address, :ship_address) + expect(transformed_address).to be_nil + expect(attributes["ship_address_attributes"]).to be_nil + end + end + end + + describe "#find_state" do + it "finds the state" do + state = find_state( + state: { + code: "VIC", + name: "Victoria", + }, + ) + expect(state).to eq(Spree::State.find_by(name: "Victoria")) + end + end + + describe "#find_country" do + it "finds the country" do + country = find_country( + country: { + code: "AU", + name: "Australia", + }, + ) + expect(country).to eq(Spree::Country.find_by(name: "Australia")) + end + end +end