From 9c2b08550468ec64ab7924c5727157ac9d9e6bc4 Mon Sep 17 00:00:00 2001 From: mateonunez Date: Thu, 2 Mar 2023 01:10:41 +0100 Subject: [PATCH] test(drill): improve index tests --- packages/drill/test/index.test.js | 109 ++++++++++++++++++++---------- 1 file changed, 74 insertions(+), 35 deletions(-) diff --git a/packages/drill/test/index.test.js b/packages/drill/test/index.test.js index 67568f04..e25ff896 100644 --- a/packages/drill/test/index.test.js +++ b/packages/drill/test/index.test.js @@ -1,11 +1,15 @@ import { test, beforeEach, afterEach } from 'tap' import setupDatabase, { killDatabase, resolveTables, resolveData } from '../drill.js' import { database, logger } from '@mateonunez/asterism-huston' +import symbols from '../lib/symbols.js' +import { allowedDatabases } from '../lib/database.js' +const { privateMethods } = symbols const { mysqlOptions, postgresOptions } = database let dbMysql, queryerMysql let dbPostgres, queryerPostgres + beforeEach(async () => { const { db: _dbMysql, queryer: _queryerMysql } = await setupDatabase(logger, 'mysql', mysqlOptions) dbMysql = _dbMysql @@ -17,69 +21,104 @@ beforeEach(async () => { }) afterEach(async () => { + queryerMysql[privateMethods].dropDatabase(mysqlOptions.databaseName) + queryerPostgres[privateMethods].dropDatabase(postgresOptions.databaseName) await killDatabase(dbMysql) await killDatabase(dbPostgres) }) -test('the database should setup correctly', async ({ end }) => { - test('mysql', async ({ ok }) => { - ok(dbMysql) - ok(queryerMysql) - }) +function getObjects (database) { + return database === 'mysql' ? { db: dbMysql, queryer: queryerMysql } : database === 'postgres' ? { db: dbPostgres, queryer: queryerPostgres } : {} +} + +function getOptions (database) { + return database === 'mysql' ? mysqlOptions : database === 'postgres' ? postgresOptions : {} +} - test('postgres', async ({ ok }) => { - ok(dbPostgres) - ok(queryerPostgres) +test('the database should setup correctly', async ({ end }) => { + allowedDatabases.forEach((supportedDatabase) => { + test(supportedDatabase, async ({ ok }) => { + const { db, queryer } = getObjects(supportedDatabase) + ok(db) + ok(queryer) + }) }) end() }) test('should resolve all the tables', ({ end }) => { - test('mysql', async ({ ok }) => { - const tables = await resolveTables(logger, queryerMysql, mysqlOptions) + allowedDatabases.forEach((supportedDatabase) => { + test(supportedDatabase, async ({ same }) => { + const { queryer } = getObjects(supportedDatabase) + const tables = await resolveTables(logger, queryer, getOptions(supportedDatabase)) - ok(tables) + same(tables, ['test']) + }) }) - test('postgres', async ({ ok }) => { - const tables = await resolveTables(logger, queryerPostgres, postgresOptions) + end() +}) - ok(tables) +test('should resolve single table', ({ end }) => { + allowedDatabases.forEach((supportedDatabase) => { + test(supportedDatabase, async ({ same, teardown }) => { + const { queryer } = getObjects(supportedDatabase) + await queryer[privateMethods].createTable('users', { + id: { + type: 'int', + primaryKey: true + } + }) + + const tables = await resolveTables(logger, queryer, { ...getOptions(supportedDatabase), tableName: 'users' }) + await queryer[privateMethods].dropTable('users') + + same(tables, ['users']) + }) }) end() }) -test('should resolve single table', ({ end }) => { - test('mysql', async ({ ok }) => { - const tables = await resolveTables(logger, queryerMysql, { mysqlOptions, tableName: 'users' }) - - ok(tables) - }) +test('should resolve single table even if it not exists', ({ end }) => { + allowedDatabases.forEach((supportedDatabase) => { + test(supportedDatabase, async ({ same, teardown }) => { + const { queryer } = getObjects(supportedDatabase) - test('postgres', async ({ ok }) => { - const tables = await resolveTables(logger, queryerPostgres, { ...postgresOptions, tableName: 'users' }) + const tables = await resolveTables(logger, queryer, { ...getOptions(supportedDatabase), tableName: 'users' }) - ok(tables) + same(tables, [null]) + }) }) end() }) test('should resolve data', ({ end }) => { - test('mysql', async ({ ok }) => { - const tables = await resolveTables(logger, queryerMysql, mysqlOptions) - const data = await resolveData(logger, queryerMysql, tables, mysqlOptions) - - ok(data) - }) - - test('postgres', async ({ ok }) => { - const tables = await resolveTables(logger, queryerPostgres, postgresOptions) - const data = await resolveData(logger, queryerPostgres, tables, postgresOptions) - - ok(data) + allowedDatabases.forEach((supportedDatabase) => { + test(supportedDatabase, async ({ same }) => { + const { queryer } = getObjects(supportedDatabase) + await queryer[privateMethods].createTable('users', { + name: { + type: 'varchar', + length: 255 + } + }, { dropIfExists: true }) + + await queryer[privateMethods].insertData('users', { name: 'John' }) + + const tables = await resolveTables(logger, queryer, { ...getOptions(supportedDatabase), tableName: 'users' }) + const data = await resolveData(logger, queryer, tables, { ...getOptions(supportedDatabase), tableName: 'users' }) + + await queryer[privateMethods].dropTable('users') + + same(data, { + users: [{ + name: 'John' + }] + }) + }) }) end()