Seed dummy data for testing

Thirumal S

2 min read

I have been working in a team that is building a Stock information management system for a large tunneling company. While testing the application, we wanted to load lots of sample data for performance testing, which can be hard to do manually. Our Q/A team also want to fill dummy data in the application for testing purposes.
To accomplish this I used the Populator and Faker gems which I came across and has a really good API to generate sample data. Let me take two tables we use – Supplier and StockMaster and illustrate how I used these gems to populate data.
First, a bit about the gems:
What is Populator?
The Populator gem allows us to populate large number of Active Record Models. This is really useful while generating different custom data for each of hundreds of records.
What is Faker?
Faker provides a lot of methods to generate random content like names, addresses, numbers, emails, designations, etc. It is a port of the Data::Faker library of Perl.
1. To install
[source]
$ gem install populator
$ gem install faker
[/source]
2. Add these gems in your Gemfile under the development group since its going to be used only during development.
[source language=”ruby”]
group :development do
gem ‘populator’
gem ‘faker’
end
[/source]
3.Create a rake task under lib/tasks/[task-name].rake (Example lib/tasks/populate.rake)
[source language=”ruby”]
namespace :db do
desc “Erase and fill database”
task :populate => :environment do
require ‘populator’
require ‘faker’
[Supplier, Stockmaster].each(&:delete_all)
Stockmaster.populate 10..100 do |stock|
stock.name = Populator.words(1..5).titleize
stock.description = Populator.sentences(2..10)
stock.price = [4.99, 19.95, 100]
stock.created_at = 2.years.ago..Time.now
end
Supplier.populate 100 do |supplier|
supplier.name = Faker::Name.name
supplier.company = Faker::Company.name
supplier.email = Faker::Internet.email
supplier.phone = Faker::PhoneNumber.phone_number
supplier.street = Faker::Address.street_address
supplier.city = Faker::Address.city
supplier.state = Faker::Address.us_state_abbr
supplier.zip = Faker::Address.zip_code
end
end
end
[/source]
Here, I have used the Populator gem for generating the dummy data for StockMaster table fields. Populator.words(),Populator.sentences() are the API methods of Populator gem. Then I have used the Faker gem for generating dummy data for Supplier table.
You might think why I have used the Faker gem for Supplier table?
Populator gem can’t generate different types of dummy data so I am using the Faker gem. Faker gem has a lot of API methods in itself. These API methods help us to generate the dummy data.
4.To run the rake task
[source]$ rake db:populate[/source]
Load data from Spreadsheet into Database
Our Q/A team then gave me a spreadsheet with some data that needed to be loaded into the app for testing. So inorder to achieve this I used the rubyXL gem.
What is rubyXL?
It is a Ruby gem for reading / writing / modifying .xlsx and .xlsm files.
1. To install
[source]$ gem install rubyXL[/source]
2. How to parse the data from XL sheet?
[source]
workbook = RubyXL::Parser.parse(“path/to/Excel/file.xlsx”)
[/source]
For example, let’s take the same StockMaster table. It has 3 columns – name, description, price. The give spreadsheet has 3 sheets and around 300 records per sheet.
Lets create a rake task lib/tasks/load_stocks.rake and add the code to load this data into he database using the rubyXL gem.
[source language=”ruby”]
namespace :db do
task :load_stocks => :environment do
workbook = RubyXL::Parser.parse(“Stocklist.xlsx”)
(0..2).each do |sheet_no|
worksheet = workbook[sheet_no]
stocks = worksheet.extract_data
stocks.each do |s|
stock = {
name: s[1].to_s.strip,
description: s[2].to_s.strip,
price: s[3].to_s.strip
}
Stockmaster.create(stock)
end
end
end
end
[/source]
This code helps you to fill the spreadsheet data into your application database. So using these gems you can easily generate and load dummy data into your application’s database for various purposes.

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *