Skip to content

Commit

Permalink
Merge pull request #15 from alepbloyd/factorybot_and_seed
Browse files Browse the repository at this point in the history
Set up FactoryBot and Faker
  • Loading branch information
alepbloyd authored Aug 10, 2022
2 parents e2a7408 + a19584a commit 0f5d0f2
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ gem 'bootsnap', '>= 1.1.0', require: false

gem 'httparty'

gem 'factory_bot'

gem 'factory_bot_rails'

gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'master'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'capybara'
Expand Down
22 changes: 19 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
GIT
remote: https://github.com/faker-ruby/faker.git
revision: 65e5bb959e14e57d4cdc002dcdb515ee99dcb497
branch: master
specs:
faker (2.22.0)
i18n (>= 1.8.11, < 2)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -72,6 +80,11 @@ GEM
docile (1.4.0)
erubi (1.11.0)
execjs (2.8.1)
factory_bot (6.2.1)
activesupport (>= 5.0.0)
factory_bot_rails (6.2.0)
factory_bot (~> 6.2.0)
railties (>= 5.0.0)
ffi (1.15.5)
ffi (1.15.5-java)
ffi (1.15.5-x64-mingw32)
Expand Down Expand Up @@ -123,9 +136,9 @@ GEM
orderly (0.1.1)
capybara (>= 1.1)
rspec (>= 2.14)
pg (1.4.2)
pg (1.4.2-x64-mingw32)
pg (1.4.2-x86-mingw32)
pg (1.4.3)
pg (1.4.3-x64-mingw32)
pg (1.4.3-x86-mingw32)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
Expand Down Expand Up @@ -261,6 +274,9 @@ DEPENDENCIES
bootsnap (>= 1.1.0)
capybara
coffee-rails (~> 4.2)
factory_bot
factory_bot_rails
faker!
httparty
jbuilder (~> 2.5)
launchy
Expand Down
45 changes: 45 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,48 @@
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
require 'factory_bot_rails'
include FactoryBot::Syntax::Methods


@bulk_discounts = []
@customers = [] #
@invoice_items = [] #
@invoices = [] #
@items = [] #
@transactions = []
@merchants = [] #



(1..1000).each do
@customers << FactoryBot.create(:customer)
end

(1..100).each do
@merchants << FactoryBot.create(:merchant)
end

(1..1000).each do
@items << FactoryBot.create(:item, merchant_id: @merchants.sample.id)
end

(1..1000).each do
@invoices << FactoryBot.create(:invoice, customer_id: @customers.sample.id)
end

(1..2000).each do
@invoice_items << FactoryBot.create(:invoice_item, invoice_id: @invoices.sample.id, item_id: @items.sample.id)
end

(1..1000).each do
@transactions << FactoryBot.create(:transaction, invoice_id: @invoices.sample.id)
end

@merchants.each do |merchant|
@bulk_discounts << FactoryBot.create(:bulk_discount, merchant_id: merchant.id, percent_discount: 10, quantity_threshold: 5)

@bulk_discounts << FactoryBot.create(:bulk_discount, merchant_id: merchant.id, percent_discount: 20, quantity_threshold: 10)

@bulk_discounts << FactoryBot.create(:bulk_discount, merchant_id: merchant.id, percent_discount: 50, quantity_threshold: 30)
end
9 changes: 9 additions & 0 deletions spec/factories/bulk_discounts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'faker'

FactoryBot.define do
factory :bulk_discount do
merchant_id {}
percent_discount {}
quantity_threshold {}
end
end
8 changes: 8 additions & 0 deletions spec/factories/customers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'faker'

FactoryBot.define do
factory :customer do
first_name { Faker::Name.first_name }
last_name {Faker::Name.last_name}
end
end
11 changes: 11 additions & 0 deletions spec/factories/invoice_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'faker'

FactoryBot.define do
factory :invoice_item do
item_id {}
invoice_id {}
quantity { rand(1..50) }
unit_price { rand(100..100_000)}
status { [0,1,2].sample }
end
end
8 changes: 8 additions & 0 deletions spec/factories/invoices.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'faker'

FactoryBot.define do
factory :invoice do
customer_id {}
status {[0,1,2].sample}
end
end
13 changes: 13 additions & 0 deletions spec/factories/items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'faker'

FactoryBot.define do
factory :item do
name { "#{[Faker::Adjective.positive,Faker::Adjective.negative].sample.titleize} #{Faker::Food.dish}" }
description {
[Faker::Quotes::Shakespeare.hamlet, Faker::Quotes::Shakespeare.as_you_like_it].sample
}
unit_price { rand(100..100_000)}
merchant_id {}
status { [0,1].sample }
end
end
8 changes: 8 additions & 0 deletions spec/factories/merchant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'faker'

FactoryBot.define do
factory :merchant do
name {"#{Faker::Hobby.activity.titleize} Shop"}
status {[0,1].sample}
end
end
10 changes: 10 additions & 0 deletions spec/factories/transactions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'faker'

FactoryBot.define do
factory :transaction do
invoice_id {}
credit_card_number {}
credit_card_expiration_date {}
result { [0,1].sample }
end
end

0 comments on commit 0f5d0f2

Please sign in to comment.