Skip to content

Commit

Permalink
refactor(test): remove tap from drill package
Browse files Browse the repository at this point in the history
Signed-off-by: mateonunez <mateonunez95@gmail.com>
  • Loading branch information
mateonunez committed Nov 11, 2023
1 parent 2aaed10 commit 31c4145
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 1,218 deletions.
6 changes: 3 additions & 3 deletions packages/drill/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"lint": "standard | snazzy",
"lint:fix": "standard --fix | snazzy",
"test": "npm run lint && c8 --100 tap --no-coverage test/**/*.test.js"
"test": "npm run lint:fix && c8 node --test --test-reporter spec"
},
"repository": {
"type": "git",
Expand All @@ -24,10 +24,10 @@
"@mateonunez/asterism-huston": "workspace:*"
},
"devDependencies": {
"@matteo.collina/tspl": "^0.1.0",
"c8": "^8.0.1",
"snazzy": "^9.0.0",
"standard": "^17.1.0",
"tap": "^16.3.9"
"standard": "^17.1.0"
},
"author": {
"email": "mateonunez95@gmail.com",
Expand Down
202 changes: 98 additions & 104 deletions packages/drill/test/common.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test } from 'tap'
import { test } from 'node:test'
import { tspl } from '@matteo.collina/tspl'
import setupDatabase, { killDatabase } from './../drill.js'
import symbols from '../lib/symbols.js'
import huston from '@mateonunez/asterism-huston'
Expand Down Expand Up @@ -26,132 +27,125 @@ 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)
})
const supportedDatabases = [
{ name: 'mysql', options: mysqlOptions },
{ name: 'postgres', options: postgresOptions }
]

test('should drop a database', async (t) => {
const { ok } = tspl(t, { plan: 2 })

for (const database of supportedDatabases) {
await t.test(database.name, async () => {
const { db, queryer } = await setupDatabase(logger, database.name, database.options)
const databaseName = 'test'
await queryer[privateMethods].createDatabase(databaseName, { dropIfExists: true })

await queryer[privateMethods].dropDatabase(databaseName)
await killDatabase(db)
ok(db._disposed)
})
}
})

test('postgres', async ({ ok }) => {
const { db, queryer } = await setupDatabase(logger, 'postgres', postgresOptions)
const databaseName = 'test'
await queryer[privateMethods].createDatabase(databaseName, { dropIfExists: true })
test('should create a new table', async (t) => {
const { ok } = tspl(t, { plan: 2 })

await queryer[privateMethods].dropDatabase(databaseName)
await killDatabase(db)
ok(db)
})
for (const database of supportedDatabases) {
await t.test(database.name, async () => {
const { db, queryer } = await setupDatabase(logger, database.name, database.options)
const tableName = 'common_table_test'
await createTable(queryer, tableName)
await queryer[privateMethods].dropTable(tableName)
await killDatabase(db)

end()
ok(db)
})
}
})

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)
test('should insert data into a table', async (t) => {
const { ok, deepStrictEqual } = tspl(t, { plan: 4 })

ok(db)
})
for (const database of supportedDatabases) {
await t.test(database.name, async () => {
const { db, queryer } = await setupDatabase(logger, database.name, database.options)
const tableName = 'common_table_test'
await createTable(queryer, tableName)
await insertIntoTable(queryer, tableName)
const data = await queryer.selectData(tableName)

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)
deepStrictEqual(data, [{ id: 1, name: 'test' }])

ok(db)
})
await queryer[privateMethods].dropTable(tableName)
await killDatabase(db)

end()
ok(db)
})
}
})

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)
test('should select data from a table', async (t) => {
const { ok, deepStrictEqual } = tspl(t, { plan: 4 })

ok(db)
})
for (const database of supportedDatabases) {
await t.test(database.name, async () => {
const { db, queryer } = await setupDatabase(logger, database.name, database.options)
const tableName = 'common_table_test'
await createTable(queryer, tableName)
await insertIntoTable(queryer, tableName)
const data = await queryer.selectData(tableName)

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)
deepStrictEqual(data, [{ id: 1, name: 'test' }])

ok(db)
})
await queryer[privateMethods].dropTable(tableName)
await killDatabase(db)

end()
ok(db)
})
}
})

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('should delete data from a table', async (t) => {
const { ok, deepStrictEqual } = tspl(t, { plan: 4 })

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)
for (const database of supportedDatabases) {
await t.test(database.name, async () => {
const { db, queryer } = await setupDatabase(logger, database.name, database.options)
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)

ok(db)
})
deepStrictEqual(data, [])

await queryer[privateMethods].dropTable(tableName)
await killDatabase(db)

end()
ok(db)
})
}
})

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('should update data from a table', async (t) => {
const { ok, deepStrictEqual } = tspl(t, { plan: 4 })

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)
for (const database of supportedDatabases) {
await t.test(database.name, async () => {
const { db, queryer } = await setupDatabase(logger, database.name, database.options)
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)

ok(db)
})
deepStrictEqual(data, [{ id: 1, name: 'test2' }])

await queryer[privateMethods].dropTable(tableName)
await killDatabase(db)

end()
ok(db)
})
}
})
15 changes: 7 additions & 8 deletions packages/drill/test/database.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { test } from 'tap'
import { test } from 'node:test'
import assert from 'node:assert'
import { generateConnectionString } from '../lib/database.js'
import huston from '@mateonunez/asterism-huston'

const { mysqlOptions, postgresOptions } = huston

test('should generate correctly the connection string', ({ end }) => {
test('mysql', async ({ same }) => {
test('should generate correctly the connection string', async (t) => {
await t.test('mysql', async () => {
const { mysql } = generateConnectionString(mysqlOptions)
same(mysql, 'mysql://root:toor@127.0.0.1:3306/db')
assert.strictEqual(mysql, 'mysql://root:toor@127.0.0.1:3306/db')
})

test('postgres', async ({ same }) => {
await t.test('postgres', async () => {
const { postgres } = generateConnectionString(postgresOptions)
same(postgres, 'postgres://postgres:toor@127.0.0.1:5432/db')
assert.strictEqual(postgres, 'postgres://postgres:toor@127.0.0.1:5432/db')
})

end()
})
25 changes: 14 additions & 11 deletions packages/drill/test/errors.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { test } from 'tap'
import { test } from 'node:test'
import assert from 'node:assert'
import setupDatabase from '../drill.js'

test('should throw an error when the database is not support', async ({ ok }) => {
try {
test('should rejects with error when the database is not support', async () => {
assert.rejects(async () => {
await setupDatabase(null, 'oracle', {})
} catch (err) {
ok(err)
}
}, {
name: 'Error',
message: 'The database "oracle" is not supported.'
})
})

test('should throw an error when the parameters are wront', async ({ ok }) => {
try {
test('should reject with error when the parameters are wrong', async () => {
assert.rejects(async () => {
await setupDatabase(null, 'mysql', { user: 'fake', password: 'kaboom' })
} catch (err) {
ok(err)
}
}, {
name: 'Error',
message: /Could not connect to mysql database at/
})
})
Loading

0 comments on commit 31c4145

Please sign in to comment.