smartdata/test/test.typescript.ts

96 lines
2.4 KiB
TypeScript
Raw 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';
2021-09-16 17:49:55 +00:00
const testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit/');
console.log(process.memoryUsage());
// the tested module
2022-05-16 22:33:44 +00:00
import * as smartdata from '../ts/index.js';
2021-09-16 17:49:55 +00:00
// =======================================
// Connecting to the database server
// =======================================
2022-05-16 22:33:44 +00:00
let smartmongoInstance: smartmongo.SmartMongo;
2021-09-16 17:49:55 +00:00
let testDb: smartdata.SmartdataDb;
const totalCars = 2000;
2021-11-12 18:16:11 +00:00
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());
2021-11-12 17:04:08 +00:00
await testDb.init();
2021-09-16 17:49:55 +00:00
});
2021-11-12 18:16:11 +00:00
tap.skip.test('should connect to atlas', async (tools) => {
2021-09-16 17:49:55 +00:00
const databaseName = `test-smartdata-${smartunique.shortId()}`;
testDb = new smartdata.SmartdataDb({
2023-08-12 21:32:02 +00:00
mongoDbUrl: await testQenv.getEnvVarOnDemand('MONGO_URL'),
2021-09-16 17:49:55 +00:00
mongoDbName: databaseName,
});
await testDb.init();
});
// =======================================
// The actual tests
// =======================================
// ------
// Collections
// ------
2021-09-17 20:34:15 +00:00
@smartdata.Manager()
class Car extends smartdata.SmartDataDbDoc<Car, Car> {
2021-09-16 17:49:55 +00:00
@smartdata.unI()
public index: string = smartunique.shortId();
@smartdata.svDb()
public color: string;
@smartdata.svDb()
public brand: string;
@smartdata.svDb()
deepData = {
sodeep: 'yes',
};
constructor(colorArg: string, brandArg: string) {
super();
this.color = colorArg;
this.brand = brandArg;
}
}
const createCarClass = (dbArg: smartdata.SmartdataDb) => {
2021-10-16 19:17:02 +00:00
smartdata.setDefaultManagerForDoc({ db: dbArg }, Car);
2021-09-16 17:49:55 +00:00
return Car;
};
2021-09-17 20:34:15 +00:00
tap.test('should produce a car', async () => {
2021-09-16 17:49:55 +00:00
const CarClass = createCarClass(testDb);
const carInstance = new CarClass('red', 'Mercedes');
await carInstance.save();
});
2021-09-17 20:34:15 +00:00
tap.test('should get a car', async () => {
const car = Car.getInstance({
2021-10-16 19:17:02 +00:00
color: 'red',
});
});
2021-09-17 20:34:15 +00:00
2021-09-16 17:49:55 +00:00
// =======================================
// close the database connection
// =======================================
2021-11-12 17:04:08 +00:00
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();
2021-11-12 17:12:59 +00:00
}
2021-09-16 17:49:55 +00:00
});
tap.start({ throwOnError: true });