Skip to content

Commit e565894

Browse files
authored
Development (mongodb#24)
* Commit performed using Copy Push Files action * remove old files * restore tests * restore test suite changes and update function names --------- Co-authored-by: MongoCaleb <[email protected]>
1 parent 372b1c1 commit e565894

File tree

2 files changed

+290
-37
lines changed

2 files changed

+290
-37
lines changed
Lines changed: 289 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,327 @@
11
import { Credentials } from "realm";
22
import { ObjectId } from "bson";
33
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";
56

67
describe("Test MongoDB CRUD operations in Functions", () => {
78
test("Test inserting and deleting a document with Functions", async () => {
8-
let insertedObjectId: ObjectId | null = null;
99

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[] = [
1337
{
38+
category: "supplies",
1439
name: "envelopes",
1540
tags: ["office", "stationary"],
1641
price: 0.5,
1742
quantity: 2,
1843
},
1944
{
45+
category: "supplies",
2046
name: "manuscript paper",
2147
tags: ["office", "stationary"],
2248
price: 0.3,
2349
quantity: 5,
2450
},
2551
];
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-
};
4352

44-
// Use Device SDK to call the Function to insert the document
4553
const app = getDeviceSDKApp();
4654
const anonCredentials = Credentials.anonymous();
4755
const user = await app.logIn(anonCredentials);
4856
expect(user).toBeTruthy;
4957

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+
5078
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;
5482

55-
if (insertResult instanceof ObjectId) {
56-
insertedObjectId = insertResult;
83+
if (insertResult.insertedId instanceof ObjectId) {
84+
resultId = insertResult.insertedId;
5785
}
5886
} catch (error) {
5987
if (error instanceof Error) {
6088
fail(error.message);
6189
}
6290
}
91+
ids.push(resultId);
92+
expect(ids[0]).toBe(resultId);
6393

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+
]
66122

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");
70198

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;
72297
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+
*/

tests/integration/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { App } from "realm";
33
import "dotenv/config";
44

55
function getDeviceSDKApp() {
6-
const appId = process.env.APP_ID;
6+
const appId = "atlas-functions-triggers-szivjrs";
77
const app = new App({ id: appId! });
88

99
return app;

0 commit comments

Comments
 (0)