sqlfixture

package module
v0.0.0-...-cdbaf32 Latest Latest
Warning

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

Go to latest
Published: May 26, 2017 License: MIT Imports: 5 Imported by: 0

README

sqlfixture

GoDoc Build Status

sqlfixture is a go library that enables simple pre-populating a MySQL database with data to be used during testing.

Fixtures are supported via:

  • Native go code
  • json files
  • yaml files

Example

This example will truncate my_table and my_other_table and insert data into them before each test is run.

In foo_test.go

package foo_test

import (
	"database/sql"
	"os"
	"testing"

	"github.com/cjsaylor/sqlfixture"
	_ "github.com/go-sql-driver/mysql"
)

func TestMain(m *testing.M) {
	db, _ := sql.Open("mysql", "tcp(localhost:3306)/test")
	err := db.Ping()
	if err != nil {
		panic(err)
	}
	setup(db)
	code := m.Run()
	os.Exit(code)
}

func setup(db *sql.DB) {
	// Setup your table schema here
	fixture := sqlfixture.New(db, sqlfixture.Tables{
		sqlfixture.Table{
			Name: "my_table",
			Rows: sqlfixture.Rows{
				sqlfixture.Row{
					"id": "1",
					"name": "Some value",
					"slug": "some-value",
					"date": "2017-05-15 00:00:00",
				},
			},
		},
		sqlfixture.Table{
			Name: "my_other_table",
			Rows: sqlfixture.Rows{
				sqlfixture.Row{
					"id": "1",
					"item": "Some item",
					"quantity": 9,
				},
				sqlfixture.Row{
					"id": "2",
					"item": "Some other item",
					"quantity": 3,
				},
			},
		},
	})
	fixture.Populate()
}

Installation

go get github.com/cjsaylor/sqlfixture

YAML support

You can import fixtures from YAML files.

We can rewrite the example above:

In foo_test.go

package foo_test

import (
	"database/sql"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"github.com/cjsaylor/sqlfixture"
	_ "github.com/go-sql-driver/mysql"
)

func TestMain(m *testing.M) {
	db, _ := sql.Open("mysql", "tcp(localhost:3306)/test")
	err := db.Ping()
	if err != nil {
		panic(err)
	}
	setup(db)
	code := m.Run()
	os.Exit(code)
}

func setup(db *sql.DB) {
	// Setup your table schema here
	filename, _ := filepath.Abs("./fixtures/test.yaml")
	yamlFile, err := ioutil.ReadFile(filename)
	if err != nil {
		t.Error("Unable to find fixture file.")
		return
	}
	fixture := sqlfixture.FromYAML(db, yamlFile)
	fixture.Populate()
}

in fixtures/test.yaml

- name: my_table
  rows:
    - id: 1
      name: "some value"
      slug: "some-value"
      date: "2017-05-15 00:00:00"
- name: my_other_table
  rows:
    - id: 1
      item: some item
      quantity: 9
    - id: 2
      item: some other item
      quantity: 3

JSON Support

Similar to the YAML support, sqlfixture also support importing data via JSON:

foo_test.go

package foo_test

import (
	"database/sql"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"github.com/cjsaylor/sqlfixture"
	_ "github.com/go-sql-driver/mysql"
)

func TestMain(m *testing.M) {
	db, _ := sql.Open("mysql", "tcp(localhost:3306)/test")
	err := db.Ping()
	if err != nil {
		panic(err)
	}
	setup(db)
	code := m.Run()
	os.Exit(code)
}

func setup(db *sql.DB) {
	// Setup your table schema here
	filename, _ := filepath.Abs("./fixtures/test.json")
	jsonFile, err := ioutil.ReadFile(filename)
	if err != nil {
		t.Error("Unable to find fixture file.")
		return
	}
	fixture := sqlfixture.FromJSON(db, jsonFile)
	fixture.Populate()
}

fixtures/test.json

[
  {
    "name": "my_table",
    "rows": [
      {
        "id": 1,
        "name": "some value",
        "slug": "some-value",
        "date": "2017-05-15 00:00:00"
      }
    ]
  },
  {
    "name": "my_other_table",
    "rows": [
      {
        "id": 1,
        "item": "some item",
        "quantity": 9
      },
      {
        "id": 2,
        "item": "some other item",
        "quantity": 3
      }
    ]
  }
]

Documentation

Overview

Package sqlfixture is a go library that enables pre-populating a MySQL database with data to be used during testing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fixture

type Fixture struct {
	Tables Tables
	// contains filtered or unexported fields
}

Fixture structure to house tables to be populated

func FromJSON

func FromJSON(db *sql.DB, jsonIn []byte) (Fixture, error)

FromJSON allows a fixture to be created from json input

func FromYAML

func FromYAML(db *sql.DB, yamlIn []byte) (Fixture, error)

FromYAML allows a fixture to be created from yaml input

func New

func New(db *sql.DB, tables Tables) Fixture

New fixture instance that will work with the database

func (*Fixture) Populate

func (f *Fixture) Populate()

Populate the database tables within this fixture Warning: This will truncate any and all data for each table in the fixture

type Row

type Row map[string]interface{}

Row represents an arbitrary key-value representation of a sql row to insert.

type Rows

type Rows []Row

Rows represents a collection of Row

type Table

type Table struct {
	Name string `json:"name" yaml:"name"`
	Rows Rows   `json:"rows" yaml:"rows"`
}

Table represents a database table with its correspoding rows to insert

type Tables

type Tables []Table

Tables represents a collection of Table

Jump to

Keyboard shortcuts

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