smartdata/test/test.typescript.ts

111 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-09-16 17:49:55 +00:00
import { tap, expect } from '@pushrocks/tapbundle';
import { Qenv } from '@pushrocks/qenv';
const testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit/');
console.log(process.memoryUsage());
// the tested module
import * as smartdata from '../ts/index';
import * as mongoPlugin from 'mongodb-memory-server';
import { smartunique } from '../ts/smartdata.plugins';
// =======================================
// Connecting to the database server
// =======================================
let testDb: smartdata.SmartdataDb;
let smartdataOptions: smartdata.IMongoDescriptor;
let mongod: mongoPlugin.MongoMemoryServer;
const totalCars = 2000;
tap.skip.test('should create a testinstance as database', async () => {
mongod = new mongoPlugin.MongoMemoryServer({});
console.log('created mongod instance');
await mongod._startUpInstance().catch((err) => {
console.log(err);
});
console.log('mongod started');
smartdataOptions = {
mongoDbName: await mongod.getDbName(),
mongoDbPass: '',
mongoDbUrl: await mongod.getUri(),
};
console.log(smartdataOptions);
testDb = new smartdata.SmartdataDb(smartdataOptions);
});
tap.test('should connect to atlas', async (tools) => {
const databaseName = `test-smartdata-${smartunique.shortId()}`;
testDb = new smartdata.SmartdataDb({
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGO_URL'),
mongoDbName: databaseName,
});
});
tap.test('should establish a connection to mongod', async () => {
await testDb.init();
});
// =======================================
// The actual tests
// =======================================
// ------
// Collections
// ------
2021-09-17 20:34:15 +00:00
@smartdata.Manager()
class Car extends smartdata.SmartDataDbDoc<Car, Car> {
2021-09-16 17:49:55 +00:00
@smartdata.unI()
public index: string = smartunique.shortId();
@smartdata.svDb()
public color: string;
@smartdata.svDb()
public brand: string;
@smartdata.svDb()
deepData = {
sodeep: 'yes',
};
constructor(colorArg: string, brandArg: string) {
super();
this.color = colorArg;
this.brand = brandArg;
}
}
const createCarClass = (dbArg: smartdata.SmartdataDb) => {
2021-10-16 19:17:02 +00:00
smartdata.setDefaultManagerForDoc({ db: dbArg }, Car);
2021-09-16 17:49:55 +00:00
return Car;
};
2021-09-17 20:34:15 +00:00
tap.test('should produce a car', async () => {
2021-09-16 17:49:55 +00:00
const CarClass = createCarClass(testDb);
const carInstance = new CarClass('red', 'Mercedes');
await carInstance.save();
});
2021-09-17 20:34:15 +00:00
tap.test('should get a car', async () => {
const car = Car.getInstance({
2021-10-16 19:17:02 +00:00
color: 'red',
});
});
2021-09-17 20:34:15 +00:00
2021-09-16 17:49:55 +00:00
// =======================================
// close the database connection
// =======================================
tap.test('should close the database connection', async (tools) => {
await testDb.mongoDb.dropDatabase();
await testDb.close();
try {
await mongod.stop();
} catch (e) {}
});
tap.start({ throwOnError: true });