/
test.ts
167 lines (144 loc) Β· 4.77 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { cargoBuild } from "./build.ts";
import { init } from "./ts/util.ts";
import { MongoClient } from "./ts/client.ts";
import { assert, assertEquals, exists } from "./test.deps.ts";
import { ObjectId } from "./ts/types.ts";
const { test } = Deno;
const dateNow = Date.now();
function getClient(): MongoClient {
const client = new MongoClient();
client.connectWithUri("mongodb://localhost:27017");
return client;
}
test("testConnectWithUri", async () => {
const client = new MongoClient();
client.connectWithUri("mongodb://localhost:27017");
const names = await client.listDatabases();
assert(names instanceof Array);
assert(names.length > 0);
});
test("testConnectWithOptions", async () => {
const client = new MongoClient();
client.connectWithOptions({
hosts: ["localhost:27017"],
});
const names = await client.listDatabases();
assert(names instanceof Array);
assert(names.length > 0);
});
test("testListCollectionNames", async () => {
const db = getClient().database("local");
const names = await db.listCollectionNames();
assertEquals(names, ["startup_log"]);
});
test("testInsertOne", async () => {
const db = getClient().database("test");
const users = db.collection("mongo_test_users");
const insertId: ObjectId = await users.insertOne({
username: "user1",
password: "pass1",
date: new Date(dateNow),
});
assertEquals(Object.keys(insertId), ["$oid"]);
const user1 = await users.findOne({
_id: ObjectId(insertId.$oid),
});
assertEquals(user1, {
_id: insertId,
username: "user1",
password: "pass1",
date: new Date(dateNow),
});
});
test("testFindOne", async () => {
const db = getClient().database("test");
const users = db.collection("mongo_test_users");
const user1 = await users.findOne();
assert(user1 instanceof Object);
assertEquals(Object.keys(user1), ["_id", "username", "password", "date"]);
const findNull = await users.findOne({ test: 1 });
assertEquals(findNull, null);
});
test("testUpdateOne", async () => {
const db = getClient().database("test");
const users = db.collection("mongo_test_users");
const result = await users.updateOne({}, { username: "USER1" });
assertEquals(result, { matchedCount: 1, modifiedCount: 1, upsertedId: null });
});
test("testDeleteOne", async () => {
const db = getClient().database("test");
const users = db.collection("mongo_test_users");
const deleteCount = await users.deleteOne({});
assertEquals(deleteCount, 1);
});
test("testInsertMany", async () => {
const db = getClient().database("test");
const users = db.collection("mongo_test_users");
const insertIds = await users.insertMany([
{
username: "many",
password: "pass1",
},
{
username: "many",
password: "pass2",
},
]);
assertEquals(insertIds.length, 2);
});
test("testFind", async () => {
const db = getClient().database("test");
const users = db.collection("mongo_test_users");
const findUsers = await users.find(
{ username: "many" },
{ skip: 1, limit: 1 },
);
assert(findUsers instanceof Array);
assertEquals(findUsers.length, 1);
const notFound = await users.find({ test: 1 });
assertEquals(notFound, []);
});
test("testCount", async () => {
const db = getClient().database("test");
const users = db.collection("mongo_test_users");
const count = await users.count({ username: "many" });
assertEquals(count, 2);
});
test("testAggregation", async () => {
const db = getClient().database("test");
const users = db.collection("mongo_test_users");
const docs = await users.aggregate([
{ $match: { username: "many" } },
{ $group: { _id: "$username", total: { $sum: 1 } } },
]);
assertEquals(docs, [{ _id: "many", total: 2 }]);
});
test("testUpdateMany", async () => {
const db = getClient().database("test");
const users = db.collection("mongo_test_users");
const result = await users.updateMany(
{ username: "many" },
{ $set: { username: "MANY" } },
);
assertEquals(result, { matchedCount: 2, modifiedCount: 2, upsertedId: null });
});
test("testDeleteMany", async () => {
const db = getClient().database("test");
const users = db.collection("mongo_test_users");
const deleteCount = await users.deleteMany({ username: "MANY" });
assertEquals(deleteCount, 2);
});
// TODO mongdb_rust official library has not implemented this feature
// test("testCreateIndexes", async () => {
// const db = getClient().database("test");
// const collection = db.collection("mongo_indexes");
// const result = await collection.createIndexes([
// { keys: { created_at: 1 }, options: { expireAfterSeconds: 10000 } }
// ]);
// console.log(result);
// });
if (await exists(".deno_plugins")) {
await Deno.remove(".deno_plugins", { recursive: true });
}
await cargoBuild();
await init("file://./target/release");