您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

matchqueue.spec.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict'
  2. const test = require('ava')
  3. const { stub } = require('sinon')
  4. const Hapi = require('@hapi/hapi')
  5. const plugin = require('../lib/plugins/profile')
  6. const Aspect = require('../lib/models/aspect')
  7. const AspectLabel = require('../lib/models/aspect_label')
  8. const Profile = require('../lib/models/profile')
  9. const MatchQueue = require('../lib/models/matchqueue')
  10. /**
  11. * Route parameters
  12. */
  13. const params = {
  14. profile_id: 1,
  15. target_id: 2,
  16. reinsert: true,
  17. include_profile: false,
  18. }
  19. const mockReturn = {
  20. queue: [
  21. { mocked_profile_id: 1, target_id: 2, is_deleted: false },
  22. { mocked_profile_id: 2, target_id: 3, is_deleted: false },
  23. { mocked_profile_id: 3, target_id: 1, is_deleted: false },
  24. ],
  25. labels: [
  26. { aspect_id: 1, a: 100, b: 100 },
  27. { aspect_id: 2, a: 100, b: 200 },
  28. { aspect_id: 3, a: 200, b: 200 },
  29. { aspect_id: 4, a: 200, b: 100 },
  30. ],
  31. aspects: [
  32. { aspect_id: 1, 1: 111, 2: 112, 3: 113, 4: 114 },
  33. { aspect_id: 2, 1: 222, 2: 222, 3: 333, 4: 222 },
  34. { aspect_id: 3, 1: 111, 2: 112, 3: 113, 4: 114 },
  35. { aspect_id: 4, 1: 111, 2: 111, 3: 111, 4: 111 },
  36. ],
  37. }
  38. const pathToTest = {
  39. method: 'PATCH',
  40. url: `/${params.profile_id}/queue/${params.target_id}/delete?include_profile=${params.include_profile}&reinsert=${params.reinsert}`,
  41. }
  42. test('path /<profile_id>/queue/<target_id> should return ok on PATCH', async t => {
  43. /**
  44. * Create a new server and register services,
  45. * models and routes for testing
  46. * -
  47. * NOTE: We use a mocked registerModel() and register
  48. * models manually. Normally this is handled by
  49. * Schwifty at runtime.
  50. */
  51. const server = Hapi.server()
  52. /**
  53. * Overload so we don't register any models
  54. * using the plugin call (see plugins/profile.js)
  55. * and Manually load the model we need for the test
  56. */
  57. server.registerModel = () => {}
  58. server.models = () => ({ Aspect, AspectLabel, MatchQueue, Profile })
  59. server.registrations = {
  60. 'main-app-plugin': {
  61. options: {},
  62. },
  63. }
  64. /**
  65. * Register Routes and Services as usual
  66. */
  67. await plugin.register(server)
  68. /**
  69. * Replace Objection model methods with our own mock functions
  70. * !: Janky - might be better to temp knex sqlite instance
  71. */
  72. stub(server.models()['AspectLabel'], 'query').returns(mockReturn.labels)
  73. stub(server.models()['Aspect'], 'query').returns(mockReturn.aspects)
  74. stub(server.models()['MatchQueue'], 'query').returns({
  75. insert: queueRecord => {
  76. t.is(queueRecord.profile_id, params.profile_id)
  77. t.is(queueRecord.target_id, params.target_id)
  78. t.is(queueRecord.is_deleted, !params.reinsert)
  79. },
  80. // Mocked for getQueue()
  81. where: () => ({
  82. where: (cmd, val) => {
  83. return mockReturn.queue
  84. },
  85. // Mocked for markAsDeleted()
  86. andWhere: () => ({
  87. patch: () => ({
  88. first: () => ({
  89. patch: 'bop',
  90. }),
  91. }),
  92. }),
  93. }),
  94. })
  95. /**
  96. * Test the server with registered models and services
  97. */
  98. const { payload } = await server.inject(pathToTest)
  99. const res = JSON.parse(payload)
  100. t.deepEqual(res.ok, true)
  101. t.deepEqual(
  102. res.data,
  103. mockReturn.queue.map(entry => entry.target_id),
  104. )
  105. t.deepEqual(res.data[0], 2)
  106. t.deepEqual(res.data[1], 3)
  107. })