RegenerateDuplicateReferralIdsTests.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. const { exec } = require('child_process')
  2. const { promisify } = require('util')
  3. const { expect } = require('chai')
  4. const logger = require('@overleaf/logger')
  5. const { filterOutput } = require('./helpers/settings')
  6. const { db } = require('../../../app/src/infrastructure/mongodb')
  7. const BATCH_SIZE = 100
  8. let n = 0
  9. function getUniqueReferralId() {
  10. return `unique_${n++}`
  11. }
  12. function getUserWithReferralId(referralId) {
  13. const email = `${Math.random()}@example.com`
  14. return {
  15. referal_id: referralId,
  16. // Make the unique indexes happy.
  17. email,
  18. emails: [{ email }],
  19. }
  20. }
  21. async function getBatch(batchCounter) {
  22. return (
  23. await db.users
  24. .find(
  25. {},
  26. {
  27. projection: { _id: 1 },
  28. skip: BATCH_SIZE * --batchCounter,
  29. limit: BATCH_SIZE,
  30. }
  31. )
  32. .toArray()
  33. ).map(user => user._id)
  34. }
  35. describe('RegenerateDuplicateReferralIds', function () {
  36. let firstBatch, secondBatch, thirdBatch, forthBatch, duplicateAcrossBatch
  37. beforeEach('insert duplicates', async function () {
  38. // full batch of duplicates
  39. await db.users.insertMany(
  40. Array(BATCH_SIZE)
  41. .fill(0)
  42. .map(() => {
  43. return getUserWithReferralId('duplicate1')
  44. })
  45. )
  46. firstBatch = await getBatch(1)
  47. // batch of 999 duplicates and 1 unique
  48. await db.users.insertMany(
  49. Array(BATCH_SIZE - 1)
  50. .fill(0)
  51. .map(() => {
  52. return getUserWithReferralId('duplicate2')
  53. })
  54. .concat([getUserWithReferralId(getUniqueReferralId())])
  55. )
  56. secondBatch = await getBatch(2)
  57. // duplicate outside batch
  58. duplicateAcrossBatch = getUniqueReferralId()
  59. await db.users.insertMany(
  60. Array(BATCH_SIZE - 1)
  61. .fill(0)
  62. .map(() => {
  63. return getUserWithReferralId(getUniqueReferralId())
  64. })
  65. .concat([getUserWithReferralId(duplicateAcrossBatch)])
  66. )
  67. thirdBatch = await getBatch(3)
  68. // no new duplicates onwards
  69. await db.users.insertMany(
  70. Array(BATCH_SIZE - 1)
  71. .fill(0)
  72. .map(() => {
  73. return getUserWithReferralId(getUniqueReferralId())
  74. })
  75. .concat([getUserWithReferralId(duplicateAcrossBatch)])
  76. )
  77. forthBatch = await getBatch(4)
  78. })
  79. let result
  80. beforeEach('run script', async function () {
  81. try {
  82. result = await promisify(exec)(
  83. [
  84. // set low BATCH_SIZE
  85. `BATCH_SIZE=${BATCH_SIZE}`,
  86. // log details on duplicate matching
  87. 'VERBOSE_LOGGING=true',
  88. // disable verbose logging
  89. 'LOG_LEVEL=ERROR',
  90. // actual command
  91. 'node',
  92. 'scripts/regenerate_duplicate_referral_ids',
  93. ].join(' ')
  94. )
  95. } catch (err) {
  96. // dump details like exit code, stdErr and stdOut
  97. logger.error({ err }, 'script failed')
  98. throw err
  99. }
  100. })
  101. it('should do the correct operations', function () {
  102. let { stderr: stdErr, stdout: stdOut } = result
  103. stdErr = stdErr.split('\n').filter(filterOutput)
  104. stdOut = stdOut.split('\n').filter(filterOutput)
  105. expect(stdErr).to.include.members([
  106. `Completed batch ending ${firstBatch[BATCH_SIZE - 1]}`,
  107. `Completed batch ending ${secondBatch[BATCH_SIZE - 1]}`,
  108. `Completed batch ending ${thirdBatch[BATCH_SIZE - 1]}`,
  109. `Completed batch ending ${forthBatch[BATCH_SIZE - 1]}`,
  110. 'Done.',
  111. '',
  112. ])
  113. expect(stdOut.filter(filterOutput)).to.include.members([
  114. // only duplicates
  115. `Running update on batch with ids ${JSON.stringify(firstBatch)}`,
  116. 'Got duplicates from looking at batch.',
  117. 'Found duplicate: duplicate1',
  118. // duplicate in batch
  119. `Running update on batch with ids ${JSON.stringify(secondBatch)}`,
  120. 'Got duplicates from looking at batch.',
  121. 'Found duplicate: duplicate2',
  122. // duplicate with next batch
  123. `Running update on batch with ids ${JSON.stringify(thirdBatch)}`,
  124. 'Got duplicates from running count.',
  125. `Found duplicate: ${duplicateAcrossBatch}`,
  126. // no new duplicates
  127. `Running update on batch with ids ${JSON.stringify(forthBatch)}`,
  128. '',
  129. ])
  130. })
  131. it('should give all users a unique refereal_id', async function () {
  132. const users = await db.users
  133. .find({}, { projection: { referal_id: 1 } })
  134. .toArray()
  135. const uniqueReferralIds = Array.from(
  136. new Set(users.map(user => user.referal_id))
  137. )
  138. expect(users).to.have.length(4 * BATCH_SIZE)
  139. expect(uniqueReferralIds).to.have.length(users.length)
  140. })
  141. })