From dc33c47b4b0d821bfe946b69c449f2e5061e6be6 Mon Sep 17 00:00:00 2001 From: Andrew nuark G Date: Sun, 5 Mar 2023 14:25:27 +0700 Subject: [PATCH] Add tests for Directus DB wrapper --- test/directus_db.test.ts | 85 ++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 8 ++-- 2 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 test/directus_db.test.ts diff --git a/test/directus_db.test.ts b/test/directus_db.test.ts new file mode 100644 index 0000000..05eeba2 --- /dev/null +++ b/test/directus_db.test.ts @@ -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 } +); diff --git a/tsconfig.json b/tsconfig.json index ad9258d..a47160d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,7 @@ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ /* 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. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ // "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. */ /* 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. */, // "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. */ @@ -99,5 +99,7 @@ /* Completeness */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */ - } + }, + + "exclude": ["test"] }