Skip to content

Commit

Permalink
finished factories
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoLombardo committed Jul 25, 2024
1 parent c64e840 commit d73fd94
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 69 deletions.
2 changes: 1 addition & 1 deletion app/models/gift.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Gift < ApplicationRecord
has_one_attached :image
has_rich_text :content

validates :name, :price, :valoration, :supplier, :image, :categories, :customizations,
validates :name, :price, :valoration, :supplier, :image, :categories,
:content, presence: true
validates :price, numericality: { greater_than: 0 }
validates :valoration, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 5 }
Expand Down
92 changes: 45 additions & 47 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,51 +44,49 @@
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# This allows you to limit a spec run to individual examples or groups
# you care about by tagging them with `:focus` metadata. When nothing
# is tagged with `:focus`, all examples get run. RSpec also provides
# aliases for `it`, `describe`, and `context` that include `:focus`
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
config.filter_run_when_matching :focus
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"
# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/
config.disable_monkey_patching!
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = "doc"
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
# # This allows you to limit a spec run to individual examples or groups
# # you care about by tagging them with `:focus` metadata. When nothing
# # is tagged with `:focus`, all examples get run. RSpec also provides
# # aliases for `it`, `describe`, and `context` that include `:focus`
# # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
# config.filter_run_when_matching :focus
#
# # Allows RSpec to persist some state between runs in order to support
# # the `--only-failures` and `--next-failure` CLI options. We recommend
# # you configure your source control system to ignore this file.
# config.example_status_persistence_file_path = "spec/examples.txt"
#
# # Limits the available syntax to the non-monkey patched syntax that is
# # recommended. For more details, see:
# # https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/
# config.disable_monkey_patching!
#
# # Many RSpec users commonly either run the entire suite or an individual
# # file, and it's useful to allow more verbose output when running an
# # individual spec file.
# if config.files_to_run.one?
# # Use the documentation formatter for detailed output,
# # unless a formatter has already been configured
# # (e.g. via a command-line flag).
# config.default_formatter = "doc"
# end
#
# # Print the 10 slowest examples and example groups at the
# # end of the spec run, to help surface which specs are running
# # particularly slow.
# config.profile_examples = 10
#
# # Run specs in random order to surface order dependencies. If you find an
# # order dependency and want to debug it, you can fix the order by providing
# # the seed, which is printed after each run.
# # --seed 1234
# config.order = :random
#
# # Seed global randomization in this process using the `--seed` CLI option.
# # Setting this allows you to use `--seed` to deterministically reproduce
# # test failures related to randomization by passing the same `--seed` value
# # as the one that triggered the failure.
# Kernel.srand config.seed
end
File renamed without changes.
4 changes: 3 additions & 1 deletion test/factories/destinations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
number { Faker::PhoneNumber.cell_phone }
schedules { "#{Faker::Number.between(from: 10, to: 18)}hs" }
cost { Faker::Number.between(from: 1, to: 100) }
purchase { nil }
after(:build) do |destination|
destination.purchase ||= build(:purchase, destinations: [destination])
end
end
end
18 changes: 6 additions & 12 deletions test/factories/gift.rb → test/factories/gifts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@
price { Faker::Number.between(from: 1, to: 1000) }
valoration { Faker::Number.between(from: 0.0, to: 5.0).round(1) }

supplier do
Supplier.all.sample
end

categories do
Category.all.sample(3)
end

customizations do
Customization.all.sample(4)
end

content do
GIFT_CONTENT_LISTS.sample
end
Expand All @@ -29,6 +17,12 @@
filename: random_image,
content_type: 'image/png'
)
gift.supplier ||= create(:supplier)
gift.categories = create_list(:category, 3)
end

trait(:with_customizations) do
customizations { create_list(:customization, 3, gifts: [gift]) }
end
# trait(:with_categories) do
# Category.all.sample(3)
Expand Down
4 changes: 3 additions & 1 deletion test/factories/payment_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
card_number { Faker::Bank.account_number }
due_date { Faker::Date.between(from: Date.today, to: 4.year.from_now) }
CVV { Faker::Number.number(digits: 3) }
user { nil }
after(:build) do |payment_method|
payment_method.user ||= build(:user, payment_methods: [payment_method])
end
end
end
17 changes: 11 additions & 6 deletions test/factories/purchases.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
FactoryBot.define do
factory :purchase do
RUT { Faker::Number.unique.number(digits: 8) }
RUT { Faker::Number.unique.number(digits: 8).to_s }
social_reason { Faker::Company.name }
payment_method { PaymentMethod.all.sample }
gift { Gift.all.sample }
suprise_delivery { Faker::Boolean.boolean }
personalization { 'Hola, esta es el mensaje personalizado que te envío' }
resend_delivery { Faker::Boolean.boolean }
company_logo { nil }
amount { Faker::Number.between(from: 1, to: 5) }
subtotal { amount * gift.price }
price { subtotal + (subtotal * 0.22).to_i + 180 }
destinations { FactoryBot.create_list(:destination, 1) }
after(:build) do |purchase|
if purchase.destinations.empty?
purchase.destinations = build_list(:destination, 1,
purchase: purchase)
end
purchase.gift = create(:gift) if purchase.gift.nil?
purchase.payment_method = create(:payment_method) if purchase.payment_method.nil?
purchase.subtotal = purchase.amount * purchase.gift_price
purchase.price = purchase.subtotal + (purchase.subtotal * 0.22).to_i + 180
end
end
end
2 changes: 1 addition & 1 deletion test/factories/supplier.rb → test/factories/suppliers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FactoryBot.define do
factory :supplier do
name { Faker::Commerce.unique.vendor }
name { Faker::Commerce.vendor }
end
end
9 changes: 9 additions & 0 deletions test/factories/users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryBot.define do
factory :user do
email { Faker::Internet.email }
password { Faker::Internet.password(min_length: 8) }
name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
company_name { Faker::Company.name }
end
end

0 comments on commit d73fd94

Please sign in to comment.