added test factory (#81)

* added test factory

* update comment
This commit is contained in:
Axel Adlercreutz
2025-02-11 09:30:27 +02:00
committed by GitHub
parent c89b54d1d1
commit 8a90a000c0
4 changed files with 78 additions and 2 deletions
+16
View File
@@ -8,6 +8,7 @@
"name": "gradesa",
"version": "0.1.0",
"dependencies": {
"@faker-js/faker": "^9.4.0",
"axios": "^1.7.9",
"dotenv": "^16.4.7",
"next": "15.1.5",
@@ -1029,6 +1030,21 @@
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
}
},
"node_modules/@faker-js/faker": {
"version": "9.4.0",
"resolved": "https://registry.npmjs.org/@faker-js/faker/-/faker-9.4.0.tgz",
"integrity": "sha512-85+k0AxaZSTowL0gXp8zYWDIrWclTbRPg/pm/V0dSFZ6W6D4lhcG3uuZl4zLsEKfEvs69xDbLN2cHQudwp95JA==",
"funding": [
{
"type": "opencollective",
"url": "https://opencollective.com/fakerjs"
}
],
"engines": {
"node": ">=18.0.0",
"npm": ">=9.0.0"
}
},
"node_modules/@humanfs/core": {
"version": "0.19.1",
"resolved": "https://registry.npmjs.org/@humanfs/core/-/core-0.19.1.tgz",
+2 -1
View File
@@ -11,6 +11,7 @@
"test": "vitest --disable-console-intercept "
},
"dependencies": {
"@faker-js/faker": "^9.4.0",
"axios": "^1.7.9",
"dotenv": "^16.4.7",
"next": "15.1.5",
@@ -27,9 +28,9 @@
"eslint": "^9",
"eslint-config-next": "15.1.5",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-unused-imports": "^4.1.4",
"eslint-plugin-react": "^7.37.4",
"eslint-plugin-react-refresh": "^0.4.18",
"eslint-plugin-unused-imports": "^4.1.4",
"jsdom": "^26.0.0",
"ts-node": "^10.9.2",
"vitest": "^3.0.4"
+45
View File
@@ -0,0 +1,45 @@
import { isTest } from "../config";
import { faker } from "@faker-js/faker";
import { createHash } from "node:crypto";
import { DB } from "../db";
/**
* @description Factory function to create a model for a given table.
* @param {string} tableName - The name of the table for which the model is being created.
* @param {Object|Function} base - The base values or a function returning base values for the model.
* @returns {function} - A function that creates a model for the given table.
*/
function modelFactory(tableName, base) {
return async function (mod) {
if (!isTest) {
throw new Error("This function is only available in test mode");
}
const baseValues = typeof base === "function" ? base() : base;
const model = { ...baseValues, ...mod };
const insertedRow = await DB.pool(
`INSERT INTO ${tableName} (${Object.keys(model).join(", ")}) VALUES (${Object.values(
model
)
.map((_, i) => `$${i + 1}`)
.join(", ")}) RETURNING *`,
Object.values(model)
);
if (insertedRow.rows.length === 0) {
throw new Error("No row was inserted");
}
return insertedRow.rows[0];
};
}
faker.seed(123);
const user = modelFactory("users", () => ({
email: faker.internet.email(),
password_hash: createHash("sha256")
.update(faker.internet.password())
.digest("hex"),
}));
export const TestFactory = {
user,
};
+14
View File
@@ -0,0 +1,14 @@
import { useTestDatabase } from "./testdb";
import { TestFactory } from "./testfactory";
import { describe, it, expect } from "vitest";
describe("User", () => {
useTestDatabase();
it("should create a user", async () => {
const user = await TestFactory.user();
expect(user).toBeDefined();
expect(user.email).toBeDefined();
expect(user.password_hash).toBeDefined();
expect(user.id).toBe("1");
});
});