Skip to content

Commit

Permalink
chore: Updated mongodb tests to node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Nov 14, 2024
1 parent a7f14de commit a6868a3
Show file tree
Hide file tree
Showing 12 changed files with 384 additions and 311 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const common = require('./collection-common')
const { STATEMENT_PREFIX } = require('./common')

common.test('unorderedBulkOp', async function unorderedBulkOpTest(t, collection, verify) {
common.test('unorderedBulkOp', async function unorderedBulkOpTest(collection, verify) {
const bulk = collection.initializeUnorderedBulkOp()
bulk
.find({
Expand All @@ -29,7 +29,7 @@ common.test('unorderedBulkOp', async function unorderedBulkOpTest(t, collection,
verify(null, [`${STATEMENT_PREFIX}/unorderedBulk/batch`], ['unorderedBulk'], { strict: false })
})

common.test('orderedBulkOp', async function unorderedBulkOpTest(t, collection, verify) {
common.test('orderedBulkOp', async function unorderedBulkOpTest(collection, verify) {
const bulk = collection.initializeOrderedBulkOp()
bulk
.find({
Expand Down
274 changes: 163 additions & 111 deletions test/versioned/mongodb/collection-common.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,36 @@
*/

'use strict'

const assert = require('node:assert')

const common = require('./collection-common')
const { STATEMENT_PREFIX } = require('./common')
const findOpt = { returnDocument: 'after' }

common.test('findOne', async function findOneTest(t, collection, verify) {
common.test('findOne', async function findOneTest(collection, verify) {
const data = await collection.findOne({ i: 15 })
t.equal(data.i, 15)
assert.equal(data.i, 15)
verify(null, [`${STATEMENT_PREFIX}/findOne`], ['findOne'], { strict: false })
})

common.test('findOneAndDelete', async function findOneAndDeleteTest(t, collection, verify) {
common.test('findOneAndDelete', async function findOneAndDeleteTest(collection, verify) {
const data = await collection.findOneAndDelete({ i: 15 })
const response = data?.value?.i || data.i
t.equal(response, 15)
assert.equal(response, 15)
verify(null, [`${STATEMENT_PREFIX}/findOneAndDelete`], ['findOneAndDelete'], { strict: false })
})

common.test('findOneAndReplace', async function findAndReplaceTest(t, collection, verify) {
common.test('findOneAndReplace', async function findAndReplaceTest(collection, verify) {
const data = await collection.findOneAndReplace({ i: 15 }, { b: 15 }, findOpt)
const response = data?.value?.b || data.b
t.equal(response, 15)
assert.equal(response, 15)
verify(null, [`${STATEMENT_PREFIX}/findOneAndReplace`], ['findOneAndReplace'], { strict: false })
})

common.test('findOneAndUpdate', async function findOneAndUpdateTest(t, collection, verify) {
common.test('findOneAndUpdate', async function findOneAndUpdateTest(collection, verify) {
const data = await collection.findOneAndUpdate({ i: 15 }, { $set: { a: 15 } }, findOpt)
const response = data?.value?.a || data.a
t.equal(response, 15)
assert.equal(response, 15)
verify(null, [`${STATEMENT_PREFIX}/findOneAndUpdate`], ['findOneAndUpdate'], { strict: false })
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@

'use strict'

const assert = require('node:assert')

const common = require('./collection-common')
const { STATEMENT_PREFIX } = require('./common')

common.test('createIndex', async function createIndexTest(t, collection, verify) {
common.test('createIndex', async function createIndexTest(collection, verify) {
const data = await collection.createIndex('i')
t.equal(data, 'i_1')
assert.equal(data, 'i_1')
verify(null, [`${STATEMENT_PREFIX}/createIndex`], ['createIndex'], { strict: false })
})

common.test('dropIndex', async function dropIndexTest(t, collection, verify) {
common.test('dropIndex', async function dropIndexTest(collection, verify) {
await collection.createIndex('i')
const data = await collection.dropIndex('i_1')
t.equal(data.ok, 1)
assert.equal(data.ok, 1)
verify(
null,
[`${STATEMENT_PREFIX}/createIndex`, `${STATEMENT_PREFIX}/dropIndex`],
Expand All @@ -26,7 +28,7 @@ common.test('dropIndex', async function dropIndexTest(t, collection, verify) {
)
})

common.test('indexes', async function indexesTest(t, collection, verify) {
common.test('indexes', async function indexesTest(collection, verify) {
const data = await collection.indexes()
const result = data && data[0]
const expectedResult = {
Expand All @@ -35,21 +37,21 @@ common.test('indexes', async function indexesTest(t, collection, verify) {
name: '_id_'
}

t.same(result, expectedResult, 'should have expected results')
assert.deepStrictEqual(result, expectedResult, 'should have expected results')

verify(null, [`${STATEMENT_PREFIX}/indexes`], ['indexes'], { strict: false })
})

common.test('indexExists', async function indexExistsTest(t, collection, verify) {
common.test('indexExists', async function indexExistsTest(collection, verify) {
const data = await collection.indexExists(['_id_'])
t.equal(data, true)
assert.equal(data, true)

verify(null, [`${STATEMENT_PREFIX}/indexExists`], ['indexExists'], { strict: false })
})

common.test('indexInformation', async function indexInformationTest(t, collection, verify) {
common.test('indexInformation', async function indexInformationTest(collection, verify) {
const data = await collection.indexInformation()
t.same(data && data._id_, [['_id', 1]], 'should have expected results')
assert.deepStrictEqual(data && data._id_, [['_id', 1]], 'should have expected results')

verify(null, [`${STATEMENT_PREFIX}/indexInformation`], ['indexInformation'], { strict: false })
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@

'use strict'

const assert = require('node:assert')

const common = require('./collection-common')
const semver = require('semver')
const { STATEMENT_PREFIX, COLLECTIONS, DB_NAME } = require('./common')

function verifyAggregateData(t, data) {
t.equal(data.length, 3, 'should have expected amount of results')
t.same(data, [{ value: 5 }, { value: 15 }, { value: 25 }], 'should have expected results')
function verifyAggregateData(data) {
assert.equal(data.length, 3, 'should have expected amount of results')
assert.deepStrictEqual(
data,
[{ value: 5 }, { value: 15 }, { value: 25 }],
'should have expected results'
)
}

common.test('aggregate', async function aggregateTest(t, collection, verify) {
common.test('aggregate', async function aggregateTest(collection, verify) {
const data = await collection
.aggregate([
{ $sort: { i: 1 } },
Expand All @@ -23,7 +29,7 @@ common.test('aggregate', async function aggregateTest(t, collection, verify) {
{ $project: { value: '$i', _id: 0 } }
])
.toArray()
verifyAggregateData(t, data)
verifyAggregateData(data)
verify(
null,
[`${STATEMENT_PREFIX}/aggregate`, `${STATEMENT_PREFIX}/toArray`],
Expand All @@ -32,74 +38,74 @@ common.test('aggregate', async function aggregateTest(t, collection, verify) {
)
})

common.test('bulkWrite', async function bulkWriteTest(t, collection, verify) {
common.test('bulkWrite', async function bulkWriteTest(collection, verify) {
const data = await collection.bulkWrite(
[{ deleteMany: { filter: {} } }, { insertOne: { document: { a: 1 } } }],
{ ordered: true, w: 1 }
)

t.equal(data.insertedCount, 1)
t.equal(data.deletedCount, 30)
assert.equal(data.insertedCount, 1)
assert.equal(data.deletedCount, 30)
verify(null, [`${STATEMENT_PREFIX}/bulkWrite`], ['bulkWrite'], { strict: false })
})

common.test('count', async function countTest(t, collection, verify) {
common.test('count', async function countTest(collection, verify) {
const data = await collection.count()
t.equal(data, 30)
assert.equal(data, 30)
verify(null, [`${STATEMENT_PREFIX}/count`], ['count'], { strict: false })
})

common.test('distinct', async function distinctTest(t, collection, verify) {
common.test('distinct', async function distinctTest(collection, verify) {
const data = await collection.distinct('mod10')
t.same(data.sort(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
assert.deepStrictEqual(data.sort(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
verify(null, [`${STATEMENT_PREFIX}/distinct`], ['distinct'], { strict: false })
})

common.test('drop', async function dropTest(t, collection, verify) {
common.test('drop', async function dropTest(collection, verify) {
const data = await collection.drop()
t.equal(data, true)
assert.equal(data, true)
verify(null, [`${STATEMENT_PREFIX}/drop`], ['drop'], { strict: false })
})

common.test('isCapped', async function isCappedTest(t, collection, verify) {
common.test('isCapped', async function isCappedTest(collection, verify) {
const data = await collection.isCapped()
t.notOk(data)
assert.equal(data, false)

verify(null, [`${STATEMENT_PREFIX}/isCapped`], ['isCapped'], { strict: false })
})

common.test('options', async function optionsTest(t, collection, verify) {
common.test('options', async function optionsTest(collection, verify) {
const data = await collection.options()

// Depending on the version of the mongo server this will change.
if (data) {
t.same(data, {}, 'should have expected results')
assert.deepStrictEqual(data, {}, 'should have expected results')
} else {
t.notOk(data, 'should have expected results')
assert.equal(data, false, 'should have expected results')
}

verify(null, [`${STATEMENT_PREFIX}/options`], ['options'], { strict: false })
})

common.test('rename', async function renameTest(t, collection, verify) {
common.test('rename', async function renameTest(collection, verify) {
await collection.rename(COLLECTIONS.collection2)

verify(null, [`${STATEMENT_PREFIX}/rename`], ['rename'], { strict: false })
})

if (semver.satisfies(common.pkgVersion, '<6.0.0')) {
common.test('stats', async function statsTest(t, collection, verify) {
common.test('stats', async function statsTest(collection, verify) {
const data = await collection.stats({ i: 5 })
t.equal(data.ns, `${DB_NAME}.${COLLECTIONS.collection1}`)
t.equal(data.count, 30)
t.equal(data.ok, 1)
assert.equal(data.ns, `${DB_NAME}.${COLLECTIONS.collection1}`)
assert.equal(data.count, 30)
assert.equal(data.ok, 1)

verify(null, [`${STATEMENT_PREFIX}/stats`], ['stats'], { strict: false })
})
}

if (semver.satisfies(common.pkgVersion, '<5.0.0')) {
common.test('mapReduce', async function mapReduceTest(t, collection, verify) {
common.test('mapReduce', async function mapReduceTest(collection, verify) {
const data = await collection.mapReduce(map, reduce, { out: { inline: 1 } })

const expectedData = [
Expand All @@ -118,7 +124,7 @@ if (semver.satisfies(common.pkgVersion, '<5.0.0')) {
// data is not sorted depending on speed of
// db calls, sort to compare vs expected collection
data.sort((a, b) => a._id - b._id)
t.same(data, expectedData)
assert.deepStrictEqual(data, expectedData)

verify(null, [`${STATEMENT_PREFIX}/mapReduce`], ['mapReduce'], { strict: false })

Expand Down
Loading

0 comments on commit a6868a3

Please sign in to comment.