From d0b2144b28a3f91638ce3f8e195b870929fa7059 Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Sun, 3 Nov 2013 13:52:43 -0500 Subject: [PATCH 1/2] Adding better fake seed data --- Gemfile | 1 + Gemfile.lock | 3 +++ db/seeds.rb | 15 +++++++++------ spec/factories/bikes.rb | 22 +++++++++++----------- spec/factories/user_profiles.rb | 12 ++++++------ spec/factories/users.rb | 1 + 6 files changed, 31 insertions(+), 23 deletions(-) diff --git a/Gemfile b/Gemfile index 2de9c1f..d09b8da 100644 --- a/Gemfile +++ b/Gemfile @@ -33,6 +33,7 @@ group :development, :test do gem 'rspec-rails', '~> 2.8.1' gem 'factory_girl_rails', '~> 1.2' gem 'pry', '~> 0.9.8' + gem 'faker' end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index da41dad..997ddc6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -77,6 +77,8 @@ GEM factory_girl_rails (1.7.0) factory_girl (~> 2.6.0) railties (>= 3.0.0) + faker (1.2.0) + i18n (~> 0.5) ffi (1.6.0) formatador (0.2.4) gherkin (2.11.6) @@ -231,6 +233,7 @@ DEPENDENCIES decent_exposure (~> 1.0.1) devise (~> 2.0.4) factory_girl_rails (~> 1.2) + faker guard-rspec guard-spork haml-rails (~> 0.3.4) diff --git a/db/seeds.rb b/db/seeds.rb index b56fc3b..e06aaf5 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -26,13 +26,16 @@ end if Rails.env.development? - #create default admin user + #create default users if User.all.empty? - FactoryGirl.create(:user) - FactoryGirl.create(:staff) - FactoryGirl.create(:bike_admin) - FactoryGirl.create(:admin) - FactoryGirl.create(:user_profile) + u = FactoryGirl.create(:user) + FactoryGirl.create(:user_profile, user_id: u.id) + u = FactoryGirl.create(:staff) + FactoryGirl.create(:user_profile, user_id: u.id) + u = FactoryGirl.create(:bike_admin) + FactoryGirl.create(:user_profile, user_id: u.id) + u = FactoryGirl.create(:admin) + FactoryGirl.create(:user_profile, user_id: u.id) end #create fake bikes diff --git a/spec/factories/bikes.rb b/spec/factories/bikes.rb index ed8c0d0..0151534 100644 --- a/spec/factories/bikes.rb +++ b/spec/factories/bikes.rb @@ -4,17 +4,17 @@ FactoryGirl.define do factory :bike do sequence(:shop_id) {|n| n} sequence :serial_number do |n| - "S/N# #{n}" + "#{Faker::Code.isbn}-#{n}" end - bike_brand_id 1 - bike_model_id 1 - color "FFFFFF" - bike_style_id 1 - seat_tube_height 5 - top_tube_length 6 - bike_wheel_size_id 1 - value 100 - bike_condition_id 1 - bike_purpose_id 1 + bike_brand_id { Random.rand(100) } + model { Faker::Commerce.product_name } + color { sprintf("%06X", Random.rand(16777215) ) } + bike_style_id { Random.rand(2) } + seat_tube_height { Random.rand(25) } + top_tube_length { Random.rand(25) } + bike_wheel_size_id { Random.rand(10) } + value { Random.rand(200) } + bike_condition_id { Random.rand(2) } + bike_purpose_id { Random.rand(2) } end end diff --git a/spec/factories/user_profiles.rb b/spec/factories/user_profiles.rb index b32f483..0d746a5 100644 --- a/spec/factories/user_profiles.rb +++ b/spec/factories/user_profiles.rb @@ -1,11 +1,11 @@ FactoryGirl.define do factory :user_profile do user_id 1 - addrStreet1 "Charles Street" - addrStreet2 "Apt #42" - addrCity "Baltimore" - addrState "MD" - addrZip "21231" - phone "(410)8675309" + addrStreet1 { Faker::Address.street_address } + addrStreet2 { Faker::Address.secondary_address } + addrCity { Faker::Address.city } + addrState { Faker::Address.state_abbr } + addrZip { Faker::Address.zip_code } + phone { Faker::PhoneNumber.cell_phone } end end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 1ad7f21..e29025d 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -1,5 +1,6 @@ FactoryGirl.define do factory :user do + sequence(:username) { |n| "user_#{n}" } sequence(:email) { |n| "user_#{n}@example.com" } password 'password' From eeefe2522757aceed90d9728f6534a8d42db7f21 Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Sun, 3 Nov 2013 13:58:26 -0500 Subject: [PATCH 2/2] Make sure grabbing valid ID, better faking --- spec/factories/bikes.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/factories/bikes.rb b/spec/factories/bikes.rb index 0151534..e2f04ea 100644 --- a/spec/factories/bikes.rb +++ b/spec/factories/bikes.rb @@ -6,15 +6,15 @@ FactoryGirl.define do sequence :serial_number do |n| "#{Faker::Code.isbn}-#{n}" end - bike_brand_id { Random.rand(100) } + bike_brand_id { BikeBrand.all.sample.id } model { Faker::Commerce.product_name } color { sprintf("%06X", Random.rand(16777215) ) } - bike_style_id { Random.rand(2) } + bike_style_id { BikeStyle.all.sample.id } seat_tube_height { Random.rand(25) } top_tube_length { Random.rand(25) } - bike_wheel_size_id { Random.rand(10) } + bike_wheel_size_id { BikeWheelSize.all.sample.id } value { Random.rand(200) } - bike_condition_id { Random.rand(2) } - bike_purpose_id { Random.rand(2) } + bike_condition_id { BikeCondition.all.sample.id } + bike_purpose_id { BikePurpose.all.sample.id } end end