UNPKG

rest-api-starter

Version:
99 lines (79 loc) 3.06 kB
/** * Created by svinci on 1/13/17. */ 'use strict'; const mocha = require('mocha'); const chai = require('chai'); const describe = mocha.describe; const it = mocha.it; const expect = chai.expect; const db = require('../app/db'); describe('DB', () => { const collectionName = 'some-collection'; const testCase = { 'someKey': 'some-value' }; it('i should be able to store a document in a collection', (done) => { db.insert(testCase, collectionName) .then((inserted) => { testCase._id = inserted._id; }) .then(() => done()) .catch((err) => done(err)); }); it('i should be able to find the inserted document by id', (done) => { db.findById(testCase._id, collectionName) .then((document) => expect(document.someKey).to.equal(testCase.someKey)) .then(() => done()) .catch((err) => done(err)); }); it('i should be able to update the inserted document', (done) => { testCase.someKey = 'new-value'; db.update(testCase._id, testCase, collectionName) .then(() => db.findById(testCase._id, collectionName)) .then((document) => expect(document.someKey).to.equal(testCase.someKey)) .then(() => done()) .catch((err) => done(err)); }); it('i should be able to remove the inserted document', (done) => { db.del(testCase._id, collectionName) .catch((err) => expect(err.name).to.equal('document.not.found')) .then(() => done()) .catch((err) => done(err)); }); }); describe('DB (custom id)', () => { const collectionName = 'some-collection'; const testCase = { '_id': 1, 'someKey': 'some-value' }; it('i should be able to store a document in a collection', (done) => { db.insert(testCase, collectionName) .then((inserted) => { testCase._id = inserted._id; }) .then(() => done()) .catch((err) => done(err)); }); it('i should be able to find the inserted document by id', (done) => { db.findById(testCase._id, collectionName) .then((document) => expect(document.someKey).to.equal(testCase.someKey)) .then(() => done()) .catch((err) => done(err)); }); it('i should be able to update the inserted document', (done) => { testCase.someKey = 'new-value'; db.update(testCase._id, testCase, collectionName) .then(() => db.findById(testCase._id, collectionName)) .then((document) => expect(document.someKey).to.equal(testCase.someKey)) .then(() => done()) .catch((err) => done(err)); }); it('i should be able to remove the inserted document', (done) => { db.del(testCase._id, collectionName) .catch((err) => expect(err.name).to.equal('document.not.found')) .then(() => done()) .catch((err) => done(err)); }); });