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

Commit

Permalink
feat: implement seed method on InMemoryDBService (#53)
Browse files Browse the repository at this point in the history
* feat: implement seed method on InMemoryDBService

Add new seed method on InMemoryDBService with ability to set amount and record factory for
generation.

Closes #7
  • Loading branch information
wesleygrimes authored Aug 22, 2019
1 parent b46caec commit 945afb1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
34 changes: 34 additions & 0 deletions lib/services/in-memory-db.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,4 +559,38 @@ describe('In Memory DB Service', () => {
}),
);
});

describe('seed', () => {
const recordFactory = (idx: number): Partial<TestEntity> => ({
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);
},
);
});
});
20 changes: 20 additions & 0 deletions lib/services/in-memory-db.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,26 @@ export class InMemoryDBService<T extends InMemoryDBEntity> {
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<T>, 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:
Expand Down
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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": {
Expand All @@ -67,5 +70,6 @@
"@semantic-release/npm",
"@semantic-release/github"
]
}
},
"dependencies": {}
}

0 comments on commit 945afb1

Please sign in to comment.