smartdata/test/test.watch.ts

116 lines
3.4 KiB
TypeScript
Raw Normal View History

2023-07-21 20:08:18 +02:00
import { tap, expect } from '@push.rocks/tapbundle';
import { Qenv } from '@push.rocks/qenv';
import * as smartmongo from '@push.rocks/smartmongo';
import { smartunique } from '../ts/plugins.js';
2022-05-17 00:33:44 +02:00
const testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit/');
console.log(process.memoryUsage());
// the tested module
import * as smartdata from '../ts/index.js';
// =======================================
// Connecting to the database server
// =======================================
let smartmongoInstance: smartmongo.SmartMongo;
let testDb: smartdata.SmartdataDb;
const totalCars = 2000;
tap.test('should create a testinstance as database', async () => {
2022-05-17 00:33:44 +02:00
smartmongoInstance = await smartmongo.SmartMongo.createAndStart();
testDb = new smartdata.SmartdataDb(await smartmongoInstance.getMongoDescriptor());
await testDb.init();
});
tap.skip.test('should connect to atlas', async (tools) => {
2022-05-17 21:26:17 +02:00
const databaseName = `test-smartdata-${smartunique.shortId()}`;
testDb = new smartdata.SmartdataDb({
2023-08-15 01:01:16 +02:00
mongoDbUrl: await testQenv.getEnvVarOnDemand('MONGO_URL'),
2022-05-17 21:26:17 +02:00
mongoDbName: databaseName,
});
await testDb.init();
});
2022-05-17 00:33:44 +02:00
@smartdata.Collection(() => testDb)
class House extends smartdata.SmartDataDbDoc<House, House> {
2022-05-17 21:26:17 +02:00
@smartdata.unI()
public id: string = smartunique.shortId();
2022-05-17 00:33:44 +02:00
@smartdata.svDb()
public data = {
id: smartunique.shortId(),
2022-11-01 18:23:57 +01:00
hello: 'hello',
};
2022-05-17 00:33:44 +02:00
}
2022-05-17 21:26:17 +02:00
tap.test('should watch a collection', async (toolsArg) => {
2022-05-17 00:33:44 +02:00
const done = toolsArg.defer();
const watcher = await House.watch({});
2022-11-01 18:23:57 +01:00
watcher.changeSubject.subscribe(async (houseArg) => {
2022-05-17 00:33:44 +02:00
console.log('hey there, we observed a house');
2022-05-17 21:26:17 +02:00
await watcher.close();
2022-05-17 00:33:44 +02:00
done.resolve();
});
const newHouse = new House();
await newHouse.save();
2022-05-17 21:26:17 +02:00
console.log('saved a house');
2022-05-17 00:33:44 +02:00
await done.promise;
2022-11-01 18:23:57 +01:00
});
2022-05-17 00:33:44 +02:00
// ======= New tests for EventEmitter and buffering support =======
tap.test('should emit change via EventEmitter', async (tools) => {
const done = tools.defer();
const watcher = await House.watch({});
watcher.on('change', async (houseArg) => {
// Expect a House instance
expect(houseArg).toBeDefined();
// Clean up
await watcher.stop();
done.resolve();
});
// Trigger an insert to generate a change event
const h = new House();
await h.save();
await done.promise;
});
tap.test('should buffer change events when bufferTimeMs is set', async (tools) => {
const done = tools.defer();
// bufferTimeMs collects events into arrays every 50ms
const watcher = await House.watch({}, { bufferTimeMs: 50 });
let received: House[];
watcher.changeSubject.subscribe(async (batch: House[]) => {
if (batch && batch.length > 0) {
received = batch;
await watcher.stop();
done.resolve();
}
});
// Rapidly insert multiple docs
const docs = [new House(), new House(), new House()];
for (const doc of docs) await doc.save();
await done.promise;
// All inserts should be in one buffered batch
expect(received.length).toEqual(docs.length);
});
2022-05-17 00:33:44 +02:00
// =======================================
// close the database connection
// =======================================
tap.test('close', async () => {
try {
await testDb.mongoDb.dropDatabase();
} catch (err) {
console.warn('dropDatabase error ignored in cleanup:', err.message || err);
}
2022-05-17 21:26:17 +02:00
await testDb.close();
2022-05-17 00:33:44 +02:00
if (smartmongoInstance) {
await smartmongoInstance.stop();
}
});
2022-11-01 18:23:57 +01:00
tap.start({ throwOnError: true });