smartdata/test/test.ts

70 lines
2.0 KiB
TypeScript
Raw Normal View History

2017-06-18 17:52:54 +00:00
import { tap, expect } from 'tapbundle'
import * as smartq from 'smartq'
import { Qenv } from 'qenv'
let testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit/')
2016-09-11 14:22:53 +00:00
// the tested module
2017-09-13 11:47:38 +00:00
import * as smartdata from '../ts/index'
2016-09-11 14:22:53 +00:00
let mongoChildProcess
2016-09-12 21:50:25 +00:00
let testDb: smartdata.Db
2016-09-12 20:11:17 +00:00
interface ITestObject1 {
2017-02-25 10:37:05 +00:00
value1: string
value2?: number
value3?: string
2016-09-12 20:11:17 +00:00
}
2017-06-18 17:52:54 +00:00
tap.test('should establish a connection to mongodb', async () => {
2017-11-16 13:23:06 +00:00
// testDb = new smartdata.Db(`mongodb://${process.env.MONGO_USER}:${process.env.MONGO_PASS}@sandbox-shard-00-00-uyw7y.mongodb.net:27017,sandbox-shard-00-01-uyw7y.mongodb.net:27017,sandbox-shard-00-02-uyw7y.mongodb.net:27017/${process.env.MONGO_DATABASE}?ssl=true&replicaSet=sandbox-shard-0&authSource=admin`)
testDb = new smartdata.Db(`mongodb://localhost:27017/${process.env.MONGO_DATABASE}`)
2017-06-18 17:52:54 +00:00
await testDb.connect()
2016-09-11 14:22:53 +00:00
})
2016-09-12 20:11:17 +00:00
// =======================================
// The actual tests
// =======================================
2017-11-16 13:23:06 +00:00
// ------
// Collections
// ------
let testDbCollection: smartdata.DbCollection<ITestObject1>
tap.test('should give me a collection', async () => {
testDbCollection = await testDb.getObjectCollectionByName<ITestObject1>('TestValue', testDb, true)
})
2017-06-18 17:52:54 +00:00
tap.test('should insert a doc into the collection', async () => {
await testDbCollection.insertOne({ value1: 'test' })
})
tap.test('should find all docs of testDbCollection', async () => {
await testDbCollection.find({}).then(async (resultArray) => {
console.log(resultArray)
expect(resultArray[ 0 ].value1).equal('test')
2017-02-25 10:37:05 +00:00
})
2017-06-18 17:52:54 +00:00
})
2016-11-17 23:42:25 +00:00
2017-06-18 17:52:54 +00:00
tap.test('should insert many docs into the collection', async () => {
await testDbCollection.insertMany([
{ value1: 'test2' },
{ value1: 'test', value2: 3, value3: 'hi' }
])
})
2017-06-18 17:52:54 +00:00
tap.test('should find a specified doc', async () => {
await testDbCollection.find({ 'value3': { '$exists': true } }).then((resultArray) => {
console.log(resultArray)
expect(resultArray[ 0 ].value3).equal('hi')
2017-02-25 10:37:05 +00:00
})
2016-09-11 14:22:53 +00:00
})
2016-11-17 21:36:12 +00:00
tap.test('should close the db Connection', async () => {
await testDb.close()
2016-11-17 21:36:12 +00:00
})
2017-06-18 17:52:54 +00:00
tap.start()