项目作者: AlexanderMac

项目描述 :
Mongo assertion library
高级语言: JavaScript
项目地址: git://github.com/AlexanderMac/mongo-assert.git
创建时间: 2019-04-04T07:49:52Z
项目社区:https://github.com/AlexanderMac/mongo-assert

开源协议:MIT License

下载


mongo-assert

WARNING :warning:: This repo is not maintained anymore.

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

  1. $ npm i -D mongo-assert

Usage

  1. const nassert = require('n-assert')
  2. const mongoassert = require('mongo-assert')
  3. const initialUsers = [
  4. {
  5. _id: nassert.getObjectId(),
  6. email: 'pen@mail.com',
  7. firstName: 'Piter',
  8. lastName: 'Pen'
  9. },
  10. {
  11. _id: nassert.getObjectId(),
  12. email: 'smith@mail.com',
  13. firstName: 'John',
  14. lastName: 'Smith'
  15. }
  16. ]
  17. it('should update user', async () => {
  18. let filter = { _id: initialUsers[0]._id }
  19. let userData = {
  20. email: 'smith-another-email@mail.com'
  21. }
  22. let updatedUser = {
  23. _id: initialUsers[0]._id,
  24. email: 'smith-another-email@mail.com'
  25. }
  26. await User.create(initialUsers)
  27. await usersSrvc.updateUser({ filter, userData })
  28. await mongoassert.assertCollection({
  29. model: User,
  30. initialDocs: initialUsers,
  31. changedDoc: updatedUser,
  32. typeOfChange: updatedUser ? 'updated' : null,
  33. sortField: '_id'
  34. })
  35. })

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