smartdata/test/test.ts

66 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-01-07 01:38:30 +00:00
import { tap, expect } from '@pushrocks/tapbundle';
import * as smartpromise from '@pushrocks/smartpromise';
2019-01-07 01:38:30 +00:00
import { Qenv } from '@pushrocks/qenv';
let 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';
import * as shortid from 'shortid';
2016-09-11 14:22:53 +00:00
// =======================================
// Connecting to the database server
// =======================================
2016-09-12 20:11:17 +00:00
let testDb = new smartdata.SmartdataDb({
mongoDbName: process.env.MONGO_DBNAME,
mongoDbUrl: process.env.MONGO_URL,
2018-07-09 22:02:04 +00:00
mongoDbPass: process.env.MONGO_PASS
});
2016-09-12 20:11:17 +00:00
tap.test('should establish a connection to the rethink Db cluster', async () => {
await testDb.connect();
});
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
2018-07-09 22:02:04 +00:00
@smartdata.Collection(testDb)
class Car extends smartdata.SmartDataDbDoc<Car> {
@smartdata.unI() index: string = shortid();
@smartdata.svDb() color: string;
@smartdata.svDb() 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();
});
2016-09-11 14:22:53 +00:00
tap.test('expect to get instance of Car', async () => {
2018-07-09 22:02:04 +00:00
let 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
// =======================================
// close the database connection
// =======================================
tap.test('should close the database connection', async tools => {
await testDb.close();
});
2017-06-18 17:52:54 +00:00
2018-07-09 22:17:16 +00:00
tap.start({throwOnError: true});