Thin MongoDB ODM built for Standard Schema
mongodb
zod
deno
1import { assert, assertEquals, assertExists } from "@std/assert";
2import { connect, disconnect, healthCheck, type ConnectOptions } from "../mod.ts";
3import { MongoMemoryServer } from "mongodb-memory-server-core";
4
5let mongoServer: MongoMemoryServer | null = null;
6
7async function setupTestServer() {
8 if (!mongoServer) {
9 mongoServer = await MongoMemoryServer.create();
10 }
11 return mongoServer.getUri();
12}
13
14Deno.test.afterEach(async () => {
15 await disconnect();
16});
17
18Deno.test.afterAll(async () => {
19 if (mongoServer) {
20 await mongoServer.stop();
21 mongoServer = null;
22 }
23});
24
25Deno.test({
26 name: "Connection: Basic - should connect without options",
27 async fn() {
28 const uri = await setupTestServer();
29 const connection = await connect(uri, "test_db");
30
31 assert(connection);
32 assert(connection.client);
33 assert(connection.db);
34 assertEquals(connection.db.databaseName, "test_db");
35 },
36 sanitizeResources: false,
37 sanitizeOps: false,
38});
39
40Deno.test({
41 name: "Connection: Options - should connect with pooling options",
42 async fn() {
43 const uri = await setupTestServer();
44 const options: ConnectOptions = {
45 maxPoolSize: 10,
46 minPoolSize: 2,
47 maxIdleTimeMS: 30000,
48 connectTimeoutMS: 5000,
49 };
50
51 const connection = await connect(uri, "test_db", options);
52
53 assert(connection);
54 assert(connection.client);
55 assert(connection.db);
56
57 // Verify connection is working
58 const adminDb = connection.db.admin();
59 const serverStatus = await adminDb.serverStatus();
60 assert(serverStatus);
61 },
62 sanitizeResources: false,
63 sanitizeOps: false,
64});
65
66Deno.test({
67 name: "Connection: Singleton - should reuse existing connection",
68 async fn() {
69 const uri = await setupTestServer();
70
71 const connection1 = await connect(uri, "test_db");
72 const connection2 = await connect(uri, "test_db");
73
74 // Should return the same connection instance
75 assertEquals(connection1, connection2);
76 assertEquals(connection1.client, connection2.client);
77 assertEquals(connection1.db, connection2.db);
78 },
79 sanitizeResources: false,
80 sanitizeOps: false,
81});
82
83Deno.test({
84 name: "Connection: Disconnect - should disconnect and allow reconnection",
85 async fn() {
86 const uri = await setupTestServer();
87
88 const connection1 = await connect(uri, "test_db");
89 assert(connection1);
90
91 await disconnect();
92
93 // Should be able to reconnect
94 const connection2 = await connect(uri, "test_db");
95 assert(connection2);
96
97 // Should be a new connection instance
98 assert(connection1 !== connection2);
99 },
100 sanitizeResources: false,
101 sanitizeOps: false,
102});
103
104Deno.test({
105 name: "Connection: Options - should apply maxPoolSize option",
106 async fn() {
107 const uri = await setupTestServer();
108 const options: ConnectOptions = {
109 maxPoolSize: 5,
110 };
111
112 const connection = await connect(uri, "test_db", options);
113
114 // Verify connection works with custom pool size
115 const collections = await connection.db.listCollections().toArray();
116 assert(Array.isArray(collections));
117 },
118 sanitizeResources: false,
119 sanitizeOps: false,
120});
121
122Deno.test({
123 name: "Connection: Multiple Databases - should handle different database names",
124 async fn() {
125 const uri = await setupTestServer();
126
127 // Connect to first database
128 const connection1 = await connect(uri, "db1");
129 assertEquals(connection1.db.databaseName, "db1");
130
131 // Disconnect first
132 await disconnect();
133
134 // Connect to second database
135 const connection2 = await connect(uri, "db2");
136 assertEquals(connection2.db.databaseName, "db2");
137 },
138 sanitizeResources: false,
139 sanitizeOps: false,
140});
141
142Deno.test({
143 name: "Health Check: should return unhealthy when not connected",
144 async fn() {
145 const result = await healthCheck();
146
147 assertEquals(result.healthy, false);
148 assertEquals(result.connected, false);
149 assertExists(result.error);
150 assert(result.error?.includes("No active connection"));
151 assertExists(result.timestamp);
152 assertEquals(result.responseTimeMs, undefined);
153 },
154 sanitizeResources: false,
155 sanitizeOps: false,
156});
157
158Deno.test({
159 name: "Health Check: should return healthy when connected",
160 async fn() {
161 const uri = await setupTestServer();
162 await connect(uri, "test_db");
163
164 const result = await healthCheck();
165
166 assertEquals(result.healthy, true);
167 assertEquals(result.connected, true);
168 assertExists(result.responseTimeMs);
169 assert(result.responseTimeMs! >= 0);
170 assertEquals(result.error, undefined);
171 assertExists(result.timestamp);
172 },
173 sanitizeResources: false,
174 sanitizeOps: false,
175});
176
177Deno.test({
178 name: "Health Check: should measure response time",
179 async fn() {
180 const uri = await setupTestServer();
181 await connect(uri, "test_db");
182
183 const result = await healthCheck();
184
185 assertEquals(result.healthy, true);
186 assertExists(result.responseTimeMs);
187 // Response time should be reasonable (less than 1 second for in-memory MongoDB)
188 assert(result.responseTimeMs! < 1000);
189 },
190 sanitizeResources: false,
191 sanitizeOps: false,
192});
193
194Deno.test({
195 name: "Health Check: should work multiple times consecutively",
196 async fn() {
197 const uri = await setupTestServer();
198 await connect(uri, "test_db");
199
200 // Run health check multiple times
201 const results = await Promise.all([
202 healthCheck(),
203 healthCheck(),
204 healthCheck(),
205 ]);
206
207 // All should be healthy
208 for (const result of results) {
209 assertEquals(result.healthy, true);
210 assertEquals(result.connected, true);
211 assertExists(result.responseTimeMs);
212 }
213 },
214 sanitizeResources: false,
215 sanitizeOps: false,
216});
217
218Deno.test({
219 name: "Health Check: should detect disconnection",
220 async fn() {
221 const uri = await setupTestServer();
222 await connect(uri, "test_db");
223
224 // First check should be healthy
225 let result = await healthCheck();
226 assertEquals(result.healthy, true);
227
228 // Disconnect
229 await disconnect();
230
231 // Second check should be unhealthy
232 result = await healthCheck();
233 assertEquals(result.healthy, false);
234 assertEquals(result.connected, false);
235 },
236 sanitizeResources: false,
237 sanitizeOps: false,
238});
239