setup.js 1.6 KB

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