2019-01-07 01:38:30 +00:00
|
|
|
import { tap, expect } from '@pushrocks/tapbundle';
|
|
|
|
import { Qenv } from '@pushrocks/qenv';
|
2017-06-23 09:40:20 +00:00
|
|
|
|
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());
|
|
|
|
|
2016-09-12 12:45:08 +00:00
|
|
|
// the tested module
|
2018-07-10 19:45:26 +00:00
|
|
|
import * as smartdata from '../ts/index';
|
2019-09-02 14:42:29 +00:00
|
|
|
|
|
|
|
import * as mongoPlugin from 'mongodb-memory-server';
|
2020-08-18 13:27:04 +00:00
|
|
|
import { smartunique } from '../ts/smartdata.plugins';
|
2016-09-11 14:22:53 +00:00
|
|
|
|
2018-01-12 00:22:58 +00:00
|
|
|
// =======================================
|
|
|
|
// Connecting to the database server
|
|
|
|
// =======================================
|
2016-09-12 20:11:17 +00:00
|
|
|
|
2019-09-02 14:42:29 +00:00
|
|
|
let testDb: smartdata.SmartdataDb;
|
2020-08-18 12:05:07 +00:00
|
|
|
let smartdataOptions: smartdata.IMongoDescriptor;
|
2019-09-02 14:42:29 +00:00
|
|
|
let mongod: mongoPlugin.MongoMemoryServer;
|
|
|
|
|
2021-02-05 21:16:45 +00:00
|
|
|
const totalCars = 2000;
|
|
|
|
|
2021-11-12 17:04:08 +00:00
|
|
|
tap.test('should create a testinstance as database', async () => {
|
2021-11-12 16:22:31 +00:00
|
|
|
mongod = await mongoPlugin.MongoMemoryServer.create();
|
2020-08-18 12:52:00 +00:00
|
|
|
console.log('created mongod instance');
|
|
|
|
console.log('mongod started');
|
2019-09-02 14:42:29 +00:00
|
|
|
smartdataOptions = {
|
2021-11-12 16:22:31 +00:00
|
|
|
mongoDbUrl: mongod.getUri(),
|
2019-09-02 14:42:29 +00:00
|
|
|
};
|
|
|
|
console.log(smartdataOptions);
|
2019-09-02 14:50:22 +00:00
|
|
|
testDb = new smartdata.SmartdataDb(smartdataOptions);
|
2021-11-12 17:04:08 +00:00
|
|
|
await testDb.init();
|
2019-09-02 14:42:29 +00:00
|
|
|
});
|
|
|
|
|
2021-11-12 17:04:08 +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({
|
|
|
|
mongoDbUrl: 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();
|
2018-07-08 21:48:14 +00:00
|
|
|
});
|
2016-09-12 20:11:17 +00:00
|
|
|
|
2017-06-23 09:40:20 +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;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-07-08 21:48:14 +00:00
|
|
|
constructor(colorArg: string, brandArg: string) {
|
|
|
|
super();
|
|
|
|
this.color = colorArg;
|
|
|
|
this.brand = brandArg;
|
2018-01-12 00:22:58 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-13 23:02:11 +00:00
|
|
|
|
2018-01-12 00:22:58 +00:00
|
|
|
tap.test('should save the car to the db', async () => {
|
2018-07-08 21:48:14 +00:00
|
|
|
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;
|
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());
|
2018-07-08 21:48:14 +00:00
|
|
|
});
|
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 () => {
|
2021-02-05 21:16:45 +00:00
|
|
|
const totalQueryCycles = totalCars / 4;
|
2020-09-09 03:51:21 +00:00
|
|
|
let counter = 0;
|
|
|
|
do {
|
|
|
|
const timeStart = Date.now();
|
2021-06-06 15:48:37 +00:00
|
|
|
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`
|
|
|
|
);
|
|
|
|
}
|
2020-09-10 10:36:00 +00:00
|
|
|
expect(myCars[0].deepData.sodeep).to.equal('yes');
|
|
|
|
expect(myCars[0].brand).to.equal('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 () => {
|
2021-02-05 21:16:45 +00:00
|
|
|
const totalQueryCycles = totalCars / 4;
|
2020-09-09 05:00:09 +00:00
|
|
|
let counter = 0;
|
|
|
|
do {
|
|
|
|
const timeStart = Date.now();
|
2021-06-06 15:48:37 +00:00
|
|
|
const myCars2 = await Car.getInstances({
|
|
|
|
deepData: {
|
2021-10-16 19:17:02 +00:00
|
|
|
sodeep: 'yes',
|
2021-06-06 15:48:37 +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 deep query a set of 2000 with memory footprint ${process.memoryUsage().rss / 1e6} MB`
|
|
|
|
);
|
|
|
|
}
|
2020-09-10 10:36:00 +00:00
|
|
|
expect(myCars2[0].deepData.sodeep).to.equal('yes');
|
|
|
|
expect(myCars2[0].brand).to.equal('Volvo');
|
2020-09-09 03:51:21 +00:00
|
|
|
counter++;
|
2021-02-05 21:16:45 +00:00
|
|
|
} while (counter < totalQueryCycles);
|
2018-07-08 21:48:14 +00:00
|
|
|
});
|
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
|
|
|
});
|
|
|
|
expect(myCar.color).to.equal('red');
|
|
|
|
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 () => {
|
2021-06-06 15:48:37 +00:00
|
|
|
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);
|
|
|
|
expect(myCars[0].color).to.equal('blue');
|
|
|
|
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
|
|
|
});
|
|
|
|
expect(myCar2.color).to.equal('red');
|
|
|
|
});
|
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();
|
2021-06-06 15:48:37 +00:00
|
|
|
const myTruck2 = await Truck.getInstance({ color: 'blue' });
|
2021-11-12 15:23:26 +00:00
|
|
|
myTruck2.color = 'red';
|
|
|
|
await myTruck2.save();
|
|
|
|
const myTruck3 = await Truck.getInstance({ color: 'blue' });
|
|
|
|
console.log(myTruck3);
|
2019-09-11 09:56:41 +00:00
|
|
|
});
|
|
|
|
|
2021-11-12 15:23:26 +00:00
|
|
|
tap.test('should use a cursor', async () => {
|
|
|
|
const cursor = await Truck.getCursor({});
|
|
|
|
cursor.forEach(async truckArg => {
|
|
|
|
console.log(truckArg.id);
|
|
|
|
});
|
|
|
|
});
|
2021-06-06 15:48:37 +00:00
|
|
|
|
2018-01-12 00:22:58 +00:00
|
|
|
// =======================================
|
|
|
|
// close the database connection
|
|
|
|
// =======================================
|
2021-11-12 17:04:08 +00:00
|
|
|
tap.test('close', async () => {
|
|
|
|
await mongod.stop();
|
2018-07-08 21:48:14 +00:00
|
|
|
await testDb.close();
|
|
|
|
});
|
2017-06-18 17:52:54 +00:00
|
|
|
|
2019-01-07 01:41:38 +00:00
|
|
|
tap.start({ throwOnError: true });
|