From 0501859c23c85108b044f5f7128e50976eabf93f Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 12:01:51 +0100 Subject: [PATCH 01/18] Extract order_cycle factories to separate file --- spec/factories.rb | 107 ------------------------- spec/factories/order_cycle_factory.rb | 108 ++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 107 deletions(-) create mode 100644 spec/factories/order_cycle_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index 6a8b2ef8b95..db4046b2263 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -19,113 +19,6 @@ factory :classification, class: Spree::Classification do end - factory :order_cycle, :parent => :simple_order_cycle do - coordinator_fees { [create(:enterprise_fee, enterprise: coordinator)] } - - after(:create) do |oc| - # Suppliers - supplier1 = create(:supplier_enterprise) - supplier2 = create(:supplier_enterprise) - - # Incoming Exchanges - ex1 = create(:exchange, :order_cycle => oc, :incoming => true, - :sender => supplier1, :receiver => oc.coordinator, - :receival_instructions => 'instructions 0') - ex2 = create(:exchange, :order_cycle => oc, :incoming => true, - :sender => supplier2, :receiver => oc.coordinator, - :receival_instructions => 'instructions 1') - ExchangeFee.create!(exchange: ex1, - enterprise_fee: create(:enterprise_fee, enterprise: ex1.sender)) - ExchangeFee.create!(exchange: ex2, - enterprise_fee: create(:enterprise_fee, enterprise: ex2.sender)) - - # Distributors - distributor1 = create(:distributor_enterprise) - distributor2 = create(:distributor_enterprise) - - # Outgoing Exchanges - ex3 = create(:exchange, :order_cycle => oc, :incoming => false, - :sender => oc.coordinator, :receiver => distributor1, - :pickup_time => 'time 0', :pickup_instructions => 'instructions 0') - ex4 = create(:exchange, :order_cycle => oc, :incoming => false, - :sender => oc.coordinator, :receiver => distributor2, - :pickup_time => 'time 1', :pickup_instructions => 'instructions 1') - ExchangeFee.create!(exchange: ex3, - enterprise_fee: create(:enterprise_fee, enterprise: ex3.receiver)) - ExchangeFee.create!(exchange: ex4, - enterprise_fee: create(:enterprise_fee, enterprise: ex4.receiver)) - - # Products with images - [ex1, ex2].each do |exchange| - product = create(:product, supplier: exchange.sender) - image = File.open(File.expand_path('../../app/assets/images/logo-white.png', __FILE__)) - Spree::Image.create({:viewable_id => product.master.id, :viewable_type => 'Spree::Variant', :alt => "position 1", :attachment => image, :position => 1}) - - exchange.variants << product.variants.first - end - - variants = [ex1, ex2].map(&:variants).flatten - [ex3, ex4].each do |exchange| - variants.each { |v| exchange.variants << v } - end - end - end - - factory :order_cycle_with_overrides, parent: :order_cycle do - after(:create) do |oc| - oc.variants.each do |variant| - create(:variant_override, variant: variant, hub: oc.distributors.first, price: variant.price + 100) - end - end - end - - factory :simple_order_cycle, :class => OrderCycle do - sequence(:name) { |n| "Order Cycle #{n}" } - - orders_open_at { 1.day.ago } - orders_close_at { 1.week.from_now } - - coordinator { Enterprise.is_distributor.first || FactoryBot.create(:distributor_enterprise) } - - transient do - suppliers [] - distributors [] - variants [] - end - - after(:create) do |oc, proxy| - proxy.suppliers.each do |supplier| - ex = create(:exchange, :order_cycle => oc, :sender => supplier, :receiver => oc.coordinator, :incoming => true, :receival_instructions => 'instructions') - proxy.variants.each { |v| ex.variants << v } - end - - proxy.distributors.each do |distributor| - ex = create(:exchange, :order_cycle => oc, :sender => oc.coordinator, :receiver => distributor, :incoming => false, :pickup_time => 'time', :pickup_instructions => 'instructions') - proxy.variants.each { |v| ex.variants << v } - end - end - end - - factory :undated_order_cycle, parent: :simple_order_cycle do - orders_open_at nil - orders_close_at nil - end - - factory :upcoming_order_cycle, parent: :simple_order_cycle do - orders_open_at { 1.week.from_now } - orders_close_at { 2.weeks.from_now } - end - - factory :open_order_cycle, parent: :simple_order_cycle do - orders_open_at { 1.week.ago } - orders_close_at { 1.week.from_now } - end - - factory :closed_order_cycle, parent: :simple_order_cycle do - orders_open_at { 2.weeks.ago } - orders_close_at { 1.week.ago } - end - factory :exchange, :class => Exchange do incoming false order_cycle { OrderCycle.first || FactoryBot.create(:simple_order_cycle) } diff --git a/spec/factories/order_cycle_factory.rb b/spec/factories/order_cycle_factory.rb new file mode 100644 index 00000000000..b4c5f460da8 --- /dev/null +++ b/spec/factories/order_cycle_factory.rb @@ -0,0 +1,108 @@ +FactoryBot.define do + factory :order_cycle, :parent => :simple_order_cycle do + coordinator_fees { [create(:enterprise_fee, enterprise: coordinator)] } + + after(:create) do |oc| + # Suppliers + supplier1 = create(:supplier_enterprise) + supplier2 = create(:supplier_enterprise) + + # Incoming Exchanges + ex1 = create(:exchange, :order_cycle => oc, :incoming => true, + :sender => supplier1, :receiver => oc.coordinator, + :receival_instructions => 'instructions 0') + ex2 = create(:exchange, :order_cycle => oc, :incoming => true, + :sender => supplier2, :receiver => oc.coordinator, + :receival_instructions => 'instructions 1') + ExchangeFee.create!(exchange: ex1, + enterprise_fee: create(:enterprise_fee, enterprise: ex1.sender)) + ExchangeFee.create!(exchange: ex2, + enterprise_fee: create(:enterprise_fee, enterprise: ex2.sender)) + + # Distributors + distributor1 = create(:distributor_enterprise) + distributor2 = create(:distributor_enterprise) + + # Outgoing Exchanges + ex3 = create(:exchange, :order_cycle => oc, :incoming => false, + :sender => oc.coordinator, :receiver => distributor1, + :pickup_time => 'time 0', :pickup_instructions => 'instructions 0') + ex4 = create(:exchange, :order_cycle => oc, :incoming => false, + :sender => oc.coordinator, :receiver => distributor2, + :pickup_time => 'time 1', :pickup_instructions => 'instructions 1') + ExchangeFee.create!(exchange: ex3, + enterprise_fee: create(:enterprise_fee, enterprise: ex3.receiver)) + ExchangeFee.create!(exchange: ex4, + enterprise_fee: create(:enterprise_fee, enterprise: ex4.receiver)) + + # Products with images + [ex1, ex2].each do |exchange| + product = create(:product, supplier: exchange.sender) + image = File.open(File.expand_path('../../app/assets/images/logo-white.png', __FILE__)) + Spree::Image.create({:viewable_id => product.master.id, :viewable_type => 'Spree::Variant', :alt => "position 1", :attachment => image, :position => 1}) + + exchange.variants << product.variants.first + end + + variants = [ex1, ex2].map(&:variants).flatten + [ex3, ex4].each do |exchange| + variants.each { |v| exchange.variants << v } + end + end + end + + factory :order_cycle_with_overrides, parent: :order_cycle do + after(:create) do |oc| + oc.variants.each do |variant| + create(:variant_override, variant: variant, hub: oc.distributors.first, price: variant.price + 100) + end + end + end + + factory :simple_order_cycle, :class => OrderCycle do + sequence(:name) { |n| "Order Cycle #{n}" } + + orders_open_at { 1.day.ago } + orders_close_at { 1.week.from_now } + + coordinator { Enterprise.is_distributor.first || FactoryBot.create(:distributor_enterprise) } + + transient do + suppliers [] + distributors [] + variants [] + end + + after(:create) do |oc, proxy| + proxy.suppliers.each do |supplier| + ex = create(:exchange, :order_cycle => oc, :sender => supplier, :receiver => oc.coordinator, :incoming => true, :receival_instructions => 'instructions') + proxy.variants.each { |v| ex.variants << v } + end + + proxy.distributors.each do |distributor| + ex = create(:exchange, :order_cycle => oc, :sender => oc.coordinator, :receiver => distributor, :incoming => false, :pickup_time => 'time', :pickup_instructions => 'instructions') + proxy.variants.each { |v| ex.variants << v } + end + end + end + + factory :undated_order_cycle, parent: :simple_order_cycle do + orders_open_at nil + orders_close_at nil + end + + factory :upcoming_order_cycle, parent: :simple_order_cycle do + orders_open_at { 1.week.from_now } + orders_close_at { 2.weeks.from_now } + end + + factory :open_order_cycle, parent: :simple_order_cycle do + orders_open_at { 1.week.ago } + orders_close_at { 1.week.from_now } + end + + factory :closed_order_cycle, parent: :simple_order_cycle do + orders_open_at { 2.weeks.ago } + orders_close_at { 1.week.ago } + end +end \ No newline at end of file From 42f2e78b1053f1d06bc950e9d313b79bf8691fa0 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 12:10:54 +0100 Subject: [PATCH 02/18] Extract subscriptions factories to separate file --- spec/factories.rb | 39 ------------------------- spec/factories/subscription_factory.rb | 40 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 spec/factories/subscription_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index db4046b2263..5fa9a03e84d 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -31,45 +31,6 @@ order_cycles { [OrderCycle.first || FactoryBot.create(:simple_order_cycle)] } end - factory :subscription, :class => Subscription do - shop { create :enterprise } - schedule { create(:schedule, order_cycles: [create(:simple_order_cycle, coordinator: shop)]) } - customer { create(:customer, enterprise: shop) } - bill_address { create(:address, :randomized) } - ship_address { create(:address, :randomized) } - payment_method { create(:payment_method, distributors: [shop]) } - shipping_method { create(:shipping_method, distributors: [shop]) } - begins_at { 1.month.ago } - - transient do - with_items false - with_proxy_orders false - end - - after(:create) do |subscription, proxy| - if proxy.with_items - subscription.subscription_line_items = build_list(:subscription_line_item, 3, subscription: subscription) - subscription.order_cycles.each do |oc| - ex = oc.exchanges.outgoing.find_by_sender_id_and_receiver_id(subscription.shop_id, subscription.shop_id) || - create(:exchange, :order_cycle => oc, :sender => subscription.shop, :receiver => subscription.shop, :incoming => false, :pickup_time => 'time', :pickup_instructions => 'instructions') - subscription.subscription_line_items.each { |sli| ex.variants << sli.variant } - end - end - - if proxy.with_proxy_orders - subscription.order_cycles.each do |oc| - subscription.proxy_orders << create(:proxy_order, subscription: subscription, order_cycle: oc) - end - end - end - end - - factory :subscription_line_item, :class => SubscriptionLineItem do - subscription - variant - quantity 1 - end - factory :proxy_order, :class => ProxyOrder do subscription order_cycle { subscription.order_cycles.first } diff --git a/spec/factories/subscription_factory.rb b/spec/factories/subscription_factory.rb new file mode 100644 index 00000000000..3ae136bd747 --- /dev/null +++ b/spec/factories/subscription_factory.rb @@ -0,0 +1,40 @@ +FactoryBot.define do + factory :subscription, :class => Subscription do + shop { create :enterprise } + schedule { create(:schedule, order_cycles: [create(:simple_order_cycle, coordinator: shop)]) } + customer { create(:customer, enterprise: shop) } + bill_address { create(:address, :randomized) } + ship_address { create(:address, :randomized) } + payment_method { create(:payment_method, distributors: [shop]) } + shipping_method { create(:shipping_method, distributors: [shop]) } + begins_at { 1.month.ago } + + transient do + with_items false + with_proxy_orders false + end + + after(:create) do |subscription, proxy| + if proxy.with_items + subscription.subscription_line_items = build_list(:subscription_line_item, 3, subscription: subscription) + subscription.order_cycles.each do |oc| + ex = oc.exchanges.outgoing.find_by_sender_id_and_receiver_id(subscription.shop_id, subscription.shop_id) || + create(:exchange, :order_cycle => oc, :sender => subscription.shop, :receiver => subscription.shop, :incoming => false, :pickup_time => 'time', :pickup_instructions => 'instructions') + subscription.subscription_line_items.each { |sli| ex.variants << sli.variant } + end + end + + if proxy.with_proxy_orders + subscription.order_cycles.each do |oc| + subscription.proxy_orders << create(:proxy_order, subscription: subscription, order_cycle: oc) + end + end + end + end + + factory :subscription_line_item, :class => SubscriptionLineItem do + subscription + variant + quantity 1 + end +end \ No newline at end of file From 91fcb7c7c40b2a5312545181bc6e35b858385556 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 12:12:45 +0100 Subject: [PATCH 03/18] Extract enterprises factories to separate file --- spec/factories.rb | 35 --------------------------- spec/factories/enterprise_factory.rb | 36 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 35 deletions(-) create mode 100644 spec/factories/enterprise_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index 5fa9a03e84d..9bf1daef5ad 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -65,36 +65,6 @@ visible true end - factory :enterprise, :class => Enterprise do - owner { FactoryBot.create :user } - sequence(:name) { |n| "Enterprise #{n}" } - sells 'any' - description 'enterprise' - long_description '

Hello, world!

This is a paragraph.

' - address { FactoryBot.create(:address) } - end - - factory :supplier_enterprise, :parent => :enterprise do - is_primary_producer true - sells "none" - end - - factory :distributor_enterprise, :parent => :enterprise do - is_primary_producer false - sells "any" - - transient do - with_payment_and_shipping false - end - - after(:create) do |enterprise, proxy| - if proxy.with_payment_and_shipping - create(:payment_method, distributors: [enterprise]) - create(:shipping_method, distributors: [enterprise]) - end - end - end - factory :enterprise_relationship do end @@ -258,11 +228,6 @@ end end - factory :distributor_enterprise_with_tax, parent: :distributor_enterprise do - charges_sales_tax { true } - allow_order_changes { true } - end - factory :completed_order_with_fees, parent: :order_with_distributor do transient do payment_fee 5 diff --git a/spec/factories/enterprise_factory.rb b/spec/factories/enterprise_factory.rb new file mode 100644 index 00000000000..0cdfe8c82c4 --- /dev/null +++ b/spec/factories/enterprise_factory.rb @@ -0,0 +1,36 @@ +FactoryBot.define do + factory :enterprise, :class => Enterprise do + owner { FactoryBot.create :user } + sequence(:name) { |n| "Enterprise #{n}" } + sells 'any' + description 'enterprise' + long_description '

Hello, world!

This is a paragraph.

' + address { FactoryBot.create(:address) } + end + + factory :supplier_enterprise, :parent => :enterprise do + is_primary_producer true + sells "none" + end + + factory :distributor_enterprise, :parent => :enterprise do + is_primary_producer false + sells "any" + + transient do + with_payment_and_shipping false + end + + after(:create) do |enterprise, proxy| + if proxy.with_payment_and_shipping + create(:payment_method, distributors: [enterprise]) + create(:shipping_method, distributors: [enterprise]) + end + end + end + + factory :distributor_enterprise_with_tax, parent: :distributor_enterprise do + charges_sales_tax { true } + allow_order_changes { true } + end +end \ No newline at end of file From 06e3328ce20c30112957221db5c08a48b85cb044 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 12:17:24 +0100 Subject: [PATCH 04/18] Extract orders factories to separate file --- spec/factories.rb | 83 -------------------------------- spec/factories/order_factory.rb | 85 +++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 83 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 9bf1daef5ad..1ddd7da3625 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -109,64 +109,6 @@ after(:create) { |c| c.set_preference(:per_kg, 0.5); c.save! } end - factory :order_with_totals_and_distribution, parent: :order_with_distributor do - transient do - shipping_fee 3 - end - - order_cycle { create(:simple_order_cycle) } - - after(:create) do |order, proxy| - product = create(:simple_product) - create(:line_item_with_shipment, shipping_fee: proxy.shipping_fee, order: order, product: product) - order.reload - end - end - - factory :order_with_distributor, :parent => :order do - distributor { create(:distributor_enterprise) } - end - - factory :order_with_taxes, parent: :completed_order_with_totals do - transient do - product_price 0 - tax_rate_amount 0 - tax_rate_name "" - end - - distributor { create(:distributor_enterprise) } - order_cycle { create(:simple_order_cycle) } - - 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) - order.reload - end - end - - factory :order_with_credit_payment, parent: :completed_order_with_totals do - distributor { create(:distributor_enterprise)} - order_cycle { create(:simple_order_cycle) } - - after(:create) do |order| - create(:payment, amount: order.total + 10000, order: order, state: "completed") - order.reload - end - end - - factory :order_without_full_payment, parent: :completed_order_with_totals do - distributor { create(:distributor_enterprise)} - order_cycle { create(:simple_order_cycle) } - - after(:create) do |order| - create(:payment, amount: order.total - 1, order: order, state: "completed") - order.reload - end - end - factory :shipping_method_with, parent: :shipping_method do trait :delivery do require_ship_address { true } @@ -228,31 +170,6 @@ end end - factory :completed_order_with_fees, parent: :order_with_distributor do - transient do - payment_fee 5 - shipping_fee 3 - end - - ship_address { create(:address) } - order_cycle { create(:simple_order_cycle) } - - after(:create) do |order, evaluator| - create(:line_item, order: order) - product = create(:simple_product) - create(:line_item, order: order, product: product) - - payment_calculator = build(:calculator_per_item, preferred_amount: evaluator.payment_fee) - payment_method = create(:payment_method, calculator: payment_calculator) - create(:payment, order: order, amount: order.total, payment_method: payment_method, state: 'checkout') - - create(:shipping_method_with, :shipping_fee, shipping_fee: evaluator.shipping_fee, distributors: [order.distributor]) - - order.reload - while !order.completed? do break unless order.next! end - end - end - factory :line_item_with_shipment, parent: :line_item do transient do shipping_fee 3 diff --git a/spec/factories/order_factory.rb b/spec/factories/order_factory.rb index 4b2a33f3d0c..f6f03f5d2f5 100644 --- a/spec/factories/order_factory.rb +++ b/spec/factories/order_factory.rb @@ -1,3 +1,88 @@ +FactoryBot.define do + factory :order_with_totals_and_distribution, parent: :order_with_distributor do + transient do + shipping_fee 3 + end + + order_cycle { create(:simple_order_cycle) } + + after(:create) do |order, proxy| + product = create(:simple_product) + create(:line_item_with_shipment, shipping_fee: proxy.shipping_fee, order: order, product: product) + order.reload + end + end + + factory :order_with_distributor, :parent => :order do + distributor { create(:distributor_enterprise) } + end + + factory :order_with_taxes, parent: :completed_order_with_totals do + transient do + product_price 0 + tax_rate_amount 0 + tax_rate_name "" + end + + distributor { create(:distributor_enterprise) } + order_cycle { create(:simple_order_cycle) } + + 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) + order.reload + end + end + + factory :order_with_credit_payment, parent: :completed_order_with_totals do + distributor { create(:distributor_enterprise)} + order_cycle { create(:simple_order_cycle) } + + after(:create) do |order| + create(:payment, amount: order.total + 10000, order: order, state: "completed") + order.reload + end + end + + factory :order_without_full_payment, parent: :completed_order_with_totals do + distributor { create(:distributor_enterprise)} + order_cycle { create(:simple_order_cycle) } + + after(:create) do |order| + create(:payment, amount: order.total - 1, order: order, state: "completed") + order.reload + end + end + + factory :completed_order_with_fees, parent: :order_with_distributor do + transient do + payment_fee 5 + shipping_fee 3 + end + + ship_address { create(:address) } + order_cycle { create(:simple_order_cycle) } + + after(:create) do |order, evaluator| + create(:line_item, order: order) + product = create(:simple_product) + create(:line_item, order: order, product: product) + + payment_calculator = build(:calculator_per_item, preferred_amount: evaluator.payment_fee) + payment_method = create(:payment_method, calculator: payment_calculator) + create(:payment, order: order, amount: order.total, payment_method: payment_method, state: 'checkout') + + create(:shipping_method_with, :shipping_fee, shipping_fee: evaluator.shipping_fee, distributors: [order.distributor]) + + order.reload + while !order.completed? do break unless order.next! end + end + end +end + FactoryBot.modify do factory :order do trait :with_line_item do From dc701f55b3b4a130389995302de77f93b03c4770 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 12:23:32 +0100 Subject: [PATCH 05/18] Extract calculators factories to separate file --- spec/factories.rb | 10 ---------- spec/factories/calculator_factory.rb | 11 +++++++++++ 2 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 spec/factories/calculator_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index 1ddd7da3625..a8f3865e845 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -79,11 +79,6 @@ address { FactoryBot.build(:address) } end - sequence(:calculator_amount) - factory :calculator_per_item, class: Spree::Calculator::PerItem do - preferred_amount { generate(:calculator_amount) } - end - factory :enterprise_fee, :class => EnterpriseFee do transient { amount nil } @@ -104,11 +99,6 @@ enterprise_role 'distributor' end - factory :weight_calculator, :class => Calculator::Weight do - after(:build) { |c| c.set_preference(:per_kg, 0.5) } - after(:create) { |c| c.set_preference(:per_kg, 0.5); c.save! } - end - factory :shipping_method_with, parent: :shipping_method do trait :delivery do require_ship_address { true } diff --git a/spec/factories/calculator_factory.rb b/spec/factories/calculator_factory.rb new file mode 100644 index 00000000000..be25fc01072 --- /dev/null +++ b/spec/factories/calculator_factory.rb @@ -0,0 +1,11 @@ +FactoryBot.define do + sequence(:calculator_amount) + factory :calculator_per_item, class: Spree::Calculator::PerItem do + preferred_amount { generate(:calculator_amount) } + end + + factory :weight_calculator, :class => Calculator::Weight do + after(:build) { |c| c.set_preference(:per_kg, 0.5) } + after(:create) { |c| c.set_preference(:per_kg, 0.5); c.save! } + end +end From bd493c392a2704698d62acc7de2a49727d20a5b1 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 12:25:10 +0100 Subject: [PATCH 06/18] Extract shipping_methods factories to separate file --- spec/factories.rb | 42 --------------------- spec/factories/shipping_method_factory.rb | 45 +++++++++++++++++++++++ 2 files changed, 45 insertions(+), 42 deletions(-) create mode 100644 spec/factories/shipping_method_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index a8f3865e845..e3459dca3b5 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -99,43 +99,6 @@ enterprise_role 'distributor' end - factory :shipping_method_with, parent: :shipping_method do - trait :delivery do - require_ship_address { true } - end - - trait :pickup do - require_ship_address { false } - end - - trait :flat_rate do - calculator { Spree::Calculator::FlatRate.new(preferred_amount: 50.0) } - end - - trait :expensive_name do - name { "Shipping" } - description { "Expensive" } - calculator { Spree::Calculator::FlatRate.new(preferred_amount: 100.55) } - end - - trait :distributor do - transient do - distributor { create :enterprise } - end - distributors { [distributor] } - end - - trait :shipping_fee do - transient do - shipping_fee 3 - end - - calculator { build(:calculator_per_item, preferred_amount: shipping_fee) } - require_ship_address { false } - distributors { [create(:distributor_enterprise_with_tax)] } - end - end - factory :shipment_with, class: Spree::Shipment do tracking 'U10000' number '100' @@ -323,11 +286,6 @@ end end - factory :shipping_method, parent: :base_shipping_method do - distributors { [Enterprise.is_distributor.first || FactoryBot.create(:distributor_enterprise)] } - display_on '' - end - factory :address do state { Spree::State.find_by_name 'Victoria' } country { Spree::Country.find_by_name 'Australia' || Spree::Country.first } diff --git a/spec/factories/shipping_method_factory.rb b/spec/factories/shipping_method_factory.rb new file mode 100644 index 00000000000..e37e7d28c6d --- /dev/null +++ b/spec/factories/shipping_method_factory.rb @@ -0,0 +1,45 @@ +FactoryBot.define do + factory :shipping_method_with, parent: :shipping_method do + trait :delivery do + require_ship_address { true } + end + + trait :pickup do + require_ship_address { false } + end + + trait :flat_rate do + calculator { Spree::Calculator::FlatRate.new(preferred_amount: 50.0) } + end + + trait :expensive_name do + name { "Shipping" } + description { "Expensive" } + calculator { Spree::Calculator::FlatRate.new(preferred_amount: 100.55) } + end + + trait :distributor do + transient do + distributor { create :enterprise } + end + distributors { [distributor] } + end + + trait :shipping_fee do + transient do + shipping_fee 3 + end + + calculator { build(:calculator_per_item, preferred_amount: shipping_fee) } + require_ship_address { false } + distributors { [create(:distributor_enterprise_with_tax)] } + 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 '' + end +end From 42f8b2efed46b0ee28f460fea6b469d2309795ec Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 12:30:36 +0100 Subject: [PATCH 07/18] Fix static file path in order_cycle_factory --- spec/factories/order_cycle_factory.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/factories/order_cycle_factory.rb b/spec/factories/order_cycle_factory.rb index b4c5f460da8..24e4055f8e1 100644 --- a/spec/factories/order_cycle_factory.rb +++ b/spec/factories/order_cycle_factory.rb @@ -38,7 +38,7 @@ # Products with images [ex1, ex2].each do |exchange| product = create(:product, supplier: exchange.sender) - image = File.open(File.expand_path('../../app/assets/images/logo-white.png', __FILE__)) + image = File.open(File.expand_path('../../../app/assets/images/logo-white.png', __FILE__)) Spree::Image.create({:viewable_id => product.master.id, :viewable_type => 'Spree::Variant', :alt => "position 1", :attachment => image, :position => 1}) exchange.variants << product.variants.first From 3e10c703bf3ebda3b67a644cdbc43904d8d3be05 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 12:43:07 +0100 Subject: [PATCH 08/18] Extract shipments factories to separate file --- spec/factories.rb | 29 --------------------------- spec/factories/shipment_factory.rb | 32 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 29 deletions(-) create mode 100644 spec/factories/shipment_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index e3459dca3b5..cee0b2bc643 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -99,30 +99,6 @@ enterprise_role 'distributor' end - factory :shipment_with, class: Spree::Shipment do - tracking 'U10000' - number '100' - cost 100.00 - state 'pending' - order - address - stock_location - - trait :shipping_method do - transient do - shipping_method { create(:shipping_method) } - end - - shipping_rates { [Spree::ShippingRate.create(shipping_method: shipping_method, selected: true)] } - - after(:create) do |shipment, evaluator| - 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 - end - factory :line_item_with_shipment, parent: :line_item do transient do shipping_fee 3 @@ -347,9 +323,4 @@ # sets the default value for variant.on_demand backorderable_default false end - - 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 diff --git a/spec/factories/shipment_factory.rb b/spec/factories/shipment_factory.rb new file mode 100644 index 00000000000..dc361889ea9 --- /dev/null +++ b/spec/factories/shipment_factory.rb @@ -0,0 +1,32 @@ +FactoryBot.define do + factory :shipment_with, class: Spree::Shipment do + tracking 'U10000' + number '100' + cost 100.00 + state 'pending' + order + address + stock_location + + trait :shipping_method do + transient do + shipping_method { create(:shipping_method) } + end + + shipping_rates { [Spree::ShippingRate.create(shipping_method: shipping_method, selected: true)] } + + after(:create) do |shipment, evaluator| + 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 + 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 2c8ce6e4e5a9c4f2dead43c1625dd805049af599 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 12:45:55 +0100 Subject: [PATCH 09/18] Extract products factories to separate file --- spec/factories.rb | 65 ----------------------------- spec/factories/product_factory.rb | 68 +++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 65 deletions(-) create mode 100644 spec/factories/product_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index cee0b2bc643..6171743c928 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -123,21 +123,6 @@ end end - factory :taxed_product, :parent => :product do - transient do - tax_rate_amount 0 - tax_rate_name "" - zone nil - end - - tax_category { create(:tax_category) } - - after(:create) do |product, proxy| - raise "taxed_product factory requires a zone" unless proxy.zone - create(:tax_rate, amount: proxy.tax_rate_amount, tax_category: product.tax_category, included_in_price: true, calculator: Spree::Calculator::DefaultTax.new, zone: proxy.zone, name: proxy.tax_rate_name) - end - end - factory :producer_property, class: ProducerProperty do value 'abc123' producer { create(:supplier_enterprise) } @@ -193,59 +178,9 @@ stripe_user_id "abc123" stripe_publishable_key "xyz456" end - - factory :product_with_image, parent: :product do - after(:create) do |product| - image = File.open(Rails.root.join('app', 'assets', 'images', 'logo-white.png')) - Spree::Image.create(attachment: image, viewable_id: product.master.id, viewable_type: 'Spree::Variant') - end - end - - factory :simple_product, parent: :base_product do - transient do - on_demand { false } - on_hand { 5 } - end - after(:create) do |product, evaluator| - product.master.on_demand = evaluator.on_demand - product.master.on_hand = evaluator.on_hand - product.variants.first.on_demand = evaluator.on_demand - product.variants.first.on_hand = evaluator.on_hand - 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 - # Fix product factory name sequence with Kernel.rand so it is not interpreted as a Spree::Product method - # Pull request: https://github.com/spree/spree/pull/1964 - # When this fix has been merged into a version of Spree that we're using, this line can be removed. - sequence(:name) { |n| "Product ##{n} - #{Kernel.rand(9999)}" } - - 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 - factory :variant do transient do on_demand { false } diff --git a/spec/factories/product_factory.rb b/spec/factories/product_factory.rb new file mode 100644 index 00000000000..f4f6e12e7d8 --- /dev/null +++ b/spec/factories/product_factory.rb @@ -0,0 +1,68 @@ +FactoryBot.define do + factory :product_with_image, parent: :product do + after(:create) do |product| + image = File.open(Rails.root.join('app', 'assets', 'images', 'logo-white.png')) + Spree::Image.create(attachment: image, viewable_id: product.master.id, viewable_type: 'Spree::Variant') + end + end + + factory :simple_product, parent: :base_product do + transient do + on_demand { false } + on_hand { 5 } + end + after(:create) do |product, evaluator| + product.master.on_demand = evaluator.on_demand + product.master.on_hand = evaluator.on_hand + product.variants.first.on_demand = evaluator.on_demand + product.variants.first.on_hand = evaluator.on_hand + end + end + + factory :taxed_product, :parent => :product do + transient do + tax_rate_amount 0 + tax_rate_name "" + zone nil + end + + tax_category { create(:tax_category) } + + after(:create) do |product, proxy| + raise "taxed_product factory requires a zone" unless proxy.zone + create(:tax_rate, amount: proxy.tax_rate_amount, tax_category: product.tax_category, included_in_price: true, calculator: Spree::Calculator::DefaultTax.new, zone: proxy.zone, name: proxy.tax_rate_name) + 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 + # Fix product factory name sequence with Kernel.rand so it is not interpreted as a Spree::Product method + # Pull request: https://github.com/spree/spree/pull/1964 + # When this fix has been merged into a version of Spree that we're using, this line can be removed. + sequence(:name) { |n| "Product ##{n} - #{Kernel.rand(9999)}" } + + 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 03fb33ba868e11a8b7c637b37a78a92182569f48 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 12:48:04 +0100 Subject: [PATCH 10/18] Extract variant factory modify to separate file --- spec/factories.rb | 16 ---------------- spec/factories/variant_factory.rb | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 6171743c928..6fd2cd2bd8d 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -181,22 +181,6 @@ end FactoryBot.modify do - factory :variant do - transient do - on_demand { false } - on_hand { 5 } - end - - unit_value 1 - unit_description '' - - after(:create) do |variant, evaluator| - variant.on_demand = evaluator.on_demand - variant.on_hand = evaluator.on_hand - variant.save - end - end - factory :address do state { Spree::State.find_by_name 'Victoria' } country { Spree::Country.find_by_name 'Australia' || Spree::Country.first } diff --git a/spec/factories/variant_factory.rb b/spec/factories/variant_factory.rb index 6eaeb9a26be..a4a7c581352 100644 --- a/spec/factories/variant_factory.rb +++ b/spec/factories/variant_factory.rb @@ -1,5 +1,19 @@ FactoryBot.modify do factory :variant do + transient do + on_demand { false } + on_hand { 5 } + end + + unit_value 1 + unit_description '' + + after(:create) do |variant, evaluator| + 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) } From 8345765ada038825b6c30ff28d1a457b93276cc1 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 12:49:17 +0100 Subject: [PATCH 11/18] Extract tag rules factories to separate file --- spec/factories.rb | 23 ----------------------- spec/factories/tag_rule_factory.rb | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 spec/factories/tag_rule_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index 6fd2cd2bd8d..a3ff0112ad7 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -137,29 +137,6 @@ bill_address { create(:address) } end - factory :filter_order_cycles_tag_rule, class: TagRule::FilterOrderCycles do - enterprise { FactoryBot.create :distributor_enterprise } - end - - factory :filter_shipping_methods_tag_rule, class: TagRule::FilterShippingMethods do - enterprise { FactoryBot.create :distributor_enterprise } - end - - factory :filter_products_tag_rule, class: TagRule::FilterProducts do - enterprise { FactoryBot.create :distributor_enterprise } - end - - factory :filter_payment_methods_tag_rule, class: TagRule::FilterPaymentMethods do - enterprise { FactoryBot.create :distributor_enterprise } - end - - factory :tag_rule, class: TagRule::DiscountOrder do - enterprise { FactoryBot.create :distributor_enterprise } - before(:create) do |tr| - tr.calculator = Spree::Calculator::FlatPercentItemTotal.new(calculable: tr) - end - 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..." diff --git a/spec/factories/tag_rule_factory.rb b/spec/factories/tag_rule_factory.rb new file mode 100644 index 00000000000..9663913820e --- /dev/null +++ b/spec/factories/tag_rule_factory.rb @@ -0,0 +1,24 @@ +FactoryBot.define do + factory :filter_order_cycles_tag_rule, class: TagRule::FilterOrderCycles do + enterprise { FactoryBot.create :distributor_enterprise } + end + + factory :filter_shipping_methods_tag_rule, class: TagRule::FilterShippingMethods do + enterprise { FactoryBot.create :distributor_enterprise } + end + + factory :filter_products_tag_rule, class: TagRule::FilterProducts do + enterprise { FactoryBot.create :distributor_enterprise } + end + + factory :filter_payment_methods_tag_rule, class: TagRule::FilterPaymentMethods do + enterprise { FactoryBot.create :distributor_enterprise } + end + + factory :tag_rule, class: TagRule::DiscountOrder do + enterprise { FactoryBot.create :distributor_enterprise } + before(:create) do |tr| + tr.calculator = Spree::Calculator::FlatPercentItemTotal.new(calculable: tr) + end + end +end From a6b3c26bbee3ee116a33a757c9fb06d7046df7a5 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 12:50:26 +0100 Subject: [PATCH 12/18] Extract users factories to separate file --- spec/factories.rb | 28 ---------------------------- spec/factories/user_factory.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 28 deletions(-) create mode 100644 spec/factories/user_factory.rb diff --git a/spec/factories.rb b/spec/factories.rb index a3ff0112ad7..317d42d5f5e 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -178,34 +178,6 @@ # Prevent inconsistent ordering in specs when all option types have the same (0) position sequence(:position) end - - factory :user do - confirmation_sent_at '1970-01-01 00:00:00' - confirmed_at '1970-01-01 00:00:01' - - before(:create) do |user, evaluator| - if evaluator.confirmation_sent_at - if evaluator.confirmed_at - user.skip_confirmation! - else - user.skip_confirmation_notification! - end - end - end - - after(:create) do |user| - user.spree_roles.clear # Remove admin role - end - end - - factory :admin_user do - confirmation_sent_at '1970-01-01 00:00:00' - confirmed_at '1970-01-01 00:00:01' - - after(:create) do |user| - user.spree_roles << Spree::Role.find_or_create_by_name!('admin') - end - end end FactoryBot.modify do diff --git a/spec/factories/user_factory.rb b/spec/factories/user_factory.rb new file mode 100644 index 00000000000..9e902c202ec --- /dev/null +++ b/spec/factories/user_factory.rb @@ -0,0 +1,29 @@ +FactoryBot.modify do + factory :user do + confirmation_sent_at '1970-01-01 00:00:00' + confirmed_at '1970-01-01 00:00:01' + + before(:create) do |user, evaluator| + if evaluator.confirmation_sent_at + if evaluator.confirmed_at + user.skip_confirmation! + else + user.skip_confirmation_notification! + end + end + end + + after(:create) do |user| + user.spree_roles.clear # Remove admin role + end + end + + factory :admin_user do + confirmation_sent_at '1970-01-01 00:00:00' + confirmed_at '1970-01-01 00:00:01' + + after(:create) do |user| + user.spree_roles << Spree::Role.find_or_create_by_name!('admin') + end + end +end From 79cf03b124b0efe7166d562d3a66e8092adf9266 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 13:01:08 +0100 Subject: [PATCH 13/18] Fix rubocop issues in spec/factories --- spec/factories.rb | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 317d42d5f5e..1279d9ced65 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -19,7 +19,7 @@ factory :classification, class: Spree::Classification do end - factory :exchange, :class => Exchange do + factory :exchange, class: Exchange do incoming false order_cycle { OrderCycle.first || FactoryBot.create(:simple_order_cycle) } sender { incoming ? FactoryBot.create(:enterprise) : order_cycle.coordinator } @@ -31,22 +31,22 @@ order_cycles { [OrderCycle.first || FactoryBot.create(:simple_order_cycle)] } end - factory :proxy_order, :class => ProxyOrder do + factory :proxy_order, class: ProxyOrder do subscription order_cycle { subscription.order_cycles.first } - before(:create) do |proxy_order, proxy| + before(:create) do |proxy_order, _proxy| if proxy_order.order proxy_order.order.update_attribute(:order_cycle_id, proxy_order.order_cycle_id) end end end - factory :variant_override, :class => VariantOverride do - price 77.77 + factory :variant_override, class: VariantOverride do + price 77.77 on_demand false - count_on_hand 11111 + count_on_hand 11_111 default_stock 2000 - resettable false + resettable false trait :on_demand do on_demand true @@ -59,7 +59,7 @@ end end - factory :inventory_item, :class => InventoryItem do + factory :inventory_item, class: InventoryItem do enterprise variant visible true @@ -71,7 +71,7 @@ factory :enterprise_role do end - factory :enterprise_group, :class => EnterpriseGroup do + factory :enterprise_group, class: EnterpriseGroup do name 'Enterprise group' sequence(:permalink) { |n| "group#{n}" } description 'this is a group' @@ -79,7 +79,7 @@ address { FactoryBot.build(:address) } end - factory :enterprise_fee, :class => EnterpriseFee do + factory :enterprise_fee, class: EnterpriseFee do transient { amount nil } sequence(:name) { |n| "Enterprise fee #{n}" } @@ -91,7 +91,7 @@ after(:create) { |ef| ef.calculator.save! } end - factory :adjustment_metadata, :class => AdjustmentMetadata do + factory :adjustment_metadata, class: AdjustmentMetadata do adjustment { FactoryBot.create(:adjustment) } enterprise { FactoryBot.create(:distributor_enterprise) } fee_name 'fee' @@ -109,7 +109,8 @@ if shipment.nil? shipping_method = create(:shipping_method_with, :shipping_fee, shipping_fee: shipping_fee) shipping_method.distributors << order.distributor if order.distributor - shipment = create(:shipment_with, :shipping_method, shipping_method: shipping_method, order: order) + shipment = create(:shipment_with, :shipping_method, shipping_method: shipping_method, + order: order) end shipment end @@ -129,7 +130,7 @@ property end - factory :customer, :class => Customer do + factory :customer, class: Customer do email { Faker::Internet.email } enterprise code { SecureRandom.base64(150) } @@ -143,7 +144,7 @@ gateway_payment_profile_id "card_1EY..." end - factory :stripe_payment_method, :class => Spree::Gateway::StripeConnect do + factory :stripe_payment_method, class: Spree::Gateway::StripeConnect do name 'Stripe' environment 'test' distributors { [FactoryBot.create(:enterprise)] } @@ -165,7 +166,11 @@ factory :payment do transient do - distributor { order.distributor || Enterprise.is_distributor.first || FactoryBot.create(:distributor_enterprise) } + distributor { + order.distributor || + Enterprise.is_distributor.first || + FactoryBot.create(:distributor_enterprise) + } end payment_method { FactoryBot.create(:payment_method, distributors: [distributor]) } end From 59593c824a4f9805a317a72eac6b963ad7b31caf Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 13:10:53 +0100 Subject: [PATCH 14/18] Fix rubocop issues in different factories --- spec/factories.rb | 2 +- spec/factories/calculator_factory.rb | 2 +- spec/factories/enterprise_factory.rb | 2 +- spec/factories/order_cycle_factory.rb | 57 +++++++++++++++++--------- spec/factories/subscription_factory.rb | 4 +- 5 files changed, 42 insertions(+), 25 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 1279d9ced65..aa0b05c8bbe 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -116,7 +116,7 @@ end end - factory :zone_with_member, :parent => :zone do + factory :zone_with_member, parent: :zone do default_tax true after(:create) do |zone| diff --git a/spec/factories/calculator_factory.rb b/spec/factories/calculator_factory.rb index be25fc01072..5cbe8ee1821 100644 --- a/spec/factories/calculator_factory.rb +++ b/spec/factories/calculator_factory.rb @@ -4,7 +4,7 @@ preferred_amount { generate(:calculator_amount) } end - factory :weight_calculator, :class => Calculator::Weight do + factory :weight_calculator, class: Calculator::Weight do after(:build) { |c| c.set_preference(:per_kg, 0.5) } after(:create) { |c| c.set_preference(:per_kg, 0.5); c.save! } end diff --git a/spec/factories/enterprise_factory.rb b/spec/factories/enterprise_factory.rb index 0cdfe8c82c4..aaf068082eb 100644 --- a/spec/factories/enterprise_factory.rb +++ b/spec/factories/enterprise_factory.rb @@ -1,5 +1,5 @@ FactoryBot.define do - factory :enterprise, :class => Enterprise do + factory :enterprise, class: Enterprise do owner { FactoryBot.create :user } sequence(:name) { |n| "Enterprise #{n}" } sells 'any' diff --git a/spec/factories/order_cycle_factory.rb b/spec/factories/order_cycle_factory.rb index 24e4055f8e1..5530d43ff56 100644 --- a/spec/factories/order_cycle_factory.rb +++ b/spec/factories/order_cycle_factory.rb @@ -1,5 +1,5 @@ FactoryBot.define do - factory :order_cycle, :parent => :simple_order_cycle do + factory :order_cycle, parent: :simple_order_cycle do coordinator_fees { [create(:enterprise_fee, enterprise: coordinator)] } after(:create) do |oc| @@ -8,12 +8,12 @@ supplier2 = create(:supplier_enterprise) # Incoming Exchanges - ex1 = create(:exchange, :order_cycle => oc, :incoming => true, - :sender => supplier1, :receiver => oc.coordinator, - :receival_instructions => 'instructions 0') - ex2 = create(:exchange, :order_cycle => oc, :incoming => true, - :sender => supplier2, :receiver => oc.coordinator, - :receival_instructions => 'instructions 1') + ex1 = create(:exchange, order_cycle: oc, incoming: true, + sender: supplier1, receiver: oc.coordinator, + receival_instructions: 'instructions 0') + ex2 = create(:exchange, order_cycle: oc, incoming: true, + sender: supplier2, receiver: oc.coordinator, + receival_instructions: 'instructions 1') ExchangeFee.create!(exchange: ex1, enterprise_fee: create(:enterprise_fee, enterprise: ex1.sender)) ExchangeFee.create!(exchange: ex2, @@ -24,12 +24,12 @@ distributor2 = create(:distributor_enterprise) # Outgoing Exchanges - ex3 = create(:exchange, :order_cycle => oc, :incoming => false, - :sender => oc.coordinator, :receiver => distributor1, - :pickup_time => 'time 0', :pickup_instructions => 'instructions 0') - ex4 = create(:exchange, :order_cycle => oc, :incoming => false, - :sender => oc.coordinator, :receiver => distributor2, - :pickup_time => 'time 1', :pickup_instructions => 'instructions 1') + ex3 = create(:exchange, order_cycle: oc, incoming: false, + sender: oc.coordinator, receiver: distributor1, + pickup_time: 'time 0', pickup_instructions: 'instructions 0') + ex4 = create(:exchange, order_cycle: oc, incoming: false, + sender: oc.coordinator, receiver: distributor2, + pickup_time: 'time 1', pickup_instructions: 'instructions 1') ExchangeFee.create!(exchange: ex3, enterprise_fee: create(:enterprise_fee, enterprise: ex3.receiver)) ExchangeFee.create!(exchange: ex4, @@ -38,8 +38,14 @@ # Products with images [ex1, ex2].each do |exchange| product = create(:product, supplier: exchange.sender) - image = File.open(File.expand_path('../../../app/assets/images/logo-white.png', __FILE__)) - Spree::Image.create({:viewable_id => product.master.id, :viewable_type => 'Spree::Variant', :alt => "position 1", :attachment => image, :position => 1}) + image = File.open(File.expand_path('../../app/assets/images/logo-white.png', __dir__)) + Spree::Image.create( + viewable_id: product.master.id, + viewable_type: 'Spree::Variant', + alt: "position 1", + attachment: image, + position: 1 + ) exchange.variants << product.variants.first end @@ -54,12 +60,14 @@ factory :order_cycle_with_overrides, parent: :order_cycle do after(:create) do |oc| oc.variants.each do |variant| - create(:variant_override, variant: variant, hub: oc.distributors.first, price: variant.price + 100) + create(:variant_override, variant: variant, + hub: oc.distributors.first, + price: variant.price + 100) end end end - factory :simple_order_cycle, :class => OrderCycle do + factory :simple_order_cycle, class: OrderCycle do sequence(:name) { |n| "Order Cycle #{n}" } orders_open_at { 1.day.ago } @@ -75,12 +83,21 @@ after(:create) do |oc, proxy| proxy.suppliers.each do |supplier| - ex = create(:exchange, :order_cycle => oc, :sender => supplier, :receiver => oc.coordinator, :incoming => true, :receival_instructions => 'instructions') + ex = create(:exchange, order_cycle: oc, + sender: supplier, + receiver: oc.coordinator, + incoming: true, + receival_instructions: 'instructions') proxy.variants.each { |v| ex.variants << v } end proxy.distributors.each do |distributor| - ex = create(:exchange, :order_cycle => oc, :sender => oc.coordinator, :receiver => distributor, :incoming => false, :pickup_time => 'time', :pickup_instructions => 'instructions') + ex = create(:exchange, order_cycle: oc, + sender: oc.coordinator, + receiver: distributor, + incoming: false, + pickup_time: 'time', + pickup_instructions: 'instructions') proxy.variants.each { |v| ex.variants << v } end end @@ -105,4 +122,4 @@ orders_open_at { 2.weeks.ago } orders_close_at { 1.week.ago } end -end \ No newline at end of file +end diff --git a/spec/factories/subscription_factory.rb b/spec/factories/subscription_factory.rb index 3ae136bd747..a7a4c0a4f24 100644 --- a/spec/factories/subscription_factory.rb +++ b/spec/factories/subscription_factory.rb @@ -1,5 +1,5 @@ FactoryBot.define do - factory :subscription, :class => Subscription do + factory :subscription, class: Subscription do shop { create :enterprise } schedule { create(:schedule, order_cycles: [create(:simple_order_cycle, coordinator: shop)]) } customer { create(:customer, enterprise: shop) } @@ -32,7 +32,7 @@ end end - factory :subscription_line_item, :class => SubscriptionLineItem do + factory :subscription_line_item, class: SubscriptionLineItem do subscription variant quantity 1 From 6efb71d903afc1cafdd825be39e73eb27076dc6a Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 13:42:27 +0100 Subject: [PATCH 15/18] Fix more rubocop issues in spec/factories --- spec/factories/calculator_factory.rb | 2 +- spec/factories/enterprise_factory.rb | 6 +++--- spec/factories/order_factory.rb | 25 +++++++++++++++-------- spec/factories/product_factory.rb | 15 ++++++++++---- spec/factories/shipment_factory.rb | 12 +++++++---- spec/factories/shipping_method_factory.rb | 2 +- spec/factories/subscription_factory.rb | 20 +++++++++++++----- 7 files changed, 56 insertions(+), 26 deletions(-) diff --git a/spec/factories/calculator_factory.rb b/spec/factories/calculator_factory.rb index 5cbe8ee1821..25dd9b0a13f 100644 --- a/spec/factories/calculator_factory.rb +++ b/spec/factories/calculator_factory.rb @@ -7,5 +7,5 @@ factory :weight_calculator, class: Calculator::Weight do after(:build) { |c| c.set_preference(:per_kg, 0.5) } after(:create) { |c| c.set_preference(:per_kg, 0.5); c.save! } - end + end end diff --git a/spec/factories/enterprise_factory.rb b/spec/factories/enterprise_factory.rb index aaf068082eb..ea93a818aab 100644 --- a/spec/factories/enterprise_factory.rb +++ b/spec/factories/enterprise_factory.rb @@ -8,12 +8,12 @@ address { FactoryBot.create(:address) } end - factory :supplier_enterprise, :parent => :enterprise do + factory :supplier_enterprise, parent: :enterprise do is_primary_producer true sells "none" end - factory :distributor_enterprise, :parent => :enterprise do + factory :distributor_enterprise, parent: :enterprise do is_primary_producer false sells "any" @@ -33,4 +33,4 @@ charges_sales_tax { true } allow_order_changes { true } end -end \ No newline at end of file +end diff --git a/spec/factories/order_factory.rb b/spec/factories/order_factory.rb index f6f03f5d2f5..bab9767ca0f 100644 --- a/spec/factories/order_factory.rb +++ b/spec/factories/order_factory.rb @@ -8,12 +8,14 @@ after(:create) do |order, proxy| product = create(:simple_product) - create(:line_item_with_shipment, shipping_fee: proxy.shipping_fee, order: order, product: product) + create(:line_item_with_shipment, shipping_fee: proxy.shipping_fee, + order: order, + product: product) order.reload end end - factory :order_with_distributor, :parent => :order do + factory :order_with_distributor, parent: :order do distributor { create(:distributor_enterprise) } end @@ -31,24 +33,27 @@ 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) + 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) order.reload end end factory :order_with_credit_payment, parent: :completed_order_with_totals do - distributor { create(:distributor_enterprise)} + distributor { create(:distributor_enterprise) } order_cycle { create(:simple_order_cycle) } after(:create) do |order| - create(:payment, amount: order.total + 10000, order: order, state: "completed") + create(:payment, amount: order.total + 10_000, order: order, state: "completed") order.reload end end factory :order_without_full_payment, parent: :completed_order_with_totals do - distributor { create(:distributor_enterprise)} + distributor { create(:distributor_enterprise) } order_cycle { create(:simple_order_cycle) } after(:create) do |order| @@ -73,9 +78,13 @@ payment_calculator = build(:calculator_per_item, preferred_amount: evaluator.payment_fee) payment_method = create(:payment_method, calculator: payment_calculator) - create(:payment, order: order, amount: order.total, payment_method: payment_method, state: 'checkout') + create(:payment, order: order, + amount: order.total, + payment_method: payment_method, + state: 'checkout') - create(:shipping_method_with, :shipping_fee, shipping_fee: evaluator.shipping_fee, distributors: [order.distributor]) + create(:shipping_method_with, :shipping_fee, shipping_fee: evaluator.shipping_fee, + distributors: [order.distributor]) order.reload while !order.completed? do break unless order.next! end diff --git a/spec/factories/product_factory.rb b/spec/factories/product_factory.rb index f4f6e12e7d8..9c97b92f7bb 100644 --- a/spec/factories/product_factory.rb +++ b/spec/factories/product_factory.rb @@ -2,7 +2,9 @@ factory :product_with_image, parent: :product do after(:create) do |product| image = File.open(Rails.root.join('app', 'assets', 'images', 'logo-white.png')) - Spree::Image.create(attachment: image, viewable_id: product.master.id, viewable_type: 'Spree::Variant') + Spree::Image.create(attachment: image, + viewable_id: product.master.id, + viewable_type: 'Spree::Variant') end end @@ -18,8 +20,8 @@ product.variants.first.on_hand = evaluator.on_hand end end - - factory :taxed_product, :parent => :product do + + factory :taxed_product, parent: :product do transient do tax_rate_amount 0 tax_rate_name "" @@ -30,7 +32,12 @@ after(:create) do |product, proxy| raise "taxed_product factory requires a zone" unless proxy.zone - create(:tax_rate, amount: proxy.tax_rate_amount, tax_category: product.tax_category, included_in_price: true, calculator: Spree::Calculator::DefaultTax.new, zone: proxy.zone, name: proxy.tax_rate_name) + create(:tax_rate, amount: proxy.tax_rate_amount, + tax_category: product.tax_category, + included_in_price: true, + calculator: Spree::Calculator::DefaultTax.new, + zone: proxy.zone, + name: proxy.tax_rate_name) end end end diff --git a/spec/factories/shipment_factory.rb b/spec/factories/shipment_factory.rb index dc361889ea9..aa4002f9c67 100644 --- a/spec/factories/shipment_factory.rb +++ b/spec/factories/shipment_factory.rb @@ -13,11 +13,15 @@ shipping_method { create(:shipping_method) } end - shipping_rates { [Spree::ShippingRate.create(shipping_method: shipping_method, selected: true)] } + shipping_rates { + [Spree::ShippingRate.create(shipping_method: shipping_method, selected: true)] + } - after(:create) do |shipment, evaluator| + after(:create) do |shipment, _evaluator| shipment.order.line_items.each do |line_item| - line_item.quantity.times { shipment.inventory_units.create(variant_id: line_item.variant_id) } + line_item.quantity.times { + shipment.inventory_units.create(variant_id: line_item.variant_id) + } end end end @@ -27,6 +31,6 @@ 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)} + initialize_with { Spree::Shipment.find_or_create_by_order_id(order.id) } end end diff --git a/spec/factories/shipping_method_factory.rb b/spec/factories/shipping_method_factory.rb index e37e7d28c6d..4d4d7c710e7 100644 --- a/spec/factories/shipping_method_factory.rb +++ b/spec/factories/shipping_method_factory.rb @@ -1,5 +1,5 @@ FactoryBot.define do - factory :shipping_method_with, parent: :shipping_method do + factory :shipping_method_with, parent: :shipping_method do trait :delivery do require_ship_address { true } end diff --git a/spec/factories/subscription_factory.rb b/spec/factories/subscription_factory.rb index a7a4c0a4f24..009f9ef8024 100644 --- a/spec/factories/subscription_factory.rb +++ b/spec/factories/subscription_factory.rb @@ -16,17 +16,27 @@ after(:create) do |subscription, proxy| if proxy.with_items - subscription.subscription_line_items = build_list(:subscription_line_item, 3, subscription: subscription) + subscription.subscription_line_items = build_list(:subscription_line_item, + 3, + subscription: subscription) subscription.order_cycles.each do |oc| - ex = oc.exchanges.outgoing.find_by_sender_id_and_receiver_id(subscription.shop_id, subscription.shop_id) || - create(:exchange, :order_cycle => oc, :sender => subscription.shop, :receiver => subscription.shop, :incoming => false, :pickup_time => 'time', :pickup_instructions => 'instructions') + ex = oc.exchanges.outgoing.find_by_sender_id_and_receiver_id( + subscription.shop_id, subscription.shop_id + ) + ex ||= create(:exchange, order_cycle: oc, + sender: subscription.shop, + receiver: subscription.shop, + incoming: false, + pickup_time: 'time', + pickup_instructions: 'instructions') subscription.subscription_line_items.each { |sli| ex.variants << sli.variant } end end if proxy.with_proxy_orders subscription.order_cycles.each do |oc| - subscription.proxy_orders << create(:proxy_order, subscription: subscription, order_cycle: oc) + subscription.proxy_orders << create(:proxy_order, subscription: subscription, + order_cycle: oc) end end end @@ -37,4 +47,4 @@ variant quantity 1 end -end \ No newline at end of file +end From 3259db69f062004631b9fb2c15c64d3c54dd935d Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 13:43:30 +0100 Subject: [PATCH 16/18] Remove unnecessary code, this code as been accepted in spree code base and included in spree v1.2 --- spec/factories/product_factory.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/spec/factories/product_factory.rb b/spec/factories/product_factory.rb index 9c97b92f7bb..897d95d477c 100644 --- a/spec/factories/product_factory.rb +++ b/spec/factories/product_factory.rb @@ -57,11 +57,6 @@ end factory :base_product do - # Fix product factory name sequence with Kernel.rand so it is not interpreted as a Spree::Product method - # Pull request: https://github.com/spree/spree/pull/1964 - # When this fix has been merged into a version of Spree that we're using, this line can be removed. - sequence(:name) { |n| "Product ##{n} - #{Kernel.rand(9999)}" } - supplier { Enterprise.is_primary_producer.first || FactoryBot.create(:supplier_enterprise) } primary_taxon { Spree::Taxon.first || FactoryBot.create(:taxon) } From 5ca382be42d953a78b06326fa54c97341d676f2b Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 13:55:47 +0100 Subject: [PATCH 17/18] Remove spec/factories.rb from rubocop exceptions, it doesnt look like a problem any longer --- .rubocop_styleguide.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.rubocop_styleguide.yml b/.rubocop_styleguide.yml index a96c96b3062..c2b08f61c06 100644 --- a/.rubocop_styleguide.yml +++ b/.rubocop_styleguide.yml @@ -13,8 +13,6 @@ AllCops: - 'script/**/*' - 'vendor/**/*' - 'node_modules/**/*' - # The parser gem fails to parse this file with out current Ruby version. - - 'spec/factories.rb' # Excluding: inadequate Naming/FileName rule rejects GemFile name with camelcase - 'engines/web/Gemfile' From 13d41dc1aaf7b15052614a845b9e303ba20f016b Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 14 Jun 2019 13:59:24 +0100 Subject: [PATCH 18/18] Remove duplicate modify entry in factories --- spec/factories.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index aa0b05c8bbe..450984f89c8 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -183,9 +183,7 @@ # Prevent inconsistent ordering in specs when all option types have the same (0) position sequence(:position) end -end -FactoryBot.modify do factory :stock_location, class: Spree::StockLocation do # keeps the test stock_location unique initialize_with { DefaultStockLocation.find_or_create }