setup.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 } = 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. async function setupPostgresDatabase() {
  10. this.timeout(60_000)
  11. await knex.migrate.latest()
  12. }
  13. async function setupMongoDatabase() {
  14. this.timeout(60_000)
  15. await mongodb.db.collection('projectHistoryChunks').createIndexes([
  16. {
  17. key: { projectId: 1, startVersion: 1 },
  18. name: 'projectId_1_startVersion_1',
  19. partialFilterExpression: { state: 'active' },
  20. unique: true,
  21. },
  22. {
  23. key: { state: 1 },
  24. name: 'state_1',
  25. partialFilterExpression: { state: 'deleted' },
  26. },
  27. ])
  28. }
  29. async function createGcsBuckets() {
  30. this.timeout(60_000)
  31. for (const bucket of [
  32. config.get('blobStore.globalBucket'),
  33. config.get('blobStore.projectBucket'),
  34. config.get('chunkStore.bucket'),
  35. config.get('zipStore.bucket'),
  36. 'fake-user-files-gcs',
  37. ]) {
  38. await fetch('http://gcs:9090/storage/v1/b', {
  39. method: 'POST',
  40. body: JSON.stringify({ name: bucket }),
  41. headers: { 'Content-Type': 'application/json' },
  42. })
  43. }
  44. }
  45. // Tear down the connection pool after all the tests have run, so the process
  46. // can exit.
  47. async function tearDownConnectionPool() {
  48. await knex.destroy()
  49. }
  50. module.exports = {
  51. setupPostgresDatabase,
  52. createGcsBuckets,
  53. tearDownConnectionPool,
  54. mochaHooks: {
  55. beforeAll: [setupPostgresDatabase, setupMongoDatabase, createGcsBuckets],
  56. afterAll: [tearDownConnectionPool],
  57. },
  58. }