Skip to content

Commit fa1dbe4

Browse files
authored
fix(shell-api): align coll.validate() args with legacy shell MONGOSH-925 (#1041)
This is a bit looser than newer versions of the legacy shell, which actually reject a single boolean argument, but this way we cover the behavior across multiple legacy shell versions.
1 parent 4bfb9cf commit fa1dbe4

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

packages/shell-api/src/collection.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,7 @@ describe('Collection', () => {
15931593
}
15941594
);
15951595
});
1596-
it('calls serviceProvider.runCommand on the collection with options', async() => {
1596+
it('calls serviceProvider.runCommand on the collection with boolean argument', async() => {
15971597
await collection.validate(true);
15981598
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
15991599
database._name,
@@ -1603,6 +1603,17 @@ describe('Collection', () => {
16031603
}
16041604
);
16051605
});
1606+
it('calls serviceProvider.runCommand on the collection with options', async() => {
1607+
await collection.validate({ full: true, repair: true });
1608+
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
1609+
database._name,
1610+
{
1611+
validate: collection._name,
1612+
full: true,
1613+
repair: true
1614+
}
1615+
);
1616+
});
16061617

16071618
it('returns whatever serviceProvider.runCommand returns', async() => {
16081619
const expectedResult = { ok: 1 };

packages/shell-api/src/collection.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,13 +1578,16 @@ export default class Collection extends ShellApiWithMongoClass {
15781578

15791579
@returnsPromise
15801580
@apiVersions([])
1581-
async validate(full = false): Promise<Document> {
1582-
this._emitCollectionApiCall('validate', { full });
1581+
async validate(options: boolean | Document = false): Promise<Document> {
1582+
this._emitCollectionApiCall('validate', { options });
1583+
if (typeof options === 'boolean') {
1584+
options = { full: options };
1585+
}
15831586
return await this._mongo._serviceProvider.runCommandWithCheck(
15841587
this._database._name,
15851588
{
15861589
validate: this._name,
1587-
full: full
1590+
...options
15881591
},
15891592
this._database._baseOptions
15901593
);

packages/shell-api/src/integration.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,36 @@ describe('Shell API (integration)', function() {
10031003
expect(await collection.findOne()).to.deep.equal({ '$x.y': 2, _id: '_id' });
10041004
});
10051005
});
1006+
1007+
describe('validate', () => {
1008+
skipIfApiStrict();
1009+
skipIfServerVersion(testServer, '< 5.0');
1010+
1011+
beforeEach(async() => {
1012+
await collection.insertOne({ foo: 'bar' });
1013+
});
1014+
1015+
it('validate can be used to validate a collection', async() => {
1016+
expect((await collection.validate({ full: true })).valid).to.equal(true);
1017+
});
1018+
1019+
it('validate accepts a repair option', async() => {
1020+
expect((await collection.validate({ full: true, repair: true })).valid).to.equal(true);
1021+
});
1022+
1023+
it('validate accepts a background option', async() => {
1024+
expect((await collection.validate({ full: false, background: true })).valid).to.equal(true);
1025+
});
1026+
1027+
it('validate fails with background: true and full: true', async() => {
1028+
try {
1029+
await collection.validate({ full: true, background: true });
1030+
expect.fail('missed exception');
1031+
} catch (err) {
1032+
expect(err.name).to.equal('MongoServerError');
1033+
}
1034+
});
1035+
});
10061036
});
10071037

10081038
describe('db', () => {

0 commit comments

Comments
 (0)