T-29: tests
This commit is contained in:
parent
1720e45f70
commit
5a23e862af
3 changed files with 123 additions and 0 deletions
|
|
@ -1042,6 +1042,70 @@ describe('MongoDBClient read-only mode (offline)', () => {
|
|||
}
|
||||
});
|
||||
|
||||
test('stream treats both null and undefined cursor.next() as EOF but passes falsy non-nullish transform results (0, false, empty string) through', async () => {
|
||||
const logger = getLogger('mongodb-read-only-stream-nullish-test');
|
||||
const realMongoClient = new MongoClient('mongodb://127.0.0.1:1/stock');
|
||||
const realCollection = realMongoClient.db('stock').collection('eodPrices');
|
||||
|
||||
const rows = [
|
||||
{ _id: '1', v: 0 },
|
||||
{ _id: '2', v: false },
|
||||
{ _id: '3', v: '' },
|
||||
{ _id: '4', v: null },
|
||||
{ _id: '5', v: 'tail' },
|
||||
];
|
||||
|
||||
const openWrappedFind = (rawCursor: object) => {
|
||||
const fakeDb = {
|
||||
databaseName: 'stock',
|
||||
collection: mock(() => ({
|
||||
collectionName: 'eodPrices',
|
||||
find: mock(() => rawCursor),
|
||||
})),
|
||||
};
|
||||
const wrappedClient = new MongoDBClient(
|
||||
{ host: '127.0.0.1', port: 1, database: 'stock', readOnly: true },
|
||||
logger
|
||||
);
|
||||
Object.assign(wrappedClient, {
|
||||
client: { db: mock(() => fakeDb) },
|
||||
db: fakeDb,
|
||||
isConnected: true,
|
||||
});
|
||||
return wrappedClient.getCollection('eodPrices').find({});
|
||||
};
|
||||
|
||||
for (const eofValue of [null, undefined] as const) {
|
||||
const rawCursor = realCollection.find({});
|
||||
let index = 0;
|
||||
let eofReads = 0;
|
||||
const closeMock = mock(async () => {});
|
||||
rawCursor.next = mock(async () => {
|
||||
if (index < rows.length) {
|
||||
return rows[index++];
|
||||
}
|
||||
eofReads++;
|
||||
return eofValue;
|
||||
}) as never;
|
||||
rawCursor.close = closeMock as never;
|
||||
|
||||
// Object mode: a raw `null` chunk would end a Readable, so falsy values are only safe
|
||||
// because the guard skips strictly nullish outputs (row 4) and pushes 0/false/'' as-is.
|
||||
const stream = openWrappedFind(rawCursor).stream({
|
||||
transform: (doc: Record<string, unknown>) => doc.v,
|
||||
});
|
||||
|
||||
const emitted: unknown[] = [];
|
||||
for await (const chunk of stream) {
|
||||
emitted.push(chunk);
|
||||
}
|
||||
|
||||
expect(emitted).toEqual([0, false, '', 'tail']);
|
||||
expect(eofReads).toBe(1);
|
||||
expect(closeMock).toHaveBeenCalledTimes(1);
|
||||
}
|
||||
});
|
||||
|
||||
test('hides raw MongoClient/Db/Collection via property descriptor reflection on read-only proxies', () => {
|
||||
const logger = getLogger('mongodb-read-only-reflection-test');
|
||||
const realMongoClient = new MongoClient('mongodb://127.0.0.1:1/stock');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue