Add tests for Directus DB wrapper

This commit is contained in:
Andrew 2023-03-05 14:25:27 +07:00
parent dafaf8d4ee
commit dc33c47b4b
2 changed files with 90 additions and 3 deletions

85
test/directus_db.test.ts Normal file
View file

@ -0,0 +1,85 @@
import { assert, describe, expect, it } from "vitest";
import { DirectusDB } from "../src/directus_db";
describe(
"Direcuts DB wrapper tests",
() => {
const db = new DirectusDB();
it("Test user registration, query and deletion", async () => {
const login = "__________test";
const password = "__________test";
// To clean if previous times test failed
await db.removeUser(login, password);
// Creating user
const resp1 = await db.registerUser(login, password);
expect(resp1).not.toBe(null);
expect(resp1!.login).toBe(login);
// Checking if user exists
const resp2 = await db.authenticateUser(login, password);
expect(resp2).not.toBe(null);
expect(resp2!.login).toBe(login);
// Creating user again, which is not allowed
const resp3 = await db.registerUser(login, password);
expect(resp3).toBe(null);
// Using double password to produce erroring response
const resp4 = await db.authenticateUser(login, password + password);
expect(resp4).toBe(null);
// Deleting user with wrong password
const result1 = await db.removeUser(login, password + password);
expect(result1).toBe(false);
// Deleting user
const result2 = await db.removeUser(login, password);
expect(result2).toBe(true);
// Checking if user exists
const resp5 = await db.authenticateUser(login, password);
expect(resp5).toBe(null);
});
it("Test user stats", async () => {
const login = "__________test";
const password = "__________test";
await db.removeUser(login, password);
const testUser = await db.registerUser(login, password);
// Checking if stats exist
let stats = await db.getStats(testUser!.id);
expect(stats).not.toBe(null);
expect(stats!.belongs_to).toBe(testUser!.id);
expect(stats!.games_won).toBe(0);
expect(stats!.games_lost).toBe(0);
// Updating stats (win times) and checking if they were updated
const result1 = await db.updateStats(testUser!.id, true, false);
expect(result1).toBe(true);
stats = await db.getStats(testUser!.id);
expect(stats).not.toBe(null);
expect(stats!.belongs_to).toBe(testUser!.id);
expect(stats!.games_won).toBe(1);
expect(stats!.games_lost).toBe(0);
// Updating stats (lost times) and checking if they were updated
const result2 = await db.updateStats(testUser!.id, false, true);
expect(result2).toBe(true);
stats = await db.getStats(testUser!.id);
expect(stats).not.toBe(null);
expect(stats!.belongs_to).toBe(testUser!.id);
expect(stats!.games_won).toBe(1);
expect(stats!.games_lost).toBe(1);
// Garbage user id should return false
const result3 = await db.updateStats("asdasdasd", false, true);
expect(result3).toBe(false);
});
},
{ timeout: 10000 }
);

View file

@ -11,7 +11,7 @@
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */ /* Language and Environment */
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, "target": "ESNext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */ // "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
@ -25,7 +25,7 @@
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
/* Modules */ /* Modules */
"module": "commonjs" /* Specify what module code is generated. */, "module": "NodeNext" /* Specify what module code is generated. */,
"rootDir": "./src" /* Specify the root folder within your source files. */, "rootDir": "./src" /* Specify the root folder within your source files. */,
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
@ -99,5 +99,7 @@
/* Completeness */ /* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */
} },
"exclude": ["test"]
} }