Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: faker error for nested lookups in test data #46

Merged
merged 1 commit into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"firstname": "{{name.lastName}}",
"[email protected]": "{{finance.amount}}",
"emailaddress1": "{{internet.email}}",
"[email protected]": "{{date.past(90)}}"
"[email protected]": "{{date.past(90)}}",
"owningteam": {
"@alias": "a nested faked record",
"name": "{{name.jobArea}}"
}
}
24 changes: 21 additions & 3 deletions driver/src/data/fakerPreprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,29 @@ export default class FakerPreprocessor extends Preprocessor {
return FakerPreprocessor.parse(FakerPreprocessor.fake(data));
}

private static fake(data: Record) : string {
return faker.fake(JSON.stringify(data));
private static fake(data: Record): string {
const fakedData = data;

Object.keys(data).forEach((key) => {
const value = fakedData[key];

if (value === null) {
return;
}

if (Array.isArray(value) && value !== null) {
fakedData[key] = value.map((arrayItem) => JSON.parse(this.fake(arrayItem as Record)));
} else if (typeof value === 'object' && value !== null) {
fakedData[key] = JSON.parse(this.fake(value as Record));
} else if (typeof value === 'string') {
fakedData[key] = faker.fake(value);
}
});

return JSON.stringify(fakedData);
}

private static parse(data: string) : Record {
private static parse(data: string): Record {
const cleansedData = JSON.parse(data);

Object.keys(cleansedData).forEach((property) => {
Expand Down
18 changes: 9 additions & 9 deletions driver/test/data/fakerPreprocessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ describe('FakerPreprocessor', () => {
describe('.preprocess(data)', () => {
it('calls faker.fake on data', async () => {
const data = { foo: 'bar' };
fakeSpy.and.returnValue('{ "foo": "baz" }');
fakeSpy.and.returnValue('baz');

fakerPreprocessor.preprocess(data);

expect(fakeSpy).toHaveBeenCalledWith(JSON.stringify(data));
expect(fakeSpy).toHaveBeenCalledWith('bar');
});

it('replaces @faker.number properties with number equivalent', () => {
const data = {
'[email protected]': '{{random.number}}',
};
fakeSpy.and.returnValue('{ "[email protected]": "10.50" }');
fakeSpy.and.returnValue('10.50');

const result = fakerPreprocessor.preprocess(data);

Expand All @@ -36,7 +36,7 @@ describe('FakerPreprocessor', () => {
const data = {
'[email protected]': '{{random.word}}',
};
fakeSpy.and.returnValue('{ "[email protected]": "baz" }');
fakeSpy.and.returnValue('baz');

expect(() => fakerPreprocessor.preprocess(data)).toThrowError();
});
Expand All @@ -46,7 +46,7 @@ describe('FakerPreprocessor', () => {
'[email protected]': '{{date.recent}}',
};
const date = new Date();
fakeSpy.and.returnValue(`{ "[email protected]": "${date}" }`);
fakeSpy.and.returnValue(`${date}`);

const result = fakerPreprocessor.preprocess(data);

Expand All @@ -59,7 +59,7 @@ describe('FakerPreprocessor', () => {
'[email protected]': '{{date.recent}}',
};
const date = new Date();
fakeSpy.and.returnValue(`{ "[email protected]": "${date}" }`);
fakeSpy.and.returnValue(`${date}`);

const result = fakerPreprocessor.preprocess(data);

Expand All @@ -72,7 +72,7 @@ describe('FakerPreprocessor', () => {
'[email protected]': 'this isn\'t a valid date',
'[email protected]': 'this isn\'t a valid datetime',
};
fakeSpy.and.returnValue('{ "[email protected]": "this isn\'t a valid date", "[email protected]": "this isn\'t a valid datetime" }');
fakeSpy.and.returnValues('this isn\'t a valid date', 'this isn\'t a valid datetime');

expect(() => fakerPreprocessor.preprocess(data)).toThrowError();
});
Expand All @@ -81,7 +81,7 @@ describe('FakerPreprocessor', () => {
const data = {
lookup: { '[email protected]': '{{random.number}}' },
};
fakeSpy.and.returnValue('{ "lookup": { "[email protected]": "10.50" } }');
fakeSpy.and.returnValue('10.50');

const result = fakerPreprocessor.preprocess(data);
const lookup = result.lookup as any;
Expand All @@ -94,7 +94,7 @@ describe('FakerPreprocessor', () => {
const data = {
relationship: [{ '[email protected]': '{{random.number}}' }],
};
fakeSpy.and.returnValue('{ "relationship": [ { "[email protected]": "10.50" } ] }');
fakeSpy.and.returnValue('10.50');

const result = fakerPreprocessor.preprocess(data);
const relatedRecord = (result.relationship as any[])[0];
Expand Down