1
1
import { Credentials } from "realm" ;
2
2
import { ObjectId } from "bson" ;
3
3
import { getDeviceSDKApp , sleep } from "../utils" ;
4
- import { DeleteResult , InsertOneResult } from "mongodb" ;
4
+ import { DeleteResult , InsertOneResult , InsertManyResult , Int32 , BSON } from "mongodb" ;
5
+ import { UpdateResult } from "realm/dist/public-types/internal" ;
5
6
6
7
describe ( "Test MongoDB CRUD operations in Functions" , ( ) => {
7
8
test ( "Test inserting and deleting a document with Functions" , async ( ) => {
8
- let insertedObjectId : ObjectId | null = null ;
9
9
10
- // Set up a new sale document to insert
11
- const saleDate = new Date ( ) ;
12
- const items = [
10
+ class Sale {
11
+ saleDate ?: Date ;
12
+ items ?: StoreItem [ ] ;
13
+ storeLocation ?: string ;
14
+ customer ?: Customer ;
15
+ couponUsed ?: false ;
16
+ purchaseMethod ?: string ;
17
+ }
18
+ class StoreItem {
19
+ category ?: string ;
20
+ name ?: string ;
21
+ tags ?: string [ ] ;
22
+ price ?: number ;
23
+ quantity ?: number ;
24
+ } ;
25
+
26
+ class Customer {
27
+ constructor (
28
+ age ?: number ,
29
+ email ?: string ,
30
+ satisfaction ?: number
31
+ ) { }
32
+ } ;
33
+
34
+ let ids : ObjectId [ ] = [ ] ;
35
+
36
+ let StoreItems : StoreItem [ ] = [
13
37
{
38
+ category : "supplies" ,
14
39
name : "envelopes" ,
15
40
tags : [ "office" , "stationary" ] ,
16
41
price : 0.5 ,
17
42
quantity : 2 ,
18
43
} ,
19
44
{
45
+ category : "supplies" ,
20
46
name : "manuscript paper" ,
21
47
tags : [ "office" , "stationary" ] ,
22
48
price : 0.3 ,
23
49
quantity : 5 ,
24
50
} ,
25
51
] ;
26
- const storeLocation = "Scranton" ;
27
- const customer = {
28
- gender : "agender" ,
29
- age : 42 ,
30
-
31
- sastisfaction : 4 ,
32
- } ;
33
- const couponUsed = false ;
34
- const purchaseMethod = "Carrier Pigeon" ;
35
- const args = {
36
- saleDate : saleDate ,
37
- items : items ,
38
- storeLocation : storeLocation ,
39
- customer : customer ,
40
- couponUsed : couponUsed ,
41
- purchaseMethod : purchaseMethod ,
42
- } ;
43
52
44
- // Use Device SDK to call the Function to insert the document
45
53
const app = getDeviceSDKApp ( ) ;
46
54
const anonCredentials = Credentials . anonymous ( ) ;
47
55
const user = await app . logIn ( anonCredentials ) ;
48
56
expect ( user ) . toBeTruthy ;
49
57
58
+ // *********** //
59
+ // InsertOne //
60
+ /* returns:
61
+ {
62
+ "insertedId": {
63
+ "$oid": "664515f357125435e7b3e9b6"
64
+ }
65
+ }*/
66
+ const customer = new Customer ( 42 , "[email protected] " , 4 ) ;
67
+
68
+ const insertOneArgs = new Sale ( ) ;
69
+ insertOneArgs . saleDate = new Date ( ) ,
70
+ insertOneArgs . items = StoreItems ,
71
+ insertOneArgs . storeLocation = "Scranton" ,
72
+ insertOneArgs . customer = customer ,
73
+ insertOneArgs . couponUsed = false ,
74
+ insertOneArgs . purchaseMethod = "Trinkets" ;
75
+
76
+ let resultId : ObjectId = new ObjectId ( ) ;
77
+
50
78
try {
51
- const insertResult = ( await user . functions . mongodbCrud_insertOne (
52
- args
53
- ) ) as InsertOneResult < Document > ;
79
+ const insertResult = ( await user . functions . crud_InsertOne (
80
+ insertOneArgs
81
+ ) ) as InsertOneResult ;
54
82
55
- if ( insertResult instanceof ObjectId ) {
56
- insertedObjectId = insertResult ;
83
+ if ( insertResult . insertedId instanceof ObjectId ) {
84
+ resultId = insertResult . insertedId ;
57
85
}
58
86
} catch ( error ) {
59
87
if ( error instanceof Error ) {
60
88
fail ( error . message ) ;
61
89
}
62
90
}
91
+ ids . push ( resultId ) ;
92
+ expect ( ids [ 0 ] ) . toBe ( resultId ) ;
63
93
64
- // Ensure document has been inserted
65
- expect ( insertedObjectId ) . not . toBeNull ;
94
+ // *********** //
95
+ // InsertMany //
96
+ /* returns {
97
+ "insertedIds": [
98
+ {
99
+ "$oid": "66451ab2478f0c527f12cab4"
100
+ },
101
+ {
102
+ "$oid": "66451ab2478f0c527f12cab5"
103
+ }
104
+ ]
105
+ }*/
106
+ const insertManyArgs = [ {
107
+ saleDate : new Date ( ) ,
108
+ StoreItems : StoreItems ,
109
+ storeLocation : "Hoboken" ,
110
+ customer : customer ,
111
+ couponUsed : false ,
112
+ purchaseMethod : "Carrier Pigeon" ,
113
+ } , {
114
+ saleDate : new Date ( ) ,
115
+ StoreItems : StoreItems ,
116
+ storeLocation : "Armpit, NJ" ,
117
+ customer : customer ,
118
+ couponUsed : false ,
119
+ purchaseMethod : "Carrier Pigeon"
120
+ }
121
+ ]
66
122
67
- const deleteResult = ( await user . functions . mongodbCrud_deleteOne (
68
- insertedObjectId
69
- ) ) as DeleteResult ;
123
+ class foo { "insertedIds" :string [ ] ; }
124
+ let insertedIds : foo = new foo ( ) ;
125
+ try {
126
+ const insertManyResult = ( await user . functions . crud_InsertMany (
127
+ insertManyArgs
128
+ ) ) as foo ;
129
+
130
+ insertedIds = insertManyResult ;
131
+
132
+ } catch ( error ) {
133
+ if ( error instanceof Error ) {
134
+ fail ( error . message ) ;
135
+ }
136
+ }
137
+ insertedIds . insertedIds . forEach ( id => {
138
+ ids . push ( new ObjectId ( id ) ) ;
139
+ } ) ;
140
+
141
+ expect ( ids ) . not . toBeNull ;
142
+ expect ( ids . length ) . toBe ( 3 ) ;
143
+
144
+ // *********** //
145
+ // Project //
146
+ let projectResult : Sale = new Sale ( ) ;
147
+
148
+ const projectionFilter = {
149
+ _id :0 ,
150
+ storeLocation :1 ,
151
+ items : 1
152
+ } ;
153
+
154
+ try {
155
+ projectResult = ( await user . functions . crud_FindOne (
156
+ ids [ 0 ] . toString ( ) , projectionFilter ) ) as Sale ;
157
+ } catch ( error ) {
158
+ if ( error instanceof Error ) {
159
+ fail ( error . message ) ;
160
+ }
161
+ }
162
+
163
+ expect ( projectResult ) . not . toBeNull ;
164
+ expect ( projectResult . storeLocation ) . not . toBeNull ;
165
+ expect ( projectResult . storeLocation ) . toBe ( "Scranton" ) ;
166
+ expect ( projectResult . items ?. length ) . toBe ( 2 ) ;
167
+ expect ( projectResult . saleDate ) . toBeNull ;
168
+
169
+
170
+ // *********** //
171
+ // REPLACE //
172
+ /* returns
173
+ "_id": {
174
+ "$oid": "6644e8613e720767b85fee9f"
175
+ },
176
+ "storeLocation": "East Appleton",
177
+ "couponUsed": true
178
+ }*/
179
+ let replaceResult : Sale = new Sale ( ) ;
180
+
181
+ const updateFilter = {
182
+ "storeLocation" : "East Appleton" ,
183
+ "couponUsed" : true ,
184
+ } ;
185
+
186
+ try {
187
+ replaceResult = ( await user . functions . crud_Replace (
188
+ ids [ 1 ] . toString ( ) , updateFilter ) ) as Sale ;
189
+ } catch ( error ) {
190
+ if ( error instanceof Error ) {
191
+ fail ( error . message ) ;
192
+ }
193
+ }
194
+
195
+ expect ( replaceResult ) . not . toBeNull ;
196
+ expect ( replaceResult . couponUsed ) . toBeTruthy ( ) ;
197
+ expect ( replaceResult . storeLocation ) . toBe ( "East Appleton" ) ;
70
198
71
- // Ensure document has been deleted
199
+ // *********** //
200
+ // UPDATEONE //
201
+ /* returns
202
+ "_id": {
203
+ "$oid": "6644e8613e720767b85fee9f"
204
+ },
205
+ "storeLocation": "East Appleton",
206
+ "couponUsed": true
207
+ }*/
208
+
209
+ const updateOneFilter = {
210
+ "storeLocation" : "Camden" ,
211
+ "customer" : 123 ,
212
+ } ;
213
+
214
+ let count :number = 0 ;
215
+ try {
216
+ const updateOneResult = ( await user . functions . crud_UpdateOne (
217
+ ids [ 1 ] , updateOneFilter ) ) as UpdateResult < Sale > ;
218
+ count = updateOneResult . modifiedCount ;
219
+ } catch ( error ) {
220
+ if ( error instanceof Error ) {
221
+ fail ( error . message ) ;
222
+ }
223
+ }
224
+
225
+ expect ( count ) . toBe ( 1 ) ;
226
+
227
+ //you have to find the updated doc; it is not returned
228
+ // *********** //
229
+ // FindOne //
230
+ let findResult : Sale = new Sale ( ) ;
231
+ try {
232
+ findResult = ( await user . functions . crud_FindOne (
233
+ ids [ 1 ] . toString ( ) , { } ) ) as Sale ;
234
+ } catch ( error ) {
235
+ if ( error instanceof Error ) {
236
+ fail ( error . message ) ;
237
+ }
238
+ }
239
+ expect ( findResult . customer ) . toBe ( 123 ) ;
240
+ expect ( findResult . storeLocation ) . toBe ( "Camden" ) ;
241
+
242
+
243
+ // *********** //
244
+ // UPDATEMANY //
245
+ /* returns
246
+ "_id": {
247
+ "$oid": "6644e8613e720767b85fee9f"
248
+ },
249
+ "storeLocation": "East Appleton",
250
+ "couponUsed": true
251
+ }*/
252
+
253
+ const updateManyFindFilter = {
254
+ "purchaseMethod" : "Carrier Pigeon"
255
+ } ;
256
+
257
+ const updateManyResultFilter = {
258
+ "storeLocation" : "Langley" ,
259
+ purchaseMethod : "Carrier Pig"
260
+ } ;
261
+
262
+ try {
263
+ const updateOneResult = ( await user . functions . crud_UpdateMany (
264
+ updateManyFindFilter , updateManyResultFilter ) ) as UpdateResult < Sale > ;
265
+ count = updateOneResult . modifiedCount ;
266
+ } catch ( error ) {
267
+ if ( error instanceof Error ) {
268
+ fail ( error . message ) ;
269
+ }
270
+ }
271
+
272
+ expect ( count ) . toBe ( 1 ) ;
273
+
274
+ // *********** //
275
+ // Find (Many) //
276
+ let findResults : Sale [ ] = [ ] ;
277
+
278
+ try {
279
+ findResults = ( await user . functions . crud_Find (
280
+ { purchaseMethod : "Carrier Pig" } ) ) as Sale [ ] ;
281
+ } catch ( error ) {
282
+ if ( error instanceof Error ) {
283
+ fail ( error . message ) ;
284
+ }
285
+ }
286
+
287
+ expect ( findResults ) . not . toBeNull ;
288
+ //expect(findResults.length).toBe(2);
289
+ expect ( findResults [ 0 ] . purchaseMethod ) . toBe ( "Carrier Pig" ) ;
290
+ expect ( findResults [ 0 ] . storeLocation ) . toBe ( "Langley" ) ;
291
+
292
+ // *********** //
293
+ // DeleteOne //
294
+ const deleteResult = ( await user . functions . crud_DeleteOne (
295
+ { purchaseMethod : "Trinkets" }
296
+ ) ) as DeleteResult ;
72
297
expect ( deleteResult ) . toBe ( 1 ) ;
73
- } , 30000 ) ;
74
- } ) ;
298
+
299
+ // *********** //
300
+ // DeleteMany //
301
+ let deleteManyCount : number ;
302
+ deleteManyCount = 0 ;
303
+
304
+ deleteManyCount = await user . functions . crud_DeleteMany ( { } ) as number ;
305
+
306
+ //commented out because if any test fails, this doesn't run
307
+ //TODO: investigate tear-down style test so this always works
308
+
309
+ //expect(deleteManyCount).toBe(2);
310
+ } , 3000 ) ;
311
+ } )
312
+
313
+ /*
314
+ order of tests:
315
+
316
+ ✓ InsertOne
317
+ ✓ InsertMany
318
+ ✓ FindOne
319
+ ✓ Project
320
+ ✓ Replace
321
+ ✓ UpdateOne
322
+ ✓ UpdateMany
323
+ ✓ Find
324
+ ✓ DeleteOne
325
+ ✓ DeleteMany
326
+
327
+ */
0 commit comments