document.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import * as assert from 'power-assert'
  2. import { Util } from '@cloudbase/database/src/util'
  3. import tcb from '../../../src/index'
  4. import * as Config from '../../config.local'
  5. import * as common from '../../common/index'
  6. describe('test/unit/document.test.ts', () => {
  7. const collName = 'db-test-document'
  8. const docIDGenerated = Util.generateDocId()
  9. const app = tcb.init(Config)
  10. const db = app.database()
  11. const data = [
  12. { category: 'Web', tags: ['JavaScript', 'C#'] },
  13. { category: 'Web', tags: ['Go', 'C#'] },
  14. { category: 'Life', tags: ['Go', 'Python', 'JavaScript'] }
  15. ]
  16. beforeAll(async () => {
  17. await common.safeCollection(db, collName)
  18. await db.collection(collName).add(data)
  19. })
  20. afterAll(async () => {
  21. await db
  22. .collection(collName)
  23. .where({
  24. _id: /.*/
  25. })
  26. .remove()
  27. })
  28. it('API - createCollection', async () => {
  29. await common.safeCollection(db, collName)
  30. // const result = await db.createCollection(collName)
  31. // assert(result)
  32. })
  33. it('docID test', () => {
  34. const document = db.collection(collName).doc(docIDGenerated)
  35. assert(document.id === docIDGenerated)
  36. })
  37. it('API - set data in empty document', async () => {
  38. const _ = db.command
  39. const document = await db
  40. .collection(collName)
  41. .doc('123123123')
  42. .set({
  43. name: 'jude'
  44. })
  45. assert(document.upsertedId)
  46. const documents = await db
  47. .collection(collName)
  48. .where({
  49. name: _.eq('jude')
  50. })
  51. .get()
  52. assert(Array.isArray(documents.data))
  53. })
  54. it('API - set data in document existed', async () => {
  55. const documents = await db
  56. .collection(collName)
  57. .limit(1)
  58. .get()
  59. const docId = documents.data[0]._id
  60. let data = await db
  61. .collection(collName)
  62. .doc(docId)
  63. .set({
  64. data: { type: 'set' }
  65. })
  66. assert(data.updated === 1)
  67. data = await db
  68. .collection(collName)
  69. .doc(docId)
  70. .set({
  71. data: { arr: [1, 2, 3], foo: 123 },
  72. array: [0, 0, 0]
  73. })
  74. assert(data.updated === 1)
  75. data = await db
  76. .collection(collName)
  77. .doc(docId)
  78. .update({
  79. data: { arr: db.command.push([4, 5, 6]), foo: db.command.inc(1) },
  80. array: db.command.pop()
  81. })
  82. assert.strictEqual(data.updated, 1)
  83. })
  84. it('API - remove document that not exist', async () => {
  85. const document = db.collection(collName).doc(docIDGenerated)
  86. const data = await document.remove()
  87. assert(!data.deleted)
  88. })
  89. it('API - remove document should success', async () => {
  90. const documents = await db.collection(collName).get()
  91. const docId = documents.data[0]._id
  92. const data = await db
  93. .collection(collName)
  94. .doc(docId)
  95. .remove()
  96. assert.strictEqual(data.deleted, 1)
  97. })
  98. })