Giter VIP home page Giter VIP logo

mongoose-fixtures's Introduction

mongoose-fixtures

Simple fixture loader for Mongoose on NodeJS.

Fixtures can be in one file, or divided up into separate files for organisation (e.g. one file per model)

The fixture files must export objects which are keyed by the Mongoose model name, each containing the data for documents within that.

NOTE: Loading fixtures will clear the existing contents of a collection!

FOR EXAMPLE: With the file below, 3 documents will be inserted into the 'User' collection and 2 into the 'Business' collection:

//fixtures.js
exports.User = [
    { name: 'Gob' },
    { name: 'Buster' },
    { name: 'Steve Holt' }
];

exports.Business = [
    { name: 'The Banana Stand' },
    { name: 'Bluth Homes' }
];

You can also load fixtures as an object where each document is keyed, in case you want to reference another document:

//users.js
var ObjectId = require('mongodb').BSONNative.ObjectID;

exports.User = {
    user1: {
        _id: new ObjectId(),
        name: 'Michael'
    },
    user2: {
        _id: new ObjectId(),
        name: 'George Michael',
        father: exports.User.user1._id
    }
}

Usage

var fixtures = require('pow-mongoose-fixtures');

//Objects
fixtures.load({
    User: [
        { name: 'Maeby' },
        { name: 'George Michael' }
    ]
});

//Files
fixtures.load(__dirname + '/fixtures/users.js', callback);

//Directories (loads all files in the directory)
fixtures.load(__dirname + '/fixtures', callback);

Installation

npm install pow-mongoose-fixtures --save-dev

mongoose-fixtures's People

Contributors

baugarten avatar capaj avatar evdb avatar frully avatar jacques-bernier avatar jimpo avatar powmedia avatar ryonlife avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

mongoose-fixtures's Issues

2dsphere not dropped

I removed a '2dsphere' index from my Mongoose schema, and ran 'load()' again, but it did not drop the index. I expected indexes to also be dropped and recreated as part of fixture resetting.

I was able to workaround the issue by manually dropping the index in question.

Other indexes besides 2dsphere indexes may be affected.

db parameter of the load function is required

I might be wrong but it appears that if you use load('/path/to/directory') without specifying the db parameter, it does not work. After having a look at the code base, it seems that if you don't specify it, the db parameter is never set.

How to use this module in multiple database with mongoose?

I have multiple database in a SINGLE NodeJS project.

example:

var conn      = mongoose.createConnection('mongodb://localhost/testA');
var conn2     = mongoose.createConnection('mongodb://localhost/testB');

// stored in 'testA' database
var ModelA    = conn.model('Model', new mongoose.Schema({
  title : { type : String, default : 'model in testA database' }
}));

// stored in 'testB' database
var ModelB    = conn2.model('Model', new mongoose.Schema({
  title : { type : String, default : 'model in testB database' }
}));

Then, I found this project get connection from mongoose.connection, but I don't set that. How to reslove?

I try to

fixtures.load(data, conn);

but failed.

TypeError: Cannot read property 'model' of undefined

hello,
I'm trying to load a file, and I get:

  TypeError: Cannot read property 'model' of undefined

I'm using pow-mongoose-fixtures 0.3.0.

It turns out that on line 18:

 if (typeof db === 'function') {
    callback = db;
    db = mongoose.connection;
}

this block is never called if I don't specified other that 1 parameters to load method.

typeof db in my case is always "undefined".

Is that because I'm using typescript?

Errors when inserting aren't propogated to the callback

I have a model that performs a check in the "pre-save" hook, and calls back with an error if the check fails. That error never makes it out of the "insertObject" code, but the objects aren't getting successfully inserted so the insertObject callback is never called and the code reliably times out in my tests.

There are two npm modules for this repository

I was experiencing an issue so I started by reading the code in this repository. It seemed like everything should work.

It turns out that I was using the mongoose-fixtures module when I should have been using the pow-mongoose-fixtures module.

Please deprecate the old module in npm. If possible, point the old module to the new module (not sure if this is doable).

paths on windows (and dev support in general)

Hi,

On line 30 of mongoose_fixtures.js there's OS specific code:

    //Get the absolute dir path if a relative path was given
    if (data.substr(0, 1) !== '/') {

I can't really use full, valid paths in Windows since the drive letter would be duplicated (e.g. c:\c:\test\fixture.js).

I wanted to send a pull request, but I couldn't run the existing tests, as npm install tries to clone "git://github.com/OpenifyIt/commons.git" which doesn't seem to exist (am I missing something in the setup, or is it outdated?).

Thanks.

Tests problem

Hello,

I did a fork and implement a plan to use the glob function (node-glob), which lets you filter with a regular expression the files / folders

But the tests are not passing here in my environment, I am using Mac OS X 10.8.3, node js v0.10.3, mongodb 2.2.0

So I could test, I changed the function "before" test, looked like this:

    before(function(done){
        mongoose.connect(process.env.MONGODB_URL);
        require('./models/country.coffee');
        done();

        // this.mongooseInitializer = new MongooseInitializer(process.env.MONGODB_URL, path.join(__dirname, './models'));

        // var functions = [
        //     this.mongooseInitializer.openConnection,
        //     this.mongooseInitializer.loadModels
        // ];
        // async.series(functions, done);
    });

It seems that the problem here in my environment is that the flow asynchronous this a problem with async.series, but I have not found

thank you
hugs

Fixtures not loading

Hi,

I have a simple test.coffee file:

fixtures    = require('pow-mongoose-fixtures')

API       = new (require(__dirname + '/../server'))
api       = supertest(API.app)

fixtures.load(__dirname + '/fixtures/users.js')

And my users.coffee:

exports.Users =
    user1: {
        firstName: 'John'
        lastName: 'Doe'
    }

The server is running my server (no kidding). Now, I don't get why the fixtures are not loading. I don't have any error but it doesn't clear the collection nor populate it.

Any idea? An example implementation?

NPM

Can you push latest github version to NPM?

Fails with capped collections

When there is a capped collection in the project, it is impossible to call remove() on it (MongoError, only drop() works).
But mongoose_fixtures.js does exactly that.
So everything dies.

I suggest to add a clause for isCapped() -> true which would clone -> drop -> rename collection as a safe way of cleaning.

pass inserted items to callback?

Is it possible to pass the newly inserted fixtures to your callback, so you don't have to query your database in your tests? I'm thinking something along the lines of:

// fixtures/user.js
module.exports.User = {
  admin: { email:'[email protected]' , ... },
  user_1: { email:'[email protected]', ... },
  user_2: { email:'[email protected]', ... }
}

// tests.js
setup(function(done){
  fixtures.load('./users.js', function(users){
    self.users = users;
    done(); 
  })
});

test("Example Test", function(done){
  app.login(self.users.user_1);
  // test something here
  done();
});

If this is something you're interested in, I'd be happy to contribute a patch. Otherwise I'll probably just wrap your fixture module in my own module to accomplish the same thing in my project.

Path gets broken on Windows

On Windows, path given to Fixtures.load() gets resolved to C:\C:\... wich leads to Error: ENOENT (it can not be found).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.