smartdata/test/test.ts

237 lines
6.2 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';
2022-06-14 20:04:34 +00:00
import * as mongodb from 'mongodb';
2019-09-02 14:42:29 +00:00
const testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit/');
2016-09-11 14:22:53 +00:00
2021-02-05 21:16:45 +00:00
console.log(process.memoryUsage());
// the tested module
2022-05-16 22:33:44 +00:00
import * as smartdata from '../ts/index.js';
2016-09-11 14:22:53 +00:00
// =======================================
// Connecting to the database server
// =======================================
2016-09-12 20:11:17 +00:00
2022-05-16 22:33:44 +00:00
let smartmongoInstance: smartmongo.SmartMongo;
2019-09-02 14:42:29 +00:00
let testDb: smartdata.SmartdataDb;
2021-02-05 21:16:45 +00:00
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();
2019-09-02 14:42:29 +00:00
});
2021-11-12 18:16:11 +00:00
tap.skip.test('should connect to atlas', async (tools) => {
2020-08-18 13:27:04 +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'),
2020-08-18 15:10:44 +00:00
mongoDbName: databaseName,
2020-08-18 13:27:04 +00:00
});
2019-01-08 18:55:13 +00:00
await testDb.init();
});
2016-09-12 20:11:17 +00:00
// =======================================
// The actual tests
// =======================================
2017-11-16 13:23:06 +00:00
// ------
// Collections
// ------
2017-06-18 17:52:54 +00:00
2019-09-02 14:42:29 +00:00
@smartdata.Collection(() => {
return testDb;
})
2020-02-19 18:30:34 +00:00
class Car extends smartdata.SmartDataDbDoc<Car, Car> {
2019-09-02 14:42:29 +00:00
@smartdata.unI()
public index: string = smartunique.shortId();
2019-09-02 14:51:22 +00:00
2019-09-02 14:42:29 +00:00
@smartdata.svDb()
public color: string;
2019-09-02 14:51:22 +00:00
2019-09-02 14:42:29 +00:00
@smartdata.svDb()
public brand: string;
2022-06-14 20:02:57 +00:00
@smartdata.svDb()
public testBuffer = Buffer.from('hello');
2020-09-09 04:51:56 +00:00
@smartdata.svDb()
deepData = {
2020-10-19 16:44:28 +00:00
sodeep: 'yes',
2020-09-09 04:51:56 +00:00
};
constructor(colorArg: string, brandArg: string) {
super();
this.color = colorArg;
this.brand = brandArg;
}
}
2024-03-27 16:30:14 +00:00
tap.test('should create a new id', async () => {
const newid = await Car.getNewId();
console.log(newid);
2024-04-13 23:24:21 +00:00
});
2024-03-27 16:30:14 +00:00
2022-06-14 20:02:57 +00:00
tap.test('should save the car to the db', async (toolsArg) => {
const myCar = new Car('red', 'Volvo');
await myCar.save();
2019-09-02 14:42:29 +00:00
2019-09-02 14:58:19 +00:00
const myCar2 = new Car('red', 'Volvo');
2019-09-02 14:42:29 +00:00
await myCar2.save();
2019-09-02 14:58:19 +00:00
2020-09-09 03:51:21 +00:00
let counter = 0;
2022-06-14 20:02:57 +00:00
const gottenCarInstance = await Car.getInstance({});
2022-06-14 20:04:34 +00:00
console.log(gottenCarInstance.testBuffer instanceof mongodb.Binary);
2021-02-05 21:16:45 +00:00
process.memoryUsage();
2020-09-09 03:51:21 +00:00
do {
const myCar3 = new Car('red', 'Renault');
await myCar3.save();
counter++;
2020-10-19 16:44:28 +00:00
if (counter % 100 === 0) {
2021-02-05 21:16:45 +00:00
console.log(
`Filled database with ${counter} of ${totalCars} Cars and memory usage ${
process.memoryUsage().rss / 1e6
} MB`
);
2020-09-25 21:05:21 +00:00
}
} while (counter < totalCars);
2021-02-05 21:16:45 +00:00
console.log(process.memoryUsage());
});
2016-09-11 14:22:53 +00:00
2020-09-10 10:36:00 +00:00
tap.test('expect to get instance of Car with shallow match', async () => {
2023-02-06 10:43:11 +00:00
const totalQueryCycles = totalCars / 2;
2020-09-09 03:51:21 +00:00
let counter = 0;
do {
const timeStart = Date.now();
const myCars = await Car.getInstances({
2020-09-10 10:12:17 +00:00
brand: 'Renault',
2020-09-09 03:51:21 +00:00
});
2021-02-05 21:16:45 +00:00
if (counter % 10 === 0) {
console.log(
`performed ${counter} of ${totalQueryCycles} total query cycles: took ${
Date.now() - timeStart
}ms to query a set of 2000 with memory footprint ${process.memoryUsage().rss / 1e6} MB`
);
}
2022-05-16 22:33:44 +00:00
expect(myCars[0].deepData.sodeep).toEqual('yes');
expect(myCars[0].brand).toEqual('Renault');
2020-09-09 05:00:09 +00:00
counter++;
2021-02-05 21:16:45 +00:00
} while (counter < totalQueryCycles);
2020-09-09 05:00:09 +00:00
});
2020-09-10 10:36:00 +00:00
tap.test('expect to get instance of Car with deep match', async () => {
2022-05-17 21:54:26 +00:00
const totalQueryCycles = totalCars / 6;
2020-09-09 05:00:09 +00:00
let counter = 0;
do {
const timeStart = Date.now();
const myCars2 = await Car.getInstances({
deepData: {
2021-10-16 19:17:02 +00:00
sodeep: 'yes',
},
});
2021-02-05 21:16:45 +00:00
if (counter % 10 === 0) {
console.log(
`performed ${counter} of ${totalQueryCycles} total query cycles: took ${
Date.now() - timeStart
}ms to deep query a set of 2000 with memory footprint ${process.memoryUsage().rss / 1e6} MB`
);
}
2022-05-16 22:33:44 +00:00
expect(myCars2[0].deepData.sodeep).toEqual('yes');
expect(myCars2[0].brand).toEqual('Volvo');
2020-09-09 03:51:21 +00:00
counter++;
2021-02-05 21:16:45 +00:00
} while (counter < totalQueryCycles);
});
2016-11-17 21:36:12 +00:00
2019-09-02 14:42:29 +00:00
tap.test('expect to get instance of Car and update it', async () => {
const myCar = await Car.getInstance<Car>({
2020-08-18 12:01:46 +00:00
brand: 'Volvo',
2019-09-02 14:42:29 +00:00
});
2022-05-16 22:33:44 +00:00
expect(myCar.color).toEqual('red');
2019-09-02 14:42:29 +00:00
myCar.color = 'blue';
await myCar.save();
});
2019-09-02 14:58:19 +00:00
tap.test('should be able to delete an instance of car', async () => {
const myCars = await Car.getInstances({
2020-08-18 12:01:46 +00:00
brand: 'Volvo',
2020-10-19 16:44:28 +00:00
color: 'blue',
2019-09-02 14:58:19 +00:00
});
2020-09-25 21:05:21 +00:00
console.log(myCars);
2022-05-16 22:33:44 +00:00
expect(myCars[0].color).toEqual('blue');
2020-09-25 21:05:21 +00:00
for (const myCar of myCars) {
await myCar.delete();
}
2019-09-02 14:42:29 +00:00
2019-09-02 14:58:19 +00:00
const myCar2 = await Car.getInstance<Car>({
2020-08-18 12:01:46 +00:00
brand: 'Volvo',
2019-09-02 14:58:19 +00:00
});
2022-05-16 22:33:44 +00:00
expect(myCar2.color).toEqual('red');
2019-09-02 14:58:19 +00:00
});
2019-09-02 14:42:29 +00:00
2019-09-11 09:56:41 +00:00
// tslint:disable-next-line: max-classes-per-file
@smartdata.Collection(() => {
return testDb;
})
2020-02-19 18:30:34 +00:00
class Truck extends smartdata.SmartDataDbDoc<Car, Car> {
2019-09-11 09:56:41 +00:00
@smartdata.unI()
public id: string = smartunique.shortId();
@smartdata.svDb()
public color: string;
@smartdata.svDb()
public brand: string;
constructor(colorArg: string, brandArg: string) {
super();
this.color = colorArg;
this.brand = brandArg;
}
}
tap.test('should store a new Truck', async () => {
const truck = new Truck('blue', 'MAN');
await truck.save();
const myTruck2 = await Truck.getInstance({ color: 'blue' });
expect(myTruck2.color).toEqual('blue');
2021-11-12 15:23:26 +00:00
myTruck2.color = 'red';
await myTruck2.save();
const myTruck3 = await Truck.getInstance({ color: 'blue' });
expect(myTruck3).toBeNull();
2019-09-11 09:56:41 +00:00
});
tap.test('should return a count', async () => {
const truckCount = await Truck.getCount();
expect(truckCount).toEqual(1);
})
2021-11-12 15:23:26 +00:00
tap.test('should use a cursor', async () => {
2022-05-16 22:33:44 +00:00
const cursor = await Car.getCursor({});
let counter = 0;
await cursor.forEach(async (carArg) => {
counter++;
counter % 50 === 0 ? console.log(`50 more of ${carArg.color}`) : null;
2021-11-12 15:23:26 +00:00
});
});
// =======================================
// close the database connection
// =======================================
2021-11-12 17:04:08 +00:00
tap.test('close', async () => {
2022-05-16 22:33:44 +00:00
if (smartmongoInstance) {
2024-04-13 23:24:21 +00:00
await smartmongoInstance.stopAndDumpToDir('./.nogit/dbdump/test.ts');
} else {
await testDb.mongoDb.dropDatabase();
await testDb.close();
2021-11-12 17:12:59 +00:00
}
2024-04-13 23:24:21 +00:00
setTimeout(() => process.exit(), 2000);
});
2017-06-18 17:52:54 +00:00
2019-01-07 01:41:38 +00:00
tap.start({ throwOnError: true });