Feb 24, 2024 • 4 min read
Node Fake Data
A quick guide on generating test data. If you have used the Pet Theory lab, this should be familar, but if not here is the code.
The general idea is to create a csv based on made up dataset. To do this, use the Fakerjs package and write the output as csv records.
If you have not worked with Fakerjs before, this is a nice short cut to get to grips with the Firestore API.
Requirements:
- Node.js installation
- Fakerjs package
Overview
In this blog post learn how to create fake test data in the CSV format.
Application Code
The template application will be based on Node.js. If you are unfamiliar with Node.js, this is a standard step to create the files required.
The package.json file provides the information relating to packages used and general descriptions.
In addition it has a helpful script section, for testing that can be generalised.
- Add a package.json
{
"name": "create-td",
"version": "1.0.0",
"description": "This is lab01 of the Pet Theory labs",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Rich Rose",
"license": "MIT",
"devDependencies": {
"@faker-js/faker": "^8.4.1"
}
}- Create an index.js file
The code is required to do the following things:
- Include package dependencies
- Add a createTestData function
Run app
The npm packages are required:
npm install --save-dev @faker-js/fakerRun the application
node index.js 10Expected Output:
Created file customers_10.csv containing 10 records.Package Dependencies
A declaration for generating data.
const fs = require("fs");
const { faker } = require("@faker-js/faker");Validate Command Line Arguments
The application expects the name of a CSV file to be added. If a filename is not present, the application will exit.
recordCount = parseInt(process.argv[2]);
if (process.argv.length != 3 || recordCount < 1 || isNaN(recordCount)) {
console.error("Include the number of test data records to create. Example:");
console.error(" node createTestData.js 100");
process.exit(1);
}
createTestData(recordCount);Custom Data Schema
Use the fakerjs package to create fake data.
The data will be written to a filename with the extension csv.
A record of the following schema is created:
| Field | Type | Method |
|---|---|---|
| Id | Number | Number |
| firstName | String | Person |
| lastName | String | Person |
| name | String | Person |
| String | Intenet | |
| phone | String | Phone |
Custom Data Generation
The createTestData function will create the above schema.
async function createTestData(recordCount) {
const fileName = `customers_${recordCount}.csv`;
var f = fs.createWriteStream(fileName);
f.write('id,name,email,phone\n')
for (let i=0; i<recordCount; i++) {
const id = faker.number.int();
const firstName = getFirstName(i);
const lastName = getLastName();
const phone = getPhoneNumber();
const name = `${firstName} ${lastName}`;
const email = getRandomCustomerEmail(firstName, lastName);
f.write(`${id},${name},${email},${phone}\n`);
}
console.log(`Created file ${fileName} containing ${recordCount} records.`);
f.close();
}Generate Email
function getRandomCustomerEmail(fName, lName) {
const pName = faker.internet.domainName();
const email = faker.internet.email({ firstName: fName, lastName: lName, provider: pName });
return email.toLowerCase();
}Generate First Name
The following function will generate first name. The function takes an integer param, that can be used to set the gender of the name.
function getFirstName(gender){
return gender%2==0 ? faker.person.firstName('female') : faker.person.firstName('male');
}Generate Last Name
The following function will generate a fake last name.
function getLastName(){
return faker.person.lastName();
}Generate Phone Number
The following function will generate a fake phone number
function getPhoneNumber(){
return faker.phone.number();
}Generated Dataset
The information below is an example dataset generated by the code.
id,name,email,phone
1744353443708928,Hattie Shields,[email protected],994-985-4343
7439950444232704,Cornelius Douglas,[email protected],445.834.1232 x04559
3571161470337024,Ella DuBuque,[email protected],(598) 995-8925 x3962
1905977706151936,Andy Auer,[email protected],1-839-408-6820 x7671
7038984255438848,Virginia Rohan,[email protected],254.784.8123 x8950
1184946481790976,Isaac Pagac,[email protected],948-787-4669 x126
437067337170944,Alma Windler,[email protected],(966) 478-7228
4056411071840256,Wilbert Zieme,[email protected],547.786.0668 x195
5202519139549184,Wendy Strosin,[email protected],225-558-2300
4725405374218240,Santos Koch-Bailey,[email protected],973.596.7081 x654Complete Code
const fs = require('fs');
const { faker } = require('@faker-js/faker');
// Function: getRandomCustomerEmail
// Param: fName - firstName, lName - lastName
function getRandomCustomerEmail(fName, lName) {
const pName = faker.internet.domainName();
const email = faker.internet.email({ firstName: fName, lastName: lName, provider: pName });
return email.toLowerCase();
}
// Function: getFirstName
// Param: Gender 0 - Female, 1 - Male
function getFirstName(gender){
return gender%2==0 ? faker.person.firstName('female') : faker.person.firstName('male');
}
// Function: getLastName
// Param: N/A
function getLastName(){
return faker.person.lastName();
}
// Function: getPhoneNumber
// Param: N/A
function getPhoneNumber(){
return faker.phone.number();
}
async function createTestData(recordCount) {
const fileName = `customers_${recordCount}.csv`;
var f = fs.createWriteStream(fileName);
f.write('id,name,email,phone\n')
for (let i=0; i<recordCount; i++) {
const id = faker.number.int();
const firstName = getFirstName(i);
const lastName = getLastName();
const phone = getPhoneNumber();
const name = `${firstName} ${lastName}`;
const email = getRandomCustomerEmail(firstName, lastName);
f.write(`${id},${name},${email},${phone}\n`);
}
console.log(`Created file ${fileName} containing ${recordCount} records.`);
f.close();
}
recordCount = parseInt(process.argv[2]);
if (process.argv.length != 3 || recordCount < 1 || isNaN(recordCount)) {
console.error('Include the number of test data records to create. Example:');
console.error(' node createTestData.js 100');
process.exit(1);
}
createTestData(recordCount);