Skip to content

Commit

Permalink
Add unit tests (#10)
Browse files Browse the repository at this point in the history
* 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
nkeil authored Mar 30, 2024
1 parent 34af2d6 commit 21f0c16
Show file tree
Hide file tree
Showing 12 changed files with 1,237 additions and 415 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ jobs:
- name: Generate prisma schema
run: npx prisma generate

# If we ever add ESLint, uncomment this
# - name: Run ESLint
# run: pnpm lint

- name: Build
run: pnpm build

- name: Test
run: pnpm test

- name: Run Prettier
run: pnpm format --list-different
75 changes: 75 additions & 0 deletions example/db.test.ts
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);
});
4 changes: 4 additions & 0 deletions example/db.ts
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());
20 changes: 0 additions & 20 deletions example/db/test.ts

This file was deleted.

22 changes: 19 additions & 3 deletions example/index.ts
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();
17 changes: 0 additions & 17 deletions example/package.json

This file was deleted.

Loading

0 comments on commit 21f0c16

Please sign in to comment.