Skip to content

fix(NODE-3712,NODE-4546): electionId should be ordered before setVersion #3174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 5, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
test: add unit test for objectId comparison
  • Loading branch information
nbbeeken committed Oct 4, 2022
commit 6892ddc6c5153d7a19173235f32c409b6d7ffe36
47 changes: 46 additions & 1 deletion test/unit/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { expect } from 'chai';

import { LEGACY_HELLO_COMMAND } from '../../src/constants';
import { MongoRuntimeError } from '../../src/error';
import { Promise as PromiseProvider } from '../../src/index';
import { ObjectId, Promise as PromiseProvider } from '../../src/index';
import {
BufferPool,
compareObjectId,
eachAsync,
HostAddress,
isHello,
Expand Down Expand Up @@ -481,4 +482,48 @@ describe('driver utils', function () {
});
});
});

describe('compareObjectId()', () => {
const table = [
{ oid1: null, oid2: null, result: 0 },
{ oid1: undefined, oid2: null, result: 0 },
{ oid1: null, oid2: undefined, result: 0 },
{ oid1: undefined, oid2: undefined, result: 0 },
{ oid1: new ObjectId('00'.repeat(12)), oid2: undefined, result: 1 },
{ oid1: new ObjectId('00'.repeat(12)), oid2: null, result: 1 },
{ oid1: undefined, oid2: new ObjectId('00'.repeat(12)), result: -1 },
{ oid1: null, oid2: new ObjectId('00'.repeat(12)), result: -1 },
{ oid1: new ObjectId('00'.repeat(12)), oid2: new ObjectId('00'.repeat(12)), result: 0 },
{
oid1: new ObjectId('00'.repeat(11) + '01'),
oid2: new ObjectId('00'.repeat(12)),
result: 1
},
{
oid1: new ObjectId('00'.repeat(12)),
oid2: new ObjectId('00'.repeat(11) + '01'),
result: -1
},
{
oid1: 2,
oid2: 1,
result: 'throws'
}
];

for (const { oid1, oid2, result } of table) {
if (result === 'throws') {
it('passing non-objectId values throw', () =>
// @ts-expect-error: Passing bad values to ensure thrown error
expect(() => compareObjectId(oid1, oid2)).to.throw());
continue;
}

const title = `comparing ${oid1} to ${oid2} returns ${
result === 0 ? 'equal' : result === -1 ? 'less than' : 'greater than'
}`;
// @ts-expect-error: not narrowed based on numeric result, but these values are correct
it(title, () => expect(compareObjectId(oid1, oid2)).to.equal(result));
}
});
});