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