smartdata/test/test.ts

110 lines
2.9 KiB
TypeScript
Raw Permalink 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
});
2019-09-02 14:50:22 +00:00
tap.skip.test('should create a smartdb', async () => {
2019-09-02 14:42:29 +00:00
testDb = new smartdata.SmartdataDb({
mongoDbName: testQenv.getEnvVarOnDemand('MONGO_DBNAME'),
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGO_URL'),
mongoDbPass: testQenv.getEnvVarOnDemand('MONGO_PASS')
});
});
2016-09-12 20:11:17 +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;
})
2018-07-09 22:02:04 +00:00
class Car extends smartdata.SmartDataDbDoc<Car> {
2019-09-02 14:42:29 +00:00
@smartdata.unI()
public index: 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 save the car to the db', async () => {
const myCar = new Car('red', 'Volvo');
await myCar.save();
2019-09-02 14:42:29 +00:00
const myCar2 = new Car('red', 'Renault');
await myCar2.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();
});
tap.test('should be able to update an instance of car', async () => {});
tap.test('should be able to delete an instance of car', async () => {});
// =======================================
// 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 });