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
|
|
|
|
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';
|
2018-01-12 00:22:58 +00:00
|
|
|
import { smartstring } from '../ts/smartdata.plugins';
|
2019-09-02 14:42:29 +00:00
|
|
|
import * as smartunique from '@pushrocks/smartunique';
|
|
|
|
|
|
|
|
import * as mongoPlugin from 'mongodb-memory-server';
|
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;
|
|
|
|
let smartdataOptions: smartdata.ISmartdataOptions;
|
|
|
|
let mongod: mongoPlugin.MongoMemoryServer;
|
|
|
|
|
|
|
|
tap.test('should create a testinstance as database', async () => {
|
|
|
|
mongod = new mongoPlugin.MongoMemoryServer();
|
|
|
|
smartdataOptions = {
|
|
|
|
mongoDbName: await mongod.getDbName(),
|
|
|
|
mongoDbPass: '',
|
|
|
|
mongoDbUrl: await mongod.getConnectionString()
|
|
|
|
};
|
|
|
|
console.log(smartdataOptions);
|
2019-09-02 14:50:22 +00:00
|
|
|
testDb = new smartdata.SmartdataDb(smartdataOptions);
|
2019-09-02 14:42:29 +00:00
|
|
|
});
|
|
|
|
|
2018-01-12 00:22:58 +00:00
|
|
|
tap.test('should establish a connection to the rethink Db cluster', async () => {
|
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;
|
|
|
|
|
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
|
|
|
|
|
|
|
const myCar3 = new Car('red', 'Renault');
|
|
|
|
await myCar3.save();
|
2018-07-08 21:48:14 +00:00
|
|
|
});
|
2016-09-11 14:22:53 +00:00
|
|
|
|
2018-01-14 16:32:04 +00:00
|
|
|
tap.test('expect to get instance of Car', async () => {
|
2019-09-02 14:42:29 +00:00
|
|
|
const myCars = await Car.getInstances<Car>({
|
2018-01-14 16:32:04 +00:00
|
|
|
brand: 'Volvo'
|
2018-07-08 21:48:14 +00:00
|
|
|
});
|
2018-07-09 22:02:04 +00:00
|
|
|
expect(myCars[0].color).to.equal('red');
|
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>({
|
|
|
|
brand: 'Volvo'
|
|
|
|
});
|
|
|
|
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>({
|
|
|
|
brand: 'Volvo'
|
|
|
|
});
|
|
|
|
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>({
|
|
|
|
brand: 'Volvo'
|
|
|
|
});
|
|
|
|
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' });
|
2019-09-11 10:12:50 +00:00
|
|
|
myTruck.id = 'foo';
|
|
|
|
await myTruck.save();
|
2020-06-11 23:05:32 +00:00
|
|
|
const myTruck2 = await Truck.getInstance<Truck>({ color: 'blue' });
|
2019-09-11 10:12:50 +00:00
|
|
|
console.log(myTruck2);
|
2019-09-11 09:56:41 +00:00
|
|
|
});
|
|
|
|
|
2018-01-12 00:22:58 +00:00
|
|
|
// =======================================
|
|
|
|
// close the database connection
|
|
|
|
// =======================================
|
2018-07-08 21:48:14 +00:00
|
|
|
tap.test('should close the database connection', async tools => {
|
|
|
|
await testDb.close();
|
2019-09-02 14:42:29 +00:00
|
|
|
await mongod.stop();
|
2018-07-08 21:48:14 +00:00
|
|
|
});
|
2017-06-18 17:52:54 +00:00
|
|
|
|
2019-01-07 01:41:38 +00:00
|
|
|
tap.start({ throwOnError: true });
|