Skip to content

Commit f8c0988

Browse files
committed
test addition for get transient
1 parent 1c533be commit f8c0988

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

tests/mocks/mongoClient.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module.exports = {
55
setup: jest.fn(),
66
close: jest.fn(),
77
getObjectMDStats: jest.fn(),
8+
isLocationTransient: jest.fn(),
9+
getObject: jest.fn(),
810
getBucketInfos: jest.fn(),
911
updateStorageConsumptionMetrics: jest.fn(),
1012
getUsersBucketCreationDate: jest.fn(),

tests/unit/CountItems/CountWorker.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,78 @@ describe('CountItems::CountWorker', () => {
216216
done();
217217
});
218218
});
219+
220+
test('should use real getIsTransient and not crash', done => {
221+
const testSendFn = jest.fn();
222+
let getObjectCall = 0;
223+
const w = new CountWorker({
224+
log: new DummyLogger(),
225+
sendFn: testSendFn,
226+
client: mongoMock,
227+
});
228+
mongoMock.setup.mockImplementationOnce(cb => cb());
229+
mongoMock.close.mockImplementationOnce(cb => cb());
230+
mongoMock.client.isConnected.mockImplementationOnce(() => false);
231+
mongoMock.getObjectMDStats.mockImplementationOnce((_a, _b, _c, _d, cb) => cb(null, { value: 42 }));
232+
mongoMock.isLocationTransient.mockImplementationOnce((_a, _b, cb) => {
233+
cb(null, false);
234+
});
235+
mongoMock.getObject = jest.fn((_a, _b, _c, _d, cb) => {
236+
getObjectCall += 1;
237+
if (getObjectCall === 1) {
238+
cb(null, 'v1');
239+
} else {
240+
cb(null, {
241+
locations: {
242+
undefined: { isTransient: true },
243+
},
244+
});
245+
}
246+
});
247+
const bucketInfo = {
248+
_name: 'test-bucket',
249+
_owner: 'any',
250+
_ownerDisplayName: 'any',
251+
_creationDate: Date.now().toString(),
252+
getLocationConstraint: jest.fn(() => 'test-location'),
253+
};
254+
w.countItems(bucketInfo, (err, results) => {
255+
expect(err).toBeNull();
256+
expect(results).toEqual({ value: 42 });
257+
done();
258+
});
259+
});
260+
261+
test('should call isLocationTransient if defined', done => {
262+
const testSendFn = jest.fn();
263+
const w = new CountWorker({
264+
log: new DummyLogger(),
265+
sendFn: testSendFn,
266+
client: mongoMock,
267+
});
268+
269+
mongoMock.setup.mockImplementationOnce(cb => cb());
270+
mongoMock.close.mockImplementationOnce(cb => cb());
271+
mongoMock.client.isConnected.mockImplementationOnce(() => false);
272+
mongoMock.isLocationTransient.mockImplementationOnce((_a, _b, cb) => {
273+
cb(null, true);
274+
});
275+
mongoMock.getObjectMDStats.mockImplementationOnce((_a, _b, _c, _d, cb) => cb(null, { value: 42 }));
276+
277+
const bucketInfo = {
278+
getLocationConstraint: () => 'some-location',
279+
_name: 'test-bucket',
280+
_owner: 'any',
281+
_ownerDisplayName: 'any',
282+
_creationDate: Date.now().toString(),
283+
};
284+
285+
w.countItems(bucketInfo, (err, results) => {
286+
expect(err).toBeNull();
287+
expect(results).toEqual({ value: 42 });
288+
expect(mongoMock.isLocationTransient).toHaveBeenCalledTimes(2);
289+
done();
290+
});
291+
});
292+
219293
});

0 commit comments

Comments
 (0)