From da81ba1761897a06b70a121595fd2c01585b1fa4 Mon Sep 17 00:00:00 2001 From: mateonunez Date: Wed, 1 Mar 2023 19:11:20 +0100 Subject: [PATCH 1/8] chore(cli): add missing search test --- packages/cli/test/index.test.mjs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/cli/test/index.test.mjs b/packages/cli/test/index.test.mjs index 020250ee..9f6c153c 100644 --- a/packages/cli/test/index.test.mjs +++ b/packages/cli/test/index.test.mjs @@ -19,3 +19,9 @@ test('migrate', async ({ ok }) => { ok(stdout.includes('Usage: asterism migrate [options] [database]')) }) + +test('search', async ({ ok }) => { + const { stdout } = await execa('node', [cliPath, 'search', '--help']) + + ok(stdout.includes('Usage: asterism search [options] [term]')) +}) From 686286784c39710310b52c33de7f36e6275a2825 Mon Sep 17 00:00:00 2001 From: mateonunez Date: Thu, 2 Mar 2023 00:17:19 +0100 Subject: [PATCH 2/8] feat(common): add databaseExists method --- packages/drill/lib/queries/common.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/drill/lib/queries/common.js b/packages/drill/lib/queries/common.js index c2db5e62..a30ef33d 100644 --- a/packages/drill/lib/queries/common.js +++ b/packages/drill/lib/queries/common.js @@ -6,6 +6,7 @@ export default async function (logger, db, sql) { return { selectData: (tableName, columns, where) => selectData(logger, db, sql, tableName, columns, where), [symbols.privateMethods]: { + databaseExists: (database) => databaseExists(logger, db, sql, database), createDatabase: (database, options) => createDatabase(logger, db, sql, database, options), dropDatabase: (database, options) => dropDatabase(logger, db, sql, database), createTable: (tableName, columns, options) => createTable(logger, db, sql, tableName, columns, options), @@ -17,6 +18,16 @@ export default async function (logger, db, sql) { } } +async function databaseExists (logger, db, sql, database) { + if (logger) logger.info(`Checking if database "${database}" exists.`) + const response = await db.query(sql` + SELECT SCHEMA_NAME + FROM INFORMATION_SCHEMA.SCHEMATA + WHERE SCHEMA_NAME = ${database} + `) + return response.length > 0 +} + async function selectData (logger, db, sql, tableName, columns = '*', where = {}) { if (logger) logger.info(`Selecting data from "${tableName}".`) const columnDefinitions = [] From 60c470e918c2b5364b8972ea3a0983d086071599 Mon Sep 17 00:00:00 2001 From: mateonunez Date: Thu, 2 Mar 2023 00:17:34 +0100 Subject: [PATCH 3/8] test(drill): improve tests --- packages/drill/test/common.test.js | 186 +++++++++++++---------------- 1 file changed, 85 insertions(+), 101 deletions(-) diff --git a/packages/drill/test/common.test.js b/packages/drill/test/common.test.js index 75168d3a..76ffb3db 100644 --- a/packages/drill/test/common.test.js +++ b/packages/drill/test/common.test.js @@ -2,6 +2,7 @@ import { test } from 'tap' import setupDatabase, { killDatabase } from './../drill.js' import symbols from '../lib/symbols.js' import { logger, database } from '@mateonunez/asterism-huston' +import { allowedDatabases } from '../lib/database.js' const { mysqlOptions, postgresOptions } = database const { privateMethods } = symbols @@ -26,131 +27,114 @@ async function insertIntoTable (queryer, tableName) { }) } -test('should drop a database', ({ end }) => { - test('mysql', async ({ ok }) => { - const { db, queryer } = await setupDatabase(logger, 'mysql', mysqlOptions) - const databaseName = 'test' - await queryer[privateMethods].createDatabase(databaseName, { dropIfExists: true }) - - await queryer[privateMethods].dropDatabase(databaseName) - await killDatabase(db) - ok(db) - }) - - test('postgres', async ({ ok }) => { - const { db, queryer } = await setupDatabase(logger, 'postgres', postgresOptions) - const databaseName = 'test' - await queryer[privateMethods].createDatabase(databaseName, { dropIfExists: true }) +function getOptions (database) { + return database === 'mysql' ? mysqlOptions : database === 'postgres' ? postgresOptions : {} +} - await queryer[privateMethods].dropDatabase(databaseName) - await killDatabase(db) - ok(db) +test('should drop a database', ({ end }) => { + allowedDatabases.forEach((supportedDatabase) => { + test(supportedDatabase, async ({ equal, teardown }) => { + teardown(async () => { + await killDatabase(db) + }) + + const { db, queryer } = await setupDatabase(logger, supportedDatabase, getOptions(supportedDatabase)) + const databaseName = 'test' + await queryer[privateMethods].createDatabase(databaseName, { dropIfExists: true }) + await queryer[privateMethods].dropDatabase(databaseName) + + equal(await queryer[privateMethods].databaseExists(databaseName), false) + }) }) - end() }) test('should create a new table', ({ end }) => { - test('mysql', async ({ ok }) => { - const { db, queryer } = await setupDatabase(logger, 'mysql', mysqlOptions) - const tableName = 'common_table_test' - await createTable(queryer, tableName) - await queryer[privateMethods].dropTable(tableName) - await killDatabase(db) - - ok(db) - }) - - test('postgres', async ({ ok }) => { - const { db, queryer } = await setupDatabase(logger, 'postgres', postgresOptions) - const tableName = 'common_table_test' - await createTable(queryer, tableName) - await queryer[privateMethods].dropTable(tableName) - await killDatabase(db) - - ok(db) + allowedDatabases.forEach((supportedDatabase) => { + test(supportedDatabase, async ({ same, teardown }) => { + teardown(async () => { + await queryer[privateMethods].dropTable(tableName) + await killDatabase(db) + }) + + const { db, queryer } = await setupDatabase(logger, supportedDatabase, getOptions(supportedDatabase)) + const tableName = 'common_table_test' + await createTable(queryer, tableName) + const data = await queryer.selectData(tableName) + + same(data, []) + }) }) end() }) test('should insert data into a table', ({ end }) => { - test('mysql', async ({ ok }) => { - const { db, queryer } = await setupDatabase(logger, 'mysql', mysqlOptions) - const tableName = 'common_table_test' - await createTable(queryer, tableName) - await insertIntoTable(queryer, tableName) - await queryer[privateMethods].dropTable(tableName) - await killDatabase(db) - - ok(db) - }) - - test('postgres', async ({ ok }) => { - const { db, queryer } = await setupDatabase(logger, 'postgres', postgresOptions) - const tableName = 'common_table_test' - await createTable(queryer, tableName) - await insertIntoTable(queryer, tableName) - await queryer[privateMethods].dropTable(tableName) - await killDatabase(db) - - ok(db) + allowedDatabases.forEach((supportedDatabase) => { + test(supportedDatabase, async ({ same, teardown }) => { + teardown(async () => { + await queryer[privateMethods].dropTable(tableName) + await killDatabase(db) + }) + + const { db, queryer } = await setupDatabase(logger, supportedDatabase, getOptions(supportedDatabase)) + const tableName = 'common_table_test' + await createTable(queryer, tableName) + await insertIntoTable(queryer, tableName) + const data = await queryer.selectData(tableName) + + same(data, [{ + id: 1, + name: 'test' + }]) + }) }) end() }) test('should delete data', ({ end }) => { - test('mysql', async ({ ok }) => { - const { db, queryer } = await setupDatabase(logger, 'mysql', mysqlOptions) - const tableName = 'common_table_test' - await createTable(queryer, tableName) - await insertIntoTable(queryer, tableName) - await queryer[privateMethods].deleteData(tableName, { id: 1 }) - await queryer[privateMethods].dropTable(tableName) - await killDatabase(db) - - ok(db) - }) - - test('postgres', async ({ ok }) => { - const { db, queryer } = await setupDatabase(logger, 'postgres', postgresOptions) - const tableName = 'common_table_test' - await createTable(queryer, tableName) - await insertIntoTable(queryer, tableName) - await queryer[privateMethods].deleteData(tableName, { id: 1 }) - await queryer[privateMethods].dropTable(tableName) - await killDatabase(db) - - ok(db) + allowedDatabases.forEach((supportedDatabase) => { + test(supportedDatabase, async ({ same, teardown }) => { + teardown(async () => { + await queryer[privateMethods].dropTable(tableName) + await killDatabase(db) + }) + + const { db, queryer } = await setupDatabase(logger, supportedDatabase, getOptions(supportedDatabase)) + const tableName = 'common_table_test' + await createTable(queryer, tableName) + await insertIntoTable(queryer, tableName) + await queryer[privateMethods].deleteData(tableName, { id: 1 }) + const data = await queryer.selectData(tableName) + + same(data, []) + }) }) end() }) test('should update data', ({ end }) => { - test('mysql', async ({ ok }) => { - const { db, queryer } = await setupDatabase(logger, 'mysql', mysqlOptions) - const tableName = 'common_table_test' - await createTable(queryer, tableName) - await insertIntoTable(queryer, tableName) - await queryer[privateMethods].updateData(tableName, { id: 1 }, { name: 'test2' }) - await queryer[privateMethods].dropTable(tableName) - await killDatabase(db) - - ok(db) - }) - - test('postgres', async ({ ok }) => { - const { db, queryer } = await setupDatabase(logger, 'postgres', postgresOptions) - const tableName = 'common_table_test' - await createTable(queryer, tableName) - await insertIntoTable(queryer, tableName) - await queryer[privateMethods].updateData(tableName, { id: 1 }, { name: 'test2' }) - await queryer[privateMethods].dropTable(tableName) - await killDatabase(db) - - ok(db) + allowedDatabases.forEach((supportedDatabase) => { + test(supportedDatabase, async ({ same, teardown }) => { + teardown(async () => { + await queryer[privateMethods].dropTable(tableName) + await killDatabase(db) + }) + + const { db, queryer } = await setupDatabase(logger, supportedDatabase, getOptions(supportedDatabase)) + const tableName = 'common_table_test' + await createTable(queryer, tableName) + await insertIntoTable(queryer, tableName) + await queryer[privateMethods].updateData(tableName, { name: 'test2' }, { id: 1 }) + const data = await queryer.selectData(tableName) + + same(data, [{ + id: 1, + name: 'test2' + }]) + }) }) end() From 148f2ac683df22e6c5b0cbbe59ca170a1619a4a4 Mon Sep 17 00:00:00 2001 From: mateonunez Date: Thu, 2 Mar 2023 00:24:31 +0100 Subject: [PATCH 4/8] test(drill): more improvements --- packages/drill/test/database.test.js | 8 ++++---- packages/drill/test/errors.test.js | 16 ++++------------ 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/packages/drill/test/database.test.js b/packages/drill/test/database.test.js index 78fa3240..ed9bd983 100644 --- a/packages/drill/test/database.test.js +++ b/packages/drill/test/database.test.js @@ -5,14 +5,14 @@ import { database } from '@mateonunez/asterism-huston' const { mysqlOptions, postgresOptions } = database test('should generate correctly the connection string', ({ end }) => { - test('mysql', async ({ same }) => { + test('mysql', async ({ equal }) => { const { mysql } = generateConnectionString(mysqlOptions) - same(mysql, 'mysql://root:toor@127.0.0.1:3306/db') + equal(mysql, 'mysql://root:toor@127.0.0.1:3306/db') }) - test('postgres', async ({ same }) => { + test('postgres', async ({ equal }) => { const { postgres } = generateConnectionString(postgresOptions) - same(postgres, 'postgres://postgres:toor@127.0.0.1:5432/db') + equal(postgres, 'postgres://postgres:toor@127.0.0.1:5432/db') }) end() diff --git a/packages/drill/test/errors.test.js b/packages/drill/test/errors.test.js index 871a6b4a..6bcece3b 100644 --- a/packages/drill/test/errors.test.js +++ b/packages/drill/test/errors.test.js @@ -1,18 +1,10 @@ import { test } from 'tap' import setupDatabase from '../drill.js' -test('should throw an error when the database is not support', async ({ ok }) => { - try { - await setupDatabase(null, 'oracle', {}) - } catch (err) { - ok(err) - } +test('should throw an error when the database is not support', async ({ rejects }) => { + await rejects(setupDatabase(null, 'fake', {}), 'Database not supported') }) -test('should throw an error when the parameters are wront', async ({ ok }) => { - try { - await setupDatabase(null, 'mysql', { user: 'fake', password: 'kaboom' }) - } catch (err) { - ok(err) - } +test('should throw an error when the parameters are wront', async ({ rejects }) => { + await rejects(setupDatabase(null, 'mysql', { user: 'fake', password: 'kaboom' }), 'Error connecting to database') }) From 6cbff4ef51283ba915799af72c9b41cb154cfd85 Mon Sep 17 00:00:00 2001 From: mateonunez Date: Thu, 2 Mar 2023 01:10:41 +0100 Subject: [PATCH 5/8] 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() From 3e3e5a708d81996df72c67ce765fa2de88a43840 Mon Sep 17 00:00:00 2001 From: mateonunez Date: Tue, 7 Mar 2023 19:01:20 +0100 Subject: [PATCH 6/8] chore(falcon): fix tests Signed-off-by: mateonunez --- packages/falcon/test/index.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/falcon/test/index.test.js b/packages/falcon/test/index.test.js index 6d7f3b3d..e4d38f0e 100644 --- a/packages/falcon/test/index.test.js +++ b/packages/falcon/test/index.test.js @@ -9,6 +9,10 @@ t.before(async () => { await seeder(undefined, 'mysql', { ...mysqlOptions, outputDir: './orama' }) }) +t.afterEach(async () => { + await dropSeed(undefined, 'mysql', { ...mysqlOptions, outputDir: './lyra' }) +}) + t.test('no database selected should default to mysql', async ({ ok }) => { const { db } = await falconMigrate(undefined, { ...mysqlOptions, outputDir: './orama' }) ok(db.isMysql) From b4c01cbee16484078ce09b8992b2f14810c1d186 Mon Sep 17 00:00:00 2001 From: mateonunez Date: Tue, 7 Mar 2023 19:02:14 +0100 Subject: [PATCH 7/8] chore(drill): improve tests Signed-off-by: mateonunez --- packages/drill/test/common.test.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/drill/test/common.test.js b/packages/drill/test/common.test.js index 76ffb3db..016c4ee4 100644 --- a/packages/drill/test/common.test.js +++ b/packages/drill/test/common.test.js @@ -38,9 +38,10 @@ test('should drop a database', ({ end }) => { await killDatabase(db) }) - const { db, queryer } = await setupDatabase(logger, supportedDatabase, getOptions(supportedDatabase)) const databaseName = 'test' - await queryer[privateMethods].createDatabase(databaseName, { dropIfExists: true }) + + const { db, queryer } = await setupDatabase(logger, supportedDatabase, getOptions(supportedDatabase)) + await queryer[privateMethods].createDatabase('test', { dropIfExists: true }) await queryer[privateMethods].dropDatabase(databaseName) equal(await queryer[privateMethods].databaseExists(databaseName), false) @@ -54,10 +55,14 @@ test('should create a new table', ({ end }) => { test(supportedDatabase, async ({ same, teardown }) => { teardown(async () => { await queryer[privateMethods].dropTable(tableName) + await queryer[privateMethods].dropDatabase('test') await killDatabase(db) }) - - const { db, queryer } = await setupDatabase(logger, supportedDatabase, getOptions(supportedDatabase)) + const { db, queryer } = await setupDatabase(logger, supportedDatabase, { + ...getOptions(supportedDatabase), + database: 'test' + }) + await queryer[privateMethods].createDatabase('test', { dropIfExists: true }) const tableName = 'common_table_test' await createTable(queryer, tableName) const data = await queryer.selectData(tableName) From e1e59121a50b58fa8f7013ee88fef46a6f498fdc Mon Sep 17 00:00:00 2001 From: mateonunez Date: Fri, 21 Apr 2023 20:29:10 +0200 Subject: [PATCH 8/8] chore: work in progress Signed-off-by: mateonunez --- packages/drill/lib/database.js | 1 - packages/drill/lib/queries/common.js | 3 --- packages/drill/lib/queries/postgres.js | 2 +- packages/drill/test/postgres.test.js | 18 ++++++++++++++++++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/drill/lib/database.js b/packages/drill/lib/database.js index a26dc05b..a68dd54a 100644 --- a/packages/drill/lib/database.js +++ b/packages/drill/lib/database.js @@ -1,6 +1,5 @@ export function generateConnectionString ({ host, port, user, password, databaseName }) { return { - /* c8 ignore next 2 */ postgres: `postgres://${user}:${encodeURIComponent(password)}@${host}:${port}${databaseName ? `/${databaseName}` : '/db'}`, mysql: `mysql://${user}:${encodeURIComponent(password)}@${host}:${port}${databaseName ? `/${databaseName}` : '/db'}` } diff --git a/packages/drill/lib/queries/common.js b/packages/drill/lib/queries/common.js index a30ef33d..b0ae1816 100644 --- a/packages/drill/lib/queries/common.js +++ b/packages/drill/lib/queries/common.js @@ -32,7 +32,6 @@ async function selectData (logger, db, sql, tableName, columns = '*', where = {} if (logger) logger.info(`Selecting data from "${tableName}".`) const columnDefinitions = [] const values = [] - /* c8 ignore next 8 */ if (columns === '*') { columnDefinitions.push(sql`*`) } else { @@ -41,7 +40,6 @@ async function selectData (logger, db, sql, tableName, columns = '*', where = {} values.push(value) } } - /* c8 ignore next 10 */ const conditions = [] for (const [name, value] of Object.entries(where)) { conditions.push(sql`${sql.ident(name)} = ${sql.value(value)}`) @@ -70,7 +68,6 @@ async function dropDatabase (logger, db, sql, database) { async function createTable (logger, db, sql, tableName, columns, options) { if (logger) logger.info(`Creating table "${tableName}".`) const columnDefinitions = [] - /* c8 ignore next 6 */ for (const [name, definition] of Object.entries(columns)) { const dangerousRaw = sql.__dangerous__rawValue(`${definition.type + (definition.length ? `(${definition.length})` : '')} ${definition.autoIncrement ? 'AUTO_INCREMENT' : ''} ${definition.primaryKey ? 'PRIMARY KEY' : ''}`) const row = sql`${sql.ident(name)} ${dangerousRaw}` diff --git a/packages/drill/lib/queries/postgres.js b/packages/drill/lib/queries/postgres.js index 61d25f43..a4d20d1c 100644 --- a/packages/drill/lib/queries/postgres.js +++ b/packages/drill/lib/queries/postgres.js @@ -25,7 +25,7 @@ async function getTables (logger, db, sql) { WHERE table_schema = 'public' AND table_type = 'BASE TABLE' `) - /* c8 ignore next 3 */ + console.log({ response }) if (response.length === 0) { if (logger) logger.warn('No tables found.') } diff --git a/packages/drill/test/postgres.test.js b/packages/drill/test/postgres.test.js index 9c345e7e..87500344 100644 --- a/packages/drill/test/postgres.test.js +++ b/packages/drill/test/postgres.test.js @@ -12,6 +12,7 @@ test('should create a new database', async ({ ok, end }) => { await queryer[privateMethods].createDatabase(databaseName, { dropIfExists: true }) await killDatabase(_db) const { db } = await setupDatabase(logger, 'postgres', { ...postgresOptions, databaseName }) + await queryer[privateMethods].dropDatabase(databaseName) await killDatabase(db) ok(_db) @@ -20,6 +21,8 @@ test('should create a new database', async ({ ok, end }) => { test('should retrieve tables', async ({ ok, end }) => { const { db, queryer } = await setupDatabase(logger, 'postgres', postgresOptions) + const databaseName = 'new_database_2' + await queryer[privateMethods].createDatabase(databaseName, { dropIfExists: true }) const tableName = 'test' await queryer[privateMethods].createTable(tableName, { id: { @@ -33,6 +36,7 @@ test('should retrieve tables', async ({ ok, end }) => { }, { dropIfExists: true }) const tables = await queryer.getTables() + await queryer[privateMethods].dropDatabase(databaseName) await killDatabase(db) ok(tables.length > 0) @@ -41,6 +45,8 @@ test('should retrieve tables', async ({ ok, end }) => { test('should retrieve single table', async ({ ok, end }) => { const { db, queryer } = await setupDatabase(logger, 'postgres', postgresOptions) + const databaseName = 'new_database_3' + await queryer[privateMethods].createDatabase(databaseName, { dropIfExists: true }) const tableName = 'test' await queryer[privateMethods].createTable(tableName, { id: { @@ -54,8 +60,20 @@ test('should retrieve single table', async ({ ok, end }) => { }, { dropIfExists: true }) const tables = await queryer.getTable('test') + await queryer[privateMethods].dropDatabase(databaseName) await killDatabase(db) ok(tables) end() }) + +test('should log warning when table does not exist', async ({ same, end }) => { + const { db, queryer } = await setupDatabase(undefined, 'postgres', postgresOptions) + await queryer[privateMethods].createDatabase('empty_database', { dropIfExists: true }) + const tables = await queryer.getTables() + await queryer[privateMethods].dropDatabase('empty_database') + await killDatabase(db) + + same(tables, []) + end() +})