📜 ⬆️ ⬇️

Filling the database with test data using Populator and Faker

Often there is a need to test the application for working with real data. Moreover, the data should be as close as possible to the real both from the qualitative side and from the quantitative one. The work on filling the database with such data is greatly simplified by the Populator and Faker gems.

Suppose we have a User model:

To generate 1000 test users for the users table, we will use the following code:

 require 'populator'
 require 'faker'

 # Populate DB with Users
 User.populate (1000) do | user |
   user.email = Faker :: Internet.email
   user.first_name = Faker :: Name.first_name
   user.last_name = Faker :: Name.last_name
   user.ssn = Faker.numerify ("### - ## - ####")
   user.security_question = Populator.words (4..10)
   user.answer = Populator.words (1..3)
   user.phone = Faker.numerify ("#" * 10)
   user.address = Faker :: Address.street_address
   user.city = Faker :: Address.city
   user.state = Faker :: Address.us_state_abbr
   user.zip = Faker :: Address.zip_code
 end


It should be noted that Populator does not inherit the ActiveRecord model, but works directly from the database in order to improve performance. Array or Range can be passed as field value, Populator will automatically select random.
')
 user.sex = ["male", "female"]
 user.age = 20..30

Source: https://habr.com/ru/post/83378/


All Articles