-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Flatten submodule structure * Initial "findRandom" test suite * findManyRandom tests * Add to CI * Add back "packageManager" field in package.json Discovered that this is used in github actions
- Loading branch information
Showing
12 changed files
with
1,237 additions
and
415 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { prisma } from './db'; | ||
import { assert, beforeEach, test } from 'vitest'; | ||
|
||
const POPULATION = 1000; | ||
const NUM_TRIALS = 10000; | ||
|
||
const STD_RATIO = 3.4641016151377544; | ||
|
||
beforeEach(async () => { | ||
await prisma.user.deleteMany(); | ||
for (let i = 1; i <= POPULATION; ++i) { | ||
await prisma.user.create({ | ||
data: { | ||
firstName: 'User' + (i % 2), | ||
lastName: i.toString(), | ||
}, | ||
}); | ||
} | ||
}); | ||
|
||
test('empty findRandom', async () => { | ||
await prisma.user.deleteMany(); | ||
const user = await prisma.user.findRandom(); | ||
assert.isNull(user); | ||
}); | ||
|
||
test('empty findManyRandom', async () => { | ||
await prisma.user.deleteMany(); | ||
const users = await prisma.user.findManyRandom(10000); | ||
assert.isEmpty(users); | ||
}); | ||
|
||
test('findRandom distribution', async () => { | ||
const results: Record<string, number> = {}; | ||
let sum = 0; | ||
for (let i = 0; i < NUM_TRIALS; ++i) { | ||
const user = await prisma.user.findRandom(); | ||
if (!user) assert.fail('findRandom returned null'); | ||
sum += +user.lastName; | ||
results[user.lastName] = (results[user.lastName] ?? 0) + 1; | ||
} | ||
|
||
// Make sure that every user was hit | ||
for (let i = 1; i <= POPULATION; ++i) { | ||
assert.property(results, i.toString()); | ||
} | ||
|
||
const mean = sum / NUM_TRIALS; | ||
const std = Math.sqrt( | ||
Object.keys(results) | ||
.map((idx) => results[idx] * Math.pow(+idx - mean, 2)) | ||
.reduce((a, b) => a + b) / NUM_TRIALS, | ||
); | ||
console.log({ mean, std }); | ||
|
||
// Expected (based on a standard uniform distribution) | ||
const expectedMean = POPULATION / 2; | ||
const expectedStd = POPULATION / STD_RATIO; | ||
|
||
assert.isBelow(mean, expectedMean * 1.02); | ||
assert.isAbove(mean, expectedMean * 0.98); | ||
|
||
assert.isBelow(std, expectedStd * 1.01); | ||
assert.isAbove(std, expectedStd * 0.99); | ||
}); | ||
|
||
test('findManyRandom', async () => { | ||
const users1 = await prisma.user.findManyRandom(POPULATION, { | ||
where: { firstName: 'User0' }, | ||
}); | ||
assert.lengthOf(users1, POPULATION / 2); | ||
|
||
const users2 = await prisma.user.findManyRandom(POPULATION + 10009); | ||
assert.lengthOf(users2, POPULATION); | ||
}); |
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,4 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
import prismaRandom from '../dist'; | ||
|
||
export const prisma = new PrismaClient().$extends(prismaRandom()); |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,20 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
import prismaRandom from '../dist'; | ||
import { prisma } from './db'; | ||
|
||
export const prisma = new PrismaClient().$extends(prismaRandom()); | ||
const main = async () => { | ||
const user = await prisma.user.findRandom(); | ||
|
||
const post = await prisma.post.findManyRandom(10, { | ||
select: { id: true, title: true }, | ||
where: { | ||
OR: [ | ||
{ title: { contains: 'prisma' } }, | ||
{ content: { contains: 'prisma' } }, | ||
], | ||
published: true, | ||
}, | ||
}); | ||
|
||
console.log({ user, post }); | ||
}; | ||
|
||
main(); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.