query.limit.test.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. const app = tcb.init(config)
  6. const db = app.database()
  7. let createCollection = null
  8. beforeAll(async () => {
  9. // 删除 1001条文档
  10. const collName = 'db-test-limit'
  11. const collection = db.collection(collName)
  12. createCollection = await common.safeCollection(db, collName)
  13. const delRes = await collection
  14. .where({
  15. luke: 'luke-limit-test'
  16. })
  17. .remove()
  18. assert(delRes.deleted >= 0)
  19. // // 创建 1001条文档
  20. let addDocs = []
  21. let i = 0
  22. while (i++ < 1001) {
  23. addDocs.push({ luke: 'luke-limit-test' })
  24. }
  25. const addRes = await collection.add(addDocs)
  26. assert(addRes.ids.length === 1001)
  27. })
  28. afterAll(async () => {
  29. const success = await createCollection.remove()
  30. assert.strictEqual(success, true)
  31. })
  32. describe('test/unit/query.limit.test.ts', () => {
  33. // 等于 1000
  34. it('query with limit 1000', async () => {
  35. const collName = 'db-test-limit'
  36. const collection = db.collection(collName)
  37. const queryRes = await collection
  38. .where({})
  39. .limit(1000)
  40. .get()
  41. assert(queryRes.data.length === 1000)
  42. })
  43. // 大于1000
  44. it('query with limit > 1000', async () => {
  45. const collName = 'db-test-limit'
  46. const collection = db.collection(collName)
  47. const queryRes = await collection
  48. .where({})
  49. .limit(1001)
  50. .get()
  51. assert(queryRes.data.length === 1000)
  52. })
  53. // 小于1000
  54. it('query with limit < 1000', async () => {
  55. const collName = 'db-test-limit'
  56. const collection = db.collection(collName)
  57. const queryRes = await collection
  58. .where({})
  59. .limit(101)
  60. .get()
  61. assert(queryRes.data.length === 101)
  62. })
  63. // 不填 默认100
  64. it('query with limit default 100', async () => {
  65. const collName = 'db-test-limit'
  66. const collection = db.collection(collName)
  67. const queryRes = await collection.where({}).get()
  68. assert(queryRes.data.length === 100)
  69. })
  70. })