node.populator-mongodb

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

npm install node.populator-mongodb
4 downloads in the last week
4 downloads in the last month

node.populator-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 = {
    customer: [
        {_id: new ObjectID("300000000000000000000001"), name: "Customer 1" },
        {_id: new ObjectID("300000000000000000000002"), name: "Customer 2" }
    ],
    project: [
        {
            _id: new ObjectID("200000000000000000000001"), 
            name: "Project 1", 
            customer: getDBRef("customer", "300000000000000000000001") 
        },
        {
            _id: new ObjectID("200000000000000000000002"), 
            name: "Project 2", 
            customer: getDBRef("customer", "300000000000000000000002") 
        }
    ]};
  • 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

See unit tests of node.generic-dao-mongoose project.

# Clone the project...
git clone https://github.com/9fevrier/node.generic-dao-mongoose.git

# Install dependencies...
cd node.generic-dao-mongoose/
npm install

# Then run unit tests...
mocha

# And view the results !
>  .......
> 7 passing (272ms) 
>

License

The MIT License (MIT)

Copyright (c) 2014 9 FĂ©vrier

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.

npm loves you