testfixtures

package module
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2016 License: MIT Imports: 10 Imported by: 0

README

Go Test Fixtures

GoDoc Build Status Go Report Card

Warning: this package will wipe the database data before loading the fixtures! It is supposed to be used on a test database. Please, double check if you are running it against the correct database.

Writing tests is hard, even more when you have to deal with an SQL database. This package aims to make writing functional tests for web apps written in Go easier.

Basically this package mimics the "Rails' way" of writing tests for database applications, where sample data is kept in fixtures files. Before the execution of every test, the test database is cleaned and the fixture data is loaded into the database.

The idea is running tests against a real database, instead of relying in mocks, which is boring to setup and may lead to production bugs not to being catch in the tests.

Installation

First, get it:

go get gopkg.in/testfixtures.v1/...

Usage

Create a folder for the fixture files. Each file should contain data for a single table and have the name <table-name>.yml:

myapp
  - myapp.go
  - myapp_test.go
  - ...
  - fixtures:
    - posts.yml
    - comments.yml
    - tags.yml
    - posts_tags.yml
    - ...

The file would look like this (it can have as many record you want):

# comments.yml
-
    id: 1
    post_id: 1
    content: This post is awesome!
    author_name: John Doe
    author_email: john@doe.com
    created_at: 2016-01-01 12:30:12
    updated_at: 2016-01-01 12:30:12

-
    id: 2
    post_id: 2
    content: Are you kidding me?
    author_name: John Doe
    author_email: john@doe.com
    created_at: 2016-01-01 12:30:12
    updated_at: 2016-01-01 12:30:12

# ...

Your tests would look like this:

package myapp

import (
    "database/sql"
    "log"

    _ "github.com/lib/pq"
    "gopkg.in/testfixtures.v1"
)

const FixturesPath = "testdata/fixtures"

func TestMain(m *testing.M) {
    // Open connection with the test database.
    // Do NOT import fixtures in a production database!
    // Existing data would be deleted
    db, err := sql.Open("postgres", "dbname=myapp_test")
    if err != nil {
        log.Fatal(err)
    }

    os.Exit(m.Run())
}

func prepareTestDatabase() {
    // see about all compatible databases in this page below
    err := testfixtures.LoadFixtures(FixturesPath, db, &testfixtures.PostgreSQLHelper{})
    if err != nil {
        log.Fatal(err)
    }
}

func TestX(t *testing.T) {
    prepareTestDatabase()
    // your test here ...
}

func TestY(t *testing.T) {
    prepareTestDatabase()
    // your test here ...
}

func TestZ(t *testing.T) {
    prepareTestDatabase()
    // your test here ...
}

Alternatively, you can use the LoadFixtureFiles function, to specify which files you want to load into the database:

err = testfixtures.LoadFixtureFiles(db, &PostgreSQLHelper{},
	"fixtures/orders.yml", "fixtures/customers.yml") // add as many files you want
if err != nil {
	log.Fatal(err)
}

Security check

In order to prevent you from accidentally wiping the wrong database, the fixture load will refuse to load if the database name (or database filename for SQLite) doesn't contains "test". If you want to disable this check, use:

testfixtures.SkipDatabaseNameCheck(true)

Sequences

For PostgreSQL or Oracle, this package also resets all sequences to a high number to prevent duplicated primary keys while running the tests. The default is 10000, but you can change that with:

testfixtures.ResetSequencesTo(10000)

Compatible databases

PostgreSQL

This package has two approaches to disable foreign keys while importing fixtures in PostgreSQL databases:

With DISABLE TRIGGER

This is the default approach. For that use:

&testfixtures.PostgreSQLHelper{}

With the above snippet this package will use DISABLE TRIGGER to temporarily disabling foreign key constraints while loading fixtures. This work with any version of PostgreSQL, but it is required to be connected in the database as a SUPERUSER. You can make a PostgreSQL user a SUPERUSER with:

ALTER USER your_user SUPERUSER;
With ALTER CONSTRAINT

This approach don't require to be connected as a SUPERUSER, but only work with PostgreSQL versions >= 9.4. Try this if you are getting foreign key violation errors with the previous approach. It is as simple as using:

&testfixtures.PostgreSQLHelper{UseAlterConstraint: true}
MySQL

Just make sure the connection string have the multistatement parameter set to true, and use:

&testfixtures.MySQLHelper{}
SQLite

SQLite is also supported. It is recommended to create foreign keys as DEFERRABLE (the default) to prevent problems. See more on the SQLite documentation. (Foreign key constraints are no-op by default on SQLite, but enabling it is recommended).

&testfixtures.SQLiteHelper{}
Microsoft SQL Server

SQL Server support requires SQL Server >= 2008. Inserting on IDENTITY columns are handled as well. Just make sure you are logged in with a user with ALTER TABLE permission.

&testfixtures.SQLServerHelper{}
Oracle

Oracle is supported as well. Use:

&testfixtures.OracleHelper{}

Contributing

Tests were written to ensure everything work as expected. You can run the tests with:

# running tests for PostgreSQL
go test -tags postgresql

# running test for MySQL
go test -tags mysql

# running tests for SQLite
go test -tags sqlite

# running tests for SQL Server
go test -tags sqlserver

# running tests for Oracle
go test -tags oracle

# running test for multiple databases at once
go test -tags 'sqlite postgresql mysql'

Travis runs tests for PostgreSQL, MySQL and SQLite.

To set the connection string of tests for each database, edit the .env file, but do not include the changes a in pull request.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrWrongCastNotAMap is returned when a map is not a map[interface{}]interface{}
	ErrWrongCastNotAMap = fmt.Errorf("Could not cast record: not a map[interface{}]interface{}")

	// ErrFileIsNotSliceOrMap is returned the the fixture file is not a slice or map.
	ErrFileIsNotSliceOrMap = fmt.Errorf("The fixture file is not a slice or map")

	// ErrKeyIsNotString is returned when a record is not of type string
	ErrKeyIsNotString = fmt.Errorf("Record map key is not string")
)

Functions

func LoadFixtureFiles added in v1.6.0

func LoadFixtureFiles(db *sql.DB, h DataBaseHelper, files ...string) error

LoadFixtureFiles load all specified fixtures files into database:

LoadFixtureFiles(db, &PostgreSQLHelper{},
	"fixtures/customers.yml", "fixtures/orders.yml")
	// add as many files you want

func LoadFixtures

func LoadFixtures(foldername string, db *sql.DB, h DataBaseHelper) error

LoadFixtures loads all fixtures in a given folder into the database:

LoadFixtures("myfixturesfolder", db, &PostgreSQLHelper{})

func ResetSequencesTo added in v1.5.0

func ResetSequencesTo(value int64)

ResetSequencesTo sets the value the sequences will be reset to. This is used by PostgreSQL and Oracle. Defaults to 10000.

func SkipDatabaseNameCheck added in v1.3.2

func SkipDatabaseNameCheck(value bool)

SkipDatabaseNameCheck If true, loading fixtures will not check if the database name constaint "test". Use with caution!

Types

type DataBaseHelper

type DataBaseHelper interface {
	// contains filtered or unexported methods
}

DataBaseHelper is the generic interface for the database helper

type MySQLHelper added in v1.1.0

type MySQLHelper struct{}

MySQLHelper is the MySQL helper for this package

type OracleHelper added in v1.5.0

type OracleHelper struct{}

OracleHelper is the Oracle database helper for this package

type PostgreSQLHelper

type PostgreSQLHelper struct {
	// UseAlterConstraint If true, the contraint disabling will do
	// using ALTER CONTRAINT sintax, only allowed in PG >= 9.4.
	// If false, the constraint disabling will use DISABLE TRIGGER ALL,
	// which requires SUPERUSER privileges.
	UseAlterConstraint bool
}

PostgreSQLHelper is the PG helper for this package

type SQLServerHelper added in v1.4.0

type SQLServerHelper struct{}

SQLServerHelper is the helper for SQL Server for this package. SQL Server >= 2008 is required.

type SQLiteHelper added in v1.3.0

type SQLiteHelper struct{}

SQLiteHelper is the SQLite Helper for this package

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL