From 8d74c69402da30a638dde59e81e9ebb5ae99c1ed Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 23 Aug 2020 13:36:29 +0100 Subject: [PATCH] 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