-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0721795
commit 8241e4d
Showing
4 changed files
with
137 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Return a random number in range. | ||
* @param min The lowest number to return. | ||
* @param max The highest number to return. | ||
*/ | ||
export function getRandomNumber(min: number = 5, max: number = 50) { | ||
return Math.floor(Math.random() * (max - min + 1) + min) | ||
} |
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,64 @@ | ||
import { name, random } from 'faker' | ||
import { factory, manyOf, oneOf, primaryKey } from '../../src' | ||
|
||
test('creates multiple entities with a fixed count', () => { | ||
const db = factory({ | ||
user: { | ||
id: primaryKey(random.uuid), | ||
firstName: name.firstName, | ||
}, | ||
}) | ||
|
||
db.user.createMany(5) | ||
|
||
const allUsers = db.user.getAll() | ||
expect(allUsers).toHaveLength(5) | ||
}) | ||
|
||
test('allows to specify count of relational entities to create', () => { | ||
const db = factory({ | ||
user: { | ||
id: primaryKey(random.uuid), | ||
country: oneOf('country'), | ||
posts: manyOf('post'), | ||
}, | ||
post: { | ||
id: primaryKey(random.uuid), | ||
title: random.words, | ||
}, | ||
country: { | ||
id: primaryKey(random.uuid), | ||
name: random.word, | ||
}, | ||
}) | ||
|
||
db.user.createMany(3, { | ||
relations: { | ||
/** | ||
* @todo How to specify that multiple entities should reuse the same relational model? | ||
* I.e. multiple users from the same country. | ||
* | ||
* @todo `Boolean` value of `oneOf` relation has no sense: | ||
* an entity cannot be created without all relational models specified. | ||
*/ | ||
country: false, | ||
posts: 2, | ||
}, | ||
}) | ||
|
||
const allUsers = db.user.getAll() | ||
const allPosts = db.post.getAll() | ||
const allCountries = db.country.getAll() | ||
|
||
expect(allUsers).toHaveLength(3) | ||
// Each of 3 random "user" should have "2" posts created. | ||
expect(allPosts).toHaveLength(6) | ||
// Each of 3 random "user" should have its own "country". | ||
expect(allCountries).toHaveLength(3) | ||
|
||
allUsers.forEach((user, index) => { | ||
expect(user.posts).toHaveLength(2) | ||
expect(user.country).toHaveProperty('id', allCountries[index].id) | ||
expect(user.country).toHaveProperty('name', allCountries[index].name) | ||
}) | ||
}) |