98 lines
2.9 KiB
TypeScript
98 lines
2.9 KiB
TypeScript
import { tap, expect } from '@push.rocks/tapbundle';
|
|
import * as smartmongo from '@push.rocks/smartmongo';
|
|
import { smartunique } from '../ts/plugins.js';
|
|
import * as smartdata from '../ts/index.js';
|
|
|
|
// Set up database connection
|
|
let smartmongoInstance: smartmongo.SmartMongo;
|
|
let testDb: smartdata.SmartdataDb;
|
|
|
|
// Define a simple document model for cursor tests
|
|
@smartdata.Collection(() => testDb)
|
|
class CursorTest extends smartdata.SmartDataDbDoc<CursorTest, CursorTest> {
|
|
@smartdata.unI()
|
|
public id: string = smartunique.shortId();
|
|
|
|
@smartdata.svDb()
|
|
public name: string;
|
|
|
|
@smartdata.svDb()
|
|
public order: number;
|
|
|
|
constructor(name: string, order: number) {
|
|
super();
|
|
this.name = name;
|
|
this.order = order;
|
|
}
|
|
}
|
|
|
|
// Initialize the in-memory MongoDB and SmartdataDB
|
|
tap.test('cursor init: start Mongo and SmartdataDb', async () => {
|
|
smartmongoInstance = await smartmongo.SmartMongo.createAndStart();
|
|
testDb = new smartdata.SmartdataDb(
|
|
await smartmongoInstance.getMongoDescriptor(),
|
|
);
|
|
await testDb.init();
|
|
});
|
|
|
|
// Insert sample documents
|
|
tap.test('cursor insert: save 5 test documents', async () => {
|
|
for (let i = 1; i <= 5; i++) {
|
|
const doc = new CursorTest(`item${i}`, i);
|
|
await doc.save();
|
|
}
|
|
const count = await CursorTest.getCount({});
|
|
expect(count).toEqual(5);
|
|
});
|
|
|
|
// Test that toArray returns all documents
|
|
tap.test('cursor toArray: retrieves all documents', async () => {
|
|
const cursor = await CursorTest.getCursor({});
|
|
const all = await cursor.toArray();
|
|
expect(all.length).toEqual(5);
|
|
});
|
|
|
|
// Test iteration via forEach
|
|
tap.test('cursor forEach: iterates through all documents', async () => {
|
|
const names: string[] = [];
|
|
const cursor = await CursorTest.getCursor({});
|
|
await cursor.forEach(async (item) => {
|
|
names.push(item.name);
|
|
});
|
|
expect(names.length).toEqual(5);
|
|
expect(names).toContain('item3');
|
|
});
|
|
|
|
// Test native cursor modifiers: limit
|
|
tap.test('cursor modifier limit: only two documents', async () => {
|
|
const cursor = await CursorTest.getCursor({}, { modifier: (c) => c.limit(2) });
|
|
const limited = await cursor.toArray();
|
|
expect(limited.length).toEqual(2);
|
|
});
|
|
|
|
// Test native cursor modifiers: sort and skip
|
|
tap.test('cursor modifier sort & skip: returns correct order', async () => {
|
|
const cursor = await CursorTest.getCursor({}, {
|
|
modifier: (c) => c.sort({ order: -1 }).skip(1),
|
|
});
|
|
const results = await cursor.toArray();
|
|
// Skipped the first (order 5), next should be 4,3,2,1
|
|
expect(results.length).toEqual(4);
|
|
expect(results[0].order).toEqual(4);
|
|
});
|
|
|
|
// Cleanup: drop database, close connections, stop Mongo
|
|
tap.test('cursor cleanup: drop DB and stop', async () => {
|
|
await testDb.mongoDb.dropDatabase();
|
|
await testDb.close();
|
|
if (smartmongoInstance) {
|
|
await smartmongoInstance.stopAndDumpToDir(
|
|
`.nogit/dbdump/test.cursor.ts`,
|
|
);
|
|
}
|
|
// Ensure process exits after cleanup
|
|
setTimeout(() => process.exit(), 2000);
|
|
});
|
|
|
|
export default tap.start();
|