Skip to content

Commit 6fc82eb

Browse files
committed
Add unit test
1 parent fc93cbd commit 6fc82eb

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ShardedDataDistribution } from './helpers';
12
import {
23
assertArgsDefinedType,
34
coerceToJSNumber,
@@ -135,6 +136,21 @@ describe('getPrintableShardStatus', function () {
135136
let serviceProvider: ServiceProvider;
136137
let inBalancerRound = false;
137138

139+
const mockedShardedDataDistribution: ShardedDataDistribution = [
140+
{
141+
ns: 'test.ns',
142+
shards: [
143+
{
144+
shardName: 'test',
145+
numOrphanedDocs: 1,
146+
numOwnedDocuments: 5,
147+
orphanedSizeBytes: 20,
148+
ownedSizeBytes: 80,
149+
},
150+
],
151+
},
152+
];
153+
138154
beforeEach(async function () {
139155
serviceProvider = await CliServiceProvider.connect(
140156
await testServer.connectionString(),
@@ -152,9 +168,20 @@ describe('getPrintableShardStatus', function () {
152168
configDatabase = new Database(mongo, 'config_test');
153169
expect(configDatabase.getName()).to.equal('config_test');
154170

171+
const mockedAdminDb = {
172+
aggregate: stub()
173+
.withArgs([{ $shardedDataDistribution: {} }])
174+
.resolves({
175+
toArray: stub().resolves(mockedShardedDataDistribution),
176+
}),
177+
};
178+
const getSiblingDB = stub();
179+
getSiblingDB.withArgs('admin').returns(mockedAdminDb);
180+
getSiblingDB.withArgs('config').returns(configDatabase);
181+
155182
db = {
156183
_maybeCachedHello: stub().returns({ msg: 'isdbgrid' }),
157-
getSiblingDB: stub().withArgs('config').returns(configDatabase),
184+
getSiblingDB,
158185
} as unknown as Database;
159186

160187
const origRunCommandWithCheck = serviceProvider.runCommandWithCheck;
@@ -209,6 +236,10 @@ describe('getPrintableShardStatus', function () {
209236
);
210237
expect(status.databases).to.have.lengthOf(1);
211238
expect(status.databases[0].database._id).to.equal('config');
239+
240+
expect(status.shardedDataDistribution).to.equal(
241+
mockedShardedDataDistribution
242+
);
212243
});
213244

214245
describe('hides all internal deprecated fields in shardingVersion', function () {

packages/shell-api/src/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,15 +734,15 @@ type ShardingStatusResult = {
734734
databases: { database: Document; collections: Document }[];
735735
};
736736

737-
type ShardedDataDistribution = {
737+
export type ShardedDataDistribution = {
738738
ns: string;
739739
shards: {
740740
shardName: string;
741741
numOrphanedDocs: number;
742742
numOwnedDocuments: number;
743743
orphanedSizeBytes: number;
744744
ownedSizeBytes: number;
745-
};
745+
}[];
746746
}[];
747747

748748
export async function getConfigDB(db: Database): Promise<Database> {

0 commit comments

Comments
 (0)