Giter Site home page Giter Site logo

sqlfixture's Introduction

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
      }
    ]
  }
]

sqlfixture's People

Contributors

cjsaylor avatar

Stargazers

Juan Basso avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.