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/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.rb b/spec/factories.rb index 455fc9b9a4..1b12e67fbb 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,22 +1,10 @@ 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 + sequence(:random_string) { Faker::Lorem.sentence } + 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 @@ -97,6 +85,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 @@ -107,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) } @@ -150,76 +119,9 @@ 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" stripe_publishable_key "xyz456" end 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 - - 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 - - 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 } - - # 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' } - end -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 } diff --git a/spec/factories/adjustment_factory.rb b/spec/factories/adjustment_factory.rb new file mode 100644 index 0000000000..239d92d6c0 --- /dev/null +++ b/spec/factories/adjustment_factory.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +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 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/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) } diff --git a/spec/factories/country_factory.rb b/spec/factories/country_factory.rb new file mode 100644 index 0000000000..c3cd8e2e46 --- /dev/null +++ b/spec/factories/country_factory.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +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/credit_card_factory.rb b/spec/factories/credit_card_factory.rb new file mode 100644 index 0000000000..27976a55c6 --- /dev/null +++ b/spec/factories/credit_card_factory.rb @@ -0,0 +1,23 @@ +# 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 +end + +FactoryBot.define do + factory :credit_card, class: TestCard do + verification_value 123 + month 12 + year { Time.zone.now.year + 1 } + number '4111111111111111' + + 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/inventory_unit_factory.rb b/spec/factories/inventory_unit_factory.rb new file mode 100644 index 0000000000..47c0bd2aae --- /dev/null +++ b/spec/factories/inventory_unit_factory.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +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/line_item_factory.rb b/spec/factories/line_item_factory.rb new file mode 100644 index 0000000000..f6de86257b --- /dev/null +++ b/spec/factories/line_item_factory.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :line_item, class: Spree::LineItem do + quantity 1 + price { BigDecimal('10.00') } + 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/options_factory.rb b/spec/factories/options_factory.rb new file mode 100644 index 0000000000..1d5b24f2b7 --- /dev/null +++ b/spec/factories/options_factory.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +FactoryBot.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 diff --git a/spec/factories/order_factory.rb b/spec/factories/order_factory.rb index 282d4772c1..fde8605c1c 100644 --- a/spec/factories/order_factory.rb +++ b/spec/factories/order_factory.rb @@ -1,4 +1,101 @@ 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.zone.now } + + distributor { create(:distributor_enterprise) } + + after(:create, &:refresh_shipment_rates) + + 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 @@ -24,6 +121,7 @@ FactoryBot.define do product_price 0 tax_rate_amount 0 tax_rate_name "" + zone { create(:zone_with_member) } end distributor { create(:distributor_enterprise) } @@ -31,13 +129,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 @@ -99,44 +195,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 { FactoryGirl.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 diff --git a/spec/factories/payment_factory.rb b/spec/factories/payment_factory.rb new file mode 100644 index 0000000000..3858b8e913 --- /dev/null +++ b/spec/factories/payment_factory.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +FactoryBot.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 diff --git a/spec/factories/payment_method_factory.rb b/spec/factories/payment_method_factory.rb new file mode 100644 index 0000000000..2eb5a674d3 --- /dev/null +++ b/spec/factories/payment_method_factory.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :payment_method, class: Spree::PaymentMethod::Check do + name 'Check' + 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 + 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/product_factory.rb b/spec/factories/product_factory.rb index 3ca3fdb77e..f13cad00ba 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 { DefaultShippingCategory.find_or_create } + + # 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 diff --git a/spec/factories/product_option_type_factory.rb b/spec/factories/product_option_type_factory.rb new file mode 100644 index 0000000000..22a8b6f96a --- /dev/null +++ b/spec/factories/product_option_type_factory.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +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..7db00a14ff --- /dev/null +++ b/spec/factories/product_property_factory.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +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..6cf7ee09b9 --- /dev/null +++ b/spec/factories/property_factory.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :property, class: Spree::Property do + name 'baseball_cap_color' + presentation 'cap color' + end +end diff --git a/spec/factories/shipment_factory.rb b/spec/factories/shipment_factory.rb index 75bc54654d..ef167d5837 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 { Spree::StockLocation.first || create(: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' @@ -6,7 +27,7 @@ FactoryBot.define do state 'pending' order address - stock_location + stock_location { Spree::StockLocation.first || create(:stock_location) } trait :shipping_method do transient do @@ -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 diff --git a/spec/factories/shipping_category_factory.rb b/spec/factories/shipping_category_factory.rb new file mode 100644 index 0000000000..9749d3a719 --- /dev/null +++ b/spec/factories/shipping_category_factory.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +FactoryBot.define 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 87cec39be9..aae807d226 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 << DefaultShippingCategory.find_or_create + 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,11 +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 { [] } - end -end diff --git a/spec/factories/stock_factory.rb b/spec/factories/stock_factory.rb new file mode 100644 index 0000000000..b501401507 --- /dev/null +++ b/spec/factories/stock_factory.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +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 diff --git a/spec/factories/stock_location_factory.rb b/spec/factories/stock_location_factory.rb new file mode 100644 index 0000000000..48fc14d772 --- /dev/null +++ b/spec/factories/stock_location_factory.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :stock_location, class: Spree::StockLocation do + # keeps the test stock_location unique + initialize_with { Spree::StockLocation.first || DefaultStockLocation.find_or_create } + + 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 + end +end diff --git a/spec/factories/tax_category_factory.rb b/spec/factories/tax_category_factory.rb new file mode 100644 index 0000000000..0cb0f3af4a --- /dev/null +++ b/spec/factories/tax_category_factory.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :tax_category, class: Spree::TaxCategory do + 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 new file mode 100644 index 0000000000..ae1168ee3f --- /dev/null +++ b/spec/factories/tax_rate_factory.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :tax_rate, class: Spree::TaxRate do + zone + amount 100.00 + tax_category + end +end diff --git a/spec/factories/taxon_factory.rb b/spec/factories/taxon_factory.rb new file mode 100644 index 0000000000..e02e87a9e9 --- /dev/null +++ b/spec/factories/taxon_factory.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +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..89d6d317fe --- /dev/null +++ b/spec/factories/taxonomy_factory.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :taxonomy, class: Spree::Taxonomy do + name 'Brand' + end +end diff --git a/spec/factories/user_factory.rb b/spec/factories/user_factory.rb index 5f9a367885..3896b7812c 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' @@ -22,14 +32,13 @@ FactoryBot.modify do user.enterprises << proxy.enterprises 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_or_create_by!(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 diff --git a/spec/factories/variant_factory.rb b/spec/factories/variant_factory.rb index a4a7c58135..5d477a5603 100644 --- a/spec/factories/variant_factory.rb +++ b/spec/factories/variant_factory.rb @@ -1,46 +1,65 @@ -FactoryBot.modify do - factory :variant do - transient do - on_demand { false } - on_hand { 5 } - end +FactoryBot.define do + sequence(:random_float) { BigDecimal("#{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 + 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 diff --git a/spec/factories/zone_factory.rb b/spec/factories/zone_factory.rb new file mode 100644 index 0000000000..1993f3c913 --- /dev/null +++ b/spec/factories/zone_factory.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :zone, class: Spree::Zone 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 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/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" diff --git a/spec/models/spree/stock_item_spec.rb b/spec/models/spree/stock_item_spec.rb index 9c9b233986..6dccf13665 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 } diff --git a/spec/services/default_stock_location_spec.rb b/spec/services/default_stock_location_spec.rb index 3bd3a1eb26..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) @@ -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, 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(