Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

dbfixtures-mysql-driver

PedroHenriques206MIT1.2.0TypeScript support: included

A MySQL driver for the NPM dbfixtures package.

db fixtures, test fixtures, fixtures

readme

Build Status

Fixtures Manager MySQL Driver

An abstraction layer for the mysql package to facilitate handling database fixtures for testing purposes, in a MySQL database. This package is ment to be used in conjunction with the dbfixtures package, but can also be used by itself.

Installation

npm install --save-dev dbfixtures-mysql-driver

NodeJS versions

  • Package version >=1.1.* supports NodeJS v8 or higher.
    NOTE: For versions 8 and 9 the node flag --harmony_promise_finally is required

  • Package version 1.0.* supports NodeJS v7 or higher.

Usage

This package exposes the create(connectionOptions: string | ConnectionConfig): Promise<IDriver> function that returns a Promise that resolves with an instance of the driver. Note: For detailed information about the connectionOptions argument, please consult these connection options.

An instance of the driver exposes the following interface

// truncates the tables with the supplied names, ignoring foreign key constraints
truncate: (tableNames: string[]) => Promise<void>

// inserts the supplied rows into the specified table, respecting foreign key constraints
insertFixtures: (tableName: string, fixtures: [{}]) => Promise<void>

// terminates the connection to the database
close: () => Promise<void>

Example

This example uses Mocha as the potential test runner.

const dbfixtures = require('dbfixtures');
const fixturesMysqlDriver = require('dbfixtures-mysql-driver');

const mysqlConnectionInfo = {
  host: 'localhost',
  port: '3306',
  user: 'root',
  password: '',
  database: 'fixtures_test',
};
const fixtures = {
  'roles': [
    { id: 1, name: 'role 1' },
    { id: 2, name: 'role 2' },
  ],
  'users': [
    { id: 1, email: 'myemail@test.net', role_id: 2 },
    { id: 2, email: 'test@gmail.com', role_id: 1 },
    { id: 3, email: 'another@email.org', role_id: 1 },
  ],
};

describe('fixtures example', function () {
  before(async function () {
    const mysqlDriver = await fixturesMysqlDriver.create(mysqlConnectionInfo);
    dbfixtures.setDrivers(mysqlDriver);
  });

  after(async function () {
    await dbfixtures.closeDrivers();
  });

  beforeEach(async function () {
    await dbfixtures.insertFixtures(fixtures);
  });

  it('should have the database seeded with the fixtures', function () {
    // ...
  });
});

Testing This Package

  • cd into the package's directory

  • run npm install

  • for unit tests run npm test -- test\unit\

  • for integration tests run npm test -- test\integration\
    NOTE: requires an active MySQL server available through localhost:3306 and with the expected setup (see below)

  • for end-to-end tests run npm test -- test\e2e\
    NOTE: requires an active MySQL server available through localhost:3306 and with the expected setup (see below)

Required database setup

The integration and end-to-end tests the database server needs to have an initial setup that creates the database and tables used in the tests.
A shell script is provided in the test/db_setup/ directory with the commands that need to be executed in the database server to correctly set it up for the tests.

Suggestion to setting up a MySQL server on your local machine

If you are using Docker, you can run the CLI command docker run --name testmysql -p 127.0.0.1:3306:3306/tcp -e MYSQL_DATABASE=fixtures_test -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -v absolute-path-to-project/test/db_setup/:/docker-entrypoint-initdb.d/ -d mariadb:10 to raise a container with the MariaDB v10.* image and make it available through localhost:3306.

NOTE: Replace absolute-path-to-project with the absolute path to the root of the repository clone in your machine.

changelog

Changelog

[1.2.0] - 2019-09-14

Changed

  • Updated NPM dependencies to latest versions
  • Updated E2E test suite with new interface of the main package "dbfixtures"
  • Updated readme with new interface of the main package "dbfixtures" in the usage example

Removed

  • Deprecated linter configuration option

[1.1.0] - 2019-01-12

Added

  • Information about supported NodeJS versions and how to setup a docker container to run the automated tests for this repository to the Readme file
  • End-to-end test covering the interaction of this driver with the core package dbfixtures
  • Shell script with commands to setup the database for running the integration and end-to-end tests

Changed

  • Minimum supported version of NodeJS increased to 8
  • Minor refactoring of the code and unit tests

[1.0.2] - 2018-02-09

Fixed

  • Fixed type definition for IDriver, replacing [{}] with {}[].
  • Added missing package-lock.json file.

[1.0.1] - 2018-01-20

Changed

  • Moved the "mysql" dependency from the devDependencies to dependencies in the package.json.

[1.0.0] - 2018-01-20

Added

  • First version of the code