@natlibfi/fixura-mongo

    1.2.2 • Public • Published

    Test fixtures with MongoDB is as easy as ABC NPM Version Build Status Test Coverage

    Test fixtures with MongoDB is as easy as ABC with Fixura.

    Usage

    ES modules

    import mongoFixturesFactory from '@natlibfi/fixura-mongo';
    const mongoFixtures = await fixturesFactory({rootPath: [__dirname, '...', 'test-fixtures']});
    await mongoFixtures.populate(['dbContents.json']);
    await mongoFixtures.dump();

    Node.js require

    import {default: mongoFixturesFactory} from '@natlibfi/fixura-mongo';
    const mongoFixtures = await fixturesFactory({rootPath: [__dirname, '...', 'test-fixtures']});
    await mongoFixtures.populate(['dbContents.json']);
    await mongoFixtures.dump();

    Configuration

    Factory parameters

    • rootPath: An array of directory names to construct the path to the test fixtures directory
    • useObjectId: A boolean indicating whether _id property in documents should be cast as Mongo ObjectId. Defaults to false.
    • format: An object of collection name - document property mapping with formatting functions as values (See examples below)
    • gridFS: An optional parameter which enables using gridFS functions. Can be boolean or an object:
      • bucketName: The name of the gridFS bucket to create (Optional)

    Mongo instance

    By default, fixura-mongo uses mongodb-memory-server. For using an externally started Mongo, set environment variable MONGO_TEST_URI.

    Usage

    Functions

    All functions are asynchronous.

    populate

    Populate the database with test data. Accepts the data as a string or an array which defines the path to the fixture file (See [fixura]https://www.npmjs.com/package/@natlibfi/fixura).

    The format of the fixture is as follows:

    {
        "foo": [{"fubar": "bar"}]
    }

    The object properties are collection names and their value is an array of documents to insert into the collection.

    dump

    Dumps the database in the same format as the fixture.

    clear

    Clears the database.

    close

    Clears and stops the database (Stopping is only applicable to mongodb-memory-server),

    getConnectionString

    Returns the URI of the server. Used for connecting clients to the database.

    populateFiles (GridFS)

    Populates the database with files using GridFS. The format is as follows:

    {
        "foo": "bar"
    }

    Where object properties are filenames and their values their content. This alternative format provides the path to file contents instead of defining it inline:

    {
        "foo": ["bar", "content.txt"]
    }

    The path is then resolved using the root directory defined in the factory function (rootPath).

    dumpFiles (GridFS)

    Dumps the files from the database. The format is an object with filenames as properties and their values are Readable streams. Passing true as the sole function arguments returns file contents as property values. Like so:

    const data = await mongoFixtures.dumpFiles(true);
    typeof data === 'string'
    // true

    clearFiles (GridFS)

    Examples

    Using the format parameter:

    import mongoFixturesFactory from '@natlibfi/fixura-mongo';
    
    const mongoFixtures = await fixturesFactory({
        rootPath: [__dirname, '...', 'test-fixtures'],
        format: {
            foo: {
                bar: v => new Date(v)
            }
        }
    });
    
    // Populates the database and formats the 'bar'-properties in documents of the foo'-collection as Dates
    await mongoFixtures.populate(['dbContents.json']);

    Removes all files from the database

    License and copyright

    Copyright (c) 2019 University Of Helsinki (The National Library Of Finland)

    This project's source code is licensed under the terms of GNU Lesser General Public License Version 3 or any later version.

    Install

    npm i @natlibfi/fixura-mongo

    DownloadsWeekly Downloads

    18

    Version

    1.2.2

    License

    LGPL-3.0+

    Unpacked Size

    106 kB

    Total Files

    29

    Last publish

    Collaborators

    • natlibfi-jonollil
    • drone-melinda