smartdata/test/test.ts

144 lines
3.6 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';
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
// =======================================
// 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
});
tap.test('should establish a connection to the rethink Db cluster', 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;
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
const myCar3 = new Car('red', 'Renault');
await myCar3.save();
});
2016-09-11 14:22:53 +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>({
brand: 'Volvo'
});
2018-07-09 22:02:04 +00:00
expect(myCars[0].color).to.equal('red');
});
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' });
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
// =======================================
tap.test('should close the database connection', async tools => {
await testDb.close();
2019-09-02 14:42:29 +00:00
await mongod.stop();
});
2017-06-18 17:52:54 +00:00
2019-01-07 01:41:38 +00:00
tap.start({ throwOnError: true });