smartdata/test/test.js

129 lines
11 KiB
JavaScript

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
require("typings-test");
const shelljs = require("shelljs");
const should = require("should");
const smartstring = require("smartstring");
// the tested module
const smartdata = require("../dist/index");
let mongoChildProcess;
let testDb;
let testDbCollection;
describe('mongodb', function () {
it('should start mongodb', function (done) {
this.timeout(30000);
mongoChildProcess = shelljs.exec('mongod --dbpath=./test/data --port 27017', { async: true, silent: true });
let doneCalled = false;
mongoChildProcess.stdout.on('data', function (data) {
console.log(smartstring.indent.indentWithPrefix(data, '*** MongoDB Process *** : '));
if (!doneCalled) {
if (/waiting for connections on port 27017/.test(data)) {
doneCalled = true;
done();
}
}
});
});
});
describe('smartdata', function () {
it('should establish a connection to mongodb', function (done) {
testDb = new smartdata.Db('mongodb://localhost:27017/smartdata');
testDb.connect().then(() => { done(); });
});
it('should create a collection', function () {
testDbCollection = new smartdata.DbCollection('something', testDb);
});
it('should insert a doc into the collection', function (done) {
testDbCollection.insertOne({ value1: 'test' }).then(() => { done(); });
});
it('should find all docs of testDbCollection', function (done) {
testDbCollection.find({}).then((resultArray) => {
console.log(resultArray);
should(resultArray[0].value1).equal('test');
done();
});
});
it('should insert many docs into the collection', function (done) {
testDbCollection.insertMany([
{ value1: 'test2' },
{ value1: 'test', value2: 3, value3: 'hi' }
]).then(() => { done(); });
});
it('should find a specified doc', function (done) {
testDbCollection.find({ 'value3': { '$exists': true } }).then((resultArray) => {
console.log(resultArray);
should(resultArray[0].value3).equal('hi');
done();
}).catch(console.log);
});
it('should close the db Connection', function () {
testDb.close();
});
it('should create an extended class', function () {
let TestCar = class TestCar extends smartdata.DbDoc {
constructor(optionsArg) {
super();
this.color = optionsArg.color;
}
};
__decorate([
smartdata.svDb(),
__metadata("design:type", String)
], TestCar.prototype, "color", void 0);
TestCar = __decorate([
smartdata.Collection(testDb),
__metadata("design:paramtypes", [Object])
], TestCar);
;
let testCarInstance = new TestCar({
color: 'red',
property2: 2
});
should(testCarInstance.saveableProperties[0]).equal('color');
console.log(TestCar);
should(testCarInstance.collection).be.instanceof(smartdata.DbCollection);
should(testCarInstance).be.instanceof(smartdata.DbDoc);
testCarInstance.save();
it('should get a collection for testCar', function () {
});
});
});
describe('mongodb', function () {
it('should kill mongodb', function (done) {
this.timeout(30000);
mongoChildProcess.stdout.on('data', function (data) {
if (/dbexit: rc: 0/.test(data)) {
done();
}
});
shelljs.exec('mongod --dbpath=./test/data --shutdown');
mongoChildProcess.kill('SIGTERM');
});
});
describe('smartdata with nedb', function () {
let testDb;
let testCollection;
it('should create a DB with nedb', function () {
testDb = new smartdata.Db('any', 'nedb');
testDb.connect();
testCollection = new smartdata.DbCollection('anyName', testDb);
});
it('should insert a doc', function (done) {
testCollection.insertOne({ value1: 'hi' }).then(() => { done(); });
});
it('should find the inserted document', function (done) {
testCollection.find({ value1: 'hi' }).then(x => {
console.log(x);
done();
});
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLHdCQUFxQjtBQUVyQixtQ0FBa0M7QUFDbEMsaUNBQWdDO0FBQ2hDLDJDQUEwQztBQUUxQyxvQkFBb0I7QUFDcEIsMkNBQTBDO0FBRTFDLElBQU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