Skip to content

Commit

Permalink
Refactor more factories
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Altmann committed Oct 24, 2015
1 parent a455a50 commit fb67692
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 67 deletions.
19 changes: 10 additions & 9 deletions test/factories/articles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
require 'ffaker'

FactoryGirl.define do
factory :article, aliases: [:appended_object] do
seller # alias for User -> see spec/factories/users.rb
factory :article do
association :seller, factory: :user
categories { |c| [c.association(:category)] }
title { Faker::Lorem.words(rand(3..5)).join(' ').titleize }
content { Faker::Lorem.paragraph(rand(7) + 1) }
Expand Down Expand Up @@ -147,31 +147,32 @@
payment_voucher true
payment_details { Faker::Lorem.paragraph(rand(2..5)) }

seller { FactoryGirl.create :seller, :paypal_data }
association :seller, factory: [:user, :paypal_data]
end

trait :with_private_user do
seller { FactoryGirl.create :private_user, :paypal_data } # adding paypal data because it is needed for with_all_transports
association :seller, factory: [:private_user, :paypal_data]
end

trait :with_legal_entity do
vat { [7, 19].sample }
seller { FactoryGirl.create :legal_entity, :paypal_data }
association :seller, factory: [:legal_entity, :paypal_data]
end

trait :with_ngo do
vat { [7, 19].sample }
seller { FactoryGirl.create :legal_entity, :paypal_data, ngo: true }
association :seller, factory: [:legal_entity, :paypal_data], ngo: true
end

trait :with_friendly_percent do
friendly_percent 75
friendly_percent_organisation { FactoryGirl.create :legal_entity, ngo: true }
association :friendly_percent_organisation, factory: :legal_entity, ngo: true
end

trait :with_friendly_percent_and_missing_bank_data do
friendly_percent 75
friendly_percent_organisation { FactoryGirl.create :legal_entity, :missing_bank_data, ngo: true }
association :friendly_percent_organisation, factory: [:legal_entity, :missing_bank_data],
ngo: true
end

trait :simple_fair do
Expand Down Expand Up @@ -201,7 +202,7 @@
end

trait :with_discount do
discount { FactoryGirl.create :discount }
association :discount
end

trait :invalid do
Expand Down
125 changes: 67 additions & 58 deletions test/factories/line_item_groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,101 @@

FactoryGirl.define do
factory :line_item_group do
cart
seller
buyer
association :cart
association :seller, factory: :user
association :buyer, factory: :user
message 'MyText'
tos_accepted false
transport_address { buyer.standard_address }
payment_address { FactoryGirl.create :address, user_id: buyer.id }
purchase_id 'F00000012'

transient do
sold { false }
sold false
articles { [FactoryGirl.create(:article, seller: seller)] }
end

after(:create) do |line_item_group, evaluator|
evaluator.articles.each do |article|
line_item_group.line_items << FactoryGirl.create(:line_item, article: article, line_item_group: line_item_group)
line_item_group
.line_items << FactoryGirl.create(:line_item, article: article,
line_item_group: line_item_group)
end
end
end

trait :with_business_transactions do
transient do
articles_attributes []
articles { [] } # dont override
create_line_items false
traits [[:paypal, :transport_type1], [:invoice, :transport_type2]]
build_or_create_bts { sold ? :create : :build }
trait :sold do
transient do
sold true
end
sold_at { Time.now }
tos_accepted true
end

seller { FactoryGirl.create(:seller, :paypal_data) } # that paypal can be selected
trait :with_business_transactions do
association :seller, factory: [:user, :paypal_data]

after(:create) do |line_item_group, evaluator|
evaluator.traits.each_with_index do |traits, index|
bt = line_item_group.business_transactions.send(evaluator.build_or_create_bts, FactoryGirl.attributes_for(:business_transaction, *traits, line_item_group: line_item_group, seller: line_item_group.seller, article_attributes: evaluator.articles_attributes[index] || {}))
line_item_group.line_items << FactoryGirl.create(:line_item, article: bt.article) if evaluator.create_line_items
transient do
articles_attributes []
articles { [] } # dont override
create_line_items false
traits [[:paypal, :transport_type1], [:invoice, :transport_type2]]
build_or_create_bts { sold ? :create : :build }
end
end
end

trait :with_voucher_seller do
association :seller, factory: [:user, :paypal_data], uses_vouchers: true
end
after(:create) do |line_item_group, evaluator|
evaluator.traits.each_with_index do |traits, index|
attributes = FactoryGirl
.attributes_for(:business_transaction, *traits,
line_item_group: line_item_group, seller: line_item_group.seller,
article_attributes: evaluator.articles_attributes[index] || {})
bt = line_item_group.business_transactions.send(evaluator.build_or_create_bts, attributes)
if evaluator.create_line_items
line_item_group.line_items << FactoryGirl.create(:line_item, article: bt.article)
end
end
end
end

trait :sold do
transient do
sold { true }
trait :with_voucher_seller do
association :seller, factory: [:user, :paypal_data], uses_vouchers: true
end
sold_at { Time.now }
tos_accepted true
end

trait :with_unified_transport do
seller { FactoryGirl.create(:legal_entity, :paypal_data, :with_unified_transport_information) }
unified_transport true
unified_transport_provider 'DHL'
unified_transport_price_cents 300
unified_transport_maximum_articles 10
end
trait :with_unified_transport do
association :seller, factory: [:legal_entity, :paypal_data,
:with_unified_transport_information]
unified_transport true
unified_transport_provider 'DHL'
unified_transport_price_cents 300
unified_transport_maximum_articles 10
end

trait :with_free_transport_at_40 do
free_transport_at_price_cents 4000
end
trait :with_free_transport_at_40 do
free_transport_at_price_cents 4000
end

trait :with_unified_payment_cash do
unified_payment true
unified_payment_method 'cash'
end
trait :with_unified_payment_cash do
unified_payment true
unified_payment_method 'cash'
end

trait :with_unified_payment_paypal do
unified_payment true
unified_payment_method 'paypal'
end
trait :with_unified_payment_paypal do
unified_payment true
unified_payment_method 'paypal'
end

trait :with_unified_payment_invoice do
unified_payment true
unified_payment_method 'invoice'
end
trait :with_unified_payment_invoice do
unified_payment true
unified_payment_method 'invoice'
end

trait :with_unified_payment_cash_on_delivery do
unified_payment true
unified_payment_method 'cash_on_delivery'
end
trait :with_unified_payment_cash_on_delivery do
unified_payment true
unified_payment_method 'cash_on_delivery'
end

trait :with_unified_payment_bank_transfer do
unified_payment true
unified_payment_method 'bank_transfer'
trait :with_unified_payment_bank_transfer do
unified_payment true
unified_payment_method 'bank_transfer'
end
end
end

0 comments on commit fb67692

Please sign in to comment.