smartdata/test/test.watch.ts

75 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2023-07-21 18:08:18 +00:00
import { tap, expect } from '@push.rocks/tapbundle';
import { Qenv } from '@push.rocks/qenv';
import * as smartmongo from '@push.rocks/smartmongo';
2022-05-16 22:33:44 +00:00
import { smartunique } from '../ts/smartdata.plugins.js';
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-16 22:33:44 +00: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 19:26:17 +00:00
const databaseName = `test-smartdata-${smartunique.shortId()}`;
testDb = new smartdata.SmartdataDb({
2023-08-14 23:01:16 +00:00
mongoDbUrl: await testQenv.getEnvVarOnDemand('MONGO_URL'),
2022-05-17 19:26:17 +00:00
mongoDbName: databaseName,
});
await testDb.init();
});
2022-05-16 22:33:44 +00:00
@smartdata.Collection(() => testDb)
class House extends smartdata.SmartDataDbDoc<House, House> {
2022-05-17 19:26:17 +00:00
@smartdata.unI()
public id: string = smartunique.shortId();
2022-05-16 22:33:44 +00:00
@smartdata.svDb()
public data = {
id: smartunique.shortId(),
2022-11-01 17:23:57 +00:00
hello: 'hello',
};
2022-05-16 22:33:44 +00:00
}
2022-05-17 19:26:17 +00:00
tap.test('should watch a collection', async (toolsArg) => {
2022-05-16 22:33:44 +00:00
const done = toolsArg.defer();
const watcher = await House.watch({});
2022-11-01 17:23:57 +00:00
watcher.changeSubject.subscribe(async (houseArg) => {
2022-05-16 22:33:44 +00:00
console.log('hey there, we observed a house');
2022-05-17 19:26:17 +00:00
await watcher.close();
2022-05-16 22:33:44 +00:00
done.resolve();
});
const newHouse = new House();
await newHouse.save();
2022-05-17 19:26:17 +00:00
console.log('saved a house');
2022-05-16 22:33:44 +00:00
await done.promise;
2022-11-01 17:23:57 +00:00
});
2022-05-16 22:33:44 +00:00
// =======================================
// close the database connection
// =======================================
tap.test('close', async () => {
2022-05-17 19:26:17 +00:00
await testDb.mongoDb.dropDatabase();
await testDb.close();
2022-05-16 22:33:44 +00:00
if (smartmongoInstance) {
await smartmongoInstance.stop();
}
});
2022-11-01 17:23:57 +00:00
tap.start({ throwOnError: true });