Skip to content

Commit

Permalink
Increase coverage by adding tests for eval, drop database, db stats, …
Browse files Browse the repository at this point in the history
…create and drop users
  • Loading branch information
saintedlama committed Oct 13, 2015
1 parent f49c38e commit 37c690b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/test-db-stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var insert = require('./insert')

insert('db stats', [{
name: 'Squirtle', type: 'water'
}, {
name: 'Starmie', type: 'water'
}, {
name: 'Charmander', type: 'fire'
}, {
name: 'Lapras', type: 'water'
}], function (db, t) {
db.stats(function (err, stats) {
t.error(err, 'Should get stats without an error')
t.ok(stats.ok, 'OK flag should be set in result')
t.ok(stats.collections)
t.ok(stats.indexes)

t.end()
})
})
14 changes: 14 additions & 0 deletions test/test-drop-database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var test = require('./tape')
var mongojs = require('../index')
var db = mongojs('test', ['a'])

test('dropDatabase', function (t) {
db.dropDatabase(function (err) {
t.error(err, 'Should drop a connected database without an error')

db.close(function (err) {
t.error(err, 'Should close a connection to a dropped database')
t.end()
})
})
})
23 changes: 23 additions & 0 deletions test/test-eval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var test = require('./tape')
var mongojs = require('../index')
var db = mongojs('test', ['a'])

test('eval', function (t) {
var sum = function (x, y) {
return x + y
}

var failing = function () {
throw new Error('Does not work')
}

db.eval(sum, 1, 2, function (err, res) {
t.error(err, 'Should eval a function without an error')
t.equal(res, 3, 'Should eval sum function')

db.eval(failing, function (err) {
t.ok(err, 'Should error when eval a function with missing operand is called')
t.end()
})
})
})
30 changes: 30 additions & 0 deletions test/test-user-create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var test = require('./tape')
var mongojs = require('../index')
var db = mongojs('test', ['a'])

test('create user', function (t) {
// Ignore errors when dropping the user
db.dropUser('mongojs', function () {
db.createUser({
user: 'mongojs',
pwd: 'topsecret',
customData: { department: 'area51' },
roles: ['readWrite']
}, function (err, res) {
t.error(err, 'Should create a user without an error')
t.ok(res.ok)

db.createUser({
user: 'mongojs',
pwd: 'topsecret',
customData: { department: 'area51' },
roles: ['readWrite']
}, function (err) {
t.ok(err, 'Should yield an error when creating a duplicate user')
t.equal(err.code, 11000, 'Should yield a duplicate user error')

t.end()
})
})
})
})
25 changes: 25 additions & 0 deletions test/test-user-drop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var test = require('./tape')
var mongojs = require('../index')
var db = mongojs('test', ['a'])

test('drop user', function (t) {
// Ignore errors when dropping the user
db.dropUser('mongojs', function () {
db.createUser({
user: 'mongojs',
pwd: 'topsecret',
customData: { department: 'area51' },
roles: ['readWrite']
}, function (err, res) {
t.error(err, 'Should create a user without an error')
t.ok(res.ok)

db.dropUser('mongojs', function (err, res) {
t.error(err, 'Should drop an existing user without an error')
t.ok(res.ok)

t.end()
})
})
})
})

0 comments on commit 37c690b

Please sign in to comment.