0.1.1 • Published 6 years ago

seqfixloader v0.1.1

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

SeqFixLoader

A YAML "Fixture"-Loader for the Sequilize ORM.

Reasoning

Like writing Integration-Tests? Yeah? Then you may like this tool.

With this Fixture-Loader it gets pretty easy to write dedicated Data for tests and load them in your Mocha-Rundown. Test a piece of code and clean up afterwards or at the beginning of the next test.

On a further note: You may want to virtualize your Project and create a Vagrant Box. For the necessary "base"-hydration of the DB you could use this tool.

Or you could write a script that dumps your existing data to "YAML"-Dumps and load them locally in your dev-env.

Getting Started

Prerequisites

  • Sequilize: ^~4.X

Installing

npm install seqfixloader --save-dev

Setup

For the FixtureLoader to work you need to provide a Sequilize-Instance with the registered Models and your DB-Config. For this you have to edit the provided config.yml in the SRC-Dir of the module-package

As an example:

/src/config.yaml
# You can rename or move the folder within the lib structure
LoaderDir: "./Loader"
FixtureDir: "./Fixture"
# Sequilize Instance which exports an instance with the registered Models
sequilize: "../test/orm.js"
method: "instance"

The "method" provides the actual sequlize instance and should be edited as necessary (it may not be called "instance" in your case)

Using the Fixture Loader

Now you're good to go. Let's assume that you use Mocha.js and Chai for assertion.

Create a YAML-File with your Fixture, like:

# Loader: test.js

- user:
    name: "Test String"
    role: "admin"
    createdAt: "2017-12-12 12:32:23"
    updatedAt: "2017-12-12 12:32:23"

It's is important that the sequilize-model is called the same. In this case: "user".

Create now a Loader for this Fixture-Group. I. e. userFixtureLoader.js

// Fixture Loader for the User Entity

const moment = require('moment');

function supports()
{
    return 'user'
}

function getDefaults()
{
    return {
        name: null,
        role: null,
        createdAt: moment().subtract(1, 'year').format('YYYY-MM-DD HH:mm:ss'),
        updatedAt: moment().format('YYYY-MM-DD HH:mm:ss')
    }
}

module.exports = {
    supports: supports(),
    getDefaults: getDefaults()
};

A Loader must have the methods: Supports and getDefaults. The Loader is in charge to "normalize" your Fixture to the Model. That means that the "getDeaults" should return the model structure with default values. This saves some time.

The Loader must be named: "entity" (i. e. what the loader Supports) followed by "FixtureLoader". Just have look at the package. There's an example Loader and Fixture.

You could write a test now as follows:

const FixtureLoader = require('seqfixloader');

describe('Something Something', function () {
    it('Should throw an error due to a wrong filepath', function () {
        new FixtureLoader().loadFixture(path.resolve(__dirname, '../src/Fixture/thisIsBullshit.yaml')).catch(output => {
            expect(output).to.be.instanceOf(Error)
        });
    });
});

or if yout want to clean the DB before the test:

const FixtureLoader = require('seqfixloader');
let instance = new FixtureLoader();

instance.cleaner().then(output => {
    instance.loadFixtures();
}).catch(error => {
    console.log(error)
});

Methods

  • loadFixtures(): Loads all fixtures in the configured Folder i. e. '/Fixture'
  • loadFixture(): Loads the File from the provided File-Path
  • cleaner(): Guess what it does?

Dependencies

  • js-yaml: ^3.10.0
  • commander: ^2.12.2