Coder Social home page Coder Social logo

mongo-fixture's Introduction

Mongo::Fixture

Build Status Code Climate

Similar to Rails 2 fixtures, but for MongoDB (using the standard mongo connector Gem ).

Show off

Single collection

Assuming you have a fixture for the collection messages with:

# test/fixtures/some_data/messages.yaml
yesterday_afternoon:
  text: Honey, pizza tonight?
  sent: 2012-09-11
today_morning:
  text: Nice date yesterday, how about next week?
  sent: 2012-09-12

You can write a brief ruby script and insert the data into your mongo db:

require 'mongo'
require 'mongo-fixture'

DB = Mongo::Connection.new.db 'messages-db' # An example connetion setup

# Insert the fixture into the database
fixture_some_data = Mongo::Fixture.new :some_data, DB

# You can now query the fixture for the data that was sent into the DB
fixture_some_data.messages.yesterday_afternoon.text # => "Honey, pizza tonight?"

The fixture is identified by the name of the folder containing the fixture YAML files. The default folder is test/fixtures.

As you can see, each record is preceded by a name, in this case yesterday_afternoon and today_morning. This names never get inserted into the database, they are just references for making it easier to access the fixture's information.

Associations

Assuming you have a fixture for the collection users with:

# test/fixtures/simple/users.yaml
john:
  name: John
  last_name: Doe
  email: [email protected]
jane:
  name: Jane
  last_name: Doe
  email: [email protected]

and for messages:

# test/fixtures/simple/messages.yaml
greeting:
  sender:
    users: john
  receiver:
    users: jane
  text: Hi Jane! Long time no see.
long_time:
  sender:
    users: jane
  receiver:
    users: john
  text: John! Long time indeed. How are you doing?

Mongo Fixture will automatically insert the object id (_id field in the database) for the referenced users. The Mongo Fixture syntax specifies that:

  sender:
    users: john

is a reference to the record named john in the collection users.

Currently Mongo Fixture does not support references from one collection to another and back from that collection to the first one.

For example given the ruby script:

# script.rb
require "mongo-fixture"

DB = Mongo::Connection.new.db 'mongo-test-db' # Just a simple example

fixture = Mongo::Fixture.new :simple, DB # Will load all the data in the fixture into the database

fixture.users               # == fixture[:users]
fixture.users.john.name     # => "John"
                            # The YAML files are parsed into a SymbolMatrix
                            # http://github.com/Fetcher/symbolmatrix

fixture.rollback            # returns users and messages to pristine status (#drop)


fixture = Mongo::Fixture.new  :simple, 
                              DB, 
                              :store => false    # The `false` flag prevent the constructor from automatically pushing
                                                 # the fixture into the database
                                                    
fixture.check               # Will fail if the user or messages collection
                            # were already occupied with something
                            
fixture.push                # Inserts the fixture in the database

fixture.rollback            # Don't forget to rollback

...naturally, mongo-fixture makes a lot more sense within some testing framework.

Many to many associations

As is custom in Mongo, a many-to-many association is implemented by passing an array of ids to a field in any of the records to associate. Mongo Fixture supports this. For example:

message:
  sender:
    users: john
  receivers:
    users: [mary, sue, harry, jack]
  text: Meeting tonight guys?

will insert into the receivers field an array with the _ids of the referenced users.

Changing fixtures directory

require 'mongo-fixture'

Mongo::Fixture.path = 'fixtures' # Now fixtures will be required from `fixtures/`

Installation

gem install mongo-fixture

Or using Bundler

gem 'mongo-fixture'

And then execute:

bundle

Future

  • Dump from a database to fixtures! This would be awesome.

License

Copyright (C) 2012 Fetcher

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

mongo-fixture's People

Contributors

xaviervia avatar

Watchers

 avatar  avatar

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.