From ebb2d078883b6e73228245e54a92a826894fd38d Mon Sep 17 00:00:00 2001 From: Haoran Lin Date: Wed, 8 Sep 2021 17:08:46 +0800 Subject: [PATCH 1/2] fix: fix failed test in mongo-4.4 --- src/collection/collection.ts | 2 +- tests/cases/04_indexes.ts | 33 ++++++++++++++++++++++++--------- tests/common.ts | 17 +++++++++++++++++ 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/collection/collection.ts b/src/collection/collection.ts index fa06c3dd..b1a57a75 100644 --- a/src/collection/collection.ts +++ b/src/collection/collection.ts @@ -313,7 +313,7 @@ export class Collection { listIndexes() { return new ListIndexesCursor< - { v: number; key: Document; name: string; ns: string } + { v: number; key: Document; name: string; ns?: string } >({ protocol: this.#protocol, dbName: this.#dbName, diff --git a/tests/cases/04_indexes.ts b/tests/cases/04_indexes.ts index 0373b9c2..f7ddf835 100644 --- a/tests/cases/04_indexes.ts +++ b/tests/cases/04_indexes.ts @@ -1,4 +1,4 @@ -import { testWithClient } from "../common.ts"; +import { compareVersion, testWithClient } from "../common.ts"; import { assertEquals } from "../test.deps.ts"; export default function indexesTests() { @@ -27,12 +27,19 @@ export default function indexesTests() { const users = db.collection("mongo_test_users"); const cursor = users.listIndexes(); const indexes = await cursor.toArray(); - assertEquals( - indexes, - [ + + const expected = compareVersion(client, [4, 2]) > 0 + ? [ + { v: 2, key: { _id: 1 }, name: "_id_" }, + { v: 2, key: { name: 1 }, name: "_name" }, + ] + : [ { v: 2, key: { _id: 1 }, name: "_id_", ns: "test.mongo_test_users" }, { v: 2, key: { name: 1 }, name: "_name", ns: "test.mongo_test_users" }, - ], + ]; + assertEquals( + indexes, + expected, ); }); @@ -52,12 +59,20 @@ export default function indexesTests() { }); const indexes = await users.listIndexes().toArray(); - + const expected = compareVersion(client, [4, 2]) > 0 + ? [ + { v: 2, key: { _id: 1 }, name: "_id_" }, + ] + : [ + { v: 2, key: { _id: 1 }, name: "_id_", ns: "test.mongo_test_users" }, + ]; assertEquals( indexes, - [ - { v: 2, key: { _id: 1 }, name: "_id_", ns: "test.mongo_test_users" }, - ], + expected, + ); + assertEquals( + indexes, + expected, ); }); } diff --git a/tests/common.ts b/tests/common.ts index 95b17552..857bbd72 100644 --- a/tests/common.ts +++ b/tests/common.ts @@ -13,6 +13,23 @@ export function testWithClient( }); } +export function compareVersion( + client: MongoClient, + version: number[], + skipPatch = true, +) { + const [thisMajor, thisMinor, thisPatch] = client.buildInfo?.versionArray!; + const [major, minor, patch] = version; + + return compareNumber(thisMajor, major) || + compareNumber(thisMinor, minor) || + (skipPatch ? 0 : compareNumber(thisPatch, patch)); +} + +function compareNumber(a: number, b: number) { + return a === b ? 0 : a > b ? 1 : -1; +} + async function getClient(): Promise { const client = new MongoClient(); await client.connect(`mongodb://${hostname}:27017`); From ab8868b8123000cd405a5421c1b56a704631a640 Mon Sep 17 00:00:00 2001 From: Haoran Lin Date: Wed, 8 Sep 2021 17:51:20 +0800 Subject: [PATCH 2/2] refactor: use semver to compare versions --- tests/cases/04_indexes.ts | 8 ++++---- tests/common.ts | 17 ----------------- tests/test.deps.ts | 1 + 3 files changed, 5 insertions(+), 21 deletions(-) diff --git a/tests/cases/04_indexes.ts b/tests/cases/04_indexes.ts index f7ddf835..6b76dc95 100644 --- a/tests/cases/04_indexes.ts +++ b/tests/cases/04_indexes.ts @@ -1,5 +1,5 @@ -import { compareVersion, testWithClient } from "../common.ts"; -import { assertEquals } from "../test.deps.ts"; +import { testWithClient } from "../common.ts"; +import { assertEquals, semver } from "../test.deps.ts"; export default function indexesTests() { testWithClient("createIndexes", async (client) => { @@ -28,7 +28,7 @@ export default function indexesTests() { const cursor = users.listIndexes(); const indexes = await cursor.toArray(); - const expected = compareVersion(client, [4, 2]) > 0 + const expected = semver.gte(client.buildInfo!.version, "4.4.0") ? [ { v: 2, key: { _id: 1 }, name: "_id_" }, { v: 2, key: { name: 1 }, name: "_name" }, @@ -59,7 +59,7 @@ export default function indexesTests() { }); const indexes = await users.listIndexes().toArray(); - const expected = compareVersion(client, [4, 2]) > 0 + const expected = semver.gte(client.buildInfo!.version, "4.4.0") ? [ { v: 2, key: { _id: 1 }, name: "_id_" }, ] diff --git a/tests/common.ts b/tests/common.ts index 857bbd72..95b17552 100644 --- a/tests/common.ts +++ b/tests/common.ts @@ -13,23 +13,6 @@ export function testWithClient( }); } -export function compareVersion( - client: MongoClient, - version: number[], - skipPatch = true, -) { - const [thisMajor, thisMinor, thisPatch] = client.buildInfo?.versionArray!; - const [major, minor, patch] = version; - - return compareNumber(thisMajor, major) || - compareNumber(thisMinor, minor) || - (skipPatch ? 0 : compareNumber(thisPatch, patch)); -} - -function compareNumber(a: number, b: number) { - return a === b ? 0 : a > b ? 1 : -1; -} - async function getClient(): Promise { const client = new MongoClient(); await client.connect(`mongodb://${hostname}:27017`); diff --git a/tests/test.deps.ts b/tests/test.deps.ts index 852b8b7d..3269ef5b 100644 --- a/tests/test.deps.ts +++ b/tests/test.deps.ts @@ -5,3 +5,4 @@ export { assertThrows, assertThrowsAsync, } from "https://deno.land/std@0.105.0/testing/asserts.ts"; +export * as semver from "https://deno.land/x/semver@v1.4.0/mod.ts";