GettingAllDocsTests.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const { ObjectId } = require('mongodb-legacy')
  15. const async = require('async')
  16. const DocstoreApp = require('./helpers/DocstoreApp')
  17. const DocstoreClient = require('./helpers/DocstoreClient')
  18. describe('Getting all docs', function () {
  19. beforeEach(function (done) {
  20. this.project_id = new ObjectId()
  21. this.docs = [
  22. {
  23. _id: new ObjectId(),
  24. lines: ['one', 'two', 'three'],
  25. ranges: {
  26. changes: [
  27. {
  28. id: new ObjectId().toString(),
  29. metadata: { user_id: 'user-id-1' },
  30. },
  31. ],
  32. },
  33. rev: 2,
  34. },
  35. {
  36. _id: new ObjectId(),
  37. lines: ['aaa', 'bbb', 'ccc'],
  38. ranges: {
  39. changes: [
  40. {
  41. id: new ObjectId().toString(),
  42. metadata: { user_id: 'user-id-2' },
  43. },
  44. ],
  45. },
  46. rev: 4,
  47. },
  48. {
  49. _id: new ObjectId(),
  50. lines: ['111', '222', '333'],
  51. ranges: {
  52. changes: [
  53. {
  54. id: new ObjectId().toString(),
  55. metadata: { user_id: 'anonymous-user' },
  56. },
  57. ],
  58. },
  59. rev: 6,
  60. },
  61. ]
  62. this.deleted_doc = {
  63. _id: new ObjectId(),
  64. lines: ['deleted'],
  65. ranges: {
  66. changes: [
  67. { id: new ObjectId().toString(), metadata: { user_id: 'user-id-3' } },
  68. ],
  69. },
  70. rev: 8,
  71. }
  72. const version = 42
  73. const jobs = Array.from(this.docs).map(doc =>
  74. (doc => {
  75. return callback => {
  76. return DocstoreClient.createDoc(
  77. this.project_id,
  78. doc._id,
  79. doc.lines,
  80. version,
  81. doc.ranges,
  82. callback
  83. )
  84. }
  85. })(doc)
  86. )
  87. jobs.push(cb => {
  88. return DocstoreClient.createDoc(
  89. this.project_id,
  90. this.deleted_doc._id,
  91. this.deleted_doc.lines,
  92. version,
  93. this.deleted_doc.ranges,
  94. err => {
  95. if (err) return done(err)
  96. return DocstoreClient.deleteDoc(
  97. this.project_id,
  98. this.deleted_doc._id,
  99. cb
  100. )
  101. }
  102. )
  103. })
  104. jobs.unshift(cb => DocstoreApp.ensureRunning(cb))
  105. return async.series(jobs, done)
  106. })
  107. it('getAllDocs should return all the (non-deleted) docs', function (done) {
  108. return DocstoreClient.getAllDocs(this.project_id, (error, res, docs) => {
  109. if (error != null) {
  110. throw error
  111. }
  112. docs.length.should.equal(this.docs.length)
  113. for (let i = 0; i < docs.length; i++) {
  114. const doc = docs[i]
  115. doc.lines.should.deep.equal(this.docs[i].lines)
  116. }
  117. return done()
  118. })
  119. })
  120. it('getAllRanges should return all the (non-deleted) doc ranges', function (done) {
  121. return DocstoreClient.getAllRanges(this.project_id, (error, res, docs) => {
  122. if (error != null) {
  123. throw error
  124. }
  125. docs.length.should.equal(this.docs.length)
  126. for (let i = 0; i < docs.length; i++) {
  127. const doc = docs[i]
  128. doc.ranges.should.deep.equal(this.docs[i].ranges)
  129. }
  130. return done()
  131. })
  132. })
  133. it('getTrackedChangesUserIds should return all the user ids from (non-deleted) ranges', function (done) {
  134. DocstoreClient.getTrackedChangesUserIds(
  135. this.project_id,
  136. (error, res, userIds) => {
  137. if (error != null) {
  138. throw error
  139. }
  140. userIds.should.deep.equal(['user-id-1', 'user-id-2'])
  141. done()
  142. }
  143. )
  144. })
  145. })