setup-db.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import * as dotenv from 'dotenv'
  2. // we should set env variables before all other imports to avoid config errors or warthog caused by DI
  3. dotenv.config({ path: './test/.env' })
  4. import { createDb, dropDb } from '../utils'
  5. import { QueryNodeManager } from '../../src'
  6. import { createDBConnection } from '../../src/db/helper'
  7. import * as Redis from 'ioredis'
  8. export async function resetDb(): Promise<void> {
  9. try {
  10. await dropDb()
  11. } catch (e) {
  12. // ignore
  13. }
  14. try {
  15. await setupDb()
  16. } catch (e) {
  17. //ignore;
  18. }
  19. }
  20. export async function setupDb(): Promise<void> {
  21. await createDb()
  22. await QueryNodeManager.migrate()
  23. }
  24. export async function clearRedis(): Promise<void> {
  25. const redisURL = process.env.REDIS_URI
  26. if (!redisURL) {
  27. throw new Error(`Redis URL is not provided`)
  28. }
  29. const redis = new Redis(redisURL)
  30. await redis.flushall()
  31. await redis.quit()
  32. }
  33. beforeEach(async () => {
  34. await setupDb()
  35. await createDBConnection()
  36. })
  37. afterEach(async () => {
  38. try {
  39. await QueryNodeManager.cleanUp()
  40. await dropDb()
  41. } catch (e) {
  42. console.error(e)
  43. }
  44. })