smartdata/test/test.ts

190 lines
4.7 KiB
TypeScript
Raw Normal View History

2019-01-07 01:38:30 +00:00
import { tap, expect } from '@pushrocks/tapbundle';
import { Qenv } from '@pushrocks/qenv';
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
// 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
// =======================================
// 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;
2020-08-18 13:27:04 +00:00
tap.skip.test('should create a testinstance as database', async () => {
2020-08-18 15:10:44 +00:00
mongod = new mongoPlugin.MongoMemoryServer({});
2020-08-18 12:52:00 +00:00
console.log('created mongod instance');
2020-08-18 15:10:44 +00:00
await mongod._startUpInstance().catch((err) => {
2020-08-18 12:53:44 +00:00
console.log(err);
});
2020-08-18 12:52:00 +00:00
console.log('mongod started');
2019-09-02 14:42:29 +00:00
smartdataOptions = {
mongoDbName: await mongod.getDbName(),
mongoDbPass: '',
2020-08-18 12:01:46 +00:00
mongoDbUrl: await mongod.getConnectionString(),
2019-09-02 14:42:29 +00:00
};
console.log(smartdataOptions);
2019-09-02 14:50:22 +00:00
testDb = new smartdata.SmartdataDb(smartdataOptions);
2019-09-02 14:42:29 +00:00
});
2020-08-18 13:27:04 +00:00
tap.test('should connect to atlas', async (tools) => {
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
});
});
2020-08-18 12:46:14 +00:00
tap.test('should establish a connection to mongod', async () => {
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;
2020-09-09 04:51:56 +00:00
@smartdata.svDb()
deepData = {
sodeep: 'yes'
};
constructor(colorArg: string, brandArg: string) {
super();
this.color = colorArg;
this.brand = brandArg;
}
}
tap.test('should save the car to the db', async () => {
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;
do {
const myCar3 = new Car('red', 'Renault');
await myCar3.save();
counter++;
} while (counter < 2000);
});
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 () => {
2020-09-09 03:51:21 +00:00
let counter = 0;
do {
const timeStart = Date.now();
const myCars = await Car.getInstances<Car>({
2020-09-10 10:12:17 +00:00
brand: 'Renault',
2020-09-09 03:51:21 +00:00
});
2020-09-09 05:00:09 +00:00
console.log(`took ${Date.now() - timeStart}`);
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++;
2020-09-09 05:05:41 +00:00
} while (counter < 30);
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 () => {
2020-09-09 05:00:09 +00:00
let counter = 0;
do {
const timeStart = Date.now();
2020-09-09 04:51:56 +00:00
const myCars2 = await Car.getInstances<Car>({
'deepData.sodeep': 'yes',
} as any);
2020-09-09 03:51:21 +00:00
console.log(`took ${Date.now() - timeStart}`);
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++;
2020-09-09 05:05:41 +00:00
} while (counter < 30);
});
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 () => {
const myCar = await Car.getInstance<Car>({
2020-08-18 12:01:46 +00:00
brand: 'Volvo',
2019-09-02 14:58:19 +00:00
});
expect(myCar.color).to.equal('blue');
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();
2020-06-11 23:05:32 +00:00
const myTruck = await Truck.getInstance<Truck>({ color: 'blue' });
myTruck.id = 'foo';
await myTruck.save();
2020-06-11 23:05:32 +00:00
const myTruck2 = await Truck.getInstance<Truck>({ color: 'blue' });
console.log(myTruck2);
2019-09-11 09:56:41 +00:00
});
// =======================================
// close the database connection
// =======================================
2020-08-18 12:01:46 +00:00
tap.test('should close the database connection', async (tools) => {
await testDb.close();
2020-08-18 13:27:04 +00:00
try {
await mongod.stop();
} catch (e) {}
});
2017-06-18 17:52:54 +00:00
2019-01-07 01:41:38 +00:00
tap.start({ throwOnError: true });