Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bye bye Spree] Make OFN independent of all spree spec factories #5943

Merged
merged 36 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
9390fd6
Remove all factories from spree so we can see exactly what factories …
luisramos0 Aug 22, 2020
e561bcc
Bring address factory from spree_core and merge with modification
luisramos0 Aug 23, 2020
172a12d
Bring credit_card factory from spree_core and merge with modification
luisramos0 Aug 23, 2020
aa90546
Bring payment factory from spree_core and merge with modification
luisramos0 Aug 23, 2020
e1fb13b
Bring payment method factory from spree_core and merge with modification
luisramos0 Aug 23, 2020
6819042
Bring options factories from spree_core and merge with modification
luisramos0 Aug 23, 2020
313e6e2
Bring stock_location factory from spree_core and merge with modification
luisramos0 Aug 23, 2020
53a8fec
Bring shipping_category factory from spree_core and merge with modifi…
luisramos0 Aug 23, 2020
4ac6664
Replace FactoryGirl with FactoryBot weverywhere
luisramos0 Aug 23, 2020
8d74c69
Bring order factory from spree_core and merge with modification
luisramos0 Aug 23, 2020
859d34c
Remove nice trick to reuse traits by copy pasting the traits to each of
luisramos0 Aug 23, 2020
acb1c41
Bring shipment factory from spree_core and merge with modification
luisramos0 Aug 23, 2020
ad8c41e
Bring shipping_method factory from spree_core and merge with modifica…
luisramos0 Aug 23, 2020
204e397
Bring user factory from spree_core and merge with modification
luisramos0 Aug 23, 2020
f6f6d9b
Bring variant factory from spree_core and merge with modification
luisramos0 Aug 23, 2020
4dd1a52
Bring random_email and random description from spree_core factories
luisramos0 Aug 23, 2020
9e7e63c
Bring line_item factory from spree_core
luisramos0 Aug 23, 2020
e2a8654
Bring adjustment factory from spree_core
luisramos0 Aug 23, 2020
ee65b4b
Bring taxon and taxonomy factories from spree_core
luisramos0 Aug 23, 2020
aa00756
Bring product factory from spree_core and merge with modification
luisramos0 Aug 23, 2020
6d05de7
Fix problem with product shipping category factory
luisramos0 Aug 23, 2020
6d9250c
Bring tax category factory from spree_core and fix a problem with the…
luisramos0 Aug 23, 2020
a432bbf
Fix problem with shipping method's shipping category creation
luisramos0 Aug 23, 2020
f56fa0b
Bring calculator factories from spree_core
luisramos0 Aug 23, 2020
2921994
Fix problem with shipment's stock location creation
luisramos0 Aug 23, 2020
2abbfad
Remove the need to import both :role and :state factory to OFN
luisramos0 Aug 23, 2020
4048957
Bring the final batch of factories from spree_core
luisramos0 Aug 23, 2020
1ddda92
Move factories from generic factories file to their respective factor…
luisramos0 Aug 23, 2020
cd22361
Fix shipping category factory
luisramos0 Aug 23, 2020
4de4cc6
Fix stock location factory
luisramos0 Aug 23, 2020
f627741
Simplify stock location factory and fix related issues in several fac…
luisramos0 Aug 23, 2020
5568fd5
Fix problem in order factory
luisramos0 Aug 23, 2020
41d67d8
Fix estimator spec
luisramos0 Aug 23, 2020
274cdef
Remove commented code
luisramos0 Aug 26, 2020
f9aac02
Run rubocop -a
luisramos0 Aug 26, 2020
94d1d5f
Run rubocop -a on spec/factories
luisramos0 Aug 26, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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) }
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/api/shipments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
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,
Expand Down
126 changes: 14 additions & 112 deletions spec/factories.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
require 'ffaker'
require 'spree/testing_support/factories'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the magic of this PR: no spree factories used.


# 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

Expand Down Expand Up @@ -97,6 +85,16 @@
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
Expand All @@ -107,35 +105,6 @@
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) }
Expand All @@ -150,76 +119,9 @@
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
17 changes: 15 additions & 2 deletions spec/factories/address_factory.rb
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down
11 changes: 11 additions & 0 deletions spec/factories/adjustment_factory.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 0 additions & 18 deletions spec/factories/calculated_adjustment_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,3 @@
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
8 changes: 8 additions & 0 deletions spec/factories/calculator_factory.rb
Original file line number Diff line number Diff line change
@@ -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) }
Expand Down
11 changes: 11 additions & 0 deletions spec/factories/country_factory.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions spec/factories/credit_card_factory.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions spec/factories/inventory_unit_factory.rb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions spec/factories/line_item_factory.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions spec/factories/options_factory.rb
Original file line number Diff line number Diff line change
@@ -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