@@ -11,6 +11,8 @@ const { expect } = Code
11
11
const DatabaseSupport = require ( '../support/database.js' )
12
12
const GroupHelper = require ( '../support/helpers/group.helper.js' )
13
13
const GroupModel = require ( '../../app/models/group.model.js' )
14
+ const ReturnVersionHelper = require ( '../support/helpers/return-version.helper.js' )
15
+ const ReturnVersionModel = require ( '../../app/models/return-version.model.js' )
14
16
const RoleHelper = require ( '../support/helpers/role.helper.js' )
15
17
const RoleModel = require ( '../../app/models/role.model.js' )
16
18
const UserGroupHelper = require ( '../support/helpers/user-group.helper.js' )
@@ -43,63 +45,69 @@ describe('User model', () => {
43
45
} )
44
46
45
47
describe ( 'Relationships' , ( ) => {
46
- describe ( 'when linking to user groups' , ( ) => {
47
- let testUserGroup
48
+ describe ( 'when linking through user groups to groups' , ( ) => {
49
+ let testGroup
48
50
49
51
beforeEach ( async ( ) => {
50
52
testRecord = await UserHelper . add ( )
51
- testUserGroup = await UserGroupHelper . add ( { userId : testRecord . id } )
53
+ testGroup = await GroupHelper . add ( )
54
+ await UserGroupHelper . add ( { userId : testRecord . id , groupId : testGroup . id } )
52
55
} )
53
56
54
57
it ( 'can successfully run a related query' , async ( ) => {
55
58
const query = await UserModel . query ( )
56
- . innerJoinRelated ( 'userGroups ' )
59
+ . innerJoinRelated ( 'groups ' )
57
60
58
61
expect ( query ) . to . exist ( )
59
62
} )
60
63
61
- it ( 'can eager load the user groups' , async ( ) => {
64
+ it ( 'can eager load the groups' , async ( ) => {
62
65
const result = await UserModel . query ( )
63
66
. findById ( testRecord . id )
64
- . withGraphFetched ( 'userGroups ' )
67
+ . withGraphFetched ( 'groups ' )
65
68
66
69
expect ( result ) . to . be . instanceOf ( UserModel )
67
70
expect ( result . id ) . to . equal ( testRecord . id )
68
71
69
- expect ( result . userGroups ) . to . be . an . array ( )
70
- expect ( result . userGroups ) . to . have . length ( 1 )
71
- expect ( result . userGroups [ 0 ] ) . to . be . an . instanceOf ( UserGroupModel )
72
- expect ( result . userGroups [ 0 ] ) . to . equal ( testUserGroup )
72
+ expect ( result . groups ) . to . be . an . array ( )
73
+ expect ( result . groups ) . to . have . length ( 1 )
74
+ expect ( result . groups [ 0 ] ) . to . be . an . instanceOf ( GroupModel )
75
+ expect ( result . groups [ 0 ] ) . to . equal ( testGroup )
73
76
} )
74
77
} )
75
78
76
- describe ( 'when linking to user roles ' , ( ) => {
77
- let testUserRole
79
+ describe ( 'when linking to return versions ' , ( ) => {
80
+ let testReturnVersions
78
81
79
82
beforeEach ( async ( ) => {
80
83
testRecord = await UserHelper . add ( )
81
- testUserRole = await UserRoleHelper . add ( { userId : testRecord . id } )
84
+
85
+ testReturnVersions = [ ]
86
+ for ( let i = 0 ; i < 2 ; i ++ ) {
87
+ const returnVersion = await ReturnVersionHelper . add ( { createdBy : testRecord . id } )
88
+ testReturnVersions . push ( returnVersion )
89
+ }
82
90
} )
83
91
84
92
it ( 'can successfully run a related query' , async ( ) => {
85
93
const query = await UserModel . query ( )
86
- . innerJoinRelated ( 'userRoles ' )
94
+ . innerJoinRelated ( 'returnVersions ' )
87
95
88
96
expect ( query ) . to . exist ( )
89
97
} )
90
98
91
- it ( 'can eager load the user roles ' , async ( ) => {
99
+ it ( 'can eager load the return versions ' , async ( ) => {
92
100
const result = await UserModel . query ( )
93
101
. findById ( testRecord . id )
94
- . withGraphFetched ( 'userRoles ' )
102
+ . withGraphFetched ( 'returnVersions ' )
95
103
96
104
expect ( result ) . to . be . instanceOf ( UserModel )
97
105
expect ( result . id ) . to . equal ( testRecord . id )
98
106
99
- expect ( result . userRoles ) . to . be . an . array ( )
100
- expect ( result . userRoles ) . to . have . length ( 1 )
101
- expect ( result . userRoles [ 0 ] ) . to . be . an . instanceOf ( UserRoleModel )
102
- expect ( result . userRoles [ 0 ] ) . to . equal ( testUserRole )
107
+ expect ( result . returnVersions ) . to . be . an . array ( )
108
+ expect ( result . returnVersions [ 0 ] ) . to . be . an . instanceOf ( ReturnVersionModel )
109
+ expect ( result . returnVersions ) . to . include ( testReturnVersions [ 0 ] )
110
+ expect ( result . returnVersions ) . to . include ( testReturnVersions [ 1 ] )
103
111
} )
104
112
} )
105
113
@@ -134,34 +142,63 @@ describe('User model', () => {
134
142
} )
135
143
} )
136
144
137
- describe ( 'when linking through user groups to groups' , ( ) => {
138
- let testGroup
145
+ describe ( 'when linking to user groups' , ( ) => {
146
+ let testUserGroup
139
147
140
148
beforeEach ( async ( ) => {
141
149
testRecord = await UserHelper . add ( )
142
- testGroup = await GroupHelper . add ( )
143
- await UserGroupHelper . add ( { userId : testRecord . id , groupId : testGroup . id } )
150
+ testUserGroup = await UserGroupHelper . add ( { userId : testRecord . id } )
144
151
} )
145
152
146
153
it ( 'can successfully run a related query' , async ( ) => {
147
154
const query = await UserModel . query ( )
148
- . innerJoinRelated ( 'groups ' )
155
+ . innerJoinRelated ( 'userGroups ' )
149
156
150
157
expect ( query ) . to . exist ( )
151
158
} )
152
159
153
- it ( 'can eager load the groups' , async ( ) => {
160
+ it ( 'can eager load the user groups' , async ( ) => {
154
161
const result = await UserModel . query ( )
155
162
. findById ( testRecord . id )
156
- . withGraphFetched ( 'groups ' )
163
+ . withGraphFetched ( 'userGroups ' )
157
164
158
165
expect ( result ) . to . be . instanceOf ( UserModel )
159
166
expect ( result . id ) . to . equal ( testRecord . id )
160
167
161
- expect ( result . groups ) . to . be . an . array ( )
162
- expect ( result . groups ) . to . have . length ( 1 )
163
- expect ( result . groups [ 0 ] ) . to . be . an . instanceOf ( GroupModel )
164
- expect ( result . groups [ 0 ] ) . to . equal ( testGroup )
168
+ expect ( result . userGroups ) . to . be . an . array ( )
169
+ expect ( result . userGroups ) . to . have . length ( 1 )
170
+ expect ( result . userGroups [ 0 ] ) . to . be . an . instanceOf ( UserGroupModel )
171
+ expect ( result . userGroups [ 0 ] ) . to . equal ( testUserGroup )
172
+ } )
173
+ } )
174
+
175
+ describe ( 'when linking to user roles' , ( ) => {
176
+ let testUserRole
177
+
178
+ beforeEach ( async ( ) => {
179
+ testRecord = await UserHelper . add ( )
180
+ testUserRole = await UserRoleHelper . add ( { userId : testRecord . id } )
181
+ } )
182
+
183
+ it ( 'can successfully run a related query' , async ( ) => {
184
+ const query = await UserModel . query ( )
185
+ . innerJoinRelated ( 'userRoles' )
186
+
187
+ expect ( query ) . to . exist ( )
188
+ } )
189
+
190
+ it ( 'can eager load the user roles' , async ( ) => {
191
+ const result = await UserModel . query ( )
192
+ . findById ( testRecord . id )
193
+ . withGraphFetched ( 'userRoles' )
194
+
195
+ expect ( result ) . to . be . instanceOf ( UserModel )
196
+ expect ( result . id ) . to . equal ( testRecord . id )
197
+
198
+ expect ( result . userRoles ) . to . be . an . array ( )
199
+ expect ( result . userRoles ) . to . have . length ( 1 )
200
+ expect ( result . userRoles [ 0 ] ) . to . be . an . instanceOf ( UserRoleModel )
201
+ expect ( result . userRoles [ 0 ] ) . to . equal ( testUserRole )
165
202
} )
166
203
} )
167
204
} )
0 commit comments