From 3d349b2121876a969ed3a674042c5a34d43b4743 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 27 Dec 2021 15:57:04 +0100 Subject: [PATCH] feat: Test Create User Mutation --- backend/jest.config.js | 2 ++ backend/package.json | 4 ++- .../src/graphql/resolver/UserResolver.test.ts | 30 +++++++++++++++++ backend/tsconfig.json | 4 ++- database/package.json | 3 ++ database/src/config/index.ts | 2 +- database/src/helpers.ts | 33 +++++++++++++++++++ database/src/index.ts | 26 ++------------- database/tsconfig.json | 4 ++- 9 files changed, 81 insertions(+), 27 deletions(-) create mode 100644 backend/src/graphql/resolver/UserResolver.test.ts create mode 100644 database/src/helpers.ts diff --git a/backend/jest.config.js b/backend/jest.config.js index 3cee980c5..18cf4c2f1 100644 --- a/backend/jest.config.js +++ b/backend/jest.config.js @@ -6,5 +6,7 @@ module.exports = { collectCoverageFrom: ['src/**/*.ts', '!**/node_modules/**'], moduleNameMapper: { '@entity/(.*)': '/../database/entity/$1', + '@dbTools/(.*)': '/../database/src/$1', + '@migrations/(.*)': '/../database/migrations/$1', }, } diff --git a/backend/package.json b/backend/package.json index e573a2704..9dcb8ec9a 100644 --- a/backend/package.json +++ b/backend/package.json @@ -59,6 +59,8 @@ "typescript": "^4.3.4" }, "_moduleAliases": { - "@entity": "../database/build/entity" + "@entity": "../database/build/entity", + "@migrations": "../database/migrations", + "@dbTools": "../database/src" } } diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts new file mode 100644 index 000000000..bd653b6df --- /dev/null +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -0,0 +1,30 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ + +import { createTestClient } from 'apollo-server-testing' +import createServer from '../../server/createServer' +import { resetDB, initialize } from '@dbTools/helpers' + +let mutate: any +let con: any + +beforeAll(async () => { + const server = await createServer({}) + con = server.con + mutate = createTestClient(server.apollo).mutate + await initialize() + await resetDB() +}) + +describe('UserResolver', () => { + describe('createUser', () => { + it('works', () => { + expect(true).toBeTruthy() + }) + }) +}) + +afterAll(async () => { + await resetDB(true) + await con.close() +}) diff --git a/backend/tsconfig.json b/backend/tsconfig.json index 73e00a1a0..9d010b198 100644 --- a/backend/tsconfig.json +++ b/backend/tsconfig.json @@ -47,7 +47,9 @@ // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ "paths": { /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ - "@entity/*": ["../database/entity/*"] + "@entity/*": ["../database/entity/*"], + "@dbTools/*": ["../database/src/*"], + "@migrations/*": ["../database/migrations/*"] }, // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ // "typeRoots": [], /* List of folders to include type definitions from. */ diff --git a/database/package.json b/database/package.json index 7ff5c60d3..6a061607a 100644 --- a/database/package.json +++ b/database/package.json @@ -45,5 +45,8 @@ "ts-mysql-migrate": "^1.0.2", "typeorm": "^0.2.38", "typeorm-seeding": "^1.6.1" + }, + "_moduleAliases": { + "@migrations": "./migrations" } } diff --git a/database/src/config/index.ts b/database/src/config/index.ts index 908d40311..86f96b9b6 100644 --- a/database/src/config/index.ts +++ b/database/src/config/index.ts @@ -13,7 +13,7 @@ const database = { const migrations = { MIGRATIONS_TABLE: process.env.MIGRATIONS_TABLE || 'migrations', - MIGRATIONS_DIRECTORY: process.env.MIGRATIONS_DIRECTORY || './migrations/', + MIGRATIONS_DIRECTORY: process.env.MIGRATIONS_DIRECTORY || '@migrations/', } const CONFIG = { ...database, ...migrations } diff --git a/database/src/helpers.ts b/database/src/helpers.ts new file mode 100644 index 000000000..1ef8b5d1f --- /dev/null +++ b/database/src/helpers.ts @@ -0,0 +1,33 @@ +import CONFIG from './config' +import { createPool, PoolConfig } from 'mysql' +import { Migration } from 'ts-mysql-migrate' + +const poolConfig: PoolConfig = { + host: CONFIG.DB_HOST, + port: CONFIG.DB_PORT, + user: CONFIG.DB_USER, + password: CONFIG.DB_PASSWORD, + database: CONFIG.DB_DATABASE, +} + +// Pool? +const pool = createPool(poolConfig) + +// Create & Initialize Migrations +const migration = new Migration({ + conn: pool, + tableName: CONFIG.MIGRATIONS_TABLE, + silent: true, + dir: '../database/migrations/', // CONFIG.MIGRATIONS_DIRECTORY, +}) + +const initialize = async () => { + await migration.initialize() +} + +const resetDB = async (closePool = false): Promise => { + await migration.reset() // use for resetting database + if (closePool) pool.end() +} + +export { resetDB, pool, migration, initialize } diff --git a/database/src/index.ts b/database/src/index.ts index 94566c9f5..5be8f4edb 100644 --- a/database/src/index.ts +++ b/database/src/index.ts @@ -1,7 +1,4 @@ import 'reflect-metadata' -import { createPool, PoolConfig } from 'mysql' -import { Migration } from 'ts-mysql-migrate' -import CONFIG from './config' import prepare from './prepare' import connection from './typeorm/connection' import { useSeeding, runSeeder } from 'typeorm-seeding' @@ -10,36 +7,19 @@ import { CreateBibiBloxbergSeed } from './seeds/users/bibi-bloxberg.seed' import { CreateRaeuberHotzenplotzSeed } from './seeds/users/raeuber-hotzenplotz.seed' import { CreateBobBaumeisterSeed } from './seeds/users/bob-baumeister.seed' import { DecayStartBlockSeed } from './seeds/decay-start-block.seed' +import { resetDB, pool, migration } from './helpers' const run = async (command: string) => { // Database actions not supported by our migration library await prepare() - // Database connection for Migrations - const poolConfig: PoolConfig = { - host: CONFIG.DB_HOST, - port: CONFIG.DB_PORT, - user: CONFIG.DB_USER, - password: CONFIG.DB_PASSWORD, - database: CONFIG.DB_DATABASE, - } - - // Pool? - const pool = createPool(poolConfig) - - // Create & Initialize Migrations - const migration = new Migration({ - conn: pool, - tableName: CONFIG.MIGRATIONS_TABLE, - dir: CONFIG.MIGRATIONS_DIRECTORY, - }) - // Database connection for TypeORM const con = await connection() if (!con || !con.isConnected) { throw new Error(`Couldn't open connection to database`) } + // Database connection for Migrations await migration.initialize() // Execute command @@ -52,7 +32,7 @@ const run = async (command: string) => { break case 'reset': // TODO protect from production - await migration.reset() // use for resetting database + await resetDB(false) // use for resetting database break case 'seed': // TODO protect from production diff --git a/database/tsconfig.json b/database/tsconfig.json index 445b9d11f..f0e707dd2 100644 --- a/database/tsconfig.json +++ b/database/tsconfig.json @@ -46,7 +46,9 @@ /* Module Resolution Options */ // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ - // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ + "paths": { /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ + "@migrations/*": ["./migrations/*"], + }, // "rootDirs": [".", "../database"], /* List of root folders whose combined content represents the structure of the project at runtime. */ // "typeRoots": [], /* List of folders to include type definitions from. */ // "types": [], /* Type declaration files to be included in compilation. */