Skip to content
This repository was archived by the owner on Feb 7, 2021. It is now read-only.

Commit 945afb1

Browse files
authored
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
1 parent b46caec commit 945afb1

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

lib/services/in-memory-db.service.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,4 +559,38 @@ describe('In Memory DB Service', () => {
559559
}),
560560
);
561561
});
562+
563+
describe('seed', () => {
564+
const recordFactory = (idx: number): Partial<TestEntity> => ({
565+
someField: `${idx}`,
566+
});
567+
568+
it.each([[0, 0], [9, 9], [10, 10], [11, 11], [10, null], [10, undefined]])(
569+
'should seed %p records given input amount of %p',
570+
(expectedAmount: number, inputAmount: number) => {
571+
// act
572+
service.seed(recordFactory, inputAmount);
573+
574+
// assert
575+
expect(service.records.length).toEqual(expectedAmount);
576+
},
577+
);
578+
579+
it.each([[0, 0], [9, 9], [10, 10], [11, 11], [10, null], [10, undefined]])(
580+
'should generate correct seed records of %p given input amount of %p',
581+
(expectedAmount: number, inputAmount: number) => {
582+
// arrange
583+
const expectedRecords = [...Array(expectedAmount).keys()].map(i => ({
584+
...recordFactory(i),
585+
id: i + 1,
586+
}));
587+
588+
// act
589+
service.seed(recordFactory, inputAmount);
590+
591+
// assert
592+
expect(service.records).toEqual(expectedRecords);
593+
},
594+
);
595+
});
562596
});

lib/services/in-memory-db.service.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,26 @@ export class InMemoryDBService<T extends InMemoryDBEntity> {
296296
return result$;
297297
}
298298

299+
/**
300+
* Randomly generate at a set of records for the given amount and record factory.
301+
* Example:
302+
* ```typescript
303+
* service.seed((i) => { myProp: i}, 100);
304+
* ```
305+
*
306+
* @param recordFactory a factory method to call when generating the random record.
307+
* @param amount the amount of records to generate, defaults to 10.
308+
*/
309+
public seed(recordFactory: (index: number) => Partial<T>, amount = 10) {
310+
amount = amount === null ? 10 : amount;
311+
312+
const recordsToCreate = [...Array(amount).keys()].map(i =>
313+
recordFactory(i),
314+
);
315+
316+
this.createMany(recordsToCreate);
317+
}
318+
299319
/**
300320
* get the next id by finding the max id in the current records array and adding 1 to that value.
301321
* Example:

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"build:dev": "tsc-watch -p tsconfig.json",
1111
"format": "prettier lib/**/*.ts --write",
1212
"format:verify": "prettier lib/**/*.ts --check",
13-
"lint": "tslint -p tsconfig.json -c tslint.json \"lib/**/*.ts\" -e \"*.spec.ts\"",
13+
"lint": "npx tslint -p tsconfig.json -c tslint.json \"lib/**/*.ts\" -e \"*.spec.ts\" --force",
1414
"test": "jest --config jest.json",
1515
"semantic-release": "semantic-release",
1616
"commit": "npx git-cz"
@@ -42,13 +42,16 @@
4242
"lint-staged": {
4343
"*.ts": [
4444
"prettier --write",
45-
"git add -f",
45+
"git add -f"
46+
],
47+
"*.ts,!*.spec.ts": [
4648
"npm run lint"
4749
]
4850
},
4951
"husky": {
5052
"hooks": {
51-
"pre-commit": "lint-staged"
53+
"pre-commit": "lint-staged",
54+
"prepare-commit-msg": "exec < /dev/tty && git cz --hook"
5255
}
5356
},
5457
"publishConfig": {
@@ -67,5 +70,6 @@
6770
"@semantic-release/npm",
6871
"@semantic-release/github"
6972
]
70-
}
73+
},
74+
"dependencies": {}
7175
}

0 commit comments

Comments
 (0)