fixtures

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

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

Go to latest
Published: Aug 31, 2021 License: MIT Imports: 16 Imported by: 0

README

gofixtures

Ruby on Rails' style test fixtures for Golang, use database transation to keep database clean state

Getting started

Install the package by:

go get -u github.com/Martin91/gofixtures

Example

  1. Reference to dummy to see demo yaml files.
  2. In your project, initalize database connection like:
    import (
        _ "github.com/go-sql-driver/mysql"
        "github.com/Martin91/gofixtures"
    )
    
    // fixtures is shipped with a builtin sql driver which supports rollback db automatically,
    //  so it is required to setup a transational *sql.DB by fixtures
    db := fixtures.OpenDB("mysql", "root:@tcp(localhost:3306)/?charset=utf8&parseTime=True&loc=Local")
    fixtures := fixtures.Load(tt.args.path, db)
    
    // do your test and something else
    
    // once the program exit, fixtures will rollback all database changes automatically
    
    // or if you want to manually rollback, run
    db.Driver().(base.TxDriverIface).ManualRollback()
    

NOTICE

This repository is still under active development and the overall design and performance is unstable.

Features (WIP, please keep looking forward to it)

[x] YAML based simple and clean syntax
[x] Built-in Faker supported
[x] Bundled field evaluators, enable you to customize dynamic data generation
[x] Support specifying database and tables
[x] Based on standard sql package, compatible with different dialects
[x] Transaction based database cleaner
[ ] Templates to support batch data

TODOs

  1. Test with different databases
  2. Complete test cases with high test coverage
  3. Review the overall architecture design

Documentation

Overview

Package fixtures implements a Ruby on Rails style test fixtures suite

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OpenDB

func OpenDB(driverName, dsn string) *sql.DB

OpenDB setup a transactional db to automatically rollback db changes, it may panics if any error encountered

driverName is the actually underlying driver, for example, `mysql`

func RegisterEvaluator

func RegisterEvaluator(name string, evaluator Evaluator)

func Rollback

func Rollback(db *sql.DB) error

Rollback manually rollback all changes since last call on Load

Types

type Collection

type Collection struct {
	DbName    string              `yaml:"db"`
	TableName string              `yaml:"table_name"`
	Rows      map[string]*Fixture `yaml:"rows"`
}

Collection maps to a table

type Evaluator

type Evaluator func() interface{}

Evaluator is used to generate data for columns

type Fixture

type Fixture struct {
	Columns map[string]interface{} // Columns fields and values of an single object
}

type Fixtures

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

Fixtures an utility to hold definitions of fixtures

func Load

func Load(path string, db *sql.DB) *Fixtures

Load parse yaml files under the directory specified by `path`, it may panics if any error encountered

func (*Fixtures) Load

func (f *Fixtures) Load() error

func (*Fixtures) Parse

func (f *Fixtures) Parse(content []byte) error

Directories

Path Synopsis
base
Package base defines common logic among different transational drivers
Package base defines common logic among different transational drivers
txmysql
Package txmysql registers a txmysql sql driver
Package txmysql registers a txmysql sql driver
txposgresql
Package txpostgresql registers a txpostgresql sql driver
Package txpostgresql registers a txpostgresql sql driver

Jump to

Keyboard shortcuts

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