collection.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import * as assert from 'power-assert'
  2. import tcb from '../../../src/index'
  3. import { ErrorCode } from '@cloudbase/database/src/constant'
  4. import * as Config from '../../config.local'
  5. import * as common from '../../common'
  6. import { create } from 'domain'
  7. describe('test/unit/collection.test.ts', () => {
  8. const collName = 'db-test-collection'
  9. const app = tcb.init(Config)
  10. const db = app.database()
  11. const collection = db.collection(collName)
  12. const data = [{ name: 'A' }, { name: 'B' }]
  13. let createColl = null
  14. it('name test', async () => {
  15. assert(collection.name === collName)
  16. createColl = await common.safeCollection(db, collName)
  17. createColl.create(data)
  18. })
  19. it('Error - use invalid docId to get reference', () => {
  20. const docId = 'abcdefg'
  21. try {
  22. collection.doc(docId)
  23. } catch (e) {
  24. assert(e.message === ErrorCode.DocIDError)
  25. }
  26. })
  27. it('API - get all data', async () => {
  28. const res = await collection.get()
  29. assert(Array.isArray(res.data))
  30. })
  31. // it('API - use where', async () => {
  32. // const field = 'name'
  33. // const value = 'huming'
  34. // const opStr = '=='
  35. // const data = await collection.where(field, opStr, value).get()
  36. // assert(Array.isArray(data.data))
  37. // })
  38. it('API - use orderBy', async () => {
  39. const field = 'name'
  40. const direction = 'asc'
  41. const data = await collection.orderBy(field, direction).get()
  42. assert(Array.isArray(data.data))
  43. })
  44. it('API - use limit', async () => {
  45. const limit = 1
  46. const data = await collection.limit(limit).get()
  47. assert(Array.isArray(data.data) && data.data.length === limit)
  48. })
  49. it('API - use offset', async () => {
  50. const offset = 2
  51. const data = await collection.skip(offset).get()
  52. assert(Array.isArray(data.data))
  53. })
  54. it('API - add one doc, update and remove', async () => {
  55. // 清除collection
  56. await createColl.remove()
  57. const res = await collection.add({
  58. name: 'huming'
  59. })
  60. assert(res.id)
  61. const data = await collection
  62. .where({
  63. name: db.command.eq('huming')
  64. })
  65. .update({
  66. age: 18
  67. })
  68. assert(data.updated > 0)
  69. const remove = await collection
  70. .where({
  71. name: db.command.eq('huming')
  72. })
  73. .remove()
  74. assert(remove.deleted > 0)
  75. })
  76. it('API - use field', async () => {
  77. // await db.createCollection(collName)
  78. await common.safeCollection(db, collName)
  79. const res = await collection.field({ age: 1 }).get()
  80. assert(Array.isArray(res.data))
  81. })
  82. it('API - add and remove with skip', async () => {
  83. const text = 'test for add and remove with skip'
  84. let i = 0
  85. while (i++ < 10) {
  86. await collection.add({
  87. text
  88. })
  89. }
  90. let result = await collection
  91. .where({
  92. text
  93. })
  94. .get()
  95. assert(result.data.length > 0)
  96. await collection
  97. .where({
  98. text
  99. })
  100. .orderBy('text', 'asc')
  101. .skip(3)
  102. .remove()
  103. result = await collection
  104. .where({
  105. text
  106. })
  107. .get()
  108. assert(result.data.length === 0)
  109. })
  110. it('API - use where', async () => {
  111. // 1. 验证where 必填object 对象参数
  112. try {
  113. await collection.where().get()
  114. } catch (e) {
  115. assert(e.message === ErrorCode.QueryParamTypeError)
  116. }
  117. // 2. 验证where 对象value不可均为undefined
  118. try {
  119. await collection.where({ a: undefined }).get()
  120. } catch (e) {
  121. assert(e.message === ErrorCode.QueryParamValueError)
  122. }
  123. await collection.add([
  124. {
  125. name: 'aaa'
  126. }
  127. ])
  128. const res1 = await collection.where({ name: 'aaa' }).get()
  129. assert(res1.data.length > 0)
  130. const res2 = await collection.where({}).get()
  131. assert(res2.data.length > 0)
  132. // 清楚当前collection
  133. await createColl.remove()
  134. })
  135. })