-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dynamic test data generation with faker.js (#42)
- Loading branch information
Showing
14 changed files
with
278 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...i.PowerApps.SpecFlowBindings.UiTests/Data/data decorated with faker moustache syntax.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"@logicalName": "contact", | ||
"@alias": "the faked record", | ||
"lastname": "{{name.firstName}}", | ||
"firstname": "{{name.lastName}}", | ||
"creditlimit@faker.number": "{{finance.amount}}", | ||
"emailaddress1": "{{internet.email}}", | ||
"birthdate@faker.date": "{{date.past(90)}}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as faker from 'faker'; | ||
import Preprocessor from './preprocessor'; | ||
import { Record } from './record'; | ||
|
||
export default class FakerPreprocessor extends Preprocessor { | ||
// eslint-disable-next-line class-methods-use-this | ||
preprocess(data: Record): Record { | ||
return FakerPreprocessor.parse(FakerPreprocessor.fake(data)); | ||
} | ||
|
||
private static fake(data: Record) : string { | ||
return faker.fake(JSON.stringify(data)); | ||
} | ||
|
||
private static parse(data: string) : Record { | ||
const cleansedData = JSON.parse(data); | ||
|
||
Object.keys(cleansedData).forEach((property) => { | ||
const val = cleansedData[property]; | ||
|
||
if (property.endsWith('@faker.number') && typeof val === 'string') { | ||
cleansedData[property.replace('@faker.number', '')] = this.parseNumber(val, property); | ||
delete cleansedData[property]; | ||
} | ||
|
||
if (property.endsWith('@faker.datetime') && typeof val === 'string') { | ||
cleansedData[property.replace('@faker.datetime', '')] = this.parseDate(val, property, false); | ||
delete cleansedData[property]; | ||
} | ||
|
||
if (property.endsWith('@faker.date') && typeof val === 'string') { | ||
cleansedData[property.replace('@faker.date', '')] = this.parseDate(val, property, true); | ||
delete cleansedData[property]; | ||
} | ||
|
||
if (Array.isArray(val)) { | ||
cleansedData[property] = val.map( | ||
(collectionRecord) => this.parse(JSON.stringify(collectionRecord)), | ||
); | ||
} | ||
|
||
if (typeof val === 'object' && val !== null) { | ||
cleansedData[property] = this.parse(JSON.stringify(val)); | ||
} | ||
}); | ||
|
||
return cleansedData; | ||
} | ||
|
||
private static parseNumber(val: string, property: string) { | ||
const num = +val; | ||
if (Number.isNaN(num)) { | ||
throw new Error(`@faker.number syntax failed to convert ${property} with value ${val} to a number.`); | ||
} | ||
|
||
return num; | ||
} | ||
|
||
private static parseDate(val: string, property: string, dateOnly: boolean) { | ||
const parsedDate = Date.parse(val); | ||
if (Number.isNaN(parsedDate)) { | ||
throw new Error(`@faker.datetime syntax failed to convert ${property} with value ${val} to a date.`); | ||
} | ||
|
||
const date = new Date(parsedDate); | ||
|
||
return dateOnly ? date.toISOString().substring(0, 10) : date; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Record } from './record'; | ||
|
||
export default abstract class Preprocessor { | ||
abstract preprocess(data: Record): Record; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { default as Driver } from './driver'; | ||
export { DataManager, DeepInsertService } from './data'; | ||
export { DataManager, DeepInsertService, FakerPreprocessor } from './data'; | ||
export { RecordRepository, MetadataRepository } from './repositories'; |
Oops, something went wrong.