From dfbd1b3ebffb35b6cce0999ef4c8a98efb330382 Mon Sep 17 00:00:00 2001 From: Erfan Date: Wed, 9 Oct 2024 20:57:23 +0330 Subject: [PATCH 1/3] Update ci.yml --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d29829e..324879a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,7 +23,7 @@ jobs: - name: Install Deno uses: denolib/setup-deno@master with: - deno-version: 1.x.x + deno-version: 2.x.x - name: Log versions run: | From a81628497b13c1e47a8cb2d8a947b3a4fa47323b Mon Sep 17 00:00:00 2001 From: Erfanium Date: Wed, 9 Oct 2024 21:12:12 +0330 Subject: [PATCH 2/3] chore: some minor changes to make deno2 happy --- .github/workflows/jsr.yml | 2 +- deps.ts | 6 +- src/client.ts | 33 ++-- src/error.ts | 2 +- tests/cases/03_crud.ts | 366 +++++++++++++++++++++----------------- tests/cases/05_srv.ts | 100 ++++++----- 6 files changed, 272 insertions(+), 237 deletions(-) diff --git a/.github/workflows/jsr.yml b/.github/workflows/jsr.yml index 6e91d51..0964a7e 100644 --- a/.github/workflows/jsr.yml +++ b/.github/workflows/jsr.yml @@ -33,4 +33,4 @@ jobs: - name: Publish to JSR run: | - deno publish \ No newline at end of file + deno publish diff --git a/deps.ts b/deps.ts index a70a811..68b1346 100644 --- a/deps.ts +++ b/deps.ts @@ -16,6 +16,6 @@ export { Timestamp, UUID, } from "jsr:@lucsoft/web-bson@^0.3.1"; -export { crypto as stdCrypto } from "jsr:@std/crypto@^0.224.0/crypto"; -export { decodeBase64, encodeBase64 } from "jsr:@std/encoding@^0.224.0/base64"; -export { encodeHex } from "jsr:@std/encoding@^0.224.0/hex"; +export { crypto as stdCrypto } from "jsr:@std/crypto@^1.0.3/crypto"; +export { decodeBase64, encodeBase64 } from "jsr:@std/encoding@^1.0.5/base64"; +export { encodeHex } from "jsr:@std/encoding@^1.0.5/hex"; diff --git a/src/client.ts b/src/client.ts index 7090500..1853a82 100644 --- a/src/client.ts +++ b/src/client.ts @@ -29,7 +29,7 @@ export class MongoClient { getCluster(): Cluster { if (!this.#cluster) { throw new MongoDriverError( - "MongoClient is not connected to the Database", + "MongoClient is not connected to the Database" ); } @@ -41,13 +41,10 @@ export class MongoClient { * * @param options Connection options or a MongoDB URI */ - async connect( - options: ConnectOptions | string, - ): Promise { + async connect(options: ConnectOptions | string): Promise { try { - const parsedOptions = typeof options === "string" - ? await parse(options) - : options; + const parsedOptions = + typeof options === "string" ? await parse(options) : options; this.#defaultDbName = parsedOptions.db; const cluster = new Cluster(parsedOptions); @@ -59,8 +56,10 @@ export class MongoClient { this.#buildInfo = await this.runCommand(this.#defaultDbName, { buildInfo: 1, }); - } catch (e) { - throw new MongoDriverError(`Connection failed: ${e.message || e}`); + } catch (e: unknown) { + throw new MongoDriverError( + `Connection failed: ${e instanceof Error ? e.message : "unknown"}` + ); } return this.database((options as ConnectOptions).db); } @@ -71,18 +70,20 @@ export class MongoClient { * @param options Options to pass to the `listDatabases` command * @returns A list of databases including their name, size on disk, and whether they are empty */ - async listDatabases(options: { - filter?: Document; - nameOnly?: boolean; - authorizedCollections?: boolean; - comment?: Document; - } = {}): Promise { + async listDatabases( + options: { + filter?: Document; + nameOnly?: boolean; + authorizedCollections?: boolean; + comment?: Document; + } = {} + ): Promise { const { databases } = await this.getCluster().protocol.commandSingle( "admin", { listDatabases: 1, ...options, - }, + } ); return databases; } diff --git a/src/error.ts b/src/error.ts index e0732a4..4e9ae22 100644 --- a/src/error.ts +++ b/src/error.ts @@ -73,7 +73,7 @@ export class MongoRuntimeError extends MongoDriverError { super(message); } - get name(): string { + override get name(): string { return "MongoRuntimeError"; } } diff --git a/tests/cases/03_crud.ts b/tests/cases/03_crud.ts index 7beb539..26fbf76 100644 --- a/tests/cases/03_crud.ts +++ b/tests/cases/03_crud.ts @@ -1,3 +1,4 @@ +import { assertInstanceOf } from "jsr:@std/assert@^0.220.1/assert_instance_of"; import type { Database, MongoClient, ObjectId } from "../../mod.ts"; import { MongoInvalidArgumentError, @@ -103,7 +104,7 @@ describe("crud operations", () => { date: new Date(dateNow), }, }, - { upsert: true }, + { upsert: true } ); assert(upsertedId); @@ -134,7 +135,7 @@ describe("crud operations", () => { username: "user1", }), MongoServerError, - "E11000", + "E11000" ); }); @@ -153,12 +154,12 @@ describe("crud operations", () => { assertEquals(findNull, undefined); const projectionUser = await users.findOne( {}, - { projection: { _id: 0, username: 1 } }, + { projection: { _id: 0, username: 1 } } ); assertEquals(Object.keys(projectionUser!), ["username"]); const projectionUserWithId = await users.findOne( {}, - { projection: { username: 1 } }, + { projection: { username: 1 } } ); assertEquals(Object.keys(projectionUserWithId!), ["_id", "username"]); }); @@ -236,7 +237,7 @@ describe("crud operations", () => { it("testFindAndModify-notfound", async () => { const users = database.collection<{ username: string; counter: number }>( - "mongo_test_users", + "mongo_test_users" ); const find = await users.findAndModify( @@ -247,7 +248,7 @@ describe("crud operations", () => { { update: { $inc: { counter: 1 } }, new: false, - }, + } ); assert(find === null); @@ -256,13 +257,16 @@ describe("crud operations", () => { it("testFindAndModify-update", async () => { const users = database.collection<{ username: string; counter: number }>( - "mongo_test_users", + "mongo_test_users" ); await users.insertOne({ username: "counter", counter: 5 }); - const updated = await users.findAndModify({ username: "counter" }, { - update: { $inc: { counter: 1 } }, - new: true, - }); + const updated = await users.findAndModify( + { username: "counter" }, + { + update: { $inc: { counter: 1 } }, + new: true, + } + ); assert(updated !== null); assertEquals(updated.counter, 6); @@ -271,12 +275,15 @@ describe("crud operations", () => { it("testFindAndModify-delete", async () => { const users = database.collection<{ username: string; counter: number }>( - "mongo_test_users", + "mongo_test_users" ); await users.insertOne({ username: "delete", counter: 10 }); - const updated = await users.findAndModify({ username: "delete" }, { - remove: true, - }); + const updated = await users.findAndModify( + { username: "delete" }, + { + remove: true, + } + ); assert(updated !== null); assertEquals(updated.counter, 10); @@ -330,9 +337,12 @@ describe("crud operations", () => { }, friends: ["Alice", "Bob"], }); - const result = await users.updateOne({}, { - $push: { friends: { $each: ["Carol"] } }, - }); + const result = await users.updateOne( + {}, + { + $push: { friends: { $each: ["Carol"] } }, + } + ); assertEquals(result, { matchedCount: 1, modifiedCount: 1, @@ -350,9 +360,12 @@ describe("crud operations", () => { }, friends: ["Alice", "Bob"], }); - const result = await users.updateOne({}, { - $pull: { friends: "Bob" }, - }); + const result = await users.updateOne( + {}, + { + $pull: { friends: "Bob" }, + } + ); assertEquals(result, { matchedCount: 1, modifiedCount: 1, @@ -370,9 +383,12 @@ describe("crud operations", () => { }, friends: ["Alice", "Bob"], }); - const result = await users.updateOne({}, { - $push: { "likes.hobbies.indoor": "board games" }, - }); + const result = await users.updateOne( + {}, + { + $push: { "likes.hobbies.indoor": "board games" }, + } + ); assertEquals(result, { matchedCount: 1, modifiedCount: 1, @@ -390,9 +406,12 @@ describe("crud operations", () => { }, friends: ["Alice", "Bob"], }); - const result = await users.updateOne({}, { - $pullAll: { "likes.hobbies.indoor": ["board games", "cooking"] }, - }); + const result = await users.updateOne( + {}, + { + $pullAll: { "likes.hobbies.indoor": ["board games", "cooking"] }, + } + ); assertEquals(result, { matchedCount: 1, modifiedCount: 1, @@ -410,9 +429,12 @@ describe("crud operations", () => { }, friends: ["Alice", "Bob"], }); - const result = await users.updateOne({}, { - $pull: { "likes.hobbies.indoor": "board games" }, - }); + const result = await users.updateOne( + {}, + { + $pull: { "likes.hobbies.indoor": "board games" }, + } + ); assertEquals(result, { matchedCount: 1, modifiedCount: 1, @@ -421,7 +443,8 @@ describe("crud operations", () => { }); }); - it("testUpdateOne Error", async () => { // TODO: move tesr errors to a new file + it("testUpdateOne Error", async () => { + // TODO: move tesr errors to a new file const users = database.collection("mongo_test_users"); await users.insertOne({ username: "user1", @@ -444,7 +467,7 @@ describe("crud operations", () => { const result = await users.updateOne( { username: "user2" }, { $set: { username: "USER2" } }, - { upsert: true }, + { upsert: true } ); assertEquals(result.matchedCount, 1); assertEquals(result.modifiedCount, 0); @@ -457,9 +480,12 @@ describe("crud operations", () => { username: "user1", password: "pass1", }); - const result = await users.replaceOne({ username: "user1" }, { - username: "user2", - }); + const result = await users.replaceOne( + { username: "user1" }, + { + username: "user2", + } + ); assertEquals(result, { matchedCount: 1, @@ -672,7 +698,7 @@ describe("crud operations", () => { ]); const result = await users.updateMany( { username: "many" }, - { $set: { username: "MANY" } }, + { $set: { username: "MANY" } } ); assertEquals(result, { matchedCount: 2, @@ -768,13 +794,13 @@ describe("crud operations", () => { acceding, all.sort((lhs, rhs) => { return lhs.uid! - rhs.uid!; - }), + }) ); assertEquals( descending, all.sort((lhs, rhs) => { return -lhs.uid! - rhs.uid!; - }), + }) ); await database.collection("mongo_test_users").drop(); @@ -802,7 +828,7 @@ describe("crud operations", () => { it("testFindWithMaxTimeMS", async () => { const supportsMaxTimeMSInFindOne = greaterOrEqual( parse(client.buildInfo!.version), - { major: 4, minor: 2, patch: 0 }, + { major: 4, minor: 2, patch: 0 } ); const users = database.collection("mongo_test_users"); @@ -813,25 +839,39 @@ describe("crud operations", () => { uid: i, }); } - const users1 = await users.find({ - uid: 0, - }, { maxTimeMS: 100 }).toArray(); + const users1 = await users + .find( + { + uid: 0, + }, + { maxTimeMS: 100 } + ) + .toArray(); assertEquals(users1.length, 1); - const user1 = await users.findOne({ - uid: 0, - }, { maxTimeMS: 100 }); + const user1 = await users.findOne( + { + uid: 0, + }, + { maxTimeMS: 100 } + ); assertEquals(user1!.uid, 0); try { - await users.find({ - uid: 0, - $where: "sleep(10) || true", - }, { maxTimeMS: 1 }).toArray(); + await users + .find( + { + uid: 0, + $where: "sleep(10) || true", + }, + { maxTimeMS: 1 } + ) + .toArray(); assert(false); } catch (e) { + assertInstanceOf(e, MongoServerError); assertEquals(e.ok, 0); assertEquals(e.codeName, "MaxTimeMSExpired"); assertEquals(e.errmsg, "operation exceeded time limit"); @@ -839,12 +879,16 @@ describe("crud operations", () => { if (supportsMaxTimeMSInFindOne) { try { - await users.findOne({ - uid: 0, - $where: "sleep(10) || true", - }, { maxTimeMS: 1 }); + await users.findOne( + { + uid: 0, + $where: "sleep(10) || true", + }, + { maxTimeMS: 1 } + ); assert(false); } catch (e) { + assertInstanceOf(e, MongoServerError); assertEquals(e.ok, 0); assertEquals(e.codeName, "MaxTimeMSExpired"); assertEquals(e.errmsg, "operation exceeded time limit"); @@ -854,128 +898,116 @@ describe("crud operations", () => { await database.collection("mongo_test_users").drop(); }); - it( - "createCollection should create a collection without options", - async () => { - const createdCollection = await database - .createCollection<{ _id: string; name: string }>( - testCollectionName, - ); + it("createCollection should create a collection without options", async () => { + const createdCollection = await database.createCollection<{ + _id: string; + name: string; + }>(testCollectionName); - assert(createdCollection); - - await database.collection(testCollectionName).drop(); - }, - ); - - it( - "createCollection should create a collection with options", - async () => { - interface IStudents { - _id: string; - name: string; - year: number; - major: string; - gpa?: number; - address: { - city: string; - street: string; - }; - } + assert(createdCollection); - // Example from https://www.mongodb.com/docs/manual/reference/operator/query/jsonSchema/#mongodb-query-op.-jsonSchema - const options: CreateCollectionOptions = { - validator: { - $jsonSchema: { - bsonType: "object", - required: ["name", "year", "major", "address"], - properties: { - name: { - bsonType: "string", - description: "must be a string and is required", - }, - year: { - bsonType: "int", - minimum: 2017, - maximum: 3017, - description: - "must be an integer in [ 2017, 3017 ] and is required", - }, - major: { - enum: ["Math", "English", "Computer Science", "History", null], - description: - "can only be one of the enum values and is required", - }, - gpa: { - bsonType: ["double"], - description: "must be a double if the field exists", - }, - address: { - bsonType: "object", - required: ["city"], - properties: { - street: { - bsonType: "string", - description: "must be a string if the field exists", - }, - city: { - bsonType: "string", - "description": "must be a string and is required", - }, + await database.collection(testCollectionName).drop(); + }); + + it("createCollection should create a collection with options", async () => { + interface IStudents { + _id: string; + name: string; + year: number; + major: string; + gpa?: number; + address: { + city: string; + street: string; + }; + } + + // Example from https://www.mongodb.com/docs/manual/reference/operator/query/jsonSchema/#mongodb-query-op.-jsonSchema + const options: CreateCollectionOptions = { + validator: { + $jsonSchema: { + bsonType: "object", + required: ["name", "year", "major", "address"], + properties: { + name: { + bsonType: "string", + description: "must be a string and is required", + }, + year: { + bsonType: "int", + minimum: 2017, + maximum: 3017, + description: + "must be an integer in [ 2017, 3017 ] and is required", + }, + major: { + enum: ["Math", "English", "Computer Science", "History", null], + description: "can only be one of the enum values and is required", + }, + gpa: { + bsonType: ["double"], + description: "must be a double if the field exists", + }, + address: { + bsonType: "object", + required: ["city"], + properties: { + street: { + bsonType: "string", + description: "must be a string if the field exists", + }, + city: { + bsonType: "string", + description: "must be a string and is required", }, }, }, }, }, - }; + }, + }; - const createdCollection = await database - .createCollection( - testCollectionName, - options, - ); + const createdCollection = await database.createCollection( + testCollectionName, + options + ); - assert(createdCollection); - - // sanity test to check whether the speicified validator from options works - // error with message: "Document failed validation" - await assertRejects( - () => - createdCollection.insertOne({ - name: "Alice", - year: 2019, - major: "History", - gpa: 3, - address: { - city: "NYC", - street: "33rd Street", - }, - }), - ); - - // TODO: refactor to clean up the test collection properly. - // It should clean up the collection when above insertion succeeds in any case, which is unwanted result. - // Refactor when test utility is more provided. - await database.collection(testCollectionName).drop(); - }, - ); - - it( - "createCollection should throw an error with invalid options", - async () => { - const invalidOptions: CreateCollectionOptions = { - capped: true, - }; + assert(createdCollection); + + // sanity test to check whether the speicified validator from options works + // error with message: "Document failed validation" + await assertRejects(() => + createdCollection.insertOne({ + name: "Alice", + year: 2019, + major: "History", + gpa: 3, + address: { + city: "NYC", + street: "33rd Street", + }, + }) + ); - await assertRejects( - () => - database.createCollection<{ _id: string; name: string }>( - testCollectionName, - invalidOptions, - ), - // error with the message "the 'size' field is required when 'capped' is true" - MongoServerError, - ); - }, - ); + // TODO: refactor to clean up the test collection properly. + // It should clean up the collection when above insertion succeeds in any case, which is unwanted result. + // Refactor when test utility is more provided. + await database.collection(testCollectionName).drop(); + }); + + it("createCollection should throw an error with invalid options", async () => { + const invalidOptions: CreateCollectionOptions = { + capped: true, + }; + + await assertRejects( + () => + database.createCollection<{ _id: string; name: string }>( + testCollectionName, + invalidOptions + ), + // error with the message "the 'size' field is required when 'capped' is true" + MongoServerError + ); + }); }); diff --git a/tests/cases/05_srv.ts b/tests/cases/05_srv.ts index 93f15f7..bee2eea 100644 --- a/tests/cases/05_srv.ts +++ b/tests/cases/05_srv.ts @@ -2,8 +2,8 @@ import { Srv } from "../../src/utils/srv.ts"; import { assertEquals, assertRejects, describe, it } from "../deps.ts"; function mockResolver( - srvRecords: Partial[] = [], - txtRecords: string[][] = [], + srvRecords: Partial[] = [], + txtRecords: string[][] = [] ) { return { resolveDns: (_url: string, type: Deno.RecordType) => { @@ -21,19 +21,18 @@ describe("SRV", () => { assertRejects( () => new Srv().resolve("foo.bar"), Error, - "Expected url in format 'host.domain.tld', received foo.bar", + "Expected url in format 'host.domain.tld', received foo.bar" ); }, }); it({ - name: - "SRV: it throws an error if SRV resolution doesn't return any SRV records", + name: "SRV: it throws an error if SRV resolution doesn't return any SRV records", fn() { assertRejects( () => new Srv(mockResolver()).resolve("mongohost.mongodomain.com"), Error, - "Expected at least one SRV record, received 0 for url mongohost.mongodomain.com", + "Expected at least one SRV record, received 0 for url mongohost.mongodomain.com" ); }, }); @@ -43,29 +42,28 @@ describe("SRV", () => { fn() { assertRejects( () => - new Srv(mockResolver([{ target: "mongohost1.mongodomain.com" }])) - .resolve("mongohost.mongodomain.com"), + new Srv( + mockResolver([{ target: "mongohost1.mongodomain.com" }]) + ).resolve("mongohost.mongodomain.com"), Error, - "Expected exactly one TXT record, received 0 for url mongohost.mongodomain.com", + "Expected exactly one TXT record, received 0 for url mongohost.mongodomain.com" ); }, }); it({ - name: - "SRV: it throws an error if TXT resolution returns more than one record", + name: "SRV: it throws an error if TXT resolution returns more than one record", fn() { assertRejects( () => new Srv( mockResolver( [{ target: "mongohost1.mongodomain.com" }], - [["replicaSet=rs-0"], ["authSource=admin"]], - ), - ) - .resolve("mongohost.mongodomain.com"), + [["replicaSet=rs-0"], ["authSource=admin"]] + ) + ).resolve("mongohost.mongodomain.com"), Error, - "Expected exactly one TXT record, received 2 for url mongohost.mongodomain.com", + "Expected exactly one TXT record, received 2 for url mongohost.mongodomain.com" ); }, }); @@ -78,12 +76,11 @@ describe("SRV", () => { new Srv( mockResolver( [{ target: "mongohost1.mongodomain.com" }], - [["replicaSet=rs-0&authSource=admin&ssl=true"]], - ), - ) - .resolve("mongohost.mongodomain.com"), + [["replicaSet=rs-0&authSource=admin&ssl=true"]] + ) + ).resolve("mongohost.mongodomain.com"), Error, - "Illegal uri options: ssl=true", + "Illegal uri options: ssl=true" ); }, }); @@ -92,23 +89,26 @@ describe("SRV", () => { name: "SRV: it correctly parses seedlist and options for valid records", async fn() { const result = await new Srv( - mockResolver([ - { - target: "mongohost1.mongodomain.com", - port: 27015, - }, - { - target: "mongohost2.mongodomain.com", - port: 27017, - }, - ], [["replicaSet=rs-0&authSource=admin"]]), + mockResolver( + [ + { + target: "mongohost1.mongodomain.com", + port: 27015, + }, + { + target: "mongohost2.mongodomain.com", + port: 27017, + }, + ], + [["replicaSet=rs-0&authSource=admin"]] + ) ).resolve("mongohost.mongodomain.com"); assertEquals(result.servers.length, 2); const server1 = result.servers.find( - (server) => server.host === "mongohost1.mongodomain.com", + (server) => server.host === "mongohost1.mongodomain.com" ); - const server2 = result.servers.find((server) => - server.host === "mongohost2.mongodomain.com" + const server2 = result.servers.find( + (server) => server.host === "mongohost2.mongodomain.com" ); assertEquals(server1!.port, 27015); assertEquals(server2!.port, 27017); @@ -119,27 +119,29 @@ describe("SRV", () => { }); it({ - name: - "SRV: it correctly parses seedlist and options for options split in two strings", + name: "SRV: it correctly parses seedlist and options for options split in two strings", async fn() { const result = await new Srv( - mockResolver([ - { - target: "mongohost1.mongodomain.com", - port: 27015, - }, - { - target: "mongohost2.mongodomain.com", - port: 27017, - }, - ], [["replicaS", "et=rs-0&authSource=admin"]]), + mockResolver( + [ + { + target: "mongohost1.mongodomain.com", + port: 27015, + }, + { + target: "mongohost2.mongodomain.com", + port: 27017, + }, + ], + [["replicaS", "et=rs-0&authSource=admin"]] + ) ).resolve("mongohost.mongodomain.com"); assertEquals(result.servers.length, 2); const server1 = result.servers.find( - (server) => server.host === "mongohost1.mongodomain.com", + (server) => server.host === "mongohost1.mongodomain.com" ); - const server2 = result.servers.find((server) => - server.host === "mongohost2.mongodomain.com" + const server2 = result.servers.find( + (server) => server.host === "mongohost2.mongodomain.com" ); assertEquals(server1!.port, 27015); assertEquals(server2!.port, 27017); From 6f6cfd6d475c45ff8f5ed06e2d61321191f8e0a4 Mon Sep 17 00:00:00 2001 From: Erfanium Date: Wed, 9 Oct 2024 21:13:41 +0330 Subject: [PATCH 3/3] apply deno fmt --- src/client.ts | 13 +++++----- tests/cases/03_crud.ts | 56 +++++++++++++++++++++--------------------- tests/cases/05_srv.ts | 47 ++++++++++++++++++----------------- 3 files changed, 60 insertions(+), 56 deletions(-) diff --git a/src/client.ts b/src/client.ts index 1853a82..bdf35fc 100644 --- a/src/client.ts +++ b/src/client.ts @@ -29,7 +29,7 @@ export class MongoClient { getCluster(): Cluster { if (!this.#cluster) { throw new MongoDriverError( - "MongoClient is not connected to the Database" + "MongoClient is not connected to the Database", ); } @@ -43,8 +43,9 @@ export class MongoClient { */ async connect(options: ConnectOptions | string): Promise { try { - const parsedOptions = - typeof options === "string" ? await parse(options) : options; + const parsedOptions = typeof options === "string" + ? await parse(options) + : options; this.#defaultDbName = parsedOptions.db; const cluster = new Cluster(parsedOptions); @@ -58,7 +59,7 @@ export class MongoClient { }); } catch (e: unknown) { throw new MongoDriverError( - `Connection failed: ${e instanceof Error ? e.message : "unknown"}` + `Connection failed: ${e instanceof Error ? e.message : "unknown"}`, ); } return this.database((options as ConnectOptions).db); @@ -76,14 +77,14 @@ export class MongoClient { nameOnly?: boolean; authorizedCollections?: boolean; comment?: Document; - } = {} + } = {}, ): Promise { const { databases } = await this.getCluster().protocol.commandSingle( "admin", { listDatabases: 1, ...options, - } + }, ); return databases; } diff --git a/tests/cases/03_crud.ts b/tests/cases/03_crud.ts index 26fbf76..3979c6d 100644 --- a/tests/cases/03_crud.ts +++ b/tests/cases/03_crud.ts @@ -104,7 +104,7 @@ describe("crud operations", () => { date: new Date(dateNow), }, }, - { upsert: true } + { upsert: true }, ); assert(upsertedId); @@ -135,7 +135,7 @@ describe("crud operations", () => { username: "user1", }), MongoServerError, - "E11000" + "E11000", ); }); @@ -154,12 +154,12 @@ describe("crud operations", () => { assertEquals(findNull, undefined); const projectionUser = await users.findOne( {}, - { projection: { _id: 0, username: 1 } } + { projection: { _id: 0, username: 1 } }, ); assertEquals(Object.keys(projectionUser!), ["username"]); const projectionUserWithId = await users.findOne( {}, - { projection: { username: 1 } } + { projection: { username: 1 } }, ); assertEquals(Object.keys(projectionUserWithId!), ["_id", "username"]); }); @@ -237,7 +237,7 @@ describe("crud operations", () => { it("testFindAndModify-notfound", async () => { const users = database.collection<{ username: string; counter: number }>( - "mongo_test_users" + "mongo_test_users", ); const find = await users.findAndModify( @@ -248,7 +248,7 @@ describe("crud operations", () => { { update: { $inc: { counter: 1 } }, new: false, - } + }, ); assert(find === null); @@ -257,7 +257,7 @@ describe("crud operations", () => { it("testFindAndModify-update", async () => { const users = database.collection<{ username: string; counter: number }>( - "mongo_test_users" + "mongo_test_users", ); await users.insertOne({ username: "counter", counter: 5 }); const updated = await users.findAndModify( @@ -265,7 +265,7 @@ describe("crud operations", () => { { update: { $inc: { counter: 1 } }, new: true, - } + }, ); assert(updated !== null); @@ -275,14 +275,14 @@ describe("crud operations", () => { it("testFindAndModify-delete", async () => { const users = database.collection<{ username: string; counter: number }>( - "mongo_test_users" + "mongo_test_users", ); await users.insertOne({ username: "delete", counter: 10 }); const updated = await users.findAndModify( { username: "delete" }, { remove: true, - } + }, ); assert(updated !== null); @@ -341,7 +341,7 @@ describe("crud operations", () => { {}, { $push: { friends: { $each: ["Carol"] } }, - } + }, ); assertEquals(result, { matchedCount: 1, @@ -364,7 +364,7 @@ describe("crud operations", () => { {}, { $pull: { friends: "Bob" }, - } + }, ); assertEquals(result, { matchedCount: 1, @@ -387,7 +387,7 @@ describe("crud operations", () => { {}, { $push: { "likes.hobbies.indoor": "board games" }, - } + }, ); assertEquals(result, { matchedCount: 1, @@ -410,7 +410,7 @@ describe("crud operations", () => { {}, { $pullAll: { "likes.hobbies.indoor": ["board games", "cooking"] }, - } + }, ); assertEquals(result, { matchedCount: 1, @@ -433,7 +433,7 @@ describe("crud operations", () => { {}, { $pull: { "likes.hobbies.indoor": "board games" }, - } + }, ); assertEquals(result, { matchedCount: 1, @@ -467,7 +467,7 @@ describe("crud operations", () => { const result = await users.updateOne( { username: "user2" }, { $set: { username: "USER2" } }, - { upsert: true } + { upsert: true }, ); assertEquals(result.matchedCount, 1); assertEquals(result.modifiedCount, 0); @@ -484,7 +484,7 @@ describe("crud operations", () => { { username: "user1" }, { username: "user2", - } + }, ); assertEquals(result, { @@ -698,7 +698,7 @@ describe("crud operations", () => { ]); const result = await users.updateMany( { username: "many" }, - { $set: { username: "MANY" } } + { $set: { username: "MANY" } }, ); assertEquals(result, { matchedCount: 2, @@ -794,13 +794,13 @@ describe("crud operations", () => { acceding, all.sort((lhs, rhs) => { return lhs.uid! - rhs.uid!; - }) + }), ); assertEquals( descending, all.sort((lhs, rhs) => { return -lhs.uid! - rhs.uid!; - }) + }), ); await database.collection("mongo_test_users").drop(); @@ -828,7 +828,7 @@ describe("crud operations", () => { it("testFindWithMaxTimeMS", async () => { const supportsMaxTimeMSInFindOne = greaterOrEqual( parse(client.buildInfo!.version), - { major: 4, minor: 2, patch: 0 } + { major: 4, minor: 2, patch: 0 }, ); const users = database.collection("mongo_test_users"); @@ -844,7 +844,7 @@ describe("crud operations", () => { { uid: 0, }, - { maxTimeMS: 100 } + { maxTimeMS: 100 }, ) .toArray(); @@ -854,7 +854,7 @@ describe("crud operations", () => { { uid: 0, }, - { maxTimeMS: 100 } + { maxTimeMS: 100 }, ); assertEquals(user1!.uid, 0); @@ -866,7 +866,7 @@ describe("crud operations", () => { uid: 0, $where: "sleep(10) || true", }, - { maxTimeMS: 1 } + { maxTimeMS: 1 }, ) .toArray(); assert(false); @@ -884,7 +884,7 @@ describe("crud operations", () => { uid: 0, $where: "sleep(10) || true", }, - { maxTimeMS: 1 } + { maxTimeMS: 1 }, ); assert(false); } catch (e) { @@ -969,7 +969,7 @@ describe("crud operations", () => { const createdCollection = await database.createCollection( testCollectionName, - options + options, ); assert(createdCollection); @@ -1004,10 +1004,10 @@ describe("crud operations", () => { () => database.createCollection<{ _id: string; name: string }>( testCollectionName, - invalidOptions + invalidOptions, ), // error with the message "the 'size' field is required when 'capped' is true" - MongoServerError + MongoServerError, ); }); }); diff --git a/tests/cases/05_srv.ts b/tests/cases/05_srv.ts index bee2eea..09f1421 100644 --- a/tests/cases/05_srv.ts +++ b/tests/cases/05_srv.ts @@ -3,7 +3,7 @@ import { assertEquals, assertRejects, describe, it } from "../deps.ts"; function mockResolver( srvRecords: Partial[] = [], - txtRecords: string[][] = [] + txtRecords: string[][] = [], ) { return { resolveDns: (_url: string, type: Deno.RecordType) => { @@ -21,18 +21,19 @@ describe("SRV", () => { assertRejects( () => new Srv().resolve("foo.bar"), Error, - "Expected url in format 'host.domain.tld', received foo.bar" + "Expected url in format 'host.domain.tld', received foo.bar", ); }, }); it({ - name: "SRV: it throws an error if SRV resolution doesn't return any SRV records", + name: + "SRV: it throws an error if SRV resolution doesn't return any SRV records", fn() { assertRejects( () => new Srv(mockResolver()).resolve("mongohost.mongodomain.com"), Error, - "Expected at least one SRV record, received 0 for url mongohost.mongodomain.com" + "Expected at least one SRV record, received 0 for url mongohost.mongodomain.com", ); }, }); @@ -43,27 +44,28 @@ describe("SRV", () => { assertRejects( () => new Srv( - mockResolver([{ target: "mongohost1.mongodomain.com" }]) + mockResolver([{ target: "mongohost1.mongodomain.com" }]), ).resolve("mongohost.mongodomain.com"), Error, - "Expected exactly one TXT record, received 0 for url mongohost.mongodomain.com" + "Expected exactly one TXT record, received 0 for url mongohost.mongodomain.com", ); }, }); it({ - name: "SRV: it throws an error if TXT resolution returns more than one record", + name: + "SRV: it throws an error if TXT resolution returns more than one record", fn() { assertRejects( () => new Srv( mockResolver( [{ target: "mongohost1.mongodomain.com" }], - [["replicaSet=rs-0"], ["authSource=admin"]] - ) + [["replicaSet=rs-0"], ["authSource=admin"]], + ), ).resolve("mongohost.mongodomain.com"), Error, - "Expected exactly one TXT record, received 2 for url mongohost.mongodomain.com" + "Expected exactly one TXT record, received 2 for url mongohost.mongodomain.com", ); }, }); @@ -76,11 +78,11 @@ describe("SRV", () => { new Srv( mockResolver( [{ target: "mongohost1.mongodomain.com" }], - [["replicaSet=rs-0&authSource=admin&ssl=true"]] - ) + [["replicaSet=rs-0&authSource=admin&ssl=true"]], + ), ).resolve("mongohost.mongodomain.com"), Error, - "Illegal uri options: ssl=true" + "Illegal uri options: ssl=true", ); }, }); @@ -100,15 +102,15 @@ describe("SRV", () => { port: 27017, }, ], - [["replicaSet=rs-0&authSource=admin"]] - ) + [["replicaSet=rs-0&authSource=admin"]], + ), ).resolve("mongohost.mongodomain.com"); assertEquals(result.servers.length, 2); const server1 = result.servers.find( - (server) => server.host === "mongohost1.mongodomain.com" + (server) => server.host === "mongohost1.mongodomain.com", ); const server2 = result.servers.find( - (server) => server.host === "mongohost2.mongodomain.com" + (server) => server.host === "mongohost2.mongodomain.com", ); assertEquals(server1!.port, 27015); assertEquals(server2!.port, 27017); @@ -119,7 +121,8 @@ describe("SRV", () => { }); it({ - name: "SRV: it correctly parses seedlist and options for options split in two strings", + name: + "SRV: it correctly parses seedlist and options for options split in two strings", async fn() { const result = await new Srv( mockResolver( @@ -133,15 +136,15 @@ describe("SRV", () => { port: 27017, }, ], - [["replicaS", "et=rs-0&authSource=admin"]] - ) + [["replicaS", "et=rs-0&authSource=admin"]], + ), ).resolve("mongohost.mongodomain.com"); assertEquals(result.servers.length, 2); const server1 = result.servers.find( - (server) => server.host === "mongohost1.mongodomain.com" + (server) => server.host === "mongohost1.mongodomain.com", ); const server2 = result.servers.find( - (server) => server.host === "mongohost2.mongodomain.com" + (server) => server.host === "mongohost2.mongodomain.com", ); assertEquals(server1!.port, 27015); assertEquals(server2!.port, 27017);