setup.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const chai = require('chai')
  2. const chaiAsPromised = require('chai-as-promised')
  3. const config = require('config')
  4. const fetch = require('node-fetch')
  5. const { knex, mongodb, redis } = require('../storage')
  6. // ensure every ObjectId has the id string as a property for correct comparisons
  7. require('mongodb').ObjectId.cacheHexString = true
  8. chai.use(chaiAsPromised)
  9. chai.config.truncateThreshold = 0
  10. async function setupPostgresDatabase() {
  11. this.timeout(60_000)
  12. await knex.migrate.latest()
  13. }
  14. async function setupMongoDatabase() {
  15. this.timeout(60_000)
  16. await mongodb.db.collection('projectHistoryChunks').createIndexes([
  17. {
  18. key: { projectId: 1, startVersion: 1 },
  19. name: 'projectId_1_startVersion_1',
  20. partialFilterExpression: { state: { $in: ['active', 'closed'] } },
  21. unique: true,
  22. },
  23. {
  24. key: { state: 1 },
  25. name: 'state_1',
  26. partialFilterExpression: { state: 'deleted' },
  27. },
  28. ])
  29. }
  30. async function createGcsBuckets() {
  31. this.timeout(60_000)
  32. for (const bucket of [
  33. config.get('blobStore.globalBucket'),
  34. config.get('blobStore.projectBucket'),
  35. config.get('chunkStore.bucket'),
  36. config.get('zipStore.bucket'),
  37. 'fake-user-files-gcs',
  38. ]) {
  39. await fetch('http://gcs:9090/storage/v1/b', {
  40. method: 'POST',
  41. body: JSON.stringify({ name: bucket }),
  42. headers: { 'Content-Type': 'application/json' },
  43. })
  44. }
  45. }
  46. // Tear down the connection pool after all the tests have run, so the process
  47. // can exit.
  48. async function tearDownConnectionPool() {
  49. await knex.destroy()
  50. await redis.disconnect()
  51. }
  52. module.exports = {
  53. setupPostgresDatabase,
  54. createGcsBuckets,
  55. tearDownConnectionPool,
  56. mochaHooks: {
  57. beforeAll: [setupPostgresDatabase, setupMongoDatabase, createGcsBuckets],
  58. afterAll: [tearDownConnectionPool],
  59. },
  60. }