Skip to content

Commit

Permalink
refactor and properly close db connections
Browse files Browse the repository at this point in the history
  • Loading branch information
gurza committed Sep 19, 2023
1 parent d381638 commit f57094b
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions tests/basic/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path'
import { TypedData } from 'ydb-sdk'
import extractMetadata from '../../src/metadata/extractMetadata'
import generateSchema from '../../src/schema'
import database from '../database'
import { getDatabase } from '../database'
import setupTable from '../setupTable'
import accessTable from './tables/access'
import userTable from './tables/user'
Expand All @@ -13,8 +13,8 @@ import userData from './tables/user/data'
const dirname = __dirname.split('/').pop()

beforeAll(async () => {
const timeout = 4500 // Should be less then Jest timeout
await database.ready(timeout)
const database = await getDatabase()

await database.tableClient.withSession(async (session) =>
Promise.all([
setupTable(session, `${dirname}/user`, userTable, userData),
Expand All @@ -24,17 +24,22 @@ beforeAll(async () => {
})

afterAll(async () => {
const database = await getDatabase()

await database.tableClient.withSession(async (session) =>
Promise.all([
session.dropTable(`${dirname}/user`),
session.dropTable(`${dirname}/access`)
])
)
await database.schemeClient.removeDirectory(dirname!)

await database.destroy()
})

describe('my database tests', () => {
test('should insert data into the database', async () => {
it('should insert data into the database', async () => {
const database = await getDatabase()
const query = `SELECT * FROM \`${dirname}/user\` where id = 1`
const result = await database.tableClient.withSessionRetry(
async (session) => {
Expand All @@ -45,7 +50,9 @@ describe('my database tests', () => {
)
expect(result[0]['id']).toBe(1)
})
test('should generate correct schema', async () => {

it('should generate correct schema', async () => {
const database = await getDatabase()
const base = path.join(__dirname, './metadata')
const metadata = await extractMetadata(base)
const gotSchema = await generateSchema(database, metadata)
Expand Down

0 comments on commit f57094b

Please sign in to comment.