node-populator-mongodb

0.0.3 • Public • Published

Node Populator for MongoDB

A simple library used to populate a MongoDB database with native driver. Usefull for unit tests.

How to install ?

npm install node-populator-mongodb

Example usage

Create testing data

'use strict;';
 
var ObjectID = require('mongodb').ObjectID;
var DBRef = require('mongodb').DBRef;
 
var getDBRef = function(collectionName, id) {
    return new DBRef(collectionName, id, 'populator_test');
};
 
module.exports = [
        {
            _id: new ObjectID("200000000000000000000001"), 
            name: "Project 1", 
            customer: getDBRef("customer", "300000000000000000000001") 
        },
        {
            _id: new ObjectID("200000000000000000000002"), 
            name: "Project 2", 
            customer: getDBRef("customer", "300000000000000000000002") 
        }
    ];
'use strict;';
 
var ObjectID = require('mongodb').ObjectID;
var DBRef = require('mongodb').DBRef;
 
var getDBRef = function(collectionName, id) {
    return new DBRef(collectionName, id, 'populator_test');
};
 
module.exports = [
        {_id: new ObjectID("300000000000000000000001"), name: "Customer 1" },
        {_id: new ObjectID("300000000000000000000002"), name: "Customer 2" }
    ];
  • Each exported key (as customer, project) is corresponding to a MongoDB collection.
  • In each collection, you must define an array containing wished records.

Create unit test

  • We'll use mocha to run unit tests.
'use strict;';
 
var data = require('testing-data'),
    PopulatorMongoDB = require('node-populator-mongodb'),
    CustomerService = require('customer-service');
 
var customerService = new CustomerService();
 
var dbUri = 'mongodb://localhost:27017/populator_test';
 
/**
 * Unit tests to validate GenericDaoMongoose.
 */
describe('Customers test', function () {
 
    /**
     * Before each test, drop the collection then populate.
     */
    beforeEach(function (done) {
        this.timeout(3000);
        var populator = new PopulatorMongoDB(dbUri, data);
        populator.populate(done);
    });
 
    /**
     * Create new object(s).
     */
    describe('create', function () {
        it('create a new customer', function (done) {
            customerService.create({name: 'customer test new 1'}, function (err, result) {
                    assert.equal(err, null);
                    assert.equal(result.name, 'customer test new 1');
                    done(err);
                }
            );
        });
    });
});

Run your tests

mocha

Example in real life

`todo`

Change history

  • v0.0.2 :
    • Fix bug: invalid path of resources.
  • v0.0.1 :
    • Initial version

License

The MIT License (MIT)

Copyright (c) 2016, 9 Février <contact@9fevrier.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package Sidebar

Install

npm i node-populator-mongodb

Weekly Downloads

4

Version

0.0.3

License

MIT

Last publish

Collaborators

  • 9fevrier