Skip to content

Commit

Permalink
refactor database tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gurza committed Sep 19, 2023
1 parent 1691ff6 commit 618ccd5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
13 changes: 8 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, { ensureDatabaseReady } from '../database'
import { getDatabase } from '../database'
import setupTable from '../setupTable'
import accessTable from './tables/access'
import userTable from './tables/user'
Expand All @@ -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([
Expand All @@ -24,7 +24,7 @@ beforeAll(async () => {
})

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

await database.tableClient.withSession(async (session) =>
Promise.all([
Expand 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) => {
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions tests/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 618ccd5

Please sign in to comment.