smartdata/test/test.ts

78 lines
2.7 KiB
TypeScript
Raw Normal View History

2016-09-11 14:22:53 +00:00
import 'typings-test'
2016-09-11 14:22:53 +00:00
import * as shelljs from 'shelljs'
import * as should from 'should'
2016-09-12 15:31:23 +00:00
import * as smartstring from 'smartstring'
2016-09-11 14:22:53 +00:00
// the tested module
import * as smartdata from '../dist/index'
2016-09-11 14:22:53 +00:00
let mongoChildProcess
let testDbConnection: smartdata.DbConnection
2016-09-11 16:01:46 +00:00
let testDbCollection: smartdata.DbCollection<any>
2016-09-11 14:22:53 +00:00
describe('mongodb', function () {
it('should start mongodb', function (done) {
2016-09-12 16:14:01 +00:00
this.timeout(30000)
mongoChildProcess = shelljs.exec('mongod --dbpath=./test/data --port 27017', { async: true, silent: true })
let doneCalled = false
2016-09-12 16:14:01 +00:00
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()
}
}
})
2016-09-11 14:22:53 +00:00
})
})
describe('smartdata', function () {
it('should establish a connection to mongodb', function (done) {
2016-09-11 14:22:53 +00:00
testDbConnection = new smartdata.DbConnection('mongodb://localhost:27017/smartdata')
testDbConnection.connect().then(() => { done() })
})
it('should create a collection', function () {
testDbCollection = new smartdata.DbCollection('something', testDbConnection)
2016-09-11 14:22:53 +00:00
})
2016-09-12 19:36:26 +00:00
it('should insert a doc into the collection', function (done) {
2016-09-12 16:14:01 +00:00
testDbCollection.insertOne({ hello: 'test' }).then(() => { done() })
})
2016-09-12 19:36:26 +00:00
it('should find all docs of testDbCollection', function (done) {
2016-09-12 16:14:01 +00:00
testDbCollection.find({}).then((resultArray) => {
console.log(resultArray)
should(resultArray[0].hello).equal('test')
done()
})
2016-09-12 15:31:23 +00:00
})
2016-09-12 19:36:26 +00:00
it('should insert many docs into the collection', function (done) {
testDbCollection.insertMany([
{ hello: 'test' },
{ wow: 'what is this', wow2: 3}
]).then(() => { done() })
})
it('should find a specified doc', function (done) {
testDbCollection.find({'wow2': {'$exists': true}}).then((resultArray) => {
console.log(resultArray)
should(resultArray[0].wow2).equal(3)
done()
}).catch(console.log)
})
it('should close the db Connection', function () {
2016-09-11 14:22:53 +00:00
testDbConnection.close()
})
})
describe('mongodb', function () {
2016-09-12 16:14:01 +00:00
it('should kill mongodb', function (done) {
this.timeout(30000)
mongoChildProcess.stdout.on('data', function (data) {
if (/dbexit: rc: 0/.test(data)) {
done()
}
})
2016-09-11 14:48:08 +00:00
shelljs.exec('mongod --dbpath=./test/data --shutdown')
2016-09-11 14:44:36 +00:00
mongoChildProcess.kill('SIGTERM')
2016-09-11 14:22:53 +00:00
})
})