From 618ccd5e73eb31aa40ae4ab626c14e1ce531fb93 Mon Sep 17 00:00:00 2001 From: Sergei Popinevskii Date: Tue, 19 Sep 2023 22:35:47 +0300 Subject: [PATCH] refactor database tests --- tests/basic/index.test.ts | 13 ++++++++----- tests/database.ts | 6 ++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/tests/basic/index.test.ts b/tests/basic/index.test.ts index 55b421d..af6dbab 100644 --- a/tests/basic/index.test.ts +++ b/tests/basic/index.test.ts @@ -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, { ensureDatabaseReady } from '../database' +import { getDatabase } from '../database' import setupTable from '../setupTable' import accessTable from './tables/access' import userTable from './tables/user' @@ -13,7 +13,7 @@ import userData from './tables/user/data' const dirname = __dirname.split('/').pop() beforeAll(async () => { - await ensureDatabaseReady(database) + const database = await getDatabase() await database.tableClient.withSession(async (session) => Promise.all([ @@ -24,7 +24,7 @@ beforeAll(async () => { }) afterAll(async () => { - await ensureDatabaseReady(database) + const database = await getDatabase() await database.tableClient.withSession(async (session) => Promise.all([ @@ -38,7 +38,8 @@ afterAll(async () => { }) 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) => { @@ -49,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) diff --git a/tests/database.ts b/tests/database.ts index 16068ac..3d0a7bd 100644 --- a/tests/database.ts +++ b/tests/database.ts @@ -17,10 +17,12 @@ const endpoint = 'grpcs://ydb.serverless.yandexcloud.net:2135' const database = process.env['DATABASE_NAME'] const driver = new Driver({ endpoint, database, authService }) -export const ensureDatabaseReady = async (driver: Driver) => { - const timeout = 4500 // Should be less then Jest timeout +export const getDatabase = async () => { + const timeout = 2000 // Should be less then Jest timeout if (!(await driver.ready(timeout))) throw new Error(`Database has not become ready in ${timeout}ms!`) + + return driver } export default driver