From 945afb199b3f02fe7b6274f44b0391ff7493ed94 Mon Sep 17 00:00:00 2001 From: Wes Grimes Date: Thu, 22 Aug 2019 10:43:12 -0400 Subject: [PATCH] feat: implement seed method on InMemoryDBService (#53) * feat: implement seed method on InMemoryDBService Add new seed method on InMemoryDBService with ability to set amount and record factory for generation. Closes #7 --- lib/services/in-memory-db.service.spec.ts | 34 +++++++++++++++++++++++ lib/services/in-memory-db.service.ts | 20 +++++++++++++ package.json | 12 +++++--- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/lib/services/in-memory-db.service.spec.ts b/lib/services/in-memory-db.service.spec.ts index 53ee44e..820434f 100644 --- a/lib/services/in-memory-db.service.spec.ts +++ b/lib/services/in-memory-db.service.spec.ts @@ -559,4 +559,38 @@ describe('In Memory DB Service', () => { }), ); }); + + describe('seed', () => { + const recordFactory = (idx: number): Partial => ({ + someField: `${idx}`, + }); + + it.each([[0, 0], [9, 9], [10, 10], [11, 11], [10, null], [10, undefined]])( + 'should seed %p records given input amount of %p', + (expectedAmount: number, inputAmount: number) => { + // act + service.seed(recordFactory, inputAmount); + + // assert + expect(service.records.length).toEqual(expectedAmount); + }, + ); + + it.each([[0, 0], [9, 9], [10, 10], [11, 11], [10, null], [10, undefined]])( + 'should generate correct seed records of %p given input amount of %p', + (expectedAmount: number, inputAmount: number) => { + // arrange + const expectedRecords = [...Array(expectedAmount).keys()].map(i => ({ + ...recordFactory(i), + id: i + 1, + })); + + // act + service.seed(recordFactory, inputAmount); + + // assert + expect(service.records).toEqual(expectedRecords); + }, + ); + }); }); diff --git a/lib/services/in-memory-db.service.ts b/lib/services/in-memory-db.service.ts index 1c30ead..46f2203 100644 --- a/lib/services/in-memory-db.service.ts +++ b/lib/services/in-memory-db.service.ts @@ -296,6 +296,26 @@ export class InMemoryDBService { return result$; } + /** + * Randomly generate at a set of records for the given amount and record factory. + * Example: + * ```typescript + * service.seed((i) => { myProp: i}, 100); + * ``` + * + * @param recordFactory a factory method to call when generating the random record. + * @param amount the amount of records to generate, defaults to 10. + */ + public seed(recordFactory: (index: number) => Partial, amount = 10) { + amount = amount === null ? 10 : amount; + + const recordsToCreate = [...Array(amount).keys()].map(i => + recordFactory(i), + ); + + this.createMany(recordsToCreate); + } + /** * get the next id by finding the max id in the current records array and adding 1 to that value. * Example: diff --git a/package.json b/package.json index b4f9aaf..f376c06 100755 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "build:dev": "tsc-watch -p tsconfig.json", "format": "prettier lib/**/*.ts --write", "format:verify": "prettier lib/**/*.ts --check", - "lint": "tslint -p tsconfig.json -c tslint.json \"lib/**/*.ts\" -e \"*.spec.ts\"", + "lint": "npx tslint -p tsconfig.json -c tslint.json \"lib/**/*.ts\" -e \"*.spec.ts\" --force", "test": "jest --config jest.json", "semantic-release": "semantic-release", "commit": "npx git-cz" @@ -42,13 +42,16 @@ "lint-staged": { "*.ts": [ "prettier --write", - "git add -f", + "git add -f" + ], + "*.ts,!*.spec.ts": [ "npm run lint" ] }, "husky": { "hooks": { - "pre-commit": "lint-staged" + "pre-commit": "lint-staged", + "prepare-commit-msg": "exec < /dev/tty && git cz --hook" } }, "publishConfig": { @@ -67,5 +70,6 @@ "@semantic-release/npm", "@semantic-release/github" ] - } + }, + "dependencies": {} }