From 9390fd6ccb9995cd08014273d75e85923acc5909 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 22 Aug 2020 18:29:19 +0100 Subject: [PATCH 01/36] Remove all factories from spree so we can see exactly what factories we need to bring from spree_core --- spec/factories.rb | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 455fc9b9a4..1b5ffe3984 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,20 +1,4 @@ require 'ffaker' -require 'spree/testing_support/factories' - -# http://www.rubydoc.info/gems/factory_bot/file/GETTING_STARTED.md -# -# The spree_core gem defines factories in several files. For example: -# -# - lib/spree/core/testing_support/factories/calculator_factory.rb -# * calculator -# * no_amount_calculator -# -# - lib/spree/core/testing_support/factories/order_factory.rb -# * order -# * order_with_totals -# * order_with_inventory_unit_shipped -# * completed_order_with_totals -# FactoryBot.define do factory :classification, class: Spree::Classification do From e561bcc895d42905bcc6c6163c94b7719f954a09 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:13:11 +0100 Subject: [PATCH 02/36] Bring address factory from spree_core and merge with modification --- spec/factories.rb | 5 ----- spec/factories/address_factory.rb | 17 +++++++++++++++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 1b5ffe3984..50d92b661d 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -162,11 +162,6 @@ FactoryBot.define do end FactoryBot.modify do - factory :address do - state { Spree::State.find_by name: 'Victoria' } - country { Spree::Country.find_by name: 'Australia' || Spree::Country.first } - end - factory :credit_card do cc_type 'visa' end diff --git a/spec/factories/address_factory.rb b/spec/factories/address_factory.rb index ec5f149d9c..2ad3cfcb6d 100644 --- a/spec/factories/address_factory.rb +++ b/spec/factories/address_factory.rb @@ -1,5 +1,18 @@ -FactoryBot.modify do - factory :address do +FactoryBot.define do + factory :address, aliases: [:bill_address, :ship_address], class: Spree::Address do + firstname 'John' + lastname 'Doe' + company 'Company' + address1 '10 Lovely Street' + address2 'Northwest' + city 'Herndon' + zipcode '20170' + phone '123-456-7890' + alternative_phone '123-456-7899' + + state { Spree::State.find_by name: 'Victoria' } + country { Spree::Country.find_by name: 'Australia' || Spree::Country.first } + trait :randomized do firstname { Faker::Name.first_name } lastname { Faker::Name.last_name } From 172a12d7d969a31ca721503e27b4c4118ec55428 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:13:50 +0100 Subject: [PATCH 03/36] Bring credit_card factory from spree_core and merge with modification --- spec/factories.rb | 4 ---- spec/factories/credit_card_factory.rb | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 spec/factories/credit_card_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index 50d92b661d..b9cf3658e6 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -162,10 +162,6 @@ FactoryBot.define do end FactoryBot.modify do - factory :credit_card do - cc_type 'visa' - end - factory :payment do transient do distributor { diff --git a/spec/factories/credit_card_factory.rb b/spec/factories/credit_card_factory.rb new file mode 100644 index 0000000000..b09c44b6ce --- /dev/null +++ b/spec/factories/credit_card_factory.rb @@ -0,0 +1,15 @@ +# allows credit card info to be saved to the database which is needed for factories to work properly +class TestCard < Spree::CreditCard + def remove_readonly_attributes(attributes) attributes; end +end + +FactoryGirl.define do + factory :credit_card, class: TestCard do + verification_value 123 + month 12 + year { Time.now.year + 1 } + number '4111111111111111' + + cc_type 'visa' + end +end From aa9054659e8058c24fe6832f170534de3da5a684 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:16:03 +0100 Subject: [PATCH 04/36] Bring payment factory from spree_core and merge with modification --- spec/factories.rb | 11 ----------- spec/factories/payment_factory.rb | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 spec/factories/payment_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index b9cf3658e6..f705ad1292 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -162,17 +162,6 @@ FactoryBot.define do end FactoryBot.modify do - factory :payment do - transient do - distributor { - order.distributor || - Enterprise.is_distributor.first || - FactoryBot.create(:distributor_enterprise) - } - end - payment_method { FactoryBot.create(:payment_method, distributors: [distributor]) } - end - factory :payment_method do distributors { [Enterprise.is_distributor.first || FactoryBot.create(:distributor_enterprise)] } end diff --git a/spec/factories/payment_factory.rb b/spec/factories/payment_factory.rb new file mode 100644 index 0000000000..267f63a7af --- /dev/null +++ b/spec/factories/payment_factory.rb @@ -0,0 +1,25 @@ +FactoryGirl.define do + factory :payment, class: Spree::Payment do + transient do + distributor { + order.distributor || + Enterprise.is_distributor.first || + FactoryBot.create(:distributor_enterprise) + } + end + + amount 45.75 + association(:source, factory: :credit_card) + order + state 'checkout' + response_code '12345' + + payment_method { FactoryBot.create(:payment_method, distributors: [distributor]) } + end + + factory :check_payment, class: Spree::Payment do + amount 45.75 + payment_method + order + end +end From e1fb13b49133f38c84d93a2eda0a00fb37c47d9d Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:19:36 +0100 Subject: [PATCH 05/36] Bring payment method factory from spree_core and merge with modification --- spec/factories.rb | 4 ---- spec/factories/payment_method_factory.rb | 13 +++++++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 spec/factories/payment_method_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index f705ad1292..9accd0c00c 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -162,10 +162,6 @@ FactoryBot.define do end FactoryBot.modify do - factory :payment_method do - distributors { [Enterprise.is_distributor.first || FactoryBot.create(:distributor_enterprise)] } - end - factory :option_type do # Prevent inconsistent ordering in specs when all option types have the same (0) position sequence(:position) diff --git a/spec/factories/payment_method_factory.rb b/spec/factories/payment_method_factory.rb new file mode 100644 index 0000000000..b957f2a56e --- /dev/null +++ b/spec/factories/payment_method_factory.rb @@ -0,0 +1,13 @@ +FactoryGirl.define do + factory :payment_method, class: Spree::PaymentMethod::Check do + name 'Check' + environment 'test' + + distributors { [Enterprise.is_distributor.first || FactoryBot.create(:distributor_enterprise)] } + end + + factory :bogus_payment_method, class: Spree::Gateway::Bogus do + name 'Credit Card' + environment 'test' + end +end From 68190424898a92b5154d1cb0696dc48cd99cb68b Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:21:49 +0100 Subject: [PATCH 06/36] Bring options factories from spree_core and merge with modification --- spec/factories.rb | 5 ----- spec/factories/options_factory.rb | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 spec/factories/options_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index 9accd0c00c..8337e48a0c 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -162,11 +162,6 @@ FactoryBot.define do end FactoryBot.modify do - factory :option_type do - # Prevent inconsistent ordering in specs when all option types have the same (0) position - sequence(:position) - end - factory :stock_location, class: Spree::StockLocation do # keeps the test stock_location unique initialize_with { DefaultStockLocation.find_or_create } diff --git a/spec/factories/options_factory.rb b/spec/factories/options_factory.rb new file mode 100644 index 0000000000..5e5464090c --- /dev/null +++ b/spec/factories/options_factory.rb @@ -0,0 +1,15 @@ +FactoryGirl.define do + factory :option_value, class: Spree::OptionValue do + name 'Size' + presentation 'S' + option_type + end + + factory :option_type, class: Spree::OptionType do + name 'foo-size' + presentation 'Size' + + # Prevent inconsistent ordering in specs when all option types have the same (0) position + sequence(:position) + end +end From 313e6e2b459a29ebcc4e020401f8ccb08f7f2b55 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:23:48 +0100 Subject: [PATCH 07/36] Bring stock_location factory from spree_core and merge with modification --- spec/factories.rb | 11 -------- spec/factories/stock_location_factory.rb | 36 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 spec/factories/stock_location_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index 8337e48a0c..de7cc38a07 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -162,17 +162,6 @@ FactoryBot.define do end FactoryBot.modify do - factory :stock_location, class: Spree::StockLocation do - # keeps the test stock_location unique - initialize_with { DefaultStockLocation.find_or_create } - - # Ensures the name attribute is not assigned after instantiating the default location - transient { name 'default' } - - # sets the default value for variant.on_demand - backorderable_default false - end - factory :shipping_category, class: Spree::ShippingCategory do initialize_with { DefaultShippingCategory.find_or_create } transient { name 'Default' } diff --git a/spec/factories/stock_location_factory.rb b/spec/factories/stock_location_factory.rb new file mode 100644 index 0000000000..4a37ff9a5d --- /dev/null +++ b/spec/factories/stock_location_factory.rb @@ -0,0 +1,36 @@ +FactoryGirl.define do + factory :stock_location, class: Spree::StockLocation do + # Ensures the name attribute is not assigned after instantiating the default location + transient { name 'default' } + + # keeps the test stock_location unique + initialize_with { DefaultStockLocation.find_or_create } + + name 'NY Warehouse' + address1 '1600 Pennsylvania Ave NW' + city 'Washington' + zipcode '20500' + phone '(202) 456-1111' + active true + + # sets the default value for variant.on_demand + backorderable_default false + + country { |stock_location| Spree::Country.first || stock_location.association(:country) } + state do |stock_location| + stock_location.country.states.first || stock_location.association(:state, :country => stock_location.country) + end + + factory :stock_location_with_items do + after(:create) do |stock_location, evaluator| + # variant will add itself to all stock_locations in an after_create + # creating a product will automatically create a master variant + product_1 = create(:product) + product_2 = create(:product) + + stock_location.stock_items.where(:variant_id => product_1.master.id).first.adjust_count_on_hand(10) + stock_location.stock_items.where(:variant_id => product_2.master.id).first.adjust_count_on_hand(20) + end + end + end +end From 53a8fec1818678e62ea1afae7e8e29f3f5539a25 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:25:32 +0100 Subject: [PATCH 08/36] Bring shipping_category factory from spree_core and merge with modification --- spec/factories.rb | 7 ------- spec/factories/shipping_category_factory.rb | 7 +++++++ 2 files changed, 7 insertions(+), 7 deletions(-) create mode 100644 spec/factories/shipping_category_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index de7cc38a07..3afc05f7c0 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -160,10 +160,3 @@ FactoryBot.define do stripe_publishable_key "xyz456" end end - -FactoryBot.modify do - factory :shipping_category, class: Spree::ShippingCategory do - initialize_with { DefaultShippingCategory.find_or_create } - transient { name 'Default' } - end -end diff --git a/spec/factories/shipping_category_factory.rb b/spec/factories/shipping_category_factory.rb new file mode 100644 index 0000000000..50c26a9905 --- /dev/null +++ b/spec/factories/shipping_category_factory.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :shipping_category, class: Spree::ShippingCategory do + initialize_with { DefaultShippingCategory.find_or_create } + transient { name 'Default' } + sequence(:name) { |n| "ShippingCategory ##{n}" } + end +end From 4ac6664502949ba8983e56c66ecabcd59e13b2d0 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:31:49 +0100 Subject: [PATCH 09/36] Replace FactoryGirl with FactoryBot weverywhere --- spec/factories/credit_card_factory.rb | 2 +- spec/factories/options_factory.rb | 2 +- spec/factories/order_factory.rb | 2 +- spec/factories/payment_factory.rb | 2 +- spec/factories/payment_method_factory.rb | 2 +- spec/factories/shipping_category_factory.rb | 2 +- spec/factories/stock_location_factory.rb | 2 +- spec/models/spree/order/checkout_spec.rb | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/factories/credit_card_factory.rb b/spec/factories/credit_card_factory.rb index b09c44b6ce..72fde1c179 100644 --- a/spec/factories/credit_card_factory.rb +++ b/spec/factories/credit_card_factory.rb @@ -3,7 +3,7 @@ class TestCard < Spree::CreditCard def remove_readonly_attributes(attributes) attributes; end end -FactoryGirl.define do +FactoryBot.define do factory :credit_card, class: TestCard do verification_value 123 month 12 diff --git a/spec/factories/options_factory.rb b/spec/factories/options_factory.rb index 5e5464090c..be425474b7 100644 --- a/spec/factories/options_factory.rb +++ b/spec/factories/options_factory.rb @@ -1,4 +1,4 @@ -FactoryGirl.define do +FactoryBot.define do factory :option_value, class: Spree::OptionValue do name 'Size' presentation 'S' diff --git a/spec/factories/order_factory.rb b/spec/factories/order_factory.rb index 282d4772c1..984e93531c 100644 --- a/spec/factories/order_factory.rb +++ b/spec/factories/order_factory.rb @@ -108,7 +108,7 @@ FactoryBot.modify do trait :with_line_item do transient do - variant { FactoryGirl.create(:variant) } + variant { FactoryBot.create(:variant) } end after(:create) do |order, evaluator| diff --git a/spec/factories/payment_factory.rb b/spec/factories/payment_factory.rb index 267f63a7af..40d02a8654 100644 --- a/spec/factories/payment_factory.rb +++ b/spec/factories/payment_factory.rb @@ -1,4 +1,4 @@ -FactoryGirl.define do +FactoryBot.define do factory :payment, class: Spree::Payment do transient do distributor { diff --git a/spec/factories/payment_method_factory.rb b/spec/factories/payment_method_factory.rb index b957f2a56e..0db3fa4732 100644 --- a/spec/factories/payment_method_factory.rb +++ b/spec/factories/payment_method_factory.rb @@ -1,4 +1,4 @@ -FactoryGirl.define do +FactoryBot.define do factory :payment_method, class: Spree::PaymentMethod::Check do name 'Check' environment 'test' diff --git a/spec/factories/shipping_category_factory.rb b/spec/factories/shipping_category_factory.rb index 50c26a9905..7f676f9027 100644 --- a/spec/factories/shipping_category_factory.rb +++ b/spec/factories/shipping_category_factory.rb @@ -1,4 +1,4 @@ -FactoryGirl.define do +FactoryBot.define do factory :shipping_category, class: Spree::ShippingCategory do initialize_with { DefaultShippingCategory.find_or_create } transient { name 'Default' } diff --git a/spec/factories/stock_location_factory.rb b/spec/factories/stock_location_factory.rb index 4a37ff9a5d..46cd0a2f4a 100644 --- a/spec/factories/stock_location_factory.rb +++ b/spec/factories/stock_location_factory.rb @@ -1,4 +1,4 @@ -FactoryGirl.define do +FactoryBot.define do factory :stock_location, class: Spree::StockLocation do # Ensures the name attribute is not assigned after instantiating the default location transient { name 'default' } diff --git a/spec/models/spree/order/checkout_spec.rb b/spec/models/spree/order/checkout_spec.rb index 5f49792aa2..1020fb0fe5 100644 --- a/spec/models/spree/order/checkout_spec.rb +++ b/spec/models/spree/order/checkout_spec.rb @@ -63,7 +63,7 @@ describe Spree::Order do end it "transitions to address" do - order.line_items << FactoryGirl.create(:line_item) + order.line_items << FactoryBot.create(:line_item) order.email = "user@example.com" order.next! expect(order.state).to eq "address" From 8d74c69402da30a638dde59e81e9ebb5ae99c1ed Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:36:29 +0100 Subject: [PATCH 10/36] Bring order factory from spree_core and merge with modification --- spec/factories/order_factory.rb | 140 ++++++++++++++++++++++---------- 1 file changed, 99 insertions(+), 41 deletions(-) diff --git a/spec/factories/order_factory.rb b/spec/factories/order_factory.rb index 984e93531c..040560b21a 100644 --- a/spec/factories/order_factory.rb +++ b/spec/factories/order_factory.rb @@ -1,4 +1,103 @@ FactoryBot.define do + factory :order, class: Spree::Order do + transient do + shipping_method { create(:shipping_method, distributors: [distributor]) } + end + + user + bill_address + completed_at nil + email { user.email } + + factory :order_with_totals do + after(:create) do |order| + create(:line_item, order: order) + order.line_items.reload # to ensure order.line_items is accessible after + end + end + + factory :order_with_line_items do + bill_address + ship_address + + ignore do + line_items_count 5 + end + + after(:create) do |order, evaluator| + create(:shipment, order: order) + order.shipments.reload + + create_list(:line_item, evaluator.line_items_count, order: order) + order.line_items.reload + order.update! + end + + factory :completed_order_with_totals do + state 'complete' + completed_at { Time.now } + + distributor { create(:distributor_enterprise) } + + after(:create) do |order| + order.refresh_shipment_rates + end + + factory :order_ready_to_ship do + payment_state 'paid' + shipment_state 'ready' + after(:create) do |order| + create(:payment, amount: order.total, order: order, state: 'completed') + order.shipments.each do |shipment| + shipment.inventory_units.each { |u| u.update_column('state', 'on_hand') } + shipment.update_column('state', 'ready') + end + order.reload + end + end + + factory :shipped_order do + after(:create) do |order| + order.shipments.each do |shipment| + shipment.inventory_units.each { |u| u.update_column('state', 'shipped') } + shipment.update_column('state', 'shipped') + end + order.reload + end + end + end + end + + trait :with_line_item do + transient do + variant { FactoryBot.create(:variant) } + end + + after(:create) do |order, evaluator| + line_item = create(:line_item_with_shipment, order: order, + variant: evaluator.variant, + shipping_method: evaluator.shipping_method) + order.shipments << line_item.target_shipment + end + end + + trait :completed do + transient do + payment_method { create(:payment_method, distributors: [distributor]) } + ship_address { create(:address) } + end + + after(:create) do |order, evaluator| + create(:payment, state: "checkout", order: order, amount: order.total, + payment_method: evaluator.payment_method) + order.update_distribution_charge! + order.ship_address = evaluator.ship_address + while !order.completed? do break unless a = order.next! end + order.select_shipping_method(evaluator.shipping_method.id) + end + end + end + factory :order_with_totals_and_distribution, parent: :order_with_distributor do transient do shipping_fee 3 @@ -99,44 +198,3 @@ FactoryBot.define do end end end - -FactoryBot.modify do - factory :order do - transient do - shipping_method { create(:shipping_method, distributors: [distributor]) } - end - - trait :with_line_item do - transient do - variant { FactoryBot.create(:variant) } - end - - after(:create) do |order, evaluator| - line_item = create(:line_item_with_shipment, order: order, - variant: evaluator.variant, - shipping_method: evaluator.shipping_method) - order.shipments << line_item.target_shipment - end - end - - trait :completed do - transient do - payment_method { create(:payment_method, distributors: [distributor]) } - ship_address { create(:address) } - end - - after(:create) do |order, evaluator| - create(:payment, state: "checkout", order: order, amount: order.total, - payment_method: evaluator.payment_method) - order.update_distribution_charge! - order.ship_address = evaluator.ship_address - while !order.completed? do break unless a = order.next! end - order.select_shipping_method(evaluator.shipping_method.id) - end - end - end - - factory :completed_order_with_totals do - distributor { create(:distributor_enterprise) } - end -end From 859d34c235c9802f908b859acad9588fcba51372 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:30:41 +0100 Subject: [PATCH 11/36] Remove nice trick to reuse traits by copy pasting the traits to each of the 3 factories I cannot figure out out to make this work again... --- spec/factories.rb | 10 ++++++++++ .../factories/calculated_adjustment_factory.rb | 18 ------------------ spec/factories/payment_method_factory.rb | 10 ++++++++++ spec/factories/shipping_method_factory.rb | 10 ++++++++++ 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 3afc05f7c0..5bbbaf064e 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -81,6 +81,16 @@ FactoryBot.define do calculator { build(:calculator_per_item, preferred_amount: amount) } after(:create) { |ef| ef.calculator.save! } + + trait :flat_rate do + transient { amount 1 } + calculator { build(:calculator_flat_rate, preferred_amount: amount) } + end + + trait :per_item do + transient { amount 1 } + calculator { build(:calculator_per_item, preferred_amount: amount) } + end end factory :adjustment_metadata, class: AdjustmentMetadata do diff --git a/spec/factories/calculated_adjustment_factory.rb b/spec/factories/calculated_adjustment_factory.rb index 5654b62746..ff43d2eaf2 100644 --- a/spec/factories/calculated_adjustment_factory.rb +++ b/spec/factories/calculated_adjustment_factory.rb @@ -3,21 +3,3 @@ FactoryBot.define do preferred_amount { generate(:calculator_amount) } end end - -FactoryBot.modify do - attach_calculator_traits = proc do - trait :flat_rate do - transient { amount 1 } - calculator { build(:calculator_flat_rate, preferred_amount: amount) } - end - - trait :per_item do - transient { amount 1 } - calculator { build(:calculator_per_item, preferred_amount: amount) } - end - end - - factory :payment_method, &attach_calculator_traits - factory :shipping_method, &attach_calculator_traits - factory :enterprise_fee, &attach_calculator_traits -end diff --git a/spec/factories/payment_method_factory.rb b/spec/factories/payment_method_factory.rb index 0db3fa4732..3be53ed70d 100644 --- a/spec/factories/payment_method_factory.rb +++ b/spec/factories/payment_method_factory.rb @@ -4,6 +4,16 @@ FactoryBot.define do environment 'test' distributors { [Enterprise.is_distributor.first || FactoryBot.create(:distributor_enterprise)] } + + trait :flat_rate do + transient { amount 1 } + calculator { build(:calculator_flat_rate, preferred_amount: amount) } + end + + trait :per_item do + transient { amount 1 } + calculator { build(:calculator_per_item, preferred_amount: amount) } + end end factory :bogus_payment_method, class: Spree::Gateway::Bogus do diff --git a/spec/factories/shipping_method_factory.rb b/spec/factories/shipping_method_factory.rb index 87cec39be9..c1a4028f1f 100644 --- a/spec/factories/shipping_method_factory.rb +++ b/spec/factories/shipping_method_factory.rb @@ -42,5 +42,15 @@ FactoryBot.modify do distributors { [Enterprise.is_distributor.first || FactoryBot.create(:distributor_enterprise)] } display_on '' zones { [] } + + trait :flat_rate do + transient { amount 1 } + calculator { build(:calculator_flat_rate, preferred_amount: amount) } + end + + trait :per_item do + transient { amount 1 } + calculator { build(:calculator_per_item, preferred_amount: amount) } + end end end From acb1c414611d449fc5dabf81b8a4a73dd295b39b Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:53:15 +0100 Subject: [PATCH 12/36] Bring shipment factory from spree_core and merge with modification --- spec/factories/shipment_factory.rb | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/spec/factories/shipment_factory.rb b/spec/factories/shipment_factory.rb index 75bc54654d..521742baf3 100644 --- a/spec/factories/shipment_factory.rb +++ b/spec/factories/shipment_factory.rb @@ -1,4 +1,25 @@ FactoryBot.define do + factory :shipment, class: Spree::Shipment do + # keeps test shipments unique per order + initialize_with { Spree::Shipment.find_or_create_by(order_id: order.id) } + + tracking 'U10000' + number '100' + cost 100.00 + state 'pending' + order + address + stock_location + + after(:create) do |shipment, evalulator| + shipment.add_shipping_method(create(:shipping_method), true) + + shipment.order.line_items.each do |line_item| + line_item.quantity.times { shipment.inventory_units.create(variant_id: line_item.variant_id) } + end + end + end + factory :shipment_with, class: Spree::Shipment do tracking 'U10000' number '100' @@ -27,10 +48,3 @@ FactoryBot.define do end end end - -FactoryBot.modify do - factory :shipment, class: Spree::Shipment do - # keeps test shipments unique per order - initialize_with { Spree::Shipment.find_or_create_by(order_id: order.id) } - end -end From ad8c41e0adf169e4a1ab27ea89d16fb5a89bc5cb Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:55:15 +0100 Subject: [PATCH 13/36] Bring shipping_method factory from spree_core and merge with modification --- spec/factories/shipping_method_factory.rb | 48 ++++++++++++++--------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/spec/factories/shipping_method_factory.rb b/spec/factories/shipping_method_factory.rb index c1a4028f1f..4b22c46e7a 100644 --- a/spec/factories/shipping_method_factory.rb +++ b/spec/factories/shipping_method_factory.rb @@ -1,4 +1,34 @@ FactoryBot.define do + factory :base_shipping_method, class: Spree::ShippingMethod do + zones { [] } + name 'UPS Ground' + + distributors { [Enterprise.is_distributor.first || FactoryBot.create(:distributor_enterprise)] } + display_on '' + + before(:create) do |shipping_method, evaluator| + shipping_method.shipping_categories << (Spree::ShippingCategory.first || create(:shipping_category)) + end + + trait :flat_rate do + transient { amount 1 } + calculator { build(:calculator_flat_rate, preferred_amount: amount) } + end + + trait :per_item do + transient { amount 1 } + calculator { build(:calculator_per_item, preferred_amount: amount) } + end + + factory :shipping_method, class: Spree::ShippingMethod do + association(:calculator, factory: :calculator, strategy: :build) + end + + factory :free_shipping_method, class: Spree::ShippingMethod do + association(:calculator, factory: :no_amount_calculator, strategy: :build) + end + end + factory :shipping_method_with, parent: :shipping_method do trait :delivery do require_ship_address { true } @@ -36,21 +66,3 @@ FactoryBot.define do end end end - -FactoryBot.modify do - factory :shipping_method, parent: :base_shipping_method do - distributors { [Enterprise.is_distributor.first || FactoryBot.create(:distributor_enterprise)] } - display_on '' - zones { [] } - - trait :flat_rate do - transient { amount 1 } - calculator { build(:calculator_flat_rate, preferred_amount: amount) } - end - - trait :per_item do - transient { amount 1 } - calculator { build(:calculator_per_item, preferred_amount: amount) } - end - end -end From 204e3979d99e764e50980b12520736cf9c40bf4e Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:57:11 +0100 Subject: [PATCH 14/36] Bring user factory from spree_core and merge with modification --- spec/factories/user_factory.rb | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/spec/factories/user_factory.rb b/spec/factories/user_factory.rb index 5f9a367885..de1774b472 100644 --- a/spec/factories/user_factory.rb +++ b/spec/factories/user_factory.rb @@ -1,9 +1,19 @@ -FactoryBot.modify do - factory :user do +FactoryBot.define do + sequence :user_authentication_token do |n| + "xxxx#{Time.now.to_i}#{rand(1000)}#{n}xxxxxxxxxxxxx" + end + + factory :user, class: Spree.user_class do transient do enterprises [] end + email { generate(:random_email) } + login { email } + password 'secret' + password_confirmation { password } + authentication_token { generate(:user_authentication_token) } if Spree.user_class.attribute_method? :authentication_token + confirmation_sent_at '1970-01-01 00:00:00' confirmed_at '1970-01-01 00:00:01' @@ -21,15 +31,14 @@ FactoryBot.modify do user.spree_roles.clear # Remove admin role user.enterprises << proxy.enterprises - end - end + end - factory :admin_user do - confirmation_sent_at '1970-01-01 00:00:00' - confirmed_at '1970-01-01 00:00:01' + factory :admin_user do + spree_roles { [Spree::Role.find_by(name: 'admin') || create(:role, name: 'admin')] } - after(:create) do |user| - user.spree_roles << Spree::Role.find_or_create_by!(name: 'admin') + after(:create) do |user| + user.spree_roles << Spree::Role.find_or_create_by!(name: 'admin') + end end end end From f6f6d9b46f2ca3503b1afaeb623c842365f5b1fe Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:58:19 +0100 Subject: [PATCH 15/36] Bring variant factory from spree_core and merge with modification --- spec/factories/variant_factory.rb | 88 +++++++++++++++++++------------ 1 file changed, 54 insertions(+), 34 deletions(-) diff --git a/spec/factories/variant_factory.rb b/spec/factories/variant_factory.rb index a4a7c58135..7489ad4001 100644 --- a/spec/factories/variant_factory.rb +++ b/spec/factories/variant_factory.rb @@ -1,46 +1,66 @@ -FactoryBot.modify do - factory :variant do - transient do - on_demand { false } - on_hand { 5 } - end +FactoryBot.define do + sequence(:random_float) { BigDecimal.new("#{rand(200)}.#{rand(99)}") } - unit_value 1 - unit_description '' + factory :base_variant, class: Spree::Variant do + price 19.99 + cost_price 17.00 + sku { SecureRandom.hex } + weight { generate(:random_float) } + height { generate(:random_float) } + width { generate(:random_float) } + depth { generate(:random_float) } - after(:create) do |variant, evaluator| - variant.on_demand = evaluator.on_demand - variant.on_hand = evaluator.on_hand - variant.save - end + product { |p| p.association(:base_product) } + option_values { [create(:option_value)] } - trait :with_order_cycle do + # ensure stock item will be created for this variant + before(:create) { create(:stock_location) if Spree::StockLocation.count == 0 } + + factory :variant do transient do - order_cycle { create(:order_cycle) } - producer { product.supplier } - coordinator { create(:distributor_enterprise) } - distributor { create(:distributor_enterprise) } - incoming_exchange_fees { [] } - outgoing_exchange_fees { [] } + on_demand { false } + on_hand { 5 } end + # on_hand 5 + product { |p| p.association(:product) } + unit_value 1 + unit_description '' + after(:create) do |variant, evaluator| - exchange_attributes = { order_cycle_id: evaluator.order_cycle.id, incoming: true, - sender_id: evaluator.producer.id, - receiver_id: evaluator.coordinator.id } - exchange = Exchange.where(exchange_attributes).first_or_create!(exchange_attributes) - exchange.variants << variant - evaluator.incoming_exchange_fees.each do |enterprise_fee| - exchange.enterprise_fees << enterprise_fee + variant.on_demand = evaluator.on_demand + variant.on_hand = evaluator.on_hand + variant.save + end + + trait :with_order_cycle do + transient do + order_cycle { create(:order_cycle) } + producer { product.supplier } + coordinator { create(:distributor_enterprise) } + distributor { create(:distributor_enterprise) } + incoming_exchange_fees { [] } + outgoing_exchange_fees { [] } end - exchange_attributes = { order_cycle_id: evaluator.order_cycle.id, incoming: false, - sender_id: evaluator.coordinator.id, - receiver_id: evaluator.distributor.id } - exchange = Exchange.where(exchange_attributes).first_or_create!(exchange_attributes) - exchange.variants << variant - (evaluator.outgoing_exchange_fees || []).each do |enterprise_fee| - exchange.enterprise_fees << enterprise_fee + after(:create) do |variant, evaluator| + exchange_attributes = { order_cycle_id: evaluator.order_cycle.id, incoming: true, + sender_id: evaluator.producer.id, + receiver_id: evaluator.coordinator.id } + exchange = Exchange.where(exchange_attributes).first_or_create!(exchange_attributes) + exchange.variants << variant + evaluator.incoming_exchange_fees.each do |enterprise_fee| + exchange.enterprise_fees << enterprise_fee + end + + exchange_attributes = { order_cycle_id: evaluator.order_cycle.id, incoming: false, + sender_id: evaluator.coordinator.id, + receiver_id: evaluator.distributor.id } + exchange = Exchange.where(exchange_attributes).first_or_create!(exchange_attributes) + exchange.variants << variant + (evaluator.outgoing_exchange_fees || []).each do |enterprise_fee| + exchange.enterprise_fees << enterprise_fee + end end end end From 4dd1a52f8e021ddf140d6ff28083de3d55ff1207 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:59:53 +0100 Subject: [PATCH 16/36] Bring random_email and random description from spree_core factories --- spec/factories.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/factories.rb b/spec/factories.rb index 5bbbaf064e..50c2a64b5d 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,6 +1,9 @@ require 'ffaker' FactoryBot.define do + sequence(:random_description) { Faker::Lorem.paragraphs(1 + Kernel.rand(5)).join("\n") } + sequence(:random_email) { Faker::Internet.email } + factory :classification, class: Spree::Classification do end From 9e7e63cc94e16176acd1851c0727332a859f6e9b Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 14:00:47 +0100 Subject: [PATCH 17/36] Bring line_item factory from spree_core --- spec/factories/line_item_factory.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 spec/factories/line_item_factory.rb diff --git a/spec/factories/line_item_factory.rb b/spec/factories/line_item_factory.rb new file mode 100644 index 0000000000..f911a0b125 --- /dev/null +++ b/spec/factories/line_item_factory.rb @@ -0,0 +1,8 @@ +FactoryBot.define do + factory :line_item, class: Spree::LineItem do + quantity 1 + price { BigDecimal.new('10.00') } + order + variant + end +end From e2a865476da2ea03cc45b57593c27c68bbfbeccf Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 14:01:59 +0100 Subject: [PATCH 18/36] Bring adjustment factory from spree_core --- spec/factories/adjustment_factory.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 spec/factories/adjustment_factory.rb diff --git a/spec/factories/adjustment_factory.rb b/spec/factories/adjustment_factory.rb new file mode 100644 index 0000000000..7c79429052 --- /dev/null +++ b/spec/factories/adjustment_factory.rb @@ -0,0 +1,9 @@ +FactoryBot.define do + factory :adjustment, class: Spree::Adjustment do + association(:adjustable, factory: :order) + amount 100.0 + label 'Shipping' + association(:source, factory: :shipment) + eligible true + end +end From ee65b4ba27094dc4bb45bc164292631bcf8f9249 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 14:08:43 +0100 Subject: [PATCH 19/36] Bring taxon and taxonomy factories from spree_core --- spec/factories/taxon_factory.rb | 7 +++++++ spec/factories/taxonomy_factory.rb | 5 +++++ 2 files changed, 12 insertions(+) create mode 100644 spec/factories/taxon_factory.rb create mode 100644 spec/factories/taxonomy_factory.rb diff --git a/spec/factories/taxon_factory.rb b/spec/factories/taxon_factory.rb new file mode 100644 index 0000000000..47e3699b10 --- /dev/null +++ b/spec/factories/taxon_factory.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :taxon, class: Spree::Taxon do + name 'Ruby on Rails' + taxonomy + parent_id nil + end +end diff --git a/spec/factories/taxonomy_factory.rb b/spec/factories/taxonomy_factory.rb new file mode 100644 index 0000000000..dc4bfe4855 --- /dev/null +++ b/spec/factories/taxonomy_factory.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :taxonomy, class: Spree::Taxonomy do + name 'Brand' + end +end From aa00756f9c04b263a954435520ca504f0dba3a97 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:52:24 +0100 Subject: [PATCH 20/36] Bring product factory from spree_core and merge with modification --- spec/factories/product_factory.rb | 69 +++++++++++++++++++------------ 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/spec/factories/product_factory.rb b/spec/factories/product_factory.rb index 3ca3fdb77e..4c36f71fc1 100644 --- a/spec/factories/product_factory.rb +++ b/spec/factories/product_factory.rb @@ -1,4 +1,46 @@ FactoryBot.define do + factory :base_product, class: Spree::Product do + sequence(:name) { |n| "Product ##{n} - #{Kernel.rand(9999)}" } + description { generate(:random_description) } + price 19.99 + cost_price 17.00 + sku 'ABC' + available_on { 1.year.ago } + deleted_at nil + + supplier { Enterprise.is_primary_producer.first || FactoryBot.create(:supplier_enterprise) } + primary_taxon { Spree::Taxon.first || FactoryBot.create(:taxon) } + + unit_value 1 + unit_description '' + + variant_unit 'weight' + variant_unit_scale 1 + variant_unit_name '' + + shipping_category { |r| Spree::ShippingCategory.first || r.association(:shipping_category) } + + # ensure stock item will be created for this products master + before(:create) { create(:stock_location) if Spree::StockLocation.count == 0 } + + factory :product do + transient do + on_hand { 5 } + end + + tax_category { |r| Spree::TaxCategory.first || r.association(:tax_category) } + + after(:create) do |product, evaluator| + product.master.on_hand = evaluator.on_hand + product.variants.first.on_hand = evaluator.on_hand + end + + factory :product_with_option_types do + after(:create) { |product| create(:product_option_type, product: product) } + end + end + end + factory :product_with_image, parent: :product do after(:create) do |product| image = File.open(Rails.root.join('app', 'assets', 'images', 'logo-white.png')) @@ -42,30 +84,3 @@ FactoryBot.define do end end end - -FactoryBot.modify do - factory :product do - transient do - on_hand { 5 } - end - - primary_taxon { Spree::Taxon.first || FactoryBot.create(:taxon) } - - after(:create) do |product, evaluator| - product.master.on_hand = evaluator.on_hand - product.variants.first.on_hand = evaluator.on_hand - end - end - - factory :base_product do - supplier { Enterprise.is_primary_producer.first || FactoryBot.create(:supplier_enterprise) } - primary_taxon { Spree::Taxon.first || FactoryBot.create(:taxon) } - - unit_value 1 - unit_description '' - - variant_unit 'weight' - variant_unit_scale 1 - variant_unit_name '' - end -end From 6d05de742ede72a8e8552163f56cc1c1d37c1c49 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 14:12:36 +0100 Subject: [PATCH 21/36] Fix problem with product shipping category factory --- spec/factories/product_factory.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/factories/product_factory.rb b/spec/factories/product_factory.rb index 4c36f71fc1..f13cad00ba 100644 --- a/spec/factories/product_factory.rb +++ b/spec/factories/product_factory.rb @@ -18,7 +18,7 @@ FactoryBot.define do variant_unit_scale 1 variant_unit_name '' - shipping_category { |r| Spree::ShippingCategory.first || r.association(:shipping_category) } + shipping_category { DefaultShippingCategory.find_or_create } # ensure stock item will be created for this products master before(:create) { create(:stock_location) if Spree::StockLocation.count == 0 } From 6d9250c014dd0b8f28afb8f7c49a3e7e2e7ef1c1 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 14:15:20 +0100 Subject: [PATCH 22/36] Bring tax category factory from spree_core and fix a problem with the stock location of the product factory --- spec/factories.rb | 1 + spec/factories/product_factory.rb | 2 +- spec/factories/tax_category_factory.rb | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 spec/factories/tax_category_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index 50c2a64b5d..94017b374e 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,6 +1,7 @@ require 'ffaker' FactoryBot.define do + sequence(:random_string) { Faker::Lorem.sentence } sequence(:random_description) { Faker::Lorem.paragraphs(1 + Kernel.rand(5)).join("\n") } sequence(:random_email) { Faker::Internet.email } diff --git a/spec/factories/product_factory.rb b/spec/factories/product_factory.rb index f13cad00ba..bf19b0717a 100644 --- a/spec/factories/product_factory.rb +++ b/spec/factories/product_factory.rb @@ -21,7 +21,7 @@ FactoryBot.define do shipping_category { DefaultShippingCategory.find_or_create } # ensure stock item will be created for this products master - before(:create) { create(:stock_location) if Spree::StockLocation.count == 0 } + before(:create) { DefaultStockLocation.find_or_create } factory :product do transient do diff --git a/spec/factories/tax_category_factory.rb b/spec/factories/tax_category_factory.rb new file mode 100644 index 0000000000..6419bb9574 --- /dev/null +++ b/spec/factories/tax_category_factory.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :tax_category, class: Spree::TaxCategory do + name { "TaxCategory - #{rand(999999)}" } + description { generate(:random_string) } + end +end From a432bbf78908695a38f9c598b65f9d37b8d19070 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 14:17:52 +0100 Subject: [PATCH 23/36] Fix problem with shipping method's shipping category creation --- spec/factories/shipping_method_factory.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/factories/shipping_method_factory.rb b/spec/factories/shipping_method_factory.rb index 4b22c46e7a..c6ecb67531 100644 --- a/spec/factories/shipping_method_factory.rb +++ b/spec/factories/shipping_method_factory.rb @@ -7,7 +7,7 @@ FactoryBot.define do display_on '' before(:create) do |shipping_method, evaluator| - shipping_method.shipping_categories << (Spree::ShippingCategory.first || create(:shipping_category)) + shipping_method.shipping_categories << DefaultShippingCategory.find_or_create end trait :flat_rate do From f56fa0b7a56e23ec92ee956d8e8464176a0c8f35 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 14:30:38 +0100 Subject: [PATCH 24/36] Bring calculator factories from spree_core --- spec/factories/calculator_factory.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/factories/calculator_factory.rb b/spec/factories/calculator_factory.rb index 0c8acaf3e5..1268a8ccb7 100644 --- a/spec/factories/calculator_factory.rb +++ b/spec/factories/calculator_factory.rb @@ -1,4 +1,12 @@ FactoryBot.define do + factory :calculator, class: Spree::Calculator::FlatRate do + after(:create) { |c| c.set_preference(:amount, 10.0) } + end + + factory :no_amount_calculator, class: Spree::Calculator::FlatRate do + after(:create) { |c| c.set_preference(:amount, 0) } + end + sequence(:calculator_amount) factory :calculator_per_item, class: Calculator::PerItem do preferred_amount { generate(:calculator_amount) } From 292199441a0fba08005bb5d6221e4b21f6b17679 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 14:29:11 +0100 Subject: [PATCH 25/36] Fix problem with shipment's stock location creation --- spec/factories/shipment_factory.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/factories/shipment_factory.rb b/spec/factories/shipment_factory.rb index 521742baf3..64f84dfe3e 100644 --- a/spec/factories/shipment_factory.rb +++ b/spec/factories/shipment_factory.rb @@ -9,7 +9,7 @@ FactoryBot.define do state 'pending' order address - stock_location + stock_location { DefaultStockLocation.find_or_create } after(:create) do |shipment, evalulator| shipment.add_shipping_method(create(:shipping_method), true) @@ -27,7 +27,7 @@ FactoryBot.define do state 'pending' order address - stock_location + stock_location { DefaultStockLocation.find_or_create } trait :shipping_method do transient do From 2abbfad18cc73c80408a1d57fe78e8c60d340664 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 14:47:30 +0100 Subject: [PATCH 26/36] Remove the need to import both :role and :state factory to OFN --- spec/factories/user_factory.rb | 2 +- spec/features/admin/configuration/states_spec.rb | 2 +- spec/services/default_stock_location_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/factories/user_factory.rb b/spec/factories/user_factory.rb index de1774b472..ef5a991461 100644 --- a/spec/factories/user_factory.rb +++ b/spec/factories/user_factory.rb @@ -34,7 +34,7 @@ FactoryBot.define do end factory :admin_user do - spree_roles { [Spree::Role.find_by(name: 'admin') || create(:role, name: 'admin')] } + spree_roles { [Spree::Role.find_or_create_by!(name: 'admin')] } after(:create) do |user| user.spree_roles << Spree::Role.find_or_create_by!(name: 'admin') diff --git a/spec/features/admin/configuration/states_spec.rb b/spec/features/admin/configuration/states_spec.rb index e3a761088d..422cabe745 100755 --- a/spec/features/admin/configuration/states_spec.rb +++ b/spec/features/admin/configuration/states_spec.rb @@ -25,7 +25,7 @@ describe "States" do end context "admin visiting states listing" do - let!(:state) { create(:state, country: country) } + let!(:state) { Spree::State.create(name: 'Alabama', country: country) } it "should correctly display the states" do visit spree.admin_country_states_path(country) diff --git a/spec/services/default_stock_location_spec.rb b/spec/services/default_stock_location_spec.rb index 3bd3a1eb26..52ee7680ca 100644 --- a/spec/services/default_stock_location_spec.rb +++ b/spec/services/default_stock_location_spec.rb @@ -33,7 +33,7 @@ describe DefaultStockLocation do context 'when a location named default already exists' do let!(:location) do country = create(:country) - state = create(:state, country: country) + state = Spree::State.create(name: 'Alabama', country: country) Spree::StockLocation.create!( name: 'default', country_id: country.id, From 4048957e2e5dac1d599ea03b1daf83ce3dfe44b7 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 14:50:09 +0100 Subject: [PATCH 27/36] Bring the final batch of factories from spree_core --- spec/factories/country_factory.rb | 9 +++++++++ spec/factories/inventory_unit_factory.rb | 8 ++++++++ spec/factories/product_option_type_factory.rb | 6 ++++++ spec/factories/product_property_factory.rb | 6 ++++++ spec/factories/property_factory.rb | 6 ++++++ spec/factories/tax_rate_factory.rb | 7 +++++++ spec/factories/zone_factory.rb | 6 ++++++ 7 files changed, 48 insertions(+) create mode 100644 spec/factories/country_factory.rb create mode 100644 spec/factories/inventory_unit_factory.rb create mode 100644 spec/factories/product_option_type_factory.rb create mode 100644 spec/factories/product_property_factory.rb create mode 100644 spec/factories/property_factory.rb create mode 100644 spec/factories/tax_rate_factory.rb create mode 100644 spec/factories/zone_factory.rb diff --git a/spec/factories/country_factory.rb b/spec/factories/country_factory.rb new file mode 100644 index 0000000000..3ea69913ba --- /dev/null +++ b/spec/factories/country_factory.rb @@ -0,0 +1,9 @@ +FactoryBot.define do + factory :country, class: Spree::Country do + iso_name 'UNITED STATES' + name 'United States of America' + iso 'US' + iso3 'USA' + numcode 840 + end +end diff --git a/spec/factories/inventory_unit_factory.rb b/spec/factories/inventory_unit_factory.rb new file mode 100644 index 0000000000..1bcf71d7d0 --- /dev/null +++ b/spec/factories/inventory_unit_factory.rb @@ -0,0 +1,8 @@ +FactoryBot.define do + factory :inventory_unit, class: Spree::InventoryUnit do + variant + order + state 'on_hand' + association(:shipment, factory: :shipment, state: 'pending') + end +end diff --git a/spec/factories/product_option_type_factory.rb b/spec/factories/product_option_type_factory.rb new file mode 100644 index 0000000000..2b6e52b644 --- /dev/null +++ b/spec/factories/product_option_type_factory.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :product_option_type, class: Spree::ProductOptionType do + product + option_type + end +end diff --git a/spec/factories/product_property_factory.rb b/spec/factories/product_property_factory.rb new file mode 100644 index 0000000000..9844faea47 --- /dev/null +++ b/spec/factories/product_property_factory.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :product_property, class: Spree::ProductProperty do + product + property + end +end diff --git a/spec/factories/property_factory.rb b/spec/factories/property_factory.rb new file mode 100644 index 0000000000..552c6f9738 --- /dev/null +++ b/spec/factories/property_factory.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :property, class: Spree::Property do + name 'baseball_cap_color' + presentation 'cap color' + end +end diff --git a/spec/factories/tax_rate_factory.rb b/spec/factories/tax_rate_factory.rb new file mode 100644 index 0000000000..1d1977074f --- /dev/null +++ b/spec/factories/tax_rate_factory.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :tax_rate, class: Spree::TaxRate do + zone + amount 100.00 + tax_category + end +end diff --git a/spec/factories/zone_factory.rb b/spec/factories/zone_factory.rb new file mode 100644 index 0000000000..d256fbdb63 --- /dev/null +++ b/spec/factories/zone_factory.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :zone, class: Spree::Zone do + name { generate(:random_string) } + description { generate(:random_string) } + end +end From 1ddda92f4dad82a50616cdef07450c385830b6be Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 14:55:45 +0100 Subject: [PATCH 28/36] Move factories from generic factories file to their respective factory file --- spec/factories.rb | 49 ------------------------ spec/factories/credit_card_factory.rb | 6 +++ spec/factories/line_item_factory.rb | 21 ++++++++++ spec/factories/payment_method_factory.rb | 14 +++++++ spec/factories/zone_factory.rb | 8 ++++ 5 files changed, 49 insertions(+), 49 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 94017b374e..1b12e67fbb 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -105,35 +105,6 @@ FactoryBot.define do enterprise_role 'distributor' end - factory :line_item_with_shipment, parent: :line_item do - transient do - shipping_fee 3 - shipping_method nil - end - - after(:build) do |line_item, evaluator| - shipment = line_item.order.reload.shipments.first - if shipment.nil? - shipping_method = evaluator.shipping_method - unless shipping_method - shipping_method = create(:shipping_method_with, :shipping_fee, shipping_fee: evaluator.shipping_fee) - shipping_method.distributors << line_item.order.distributor if line_item.order.distributor - end - shipment = create(:shipment_with, :shipping_method, shipping_method: shipping_method, - order: line_item.order) - end - line_item.target_shipment = shipment - end - end - - factory :zone_with_member, parent: :zone do - default_tax true - - after(:create) do |zone| - Spree::ZoneMember.create!(zone: zone, zoneable: Spree::Country.find_by(name: 'Australia')) - end - end - factory :producer_property, class: ProducerProperty do value 'abc123' producer { create(:supplier_enterprise) } @@ -148,26 +119,6 @@ FactoryBot.define do bill_address { create(:address) } end - # A card that has been added to the user's profile and can be re-used. - factory :stored_credit_card, parent: :credit_card do - gateway_customer_profile_id "cus_F2T..." - gateway_payment_profile_id "card_1EY..." - end - - factory :stripe_payment_method, class: Spree::Gateway::StripeConnect do - name 'Stripe' - environment 'test' - distributors { [FactoryBot.create(:enterprise)] } - preferred_enterprise_id { distributors.first.id } - end - - factory :stripe_sca_payment_method, class: Spree::Gateway::StripeSCA do - name 'StripeSCA' - environment 'test' - distributors { [FactoryBot.create(:stripe_account).enterprise] } - preferred_enterprise_id { distributors.first.id } - end - factory :stripe_account do enterprise { FactoryBot.create(:distributor_enterprise) } stripe_user_id "abc123" diff --git a/spec/factories/credit_card_factory.rb b/spec/factories/credit_card_factory.rb index 72fde1c179..3df2d1f080 100644 --- a/spec/factories/credit_card_factory.rb +++ b/spec/factories/credit_card_factory.rb @@ -12,4 +12,10 @@ FactoryBot.define do cc_type 'visa' end + + # A card that has been added to the user's profile and can be re-used. + factory :stored_credit_card, parent: :credit_card do + gateway_customer_profile_id "cus_F2T..." + gateway_payment_profile_id "card_1EY..." + end end diff --git a/spec/factories/line_item_factory.rb b/spec/factories/line_item_factory.rb index f911a0b125..25b525555c 100644 --- a/spec/factories/line_item_factory.rb +++ b/spec/factories/line_item_factory.rb @@ -5,4 +5,25 @@ FactoryBot.define do order variant end + + factory :line_item_with_shipment, parent: :line_item do + transient do + shipping_fee 3 + shipping_method nil + end + + after(:build) do |line_item, evaluator| + shipment = line_item.order.reload.shipments.first + if shipment.nil? + shipping_method = evaluator.shipping_method + unless shipping_method + shipping_method = create(:shipping_method_with, :shipping_fee, shipping_fee: evaluator.shipping_fee) + shipping_method.distributors << line_item.order.distributor if line_item.order.distributor + end + shipment = create(:shipment_with, :shipping_method, shipping_method: shipping_method, + order: line_item.order) + end + line_item.target_shipment = shipment + end + end end diff --git a/spec/factories/payment_method_factory.rb b/spec/factories/payment_method_factory.rb index 3be53ed70d..b75a5037b3 100644 --- a/spec/factories/payment_method_factory.rb +++ b/spec/factories/payment_method_factory.rb @@ -20,4 +20,18 @@ FactoryBot.define do name 'Credit Card' environment 'test' end + + factory :stripe_payment_method, class: Spree::Gateway::StripeConnect do + name 'Stripe' + environment 'test' + distributors { [FactoryBot.create(:enterprise)] } + preferred_enterprise_id { distributors.first.id } + end + + factory :stripe_sca_payment_method, class: Spree::Gateway::StripeSCA do + name 'StripeSCA' + environment 'test' + distributors { [FactoryBot.create(:stripe_account).enterprise] } + preferred_enterprise_id { distributors.first.id } + end end diff --git a/spec/factories/zone_factory.rb b/spec/factories/zone_factory.rb index d256fbdb63..9496ebb8a3 100644 --- a/spec/factories/zone_factory.rb +++ b/spec/factories/zone_factory.rb @@ -3,4 +3,12 @@ FactoryBot.define do name { generate(:random_string) } description { generate(:random_string) } end + + factory :zone_with_member, parent: :zone do + default_tax true + + after(:create) do |zone| + Spree::ZoneMember.create!(zone: zone, zoneable: Spree::Country.find_by(name: 'Australia')) + end + end end From cd22361d131235b887c9c94f01ae57ccf5d38e32 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 15:28:42 +0100 Subject: [PATCH 29/36] Fix shipping category factory --- spec/factories/shipping_category_factory.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/factories/shipping_category_factory.rb b/spec/factories/shipping_category_factory.rb index 7f676f9027..76bebeb25a 100644 --- a/spec/factories/shipping_category_factory.rb +++ b/spec/factories/shipping_category_factory.rb @@ -1,7 +1,5 @@ FactoryBot.define do factory :shipping_category, class: Spree::ShippingCategory do initialize_with { DefaultShippingCategory.find_or_create } - transient { name 'Default' } - sequence(:name) { |n| "ShippingCategory ##{n}" } end end From 4de4cc642db7d4e1fd4b03c8cd8b464937d6add9 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 15:47:12 +0100 Subject: [PATCH 30/36] Fix stock location factory --- spec/factories/stock_location_factory.rb | 3 --- spec/services/default_stock_location_spec.rb | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/spec/factories/stock_location_factory.rb b/spec/factories/stock_location_factory.rb index 46cd0a2f4a..452176fae1 100644 --- a/spec/factories/stock_location_factory.rb +++ b/spec/factories/stock_location_factory.rb @@ -1,8 +1,5 @@ FactoryBot.define do factory :stock_location, class: Spree::StockLocation do - # Ensures the name attribute is not assigned after instantiating the default location - transient { name 'default' } - # keeps the test stock_location unique initialize_with { DefaultStockLocation.find_or_create } diff --git a/spec/services/default_stock_location_spec.rb b/spec/services/default_stock_location_spec.rb index 52ee7680ca..7423d97293 100644 --- a/spec/services/default_stock_location_spec.rb +++ b/spec/services/default_stock_location_spec.rb @@ -22,7 +22,7 @@ describe DefaultStockLocation do describe '.destroy_all' do it "removes all stock locations named 'default'" do - create(:stock_location, name: 'default') + create(:stock_location) expect { described_class.destroy_all } .to change { Spree::StockLocation.count }.to(0) From f6277416ce2fb72a0c42ff53fd51f586be419727 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 16:03:39 +0100 Subject: [PATCH 31/36] Simplify stock location factory and fix related issues in several factories --- spec/controllers/api/shipments_controller_spec.rb | 2 +- spec/factories/product_factory.rb | 2 +- spec/factories/shipment_factory.rb | 4 ++-- spec/factories/stock_location_factory.rb | 15 +-------------- spec/models/spree/stock_item_spec.rb | 10 +++++++++- 5 files changed, 14 insertions(+), 19 deletions(-) diff --git a/spec/controllers/api/shipments_controller_spec.rb b/spec/controllers/api/shipments_controller_spec.rb index f4dba19f11..7c13fe3318 100644 --- a/spec/controllers/api/shipments_controller_spec.rb +++ b/spec/controllers/api/shipments_controller_spec.rb @@ -29,7 +29,7 @@ describe Api::ShipmentsController, type: :controller do let(:current_api_user) { build(:admin_user) } let!(:order) { shipment.order } let(:order_ship_address) { create(:address) } - let!(:stock_location) { create(:stock_location_with_items) } + let!(:stock_location) { Spree::StockLocation.first || create(:stock_location) } let!(:variant) { create(:variant) } let(:params) do { quantity: 2, diff --git a/spec/factories/product_factory.rb b/spec/factories/product_factory.rb index bf19b0717a..f13cad00ba 100644 --- a/spec/factories/product_factory.rb +++ b/spec/factories/product_factory.rb @@ -21,7 +21,7 @@ FactoryBot.define do shipping_category { DefaultShippingCategory.find_or_create } # ensure stock item will be created for this products master - before(:create) { DefaultStockLocation.find_or_create } + before(:create) { create(:stock_location) if Spree::StockLocation.count == 0 } factory :product do transient do diff --git a/spec/factories/shipment_factory.rb b/spec/factories/shipment_factory.rb index 64f84dfe3e..17d465ced1 100644 --- a/spec/factories/shipment_factory.rb +++ b/spec/factories/shipment_factory.rb @@ -9,7 +9,7 @@ FactoryBot.define do state 'pending' order address - stock_location { DefaultStockLocation.find_or_create } + stock_location { Spree::StockLocation.first || create(:stock_location) } after(:create) do |shipment, evalulator| shipment.add_shipping_method(create(:shipping_method), true) @@ -27,7 +27,7 @@ FactoryBot.define do state 'pending' order address - stock_location { DefaultStockLocation.find_or_create } + stock_location { Spree::StockLocation.first || create(:stock_location) } trait :shipping_method do transient do diff --git a/spec/factories/stock_location_factory.rb b/spec/factories/stock_location_factory.rb index 452176fae1..393baa1e7b 100644 --- a/spec/factories/stock_location_factory.rb +++ b/spec/factories/stock_location_factory.rb @@ -1,9 +1,8 @@ FactoryBot.define do factory :stock_location, class: Spree::StockLocation do # keeps the test stock_location unique - initialize_with { DefaultStockLocation.find_or_create } + initialize_with { Spree::StockLocation.first || DefaultStockLocation.find_or_create } - name 'NY Warehouse' address1 '1600 Pennsylvania Ave NW' city 'Washington' zipcode '20500' @@ -17,17 +16,5 @@ FactoryBot.define do state do |stock_location| stock_location.country.states.first || stock_location.association(:state, :country => stock_location.country) end - - factory :stock_location_with_items do - after(:create) do |stock_location, evaluator| - # variant will add itself to all stock_locations in an after_create - # creating a product will automatically create a master variant - product_1 = create(:product) - product_2 = create(:product) - - stock_location.stock_items.where(:variant_id => product_1.master.id).first.adjust_count_on_hand(10) - stock_location.stock_items.where(:variant_id => product_2.master.id).first.adjust_count_on_hand(20) - end - end end end diff --git a/spec/models/spree/stock_item_spec.rb b/spec/models/spree/stock_item_spec.rb index 9c9b233986..ed634e854f 100644 --- a/spec/models/spree/stock_item_spec.rb +++ b/spec/models/spree/stock_item_spec.rb @@ -3,7 +3,15 @@ require 'spec_helper' RSpec.describe Spree::StockItem do - let(:stock_location) { create(:stock_location_with_items) } + let(:stock_location) { create(:stock_location) } + + before do + product_1 = create(:product) + product_2 = create(:product) + + stock_location.stock_items.where(:variant_id => product_1.master.id).first.adjust_count_on_hand(10) + stock_location.stock_items.where(:variant_id => product_2.master.id).first.adjust_count_on_hand(20) + end subject { stock_location.stock_items.order(:id).first } From 5568fd58261e48b77b361b390d1c544b5d4b4135 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 15:22:48 +0100 Subject: [PATCH 32/36] Fix problem in order factory We dont need the global zone --- spec/factories/order_factory.rb | 13 ++++++------- spec/services/tax_rate_finder_spec.rb | 5 ++--- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/spec/factories/order_factory.rb b/spec/factories/order_factory.rb index 040560b21a..05dc2bddea 100644 --- a/spec/factories/order_factory.rb +++ b/spec/factories/order_factory.rb @@ -123,6 +123,7 @@ FactoryBot.define do product_price 0 tax_rate_amount 0 tax_rate_name "" + zone { create(:zone_with_member) } end distributor { create(:distributor_enterprise) } @@ -130,13 +131,11 @@ FactoryBot.define do after(:create) do |order, proxy| order.distributor.update_attribute(:charges_sales_tax, true) - Spree::Zone.global.update_attribute(:default_tax, true) - - p = FactoryBot.create(:taxed_product, zone: Spree::Zone.global, - price: proxy.product_price, - tax_rate_amount: proxy.tax_rate_amount, - tax_rate_name: proxy.tax_rate_name) - FactoryBot.create(:line_item, order: order, product: p, price: p.price) + product = FactoryBot.create(:taxed_product, zone: proxy.zone, + price: proxy.product_price, + tax_rate_amount: proxy.tax_rate_amount, + tax_rate_name: proxy.tax_rate_name) + FactoryBot.create(:line_item, order: order, product: product, price: product.price) order.reload end end diff --git a/spec/services/tax_rate_finder_spec.rb b/spec/services/tax_rate_finder_spec.rb index 6881d66ebf..7af1be76b8 100644 --- a/spec/services/tax_rate_finder_spec.rb +++ b/spec/services/tax_rate_finder_spec.rb @@ -6,11 +6,10 @@ describe TaxRateFinder do let(:included_tax) { BigDecimal(20) } let(:tax_rate) { create_rate(0.2) } let(:tax_category) { create(:tax_category, tax_rates: [tax_rate]) } - # This zone is used by :order_with_taxes and needs to match it - let(:zone) { create(:zone, name: "GlobalZone") } + let(:zone) { create(:zone_with_member) } let(:shipment) { create(:shipment) } let(:enterprise_fee) { create(:enterprise_fee, tax_category: tax_category) } - let(:order) { create(:order_with_taxes) } + let(:order) { create(:order_with_taxes, zone: zone) } it "finds the tax rate of a shipping fee" do rates = TaxRateFinder.new.tax_rates( From 41d67d8b2ee4d852aebadcbba8999b177481ce89 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 17:49:01 +0100 Subject: [PATCH 33/36] Fix estimator spec --- .../order_management/stock/estimator_spec.rb | 2 +- spec/factories/stock_factory.rb | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 spec/factories/stock_factory.rb diff --git a/engines/order_management/spec/services/order_management/stock/estimator_spec.rb b/engines/order_management/spec/services/order_management/stock/estimator_spec.rb index 6d14903cbd..e71d452798 100644 --- a/engines/order_management/spec/services/order_management/stock/estimator_spec.rb +++ b/engines/order_management/spec/services/order_management/stock/estimator_spec.rb @@ -5,7 +5,7 @@ require 'spec_helper' module OrderManagement module Stock describe Estimator do - let!(:shipping_method) { create(:shipping_method, zones: [Spree::Zone.global] ) } + let!(:shipping_method) { create(:shipping_method, zones: [create(:zone)] ) } let(:package) { build(:stock_package_fulfilled) } let(:order) { package.order } subject { Estimator.new(order) } diff --git a/spec/factories/stock_factory.rb b/spec/factories/stock_factory.rb new file mode 100644 index 0000000000..9cb4b2a2eb --- /dev/null +++ b/spec/factories/stock_factory.rb @@ -0,0 +1,20 @@ +FactoryBot.define do + factory :stock_package, class: Spree::Stock::Package do + ignore do + stock_location { build(:stock_location) } + order { create(:order_with_line_items, line_items_count: 2) } + contents [] + end + + initialize_with { new(stock_location, order, contents) } + + factory :stock_package_fulfilled do + after(:build) do |package, evaluator| + evaluator.order.line_items.reload + evaluator.order.line_items.each do |line_item| + package.add line_item.variant, line_item.quantity, :on_hand + end + end + end + end +end From 274cdefa262cd4ac3af76e0b2b2c4279d6ce4fac Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Wed, 26 Aug 2020 22:14:24 +0100 Subject: [PATCH 34/36] Remove commented code --- spec/factories/variant_factory.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/factories/variant_factory.rb b/spec/factories/variant_factory.rb index 7489ad4001..8000e2c6dd 100644 --- a/spec/factories/variant_factory.rb +++ b/spec/factories/variant_factory.rb @@ -22,7 +22,6 @@ FactoryBot.define do on_hand { 5 } end - # on_hand 5 product { |p| p.association(:product) } unit_value 1 unit_description '' From f9aac02e1323e0693adb18bdf64569c35eb69398 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Wed, 26 Aug 2020 22:16:02 +0100 Subject: [PATCH 35/36] Run rubocop -a --- spec/models/spree/stock_item_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/models/spree/stock_item_spec.rb b/spec/models/spree/stock_item_spec.rb index ed634e854f..6dccf13665 100644 --- a/spec/models/spree/stock_item_spec.rb +++ b/spec/models/spree/stock_item_spec.rb @@ -9,8 +9,8 @@ RSpec.describe Spree::StockItem do product_1 = create(:product) product_2 = create(:product) - stock_location.stock_items.where(:variant_id => product_1.master.id).first.adjust_count_on_hand(10) - stock_location.stock_items.where(:variant_id => product_2.master.id).first.adjust_count_on_hand(20) + stock_location.stock_items.where(variant_id: product_1.master.id).first.adjust_count_on_hand(10) + stock_location.stock_items.where(variant_id: product_2.master.id).first.adjust_count_on_hand(20) end subject { stock_location.stock_items.order(:id).first } From 94d1d5f3aebb48f16d417bcca9f3242fb18e3718 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Wed, 26 Aug 2020 22:18:40 +0100 Subject: [PATCH 36/36] Run rubocop -a on spec/factories --- spec/factories/adjustment_factory.rb | 2 ++ spec/factories/country_factory.rb | 2 ++ spec/factories/credit_card_factory.rb | 4 +++- spec/factories/inventory_unit_factory.rb | 2 ++ spec/factories/line_item_factory.rb | 4 +++- spec/factories/options_factory.rb | 2 ++ spec/factories/order_factory.rb | 8 +++----- spec/factories/payment_factory.rb | 2 ++ spec/factories/payment_method_factory.rb | 2 ++ spec/factories/product_option_type_factory.rb | 2 ++ spec/factories/product_property_factory.rb | 2 ++ spec/factories/property_factory.rb | 2 ++ spec/factories/shipment_factory.rb | 2 +- spec/factories/shipping_category_factory.rb | 4 +++- spec/factories/shipping_method_factory.rb | 2 +- spec/factories/stock_factory.rb | 2 ++ spec/factories/stock_location_factory.rb | 4 +++- spec/factories/tax_category_factory.rb | 4 +++- spec/factories/tax_rate_factory.rb | 2 ++ spec/factories/taxon_factory.rb | 2 ++ spec/factories/taxonomy_factory.rb | 2 ++ spec/factories/user_factory.rb | 4 ++-- spec/factories/variant_factory.rb | 2 +- spec/factories/zone_factory.rb | 2 ++ 24 files changed, 51 insertions(+), 15 deletions(-) diff --git a/spec/factories/adjustment_factory.rb b/spec/factories/adjustment_factory.rb index 7c79429052..239d92d6c0 100644 --- a/spec/factories/adjustment_factory.rb +++ b/spec/factories/adjustment_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :adjustment, class: Spree::Adjustment do association(:adjustable, factory: :order) diff --git a/spec/factories/country_factory.rb b/spec/factories/country_factory.rb index 3ea69913ba..c3cd8e2e46 100644 --- a/spec/factories/country_factory.rb +++ b/spec/factories/country_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :country, class: Spree::Country do iso_name 'UNITED STATES' diff --git a/spec/factories/credit_card_factory.rb b/spec/factories/credit_card_factory.rb index 3df2d1f080..27976a55c6 100644 --- a/spec/factories/credit_card_factory.rb +++ b/spec/factories/credit_card_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # allows credit card info to be saved to the database which is needed for factories to work properly class TestCard < Spree::CreditCard def remove_readonly_attributes(attributes) attributes; end @@ -7,7 +9,7 @@ FactoryBot.define do factory :credit_card, class: TestCard do verification_value 123 month 12 - year { Time.now.year + 1 } + year { Time.zone.now.year + 1 } number '4111111111111111' cc_type 'visa' diff --git a/spec/factories/inventory_unit_factory.rb b/spec/factories/inventory_unit_factory.rb index 1bcf71d7d0..47c0bd2aae 100644 --- a/spec/factories/inventory_unit_factory.rb +++ b/spec/factories/inventory_unit_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :inventory_unit, class: Spree::InventoryUnit do variant diff --git a/spec/factories/line_item_factory.rb b/spec/factories/line_item_factory.rb index 25b525555c..f6de86257b 100644 --- a/spec/factories/line_item_factory.rb +++ b/spec/factories/line_item_factory.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + FactoryBot.define do factory :line_item, class: Spree::LineItem do quantity 1 - price { BigDecimal.new('10.00') } + price { BigDecimal('10.00') } order variant end diff --git a/spec/factories/options_factory.rb b/spec/factories/options_factory.rb index be425474b7..1d5b24f2b7 100644 --- a/spec/factories/options_factory.rb +++ b/spec/factories/options_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :option_value, class: Spree::OptionValue do name 'Size' diff --git a/spec/factories/order_factory.rb b/spec/factories/order_factory.rb index 05dc2bddea..fde8605c1c 100644 --- a/spec/factories/order_factory.rb +++ b/spec/factories/order_factory.rb @@ -35,13 +35,11 @@ FactoryBot.define do factory :completed_order_with_totals do state 'complete' - completed_at { Time.now } + completed_at { Time.zone.now } distributor { create(:distributor_enterprise) } - after(:create) do |order| - order.refresh_shipment_rates - end + after(:create, &:refresh_shipment_rates) factory :order_ready_to_ship do payment_state 'paid' @@ -95,7 +93,7 @@ FactoryBot.define do while !order.completed? do break unless a = order.next! end order.select_shipping_method(evaluator.shipping_method.id) end - end + end end factory :order_with_totals_and_distribution, parent: :order_with_distributor do diff --git a/spec/factories/payment_factory.rb b/spec/factories/payment_factory.rb index 40d02a8654..3858b8e913 100644 --- a/spec/factories/payment_factory.rb +++ b/spec/factories/payment_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :payment, class: Spree::Payment do transient do diff --git a/spec/factories/payment_method_factory.rb b/spec/factories/payment_method_factory.rb index b75a5037b3..2eb5a674d3 100644 --- a/spec/factories/payment_method_factory.rb +++ b/spec/factories/payment_method_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :payment_method, class: Spree::PaymentMethod::Check do name 'Check' diff --git a/spec/factories/product_option_type_factory.rb b/spec/factories/product_option_type_factory.rb index 2b6e52b644..22a8b6f96a 100644 --- a/spec/factories/product_option_type_factory.rb +++ b/spec/factories/product_option_type_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :product_option_type, class: Spree::ProductOptionType do product diff --git a/spec/factories/product_property_factory.rb b/spec/factories/product_property_factory.rb index 9844faea47..7db00a14ff 100644 --- a/spec/factories/product_property_factory.rb +++ b/spec/factories/product_property_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :product_property, class: Spree::ProductProperty do product diff --git a/spec/factories/property_factory.rb b/spec/factories/property_factory.rb index 552c6f9738..6cf7ee09b9 100644 --- a/spec/factories/property_factory.rb +++ b/spec/factories/property_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :property, class: Spree::Property do name 'baseball_cap_color' diff --git a/spec/factories/shipment_factory.rb b/spec/factories/shipment_factory.rb index 17d465ced1..ef167d5837 100644 --- a/spec/factories/shipment_factory.rb +++ b/spec/factories/shipment_factory.rb @@ -11,7 +11,7 @@ FactoryBot.define do address stock_location { Spree::StockLocation.first || create(:stock_location) } - after(:create) do |shipment, evalulator| + after(:create) do |shipment, _evalulator| shipment.add_shipping_method(create(:shipping_method), true) shipment.order.line_items.each do |line_item| diff --git a/spec/factories/shipping_category_factory.rb b/spec/factories/shipping_category_factory.rb index 76bebeb25a..9749d3a719 100644 --- a/spec/factories/shipping_category_factory.rb +++ b/spec/factories/shipping_category_factory.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + FactoryBot.define do - factory :shipping_category, class: Spree::ShippingCategory do + factory :shipping_category, class: Spree::ShippingCategory do initialize_with { DefaultShippingCategory.find_or_create } end end diff --git a/spec/factories/shipping_method_factory.rb b/spec/factories/shipping_method_factory.rb index c6ecb67531..aae807d226 100644 --- a/spec/factories/shipping_method_factory.rb +++ b/spec/factories/shipping_method_factory.rb @@ -6,7 +6,7 @@ FactoryBot.define do distributors { [Enterprise.is_distributor.first || FactoryBot.create(:distributor_enterprise)] } display_on '' - before(:create) do |shipping_method, evaluator| + before(:create) do |shipping_method, _evaluator| shipping_method.shipping_categories << DefaultShippingCategory.find_or_create end diff --git a/spec/factories/stock_factory.rb b/spec/factories/stock_factory.rb index 9cb4b2a2eb..b501401507 100644 --- a/spec/factories/stock_factory.rb +++ b/spec/factories/stock_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :stock_package, class: Spree::Stock::Package do ignore do diff --git a/spec/factories/stock_location_factory.rb b/spec/factories/stock_location_factory.rb index 393baa1e7b..48fc14d772 100644 --- a/spec/factories/stock_location_factory.rb +++ b/spec/factories/stock_location_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :stock_location, class: Spree::StockLocation do # keeps the test stock_location unique @@ -14,7 +16,7 @@ FactoryBot.define do country { |stock_location| Spree::Country.first || stock_location.association(:country) } state do |stock_location| - stock_location.country.states.first || stock_location.association(:state, :country => stock_location.country) + stock_location.country.states.first || stock_location.association(:state, country: stock_location.country) end end end diff --git a/spec/factories/tax_category_factory.rb b/spec/factories/tax_category_factory.rb index 6419bb9574..0cb0f3af4a 100644 --- a/spec/factories/tax_category_factory.rb +++ b/spec/factories/tax_category_factory.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + FactoryBot.define do factory :tax_category, class: Spree::TaxCategory do - name { "TaxCategory - #{rand(999999)}" } + name { "TaxCategory - #{rand(999_999)}" } description { generate(:random_string) } end end diff --git a/spec/factories/tax_rate_factory.rb b/spec/factories/tax_rate_factory.rb index 1d1977074f..ae1168ee3f 100644 --- a/spec/factories/tax_rate_factory.rb +++ b/spec/factories/tax_rate_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :tax_rate, class: Spree::TaxRate do zone diff --git a/spec/factories/taxon_factory.rb b/spec/factories/taxon_factory.rb index 47e3699b10..e02e87a9e9 100644 --- a/spec/factories/taxon_factory.rb +++ b/spec/factories/taxon_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :taxon, class: Spree::Taxon do name 'Ruby on Rails' diff --git a/spec/factories/taxonomy_factory.rb b/spec/factories/taxonomy_factory.rb index dc4bfe4855..89d6d317fe 100644 --- a/spec/factories/taxonomy_factory.rb +++ b/spec/factories/taxonomy_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :taxonomy, class: Spree::Taxonomy do name 'Brand' diff --git a/spec/factories/user_factory.rb b/spec/factories/user_factory.rb index ef5a991461..3896b7812c 100644 --- a/spec/factories/user_factory.rb +++ b/spec/factories/user_factory.rb @@ -31,14 +31,14 @@ FactoryBot.define do user.spree_roles.clear # Remove admin role user.enterprises << proxy.enterprises - end + end factory :admin_user do spree_roles { [Spree::Role.find_or_create_by!(name: 'admin')] } after(:create) do |user| user.spree_roles << Spree::Role.find_or_create_by!(name: 'admin') - end + end end end end diff --git a/spec/factories/variant_factory.rb b/spec/factories/variant_factory.rb index 8000e2c6dd..5d477a5603 100644 --- a/spec/factories/variant_factory.rb +++ b/spec/factories/variant_factory.rb @@ -1,5 +1,5 @@ FactoryBot.define do - sequence(:random_float) { BigDecimal.new("#{rand(200)}.#{rand(99)}") } + sequence(:random_float) { BigDecimal("#{rand(200)}.#{rand(99)}") } factory :base_variant, class: Spree::Variant do price 19.99 diff --git a/spec/factories/zone_factory.rb b/spec/factories/zone_factory.rb index 9496ebb8a3..1993f3c913 100644 --- a/spec/factories/zone_factory.rb +++ b/spec/factories/zone_factory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :zone, class: Spree::Zone do name { generate(:random_string) }