Skip to content

Commit

Permalink
Merge next into master (#236)
Browse files Browse the repository at this point in the history
* Update for Fastify v5 (#234)

* Update for fastify 5

* fixed linting issues

* update CI version

* added t.plan

* removed disabled coverage

* Update for Fastify v5 (#234)

* Update for fastify 5

* fixed linting issues

* update CI version

* added t.plan

* removed disabled coverage

* update fastify deps

---------

Co-authored-by: Cristian Barlutiu <[email protected]>
  • Loading branch information
jsumners and synapse authored Jul 9, 2024
1 parent 6c37edf commit 113d9ad
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ on:

jobs:
test:
uses: fastify/workflows/.github/workflows/plugins-ci-mongo.yml@7993e1ea858b5c3ceff1aa154fa44b892f311ac3
uses: fastify/workflows/.github/workflows/plugins-ci-mongo.yml@v4.1.0
with:
lint: true
license-check: true
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@
},
"homepage": "https://github.com/fastify/fastify-mongodb#readme",
"devDependencies": {
"@fastify/pre-commit": "^2.0.2",
"@types/node": "^20.6.2",
"fastify": "^4.23.2",
"@fastify/pre-commit": "^2.1.0",
"@types/node": "^20.11.30",
"fastify": "^5.0.0-alpha.3",
"standard": "^17.1.0",
"tap": "^16.1.0",
"tap": "^18.7.1",
"tsd": "^0.31.0"
},
"dependencies": {
"fastify-plugin": "^4.5.1",
"mongodb": "^6.1.0"
"fastify-plugin": "^5.0.0-pre.fv5.1",
"mongodb": "^6.5.0"
},
"publishConfig": {
"access": "public"
Expand Down
155 changes: 77 additions & 78 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,54 @@ const CLIENT_NAME = 'client_name'
const ANOTHER_DATABASE_NAME = 'my_awesome_database'
const COLLECTION_NAME = 'mycoll'

t.Test.prototype.addAssert('objectId', 1, function (ObjectId, message, extra) {
function objectIdTest (t, ObjectId, message) {
t.plan(4)
message = message || 'expect ObjectId value'

const obj1 = new ObjectId()
assert.ok(obj1)
t.ok(obj1)

const obj2 = new ObjectId(obj1)
assert.ok(obj2)
assert.ok(obj1.equals(obj2))
t.ok(obj2)
t.ok(obj1.equals(obj2))

const obj3 = new ObjectId()
assert.ok(!obj1.equals(obj3))

return this.pass(message)
})
t.ok(!obj1.equals(obj3))
}

t.Test.prototype.addAssert('client', 1, function (client, message, extra) {
function clientTest (t, client, message) {
t.plan(1)
message = message || 'expect client'
const db = client.db(DATABASE_NAME)

const col = db.collection(COLLECTION_NAME)

return this.resolves(async () => {
t.resolves(async () => {
const r = await col.insertMany([{ a: 1 }])
assert.strictEqual(1, r.insertedCount)
}, message, extra)
})
}, message)
}

t.Test.prototype.addAssert('database', 1, function (db, message, extra) {
function databaseTest (t, db, message) {
t.plan(1)
message = message || 'expect database'

const col = db.collection(COLLECTION_NAME)

return this.resolves(async () => {
t.resolves(async () => {
const r = await col.insertMany([{ a: 1 }])
assert.deepEqual(1, r.insertedCount)
}, message, extra)
})
}, message)
}

test('re-export ObjectId', async (t) => {
t.plan(1)

t.objectId(fastifyMongo.ObjectId)
t.test(async t => objectIdTest(t, fastifyMongo.ObjectId))
})

test('re-export ObjectId destructured', async (t) => {
t.plan(1)

t.objectId(ObjectId)
t.test(async t => objectIdTest(t, ObjectId))
})

test('export of mongodb', async (t) => {
Expand All @@ -83,8 +82,8 @@ test('{ url: NO_DATABASE_MONGODB_URL }', async (t) => {
t.ok(fastify.mongo.ObjectId)
t.notOk(fastify.mongo.db)

t.objectId(fastify.mongo.ObjectId)
t.client(fastify.mongo.client)
t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
t.test(async t => clientTest(t, fastify.mongo.client))
})

test('{ url: MONGODB_URL }', async (t) => {
Expand All @@ -97,9 +96,9 @@ test('{ url: MONGODB_URL }', async (t) => {
t.ok(fastify.mongo.db)
t.equal(fastify.mongo.db.databaseName, DATABASE_NAME)

t.objectId(fastify.mongo.ObjectId)
t.client(fastify.mongo.client)
t.database(fastify.mongo.db)
t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
t.test(async t => clientTest(t, fastify.mongo.client))
t.test(async t => databaseTest(t, fastify.mongo.db))
})

test('{ url: NO_DATABASE_MONGODB_URL, name: CLIENT_NAME }', async (t) => {
Expand All @@ -115,11 +114,11 @@ test('{ url: NO_DATABASE_MONGODB_URL, name: CLIENT_NAME }', async (t) => {
t.ok(fastify.mongo[CLIENT_NAME].ObjectId)
t.notOk(fastify.mongo[CLIENT_NAME].db)

t.objectId(fastify.mongo.ObjectId)
t.client(fastify.mongo.client)
t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
t.test(async t => clientTest(t, fastify.mongo.client))

t.objectId(fastify.mongo[CLIENT_NAME].ObjectId)
t.client(fastify.mongo[CLIENT_NAME].client)
t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId))
t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client))
})

test('{ url: MONGODB_URL, name: CLIENT_NAME }', async (t) => {
Expand All @@ -137,13 +136,13 @@ test('{ url: MONGODB_URL, name: CLIENT_NAME }', async (t) => {
t.ok(fastify.mongo[CLIENT_NAME].db)
t.equal(fastify.mongo[CLIENT_NAME].db.databaseName, DATABASE_NAME)

t.objectId(fastify.mongo.ObjectId)
t.client(fastify.mongo.client)
t.database(fastify.mongo.db)
t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
t.test(async t => clientTest(t, fastify.mongo.client))
t.test(async t => databaseTest(t, fastify.mongo.db))

t.objectId(fastify.mongo[CLIENT_NAME].ObjectId)
t.client(fastify.mongo[CLIENT_NAME].client)
t.database(fastify.mongo[CLIENT_NAME].db)
t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId))
t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client))
t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db))
})

test('{ url: NO_DATABASE_MONGODB_URL, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME }', async (t) => {
Expand All @@ -161,13 +160,13 @@ test('{ url: NO_DATABASE_MONGODB_URL, name: CLIENT_NAME, database: ANOTHER_DATAB
t.ok(fastify.mongo[CLIENT_NAME].db)
t.equal(fastify.mongo[CLIENT_NAME].db.databaseName, ANOTHER_DATABASE_NAME)

t.objectId(fastify.mongo.ObjectId)
t.client(fastify.mongo.client)
t.database(fastify.mongo.db)
t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
t.test(async t => clientTest(t, fastify.mongo.client))
t.test(async t => databaseTest(t, fastify.mongo.db))

t.objectId(fastify.mongo[CLIENT_NAME].ObjectId)
t.client(fastify.mongo[CLIENT_NAME].client)
t.database(fastify.mongo[CLIENT_NAME].db)
t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId))
t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client))
t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db))
})

test('{ url: MONGODB_URL, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME }', async (t) => {
Expand All @@ -185,13 +184,13 @@ test('{ url: MONGODB_URL, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME }',
t.ok(fastify.mongo[CLIENT_NAME].db)
t.equal(fastify.mongo[CLIENT_NAME].db.databaseName, ANOTHER_DATABASE_NAME)

t.objectId(fastify.mongo.ObjectId)
t.client(fastify.mongo.client)
t.database(fastify.mongo.db)
t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
t.test(async t => clientTest(t, fastify.mongo.client))
t.test(async t => databaseTest(t, fastify.mongo.db))

t.objectId(fastify.mongo[CLIENT_NAME].ObjectId)
t.client(fastify.mongo[CLIENT_NAME].client)
t.database(fastify.mongo[CLIENT_NAME].db)
t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId))
t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client))
t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db))
})

test('{ url: NO_DATABASE_MONGODB_URL, database: ANOTHER_DATABASE_NAME }', async (t) => {
Expand All @@ -204,9 +203,9 @@ test('{ url: NO_DATABASE_MONGODB_URL, database: ANOTHER_DATABASE_NAME }', async
t.ok(fastify.mongo.db)
t.equal(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME)

t.objectId(fastify.mongo.ObjectId)
t.client(fastify.mongo.client)
t.database(fastify.mongo.db)
t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
t.test(async t => clientTest(t, fastify.mongo.client))
t.test(async t => databaseTest(t, fastify.mongo.db))
})

test('{ url: MONGODB_URL, database: ANOTHER_DATABASE_NAME }', async (t) => {
Expand All @@ -219,9 +218,9 @@ test('{ url: MONGODB_URL, database: ANOTHER_DATABASE_NAME }', async (t) => {
t.ok(fastify.mongo.db)
t.equal(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME)

t.objectId(fastify.mongo.ObjectId)
t.client(fastify.mongo.client)
t.database(fastify.mongo.db)
t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
t.test(async t => clientTest(t, fastify.mongo.client))
t.test(async t => databaseTest(t, fastify.mongo.db))
})

test('{ client: client }', async (t) => {
Expand All @@ -236,8 +235,8 @@ test('{ client: client }', async (t) => {
t.ok(fastify.mongo.ObjectId)
t.notOk(fastify.mongo.db)

t.objectId(fastify.mongo.ObjectId)
t.client(fastify.mongo.client)
t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
t.test(async t => clientTest(t, fastify.mongo.client))
})

test('{ client: client, database: DATABASE_NAME }', async (t) => {
Expand All @@ -253,9 +252,9 @@ test('{ client: client, database: DATABASE_NAME }', async (t) => {
t.ok(fastify.mongo.db)
t.equal(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME)

t.objectId(fastify.mongo.ObjectId)
t.client(fastify.mongo.client)
t.database(fastify.mongo.db)
t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
t.test(async t => clientTest(t, fastify.mongo.client))
t.test(async t => databaseTest(t, fastify.mongo.db))
})

test('{ client: client, name: CLIENT_NAME }', async (t) => {
Expand All @@ -274,11 +273,11 @@ test('{ client: client, name: CLIENT_NAME }', async (t) => {
t.ok(fastify.mongo[CLIENT_NAME].ObjectId)
t.notOk(fastify.mongo[CLIENT_NAME].db)

t.objectId(fastify.mongo.ObjectId)
t.client(fastify.mongo.client)
t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
t.test(async t => clientTest(t, fastify.mongo.client))

t.objectId(fastify.mongo[CLIENT_NAME].ObjectId)
t.client(fastify.mongo[CLIENT_NAME].client)
t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId))
t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client))
})

test('{ client: client, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME }', async (t) => {
Expand All @@ -299,13 +298,13 @@ test('{ client: client, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME }', a
t.ok(fastify.mongo[CLIENT_NAME].db)
t.equal(fastify.mongo[CLIENT_NAME].db.databaseName, ANOTHER_DATABASE_NAME)

t.objectId(fastify.mongo.ObjectId)
t.client(fastify.mongo.client)
t.database(fastify.mongo.db)
t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
t.test(async t => clientTest(t, fastify.mongo.client))
t.test(async t => databaseTest(t, fastify.mongo.db))

t.objectId(fastify.mongo[CLIENT_NAME].ObjectId)
t.client(fastify.mongo[CLIENT_NAME].client)
t.database(fastify.mongo[CLIENT_NAME].db)
t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId))
t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client))
t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db))
})

test('{ client: client } does not set onClose', async (t) => {
Expand All @@ -317,7 +316,7 @@ test('{ client: client } does not set onClose', async (t) => {
await fastify.ready()
await fastify.close()

t.database(fastify.mongo.db)
t.test(async t => databaseTest(t, fastify.mongo.db))
})

test('{ }', async (t) => {
Expand Down Expand Up @@ -385,17 +384,17 @@ test('double register with different name', async (t) => {
t.ok(fastify.mongo.client2.db)
t.equal(fastify.mongo.client2.db.databaseName, ANOTHER_DATABASE_NAME)

t.objectId(fastify.mongo.ObjectId)
t.client(fastify.mongo.client)
t.database(fastify.mongo.db)
t.test(async t => objectIdTest(t, fastify.mongo.ObjectId))
t.test(async t => clientTest(t, fastify.mongo.client))
t.test(async t => databaseTest(t, fastify.mongo.db))

t.objectId(fastify.mongo.client1.ObjectId)
t.client(fastify.mongo.client1.client)
t.database(fastify.mongo.client1.db)
t.test(async t => objectIdTest(t, fastify.mongo.client1.ObjectId))
t.test(async t => clientTest(t, fastify.mongo.client1.client))
t.test(async t => databaseTest(t, fastify.mongo.client1.db))

t.objectId(fastify.mongo.client2.ObjectId)
t.client(fastify.mongo.client2.client)
t.database(fastify.mongo.client2.db)
t.test(async t => objectIdTest(t, fastify.mongo.client2.ObjectId))
t.test(async t => clientTest(t, fastify.mongo.client2.client))
t.test(async t => databaseTest(t, fastify.mongo.client2.db))
})

test('double register with the same name', async (t) => {
Expand Down

0 comments on commit 113d9ad

Please sign in to comment.