BatchedUpdateTests.mjs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { spawnSync } from 'node:child_process'
  2. import { expect } from 'chai'
  3. import { db, ObjectId } from '../../../app/src/infrastructure/mongodb.js'
  4. describe('BatchedUpdateTests', function () {
  5. it('can handle non linear insert order', async function () {
  6. await db.systemmessages.insertOne({
  7. content: '1',
  8. _id: new ObjectId('500000000000000000000000'),
  9. })
  10. await db.systemmessages.insertOne({
  11. content: '2',
  12. _id: new ObjectId('400000000000000000000000'),
  13. })
  14. await db.systemmessages.insertOne({
  15. content: '3',
  16. _id: new ObjectId('600000000000000000000000'),
  17. })
  18. await db.systemmessages.insertOne({
  19. content: '4',
  20. _id: new ObjectId('300000000000000000000000'),
  21. })
  22. spawnSync(process.argv0, [
  23. '--input-type=module',
  24. '-e',
  25. 'import { batchedUpdateWithResultHandling } from "@overleaf/mongo-utils/batchedUpdate.js"; import { db } from "./app/src/infrastructure/mongodb.js"; batchedUpdateWithResultHandling(db.systemmessages, { content: { $ne: "42" }}, { $set: { content: "42" } })',
  26. ])
  27. await expect(
  28. db.systemmessages.find({}).project({ content: 1, _id: 0 }).toArray()
  29. ).to.eventually.deep.equal([
  30. { content: '42' },
  31. { content: '42' },
  32. { content: '42' },
  33. { content: '42' },
  34. ])
  35. })
  36. it('can handle ids sitting on the edge', async function () {
  37. const edge = '3028de800000000000000000'
  38. await db.systemmessages.insertOne({
  39. content: '1',
  40. _id: new ObjectId('300000000000000000000000'),
  41. })
  42. await db.systemmessages.insertOne({
  43. content: '2',
  44. _id: new ObjectId(),
  45. })
  46. await db.systemmessages.insertOne({
  47. content: '3',
  48. _id: new ObjectId('400000000000000000000000'),
  49. })
  50. const { stderr } = spawnSync(
  51. process.argv0,
  52. [
  53. '--input-type=module',
  54. '-e',
  55. 'import { batchedUpdateWithResultHandling } from "@overleaf/mongo-utils/batchedUpdate.js"; import { db } from "./app/src/infrastructure/mongodb.js"; batchedUpdateWithResultHandling(db.systemmessages, { content: { $ne: "42" }}, { $set: { content: "42" } })',
  56. ],
  57. { encoding: 'utf-8' }
  58. )
  59. expect(
  60. await db.systemmessages.find({}).project({ content: 1, _id: 0 }).toArray()
  61. ).to.deep.equal([{ content: '42' }, { content: '42' }, { content: '42' }])
  62. expect(stderr).to.include(
  63. 'Completed batch ending 300000000000000000000000 (1995-07-09T16:12:48.000Z)'
  64. )
  65. expect(stderr).to.include(
  66. `Completed batch ending ${edge} (1995-08-09T16:12:48.000Z)`
  67. ) // hit the edge
  68. expect(stderr).to.include(
  69. 'Completed batch ending 400000000000000000000000 (2004-01-10T13:37:04.000Z)'
  70. )
  71. })
  72. it('can handle ids sitting on the edge descending', async function () {
  73. const edge = '3fd721800000000000000000'
  74. await db.systemmessages.insertOne({
  75. content: '1',
  76. _id: new ObjectId('300000000000000000000000'),
  77. })
  78. await db.systemmessages.insertOne({
  79. content: '2',
  80. _id: new ObjectId(edge),
  81. })
  82. await db.systemmessages.insertOne({
  83. content: '3',
  84. _id: new ObjectId('400000000000000000000000'),
  85. })
  86. const { stderr } = spawnSync(
  87. process.argv0,
  88. [
  89. '--input-type=module',
  90. '-e',
  91. 'import { batchedUpdateWithResultHandling } from "@overleaf/mongo-utils/batchedUpdate.js"; import { db } from "./app/src/infrastructure/mongodb.js"; batchedUpdateWithResultHandling(db.systemmessages, { content: { $ne: "42" }}, { $set: { content: "42" } })',
  92. ],
  93. {
  94. encoding: 'utf-8',
  95. env: {
  96. ...process.env,
  97. BATCH_DESCENDING: 'true',
  98. BATCH_RANGE_START: '400000000000000000000001',
  99. },
  100. }
  101. )
  102. expect(
  103. await db.systemmessages.find({}).project({ content: 1, _id: 0 }).toArray()
  104. ).to.deep.equal([{ content: '42' }, { content: '42' }, { content: '42' }])
  105. expect(stderr).to.include(
  106. 'Completed batch ending 400000000000000000000000 (2004-01-10T13:37:04.000Z)'
  107. )
  108. expect(stderr).to.include(
  109. `Completed batch ending ${edge} (2003-12-10T13:37:04.000Z)`
  110. ) // hit the edge
  111. expect(stderr).to.include(
  112. 'Completed batch ending 300000000000000000000000 (1995-07-09T16:12:48.000Z)'
  113. )
  114. })
  115. it('can handle dates as input', async function () {
  116. await db.systemmessages.insertOne({
  117. content: '1',
  118. _id: new ObjectId('500000000000000000000000'),
  119. })
  120. await db.systemmessages.insertOne({
  121. content: '2',
  122. _id: new ObjectId('400000000000000000000000'),
  123. })
  124. await db.systemmessages.insertOne({
  125. content: '3',
  126. _id: new ObjectId('600000000000000000000000'),
  127. })
  128. await db.systemmessages.insertOne({
  129. content: '4',
  130. _id: new ObjectId('300000000000000000000000'),
  131. })
  132. spawnSync(
  133. process.argv0,
  134. [
  135. '--input-type=module',
  136. '-e',
  137. 'import { batchedUpdateWithResultHandling } from "@overleaf/mongo-utils/batchedUpdate.js"; import { db } from "./app/src/infrastructure/mongodb.js"; batchedUpdateWithResultHandling(db.systemmessages, { content: { $ne: "42" }}, { $set: { content: "42" } })',
  138. ],
  139. {
  140. env: {
  141. ...process.env,
  142. BATCH_RANGE_START: '2004-01-10T13:37:03.000Z',
  143. BATCH_RANGE_END: '2012-07-13T11:01:20.000Z',
  144. },
  145. }
  146. )
  147. await expect(
  148. db.systemmessages.find({}).project({ content: 1, _id: 0 }).toArray()
  149. ).to.eventually.deep.equal([
  150. { content: '42' },
  151. { content: '42' },
  152. { content: '3' },
  153. { content: '4' },
  154. ])
  155. })
  156. })