fixture

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

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

Go to latest
Published: Jul 27, 2020 License: MIT Imports: 8 Imported by: 5

README

go-fixture

GoDoc CircleCI Go Report Card

Management the fixtures of the database for testing.

features

  • Load database fixture from .yaml or .sql file
  • Before cleanup table(.yaml only)
  • Support databases
    • MySQL
    • and more drivers are todo...

usage

  • Import go-fixture and drirver go-fixture/mysql as like database/sql driver
import (
  "database/sql"

  _ "github.com/go-sql-driver/mysql"
  "github.com/takashabe/go-fixture"
  _ "github.com/takashabe/go-fixture/mysql"
)

func main() {
  // omit error handling
  db, _ := sql.Open("mysql", "fixture@/db_fixture")
  f, _ := fixture.NewFixture(db, "mysql")
  f.Load("fixture/setup.yml")
  f.Load("fixture/setup.sql")
}

fixture file format

.yml .yaml
table: foo
record:
  - id: 1
    first_name: foo
    last_name: bar
  - id: 2
    first_name: piyo
    last_name: fuga
.sql
  • Need to add a semicolon at the end of the line
  • If table cleanup is required it will need to be in the fixture file
DELETE FROM person;
INSERT INTO person(id, name) VALUES (1, 'foo');

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFailRegisterDriver = errors.New("failed to register driver")
	ErrFailReadFile       = errors.New("failed to read file")
	ErrInvalidFixture     = errors.New("invalid fixture file format")
	ErrNotFoundDriver     = errors.New("unknown driver(forgotten import?)")
	ErrUnknownFileExt     = errors.New("unknown file ext")
)

error variables

Functions

func Register

func Register(name string, driver Driver)

Register registers driver

Types

type Driver

type Driver interface {
	TrimComment(sql string) string
	EscapeKeyword(keyword string) string
	EscapeValue(value string) string
	ExecSQL(tx *sql.Tx, sql string) error
}

Driver is database adapter

type Fixture

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

Fixture supply fixture methods sample:

import _ "github.com/takashabe/go-fixture/mysql"
...
  fixture.Load("path/to/hoge.yml", &Hoge{})

func NewFixture

func NewFixture(db *sql.DB, driverName string) (*Fixture, error)

NewFixture returns initialized Fixture

func (*Fixture) Load

func (f *Fixture) Load(path string) error

Load load .yml script

type QueryModelWithYaml

type QueryModelWithYaml struct {
	Table  string              `yaml:"table"`
	Record []map[string]string `yaml:"record"`
}

QueryModelWithYaml represent fixture yaml file mapper

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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