setup.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. ]) {
  34. await fetch('http://gcs:9090/storage/v1/b', {
  35. method: 'POST',
  36. body: JSON.stringify({ name: bucket }),
  37. headers: { 'Content-Type': 'application/json' },
  38. })
  39. }
  40. }
  41. // Tear down the connection pool after all the tests have run, so the process
  42. // can exit.
  43. async function tearDownConnectionPool() {
  44. await knex.destroy()
  45. }
  46. module.exports = {
  47. setupPostgresDatabase,
  48. createGcsBuckets,
  49. tearDownConnectionPool,
  50. mochaHooks: {
  51. beforeAll: [setupPostgresDatabase, setupMongoDatabase, createGcsBuckets],
  52. afterAll: [tearDownConnectionPool],
  53. },
  54. }