0.3.1 • Published 3 years ago

mongo-assert v0.3.1

Weekly downloads
55
License
MIT
Repository
github
Last release
3 years ago

mongo-assert

Mongo assertion library.

Build Status Code Coverage npm version

Why

This library is created to assert made changes in mongodb. Useful when you want to be sure that method added, updated or deleted only one document, and other were unchanged.

Install

$ npm i -D mongo-assert

Usage

const nassert = require('n-assert')
const mongoassert = require('mongo-assert')

const initialUsers = [
  {
    _id: nassert.getObjectId(),
    email: 'pen@mail.com',
    firstName: 'Piter',
    lastName: 'Pen'
  },
  {
    _id: nassert.getObjectId(),
    email: 'smith@mail.com',
    firstName: 'John',
    lastName: 'Smith'
  }
]

it('should update user', async () => {
  let filter = { _id: initialUsers[0]._id }
  let userData = {
      email: 'smith-another-email@mail.com'
  }
  let updatedUser = {
    _id: initialUsers[0]._id,
    email: 'smith-another-email@mail.com'
  }

  await User.create(initialUsers)
  await usersSrvc.updateUser({ filter, userData })

  await mongoassert.assertCollection({
    model: User,
    initialDocs: initialUsers,
    changedDoc: updatedUser,
    typeOfChange: updatedUser ? 'updated' : null,
    sortField: '_id'
  })
})

API

  • assertCollection({ model, initialDocs, changedDoc, typeOfChange, sortField }) Asserts mongodb collection. Loads all documents in the collection, merges initial collection with changed document and asserts.

    • model - mongoose model.
    • initialDocs - initial documents collection.
    • changedDoc - changed document, must be omitted or undefined if collection is unchanged.
    • typeOfChange - the type of the change (created, updated, deleted), must be omitted if collection is unchanged.
    • sortField - the field which should be used for sorting actual and expected collections before asseting.

Licence

Licensed under the MIT license.

Author

Alexander Mac