BackFillDocRevTests.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const { db, ObjectId } = require('../../../app/src/infrastructure/mongodb')
  2. const { promisify } = require('util')
  3. const { exec } = require('child_process')
  4. const logger = require('@overleaf/logger/logging-manager')
  5. const { expect } = require('chai')
  6. describe('BackFillDocRevTests', function () {
  7. const docId1 = new ObjectId()
  8. const docId2 = new ObjectId()
  9. const docId3 = new ObjectId()
  10. beforeEach('insert docs', async function () {
  11. await db.docs.insertMany([
  12. { _id: docId1, deleted: true },
  13. { _id: docId2 },
  14. { _id: docId3, rev: 42 },
  15. ])
  16. })
  17. async function runScript(dryRun) {
  18. let result
  19. try {
  20. result = await promisify(exec)(
  21. [
  22. 'VERBOSE_LOGGING=true',
  23. 'node',
  24. 'scripts/back_fill_doc_rev',
  25. dryRun,
  26. ].join(' ')
  27. )
  28. } catch (error) {
  29. // dump details like exit code, stdErr and stdOut
  30. logger.error({ error }, 'script failed')
  31. throw error
  32. }
  33. const { stdout: stdOut } = result
  34. expect(stdOut).to.include('rev missing 2 | deleted=true 1')
  35. expect(stdOut).to.match(
  36. new RegExp(`Running update on batch with ids .+${docId1}`)
  37. )
  38. expect(stdOut).to.match(
  39. new RegExp(`Running update on batch with ids .+${docId2}`)
  40. )
  41. expect(stdOut).to.not.match(
  42. new RegExp(`Running update on batch with ids .+${docId3}`)
  43. )
  44. }
  45. describe('dry-run=true', function () {
  46. beforeEach('run script', async function () {
  47. await runScript('--dry-run=true')
  48. })
  49. it('should not back fill the rev', async function () {
  50. const docs = await db.docs.find({}, { $sort: { _id: 1 } }).toArray()
  51. expect(docs).to.deep.equal([
  52. { _id: docId1, deleted: true },
  53. { _id: docId2 },
  54. { _id: docId3, rev: 42 },
  55. ])
  56. })
  57. })
  58. describe('dry-run=false', function () {
  59. beforeEach('run script', async function () {
  60. await runScript('--dry-run=false')
  61. })
  62. it('should back fill the rev', async function () {
  63. const docs = await db.docs.find({}, { $sort: { _id: 1 } }).toArray()
  64. expect(docs).to.deep.equal([
  65. { _id: docId1, rev: 1, deleted: true },
  66. { _id: docId2, rev: 1 },
  67. { _id: docId3, rev: 42 },
  68. ])
  69. })
  70. })
  71. })