index.test.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import * as assert from 'power-assert'
  2. import tcb from '../../src/index'
  3. import * as config from '../config.local'
  4. import * as common from '../common/index'
  5. import { ERROR } from '../../lib/const/code'
  6. describe('test/index.test.ts', () => {
  7. const app = tcb.init({
  8. ...config,
  9. // env: Mock.env,
  10. // mpAppId: Mock.appId,
  11. sessionToken: undefined
  12. })
  13. let db = null
  14. try {
  15. db = app.database('env')
  16. } catch (err) {
  17. assert(err.code === ERROR.INVALID_PARAM.code)
  18. }
  19. db = app.database()
  20. const _ = db.command
  21. const collName = 'db-test-index'
  22. const collection = db.collection(collName)
  23. const initialData = {
  24. name: 'aaa',
  25. array: [1, 2, 3, [4, 5, 6], { a: 1, b: { c: 'fjasklfljkas', d: false } }],
  26. data: {
  27. a: 'a',
  28. b: 'b',
  29. c: 'c'
  30. },
  31. null: null,
  32. date: new Date(),
  33. // regex: new db.RegExp({
  34. // regexp: '.*',
  35. // options: 'i'
  36. // }),
  37. deepObject: {
  38. 'l-02-01': {
  39. 'l-03-01': {
  40. 'l-04-01': {
  41. level: 1,
  42. name: 'l-01',
  43. flag: '0'
  44. }
  45. }
  46. }
  47. }
  48. }
  49. beforeAll(async () => {
  50. await common.safeCollection(db, collName)
  51. })
  52. afterAll(async () => {
  53. await db
  54. .collection(collName)
  55. .where({
  56. _id: /.*/
  57. })
  58. .remove()
  59. })
  60. it('插入字段null', async () => {
  61. const addRes = await collection.add({ name: null })
  62. assert(addRes.id !== undefined)
  63. const queryRes = await collection.where({ _id: addRes.id }).get()
  64. assert(queryRes.data)
  65. })
  66. it('更新字段为空数组', async () => {
  67. const addRes = await collection.add({ a: 1 })
  68. const updateRes = await collection.where({ _id: addRes.id }).update({ a: [] })
  69. const queryRes = await collection.where({ _id: addRes.id }).get()
  70. assert(Array.isArray(queryRes.data[0].a))
  71. await collection.where({ _id: addRes.id }).remove()
  72. })
  73. it('验证插入24位可转objid 的id', async () => {
  74. // 删除{a: 'test'}的数据
  75. const deleteRes = await collection.where({ a: 'test' }).remove()
  76. const addRes = await collection.add({ a: 'test', _id: '5e7b474a5e54c773b58d7b39' })
  77. const queryRes = await collection.where({ _id: '5e7b474a5e54c773b58d7b39' }).get()
  78. assert(queryRes.data.length === 1)
  79. })
  80. it('验证throwOnCode', async () => {
  81. const app1 = tcb.init({
  82. ...config,
  83. throwOnCode: false
  84. })
  85. const db = app1.database()
  86. const _ = db.command
  87. const collName = 'db-test-index'
  88. const collection = db.collection(collName)
  89. const res = await collection.add({ $_key: 1 })
  90. assert(res.code === 'DATABASE_REQUEST_FAILED')
  91. })
  92. it('mock 插入多条', async () => {
  93. // 构建4W条数据
  94. let mockData = [],
  95. i = 0
  96. while (i++ < 10) {
  97. mockData.push({ string: 'a', int: -1 })
  98. }
  99. const addRes = await collection.add(mockData)
  100. assert(addRes.ids.length === 10)
  101. })
  102. it('清楚mock数据', async () => {
  103. const deleteRes = await collection.where({ int: -1 }).remove()
  104. assert(deleteRes.deleted === 10)
  105. })
  106. it('验证 无 query count', async () => {
  107. const countRes = await collection.count()
  108. assert(countRes.total >= 0)
  109. })
  110. it('验证 无 query update', async () => {
  111. try {
  112. await collection.update({ a: 1 })
  113. } catch (e) {
  114. assert(e.code === 'INVALID_PARAM')
  115. }
  116. })
  117. it('document query custom timeout', async () => {
  118. const res = await collection
  119. .where({})
  120. .options({ timeout: 3000 })
  121. .limit(1)
  122. .get()
  123. assert(res.data)
  124. })
  125. it('Document - CRUD', async () => {
  126. // Create
  127. const initialData = {
  128. // $key: '1',
  129. 'key.': '1'
  130. }
  131. const res = await collection.add(initialData)
  132. // assert(res.ids.length > 0)
  133. assert(res.id)
  134. assert(res.requestId)
  135. // Read
  136. const id = res.id
  137. let result = await collection
  138. .where({
  139. _id: id
  140. })
  141. .update({
  142. data: { a: null, b: null, c: null }
  143. })
  144. assert(result.updated > 0)
  145. result = await collection.where({ _id: id }).get()
  146. assert(result.data[0])
  147. assert.deepStrictEqual(result.data[0].data, { a: null, b: null, c: null })
  148. // Delete
  149. const deleteRes = await collection.doc(id).remove()
  150. assert.strictEqual(deleteRes.deleted, 1)
  151. })
  152. it('Document - query', async () => {
  153. assert((await collection.add({ a: 1, b: 100 })).id)
  154. assert((await collection.add({ a: 10, b: 1 })).id)
  155. const query = _.or({ b: _.and(_.gte(1), _.lte(10)) }, { b: _.and(_.gt(99), _.lte(101)) })
  156. const result = await collection.where(query).get()
  157. assert(result.data.length >= 2)
  158. // Delete
  159. const deleteRes = await collection.where(query).remove()
  160. assert(deleteRes.deleted, 2)
  161. })
  162. it('复合and', async () => {
  163. const result = await collection
  164. .where({
  165. date: _.gt(20190401).and(_.lte(20190430)),
  166. hour: _.gt(8).and(_.lte(12))
  167. })
  168. .get()
  169. assert(!result.code)
  170. })
  171. it('验证自定义超时', async () => {
  172. try {
  173. const result = await collection
  174. .where({
  175. date: _.gt(20190401).and(_.lte(20190430)),
  176. hour: _.gt(8).and(_.lte(12))
  177. })
  178. .options({ timeout: 10 })
  179. .get()
  180. } catch (err) {
  181. assert(err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT')
  182. }
  183. })
  184. // option更新单个 or 多个
  185. it('Document - doc().option().update()', async () => {
  186. const addRes = await collection.add([{ testNum: 1 }, { testNum: 1 }, { testNum: 1 }])
  187. assert(addRes.ids.length === 3)
  188. const updateSingleRes = await collection
  189. .where({ testNum: 1 })
  190. .options({ multiple: false })
  191. .update({ testNum: _.inc(1) })
  192. assert(updateSingleRes.updated === 1)
  193. const updateMultiRes = await collection
  194. .where({ testNum: _.gt(0) })
  195. .options({ multiple: true })
  196. .update({ testNum: _.inc(1) })
  197. assert(updateMultiRes.updated === 3)
  198. // 不传multiple字段
  199. const updateMultiRes1 = await collection
  200. .where({ testNum: _.gt(0) })
  201. .options({})
  202. .update({ testNum: _.inc(1) })
  203. assert(updateMultiRes1.updated === 3)
  204. // 不设options
  205. const updateMultiRes2 = await collection
  206. .where({ testNum: _.gt(0) })
  207. .options({})
  208. .update({ testNum: _.inc(1) })
  209. assert(updateMultiRes2.updated === 3)
  210. })
  211. // options 删除单个 or 多个
  212. it('Document - doc().option().delete()', async () => {
  213. const deleteSingleRes = await collection
  214. .where({ testNum: _.gt(0) })
  215. .options({ multiple: false })
  216. .remove()
  217. assert(deleteSingleRes.deleted === 1)
  218. const deleteMultiRes = await collection
  219. .where({ testNum: _.gt(0) })
  220. // .options({ multiple: true })
  221. .options({})
  222. .remove()
  223. assert(deleteMultiRes.deleted === 2)
  224. })
  225. it('验证findAndModify', async () => {
  226. const addRes = await collection.add([{ a: 1 }, { a: 1 }])
  227. const res = await collection.where({ a: 1 }).updateAndReturn({
  228. a: 2
  229. })
  230. assert(res.doc.a === 2)
  231. })
  232. it('验证findAndModify 更新相同值', async () => {
  233. const addRes = await collection.add([{ a: 1 }])
  234. const res = await collection.where({ a: 1 }).updateAndReturn({
  235. a: 1
  236. })
  237. assert(res.updated === 1)
  238. })
  239. it('验证findAndModify 更新失败', async () => {
  240. const res = await collection.where({ a: -1 }).updateAndReturn({ a: 1 })
  241. assert(res.updated === 0 && res.doc === null)
  242. })
  243. it.only('test insert array key', async () => {
  244. const addRes = await collection.add({
  245. a: ['aa', 'cc']
  246. })
  247. console.log('addRes:', addRes)
  248. })
  249. // it.only('insert null', async () => {
  250. // const addRes = await collection.add(null)
  251. // console.log('addRes:', addRes)
  252. // })
  253. })