浏览代码

:gear: adjust test for queue return

tags/0.0.1
j 4 年前
父节点
当前提交
9a71310e67
共有 2 个文件被更改,包括 49 次插入40 次删除
  1. 15
    14
      backend/lib/routes/profile/patch-queue.js
  2. 34
    26
      backend/tests/matchqueue.spec.js

+ 15
- 14
backend/lib/routes/profile/patch-queue.js 查看文件

20
                 responses: Joi.array().items(),
20
                 responses: Joi.array().items(),
21
                 user_type: Joi.any(),
21
                 user_type: Joi.any(),
22
             }),
22
             }),
23
-        )
23
+        ),
24
     ),
24
     ),
25
     error: Joi.object({
25
     error: Joi.object({
26
         error: Joi.string(),
26
         error: Joi.string(),
27
     }),
27
     }),
28
 }
28
 }
29
 
29
 
30
-
31
 const validators = {
30
 const validators = {
32
     params: Joi.object({ profile_id: Joi.number(), target_id: Joi.number() }),
31
     params: Joi.object({ profile_id: Joi.number(), target_id: Joi.number() }),
33
-    query: Joi.object({ include_profile:Joi.bool(), reinsert: Joi.boolean() }),
32
+    query: Joi.object({ include_profile: Joi.bool(), reinsert: Joi.boolean() }),
34
 }
33
 }
35
 
34
 
36
 module.exports = {
35
 module.exports = {
44
         handler: async function (request, h) {
43
         handler: async function (request, h) {
45
             const { profile_id, target_id } = request.params
44
             const { profile_id, target_id } = request.params
46
             const { include_profile, reinsert } = request.query
45
             const { include_profile, reinsert } = request.query
47
-            const { profileService, matchQueueService } = request.server.services()
46
+            const { profileService, matchQueueService } =
47
+                request.server.services()
48
             const updatedQueue = await matchQueueService.markAsDeleted(
48
             const updatedQueue = await matchQueueService.markAsDeleted(
49
                 profile_id,
49
                 profile_id,
50
                 target_id,
50
                 target_id,
51
                 reinsert,
51
                 reinsert,
52
             )
52
             )
53
             const queueIds = updatedQueue.map(entry => entry.target_id)
53
             const queueIds = updatedQueue.map(entry => entry.target_id)
54
-            const res =  {
55
-                ok:true,
54
+            const res = {
55
+                ok: true,
56
                 handler: pluginConfig.handlerType,
56
                 handler: pluginConfig.handlerType,
57
-                data: queueIds
57
+                data: queueIds,
58
             }
58
             }
59
-            if(include_profile) {
59
+            if (include_profile) {
60
                 res.data = await profileService.getProfilesFor(queueIds)
60
                 res.data = await profileService.getProfilesFor(queueIds)
61
             }
61
             }
62
             try {
62
             try {
63
                 return h.response(res).code(200)
63
                 return h.response(res).code(200)
64
             } catch (err) {
64
             } catch (err) {
65
-                return h.response({
66
-                    ok:false,
67
-                    handler: pluginConfig.handlerType,
68
-                    data: { error: `${err}`}
69
-                }).code(409)
70
-
65
+                return h
66
+                    .response({
67
+                        ok: false,
68
+                        handler: pluginConfig.handlerType,
69
+                        data: { error: `${err}` },
70
+                    })
71
+                    .code(409)
71
             }
72
             }
72
         },
73
         },
73
         /** Validate based on validators object */
74
         /** Validate based on validators object */

+ 34
- 26
backend/tests/matchqueue.spec.js 查看文件

1
 'use strict'
1
 'use strict'
2
 
2
 
3
 const test = require('ava')
3
 const test = require('ava')
4
-const { stub } = require('sinon')
4
+const { stub, mock } = require('sinon')
5
 const Hapi = require('@hapi/hapi')
5
 const Hapi = require('@hapi/hapi')
6
 
6
 
7
 const plugin = require('../lib/plugins/profile')
7
 const plugin = require('../lib/plugins/profile')
8
 
8
 
9
 const MatchQueue = require('../lib/models/matchqueue')
9
 const MatchQueue = require('../lib/models/matchqueue')
10
-
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
+}
11
 test('path / should return ok on GET', async t => {
26
 test('path / should return ok on GET', async t => {
12
     /**
27
     /**
13
      * Create a new server and register services,
28
      * Create a new server and register services,
18
      * Schwifty at runtime.
33
      * Schwifty at runtime.
19
      */
34
      */
20
     const server = Hapi.server()
35
     const server = Hapi.server()
36
+    /**
37
+     * Overload so we don't register any models
38
+     * using the plugin call (see plugins/profile.js)
39
+     * and Manually load the model we need for the test
40
+     */
21
     server.registerModel = () => {}
41
     server.registerModel = () => {}
22
     server.models = () => ({ MatchQueue })
42
     server.models = () => ({ MatchQueue })
23
-    await plugin.register(server)
24
-
25
     /**
43
     /**
26
-     * Route parameters
44
+     * Register Routes and Services as usual
27
      */
45
      */
28
-    const profile_id = 1
29
-    const target_id = 2
30
-    const reinsert = true
31
-
46
+    await plugin.register(server)
32
     /**
47
     /**
33
      * Replace Objection model methods with our own custom functions
48
      * Replace Objection model methods with our own custom functions
34
      * !: Kinda janky - might be better to temp knex sqlite instance
49
      * !: Kinda janky - might be better to temp knex sqlite instance
43
             }),
58
             }),
44
         }),
59
         }),
45
         insert: queueRecord => {
60
         insert: queueRecord => {
46
-            t.is(queueRecord.profile_id, profile_id)
47
-            t.is(queueRecord.target_id, target_id)
48
-            t.is(queueRecord.is_deleted, !reinsert)
61
+            t.is(queueRecord.profile_id, params.profile_id)
62
+            t.is(queueRecord.target_id, params.target_id)
63
+            t.is(queueRecord.is_deleted, !params.reinsert)
49
         },
64
         },
50
         // Mocked for getQueue()
65
         // Mocked for getQueue()
51
         where: () => ({
66
         where: () => ({
52
-            andWhere: (cmd, val) => {
53
-                return [
54
-                    { mocked_profile_id: 1, is_deleted: val },
55
-                    { mocked_profile_id: 2, is_deleted: val },
56
-                    { mocked_profile_id: 3, is_deleted: val },
57
-                ]
58
-            },
67
+            andWhere: (cmd, val) => mockReturn.queue,
59
         }),
68
         }),
60
     })
69
     })
61
 
70
 
64
      */
73
      */
65
     const { payload } = await server.inject({
74
     const { payload } = await server.inject({
66
         method: 'PATCH',
75
         method: 'PATCH',
67
-        url: `/${profile_id}/queue/${target_id}/delete?reinsert=${reinsert}`,
76
+        url: `/${params.profile_id}/queue/${params.target_id}/delete?include_profile=${params.include_profile}&reinsert=${params.reinsert}`,
68
     })
77
     })
69
     const res = JSON.parse(payload)
78
     const res = JSON.parse(payload)
70
-
71
-    t.deepEqual(res, [
72
-        { mocked_profile_id: 1, is_deleted: false },
73
-        { mocked_profile_id: 2, is_deleted: false },
74
-        { mocked_profile_id: 3, is_deleted: false },
75
-    ])
79
+    t.deepEqual(res.ok, true)
80
+    t.deepEqual(
81
+        res.data,
82
+        mockReturn.queue.map(entry => entry.target_id),
83
+    )
76
 })
84
 })

正在加载...
取消
保存