Pārlūkot izejas kodu

:gear: adding score test

tags/0.0.1
j 4 gadus atpakaļ
vecāks
revīzija
43a35c97d9

+ 2
- 0
backend/lib/routes/profile/score.js Parādīt failu

@@ -53,11 +53,13 @@ module.exports = {
53 53
             const distanceUnit = request.query.unit
54 54
                 ? request.query.unit
55 55
                 : 'mile'
56
+
56 57
             const scoredProfiles = await profileService.scoreProfilesFor(
57 58
                 profileId,
58 59
                 maxDistanceMiles,
59 60
                 distanceUnit,
60 61
             )
62
+
61 63
             await matchQueueService.saveMatchQueue(
62 64
                 profileId,
63 65
                 scoredProfiles.map(profile => profile.profile_id),

+ 9
- 7
backend/tests/matchqueue.spec.js Parādīt failu

@@ -1,7 +1,7 @@
1 1
 'use strict'
2 2
 
3 3
 const test = require('ava')
4
-const { stub, mock } = require('sinon')
4
+const { stub } = require('sinon')
5 5
 const Hapi = require('@hapi/hapi')
6 6
 
7 7
 const plugin = require('../lib/plugins/profile')
@@ -23,6 +23,11 @@ const mockReturn = {
23 23
         { mocked_profile_id: 3, target_id: 1, is_deleted: false },
24 24
     ],
25 25
 }
26
+const pathToTest = {
27
+    method: 'PATCH',
28
+    url: `/${params.profile_id}/queue/${params.target_id}/delete?include_profile=${params.include_profile}&reinsert=${params.reinsert}`,
29
+}
30
+
26 31
 test('path / should return ok on GET', async t => {
27 32
     /**
28 33
      * Create a new server and register services,
@@ -45,8 +50,8 @@ test('path / should return ok on GET', async t => {
45 50
      */
46 51
     await plugin.register(server)
47 52
     /**
48
-     * Replace Objection model methods with our own custom functions
49
-     * !: Kinda janky - might be better to temp knex sqlite instance
53
+     * Replace Objection model methods with our own mock functions
54
+     * !: Janky - might be better to temp knex sqlite instance
50 55
      */
51 56
     stub(server.models()['MatchQueue'], 'query').returns({
52 57
         // Mocked for markAsDeleted()
@@ -71,10 +76,7 @@ test('path / should return ok on GET', async t => {
71 76
     /**
72 77
      * Test the server with registered models and services
73 78
      */
74
-    const { payload } = await server.inject({
75
-        method: 'PATCH',
76
-        url: `/${params.profile_id}/queue/${params.target_id}/delete?include_profile=${params.include_profile}&reinsert=${params.reinsert}`,
77
-    })
79
+    const { payload } = await server.inject(pathToTest)
78 80
     const res = JSON.parse(payload)
79 81
     t.deepEqual(res.ok, true)
80 82
     t.deepEqual(

+ 128
- 0
backend/tests/score.spec.js Parādīt failu

@@ -0,0 +1,128 @@
1
+'use strict'
2
+
3
+const test = require('ava')
4
+const { stub } = require('sinon')
5
+const Hapi = require('@hapi/hapi')
6
+
7
+const plugin = require('../lib/plugins/profile')
8
+
9
+const Profile = require('../lib/models/profile')
10
+const ZipCode = require('../lib/models/zip-code')
11
+const MatchQueue = require('../lib/models/matchqueue')
12
+/**
13
+ * Route parameters
14
+ */
15
+const params = {
16
+    profile_id: 1,
17
+    max_distance: 1000,
18
+}
19
+const mockReturn = {
20
+    user: [
21
+        {
22
+            profile_id: 1,
23
+            user: { is_poster: 1 },
24
+            responses: [
25
+                { val: '120' },
26
+                { val: '200' },
27
+                { response_key_id: 16, val: '90065' },
28
+            ],
29
+        },
30
+        {
31
+            profile_id: 2,
32
+            user: { is_poster: 0 },
33
+            responses: [
34
+                { val: '120' },
35
+                { val: '200' },
36
+                { response_key_id: 16, val: '96741' },
37
+            ],
38
+        },
39
+        {
40
+            profile_id: 3,
41
+            user: { is_poster: 0 },
42
+            responses: [
43
+                { val: '180' },
44
+                { val: '140' },
45
+                { response_key_id: 16, val: '97002' },
46
+            ],
47
+        },
48
+    ],
49
+}
50
+const pathToTest = {
51
+    method: 'GET',
52
+    url: `/${params.profile_id}/score?max_distance=${params.max_distance}`,
53
+}
54
+
55
+test(`path ${pathToTest.path} should return ok on GET`, async t => {
56
+    /**
57
+     * Create a new server and register services,
58
+     * models and routes for testing
59
+     * -
60
+     * NOTE: We use a mocked registerModel() and register
61
+     * models manually. Normally this is handled by
62
+     * Schwifty at runtime.
63
+     */
64
+    const server = Hapi.server()
65
+    /**
66
+     * Overload so we don't register any models
67
+     * using the plugin call (see plugins/profile.js)
68
+     * and Manually load the model we need for the test
69
+     */
70
+    server.registerModel = () => {}
71
+    server.models = () => ({ MatchQueue, Profile, ZipCode })
72
+    /**
73
+     * Register Routes and Services as usual
74
+     */
75
+    await plugin.register(server)
76
+    /**
77
+     * Replace Objection model methods with our own mock functions
78
+     * !: Janky - might be better to temp knex sqlite instance
79
+     */
80
+    stub(server.models()['Profile'], 'query').returns({
81
+        // Mocked for pathToTest(), scoreProfilesFor()
82
+        findOne: () => ({
83
+            withGraphFetched: () => ({
84
+                withGraphFetched: () => mockReturn.user[0],
85
+            }),
86
+        }),
87
+        // Mocked for scoreProfilesFor()
88
+        withGraphFetched: () => ({
89
+            withGraphFetched: () => [mockReturn.user[1], mockReturn.user[2]],
90
+        }),
91
+        // Mocked for _getProfileIdsForUserId()
92
+        where: () => ({}),
93
+        // Mocked for getCompleteProfilesFor(), getProfilesFor()
94
+        whereIn: () => ({
95
+            withGraphFetched: () => ({}),
96
+        }),
97
+        // Mocked for deleteProfile()
98
+        delete: () => ({
99
+            where: () => ({}),
100
+        }),
101
+    })
102
+    stub(server.models()['ZipCode'], 'query').returns({
103
+        // Mocked for _latLonForZip()
104
+        findOne: () => ({
105
+            latitude: 38.0 + Math.random(),
106
+            longitude: -121.0 + Math.random(),
107
+        }),
108
+    })
109
+    stub(server.models()['MatchQueue'], 'query').returns({
110
+        patch: () => ({
111
+            where: () => ({}),
112
+        }),
113
+        insert: () => ({}),
114
+        where: () => ({
115
+            andWhere: () => ({}),
116
+        }),
117
+    })
118
+
119
+    /**
120
+     * Test the server with registered models and services
121
+     */
122
+    const { payload } = await server.inject(pathToTest)
123
+    const res = JSON.parse(payload)
124
+
125
+    t.deepEqual(res.ok, true)
126
+    t.is(res.data.length, 2)
127
+    t.is(res.data[0].profile_id, 3)
128
+})

Notiek ielāde…
Atcelt
Saglabāt