geo.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import * as assert from 'power-assert'
  2. import * as Mock from '../unit/mock'
  3. import tcb from '../../../src/index'
  4. import * as config from '../../config.local'
  5. import * as common from '../../common/index'
  6. describe('GEO类型', () => {
  7. const app = tcb.init(config)
  8. const db = app.database()
  9. const collName = 'db-test-geo'
  10. const collection = db.collection(collName)
  11. const longitude = -180
  12. const latitude = 20
  13. const point = new db.Geo.Point(longitude, latitude)
  14. const initialData = {
  15. point,
  16. pointArr: [point, point, point],
  17. uuid: '416a4700-e0d3-11e8-911a-8888888888',
  18. string: '新增单个string字段1。新增单个string字段1。',
  19. due: new Date('2018-09-01'),
  20. int: 100,
  21. geo: new db.Geo.Point(90, 23),
  22. array: [
  23. {
  24. string: '99999999',
  25. due: new Date('2018-09-01'),
  26. geo: new db.Geo.Point(90, 23)
  27. },
  28. {
  29. string: '0000000',
  30. geo: new db.Geo.Point(90, 23),
  31. null: null
  32. }
  33. ]
  34. }
  35. // const nameList = ["f", "b", "e", "d", "a", "c"];
  36. beforeAll(async () => {
  37. await common.safeCollection(db, collName)
  38. })
  39. afterAll(async () => {
  40. await db
  41. .collection(collName)
  42. .where({
  43. _id: /.*/
  44. })
  45. .remove()
  46. })
  47. // it('Document - createCollection()', async () => {
  48. // await common.safeCollection(db, collName)
  49. // })
  50. it('GEO Point - CRUD', async () => {
  51. // Create
  52. let res = await collection.add(initialData)
  53. // const id = res.ids[0]
  54. const id = res.id
  55. assert(id)
  56. assert(res.requestId)
  57. const res2 = await collection.doc(id).set(initialData)
  58. assert.strictEqual(res2.updated, 1)
  59. assert(res2.requestId)
  60. // Read
  61. let result = await collection
  62. .where({
  63. _id: id
  64. })
  65. .get()
  66. assert(result.data.length > 0)
  67. assert.deepEqual(result.data[0].point, { longitude, latitude })
  68. // TODO: 现在对 GEO 进行 $eq 操作,小概率会查不到,需要修改查询的底层结构
  69. // result = await collection
  70. // .where({
  71. // point: db.command.eq(point)
  72. // })
  73. // .get()
  74. // console.log(point, result)
  75. // assert(result.data.length > 0)
  76. // result = await collection
  77. // .where({
  78. // point: db.command.or(db.command.eq(point))
  79. // })
  80. // .get()
  81. // console.log(point, result)
  82. // assert(result.data.length > 0)
  83. // result = await collection
  84. // .where({
  85. // point: point
  86. // })
  87. // .get()
  88. // console.log(result)
  89. // assert(result.data.length > 0)
  90. // Delete
  91. const deleteRes = await collection
  92. .where({
  93. _id: id
  94. })
  95. .remove()
  96. console.log(deleteRes)
  97. assert.strictEqual(deleteRes.deleted, 1)
  98. })
  99. })