smartdata/test/test.easystore.ts

65 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

2021-10-16 17:54:05 +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;
2021-11-12 18:03:06 +00:00
tap.test('should create a testinstance as database', async () => {
2021-11-12 16:22:31 +00:00
mongod = await mongoPlugin.MongoMemoryServer.create();
2021-10-16 17:54:05 +00:00
console.log('created mongod instance');
console.log('mongod started');
smartdataOptions = {
2021-11-12 16:22:31 +00:00
mongoDbUrl: mongod.getUri(),
2021-10-16 17:54:05 +00:00
};
console.log(smartdataOptions);
testDb = new smartdata.SmartdataDb(smartdataOptions);
await testDb.init();
});
2021-11-12 18:03:06 +00:00
tap.skip.test('should connect to atlas', async (tools) => {
2021-11-12 16:32:43 +00:00
const databaseName = `test-smartdata-${smartunique.shortId()}`;
testDb = new smartdata.SmartdataDb({
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGO_URL'),
mongoDbName: databaseName,
});
2021-11-12 17:04:08 +00:00
await testDb.init();
2021-11-12 16:32:43 +00:00
});
2021-10-16 17:54:05 +00:00
let easyStore: smartdata.EasyStore<{
key1: string;
key2: string;
}>;
tap.test('should create an easystore', async () => {
easyStore = await testDb.createEasyStore('hellothere');
await easyStore.writeKey('key1', 'hello');
const retrievedKey = await easyStore.readKey('key1');
expect(retrievedKey).to.equal('hello');
2021-10-16 19:17:02 +00:00
});
2021-10-16 17:54:05 +00:00
tap.test('close', async () => {
2021-11-12 17:12:59 +00:00
if (mongod) {
await mongod.stop();
} else {
await testDb.mongoDb.dropDatabase();
}
2021-11-12 17:04:08 +00:00
await testDb.close();
2021-10-16 19:17:02 +00:00
});
2021-10-16 17:54:05 +00:00
2021-10-16 19:17:02 +00:00
tap.start();