Skip to content

tomi77/node-bookshelf-fixture-loader

Repository files navigation

bookshelf-fixture-loader

Bookshelf fixtures loader

Build Status Coverage Status Code Climate dependencies Status devDependencies Status peerDependencies Status Downloads

Installation

npm install bookshelf-fixture-loader --save-dev

Usage

Fixture file format is inspired by Django fixture file format.

fixtures/test.yaml

- id: 2
  model: 'Test'
  fields:
    name: 'test 2'

fixtures/test.json

[
  {
    "id": 1,
    "model": "Test",
    "fields": {
      "name": "test 1"
    }
  }
]

In test file:

var knex = require('knex')({
  client: 'sqlite3',
  connection: {
    filename: ':memory:'
  },
  useNullAsDefault: true
});

var bookshelf = require('bookshelf')(knex);
bookshelf.plugin('registry');

var FixtureLoader = require('bookshelf-fixture-loader')(bookshelf, __dirname);
var fixtures = new FixtureLoader();
var Model = bookshelf.model('Test');

describe('FixtureLoader', function() {
  it('should load json file', function() {
    fixtures.load('test.json')
    .insert()
    .then(function() {
      return Model.forge({id: 1}).fetch();
    })
    .then(function(row) {
      row.get('name').should.equal('test 1');
    });
  });
});

or define absolute path:

new FixtureLoader('/path/to/fixtures/test.yaml');