tachyon

package module
v0.0.0-...-4153f30 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2018 License: MIT Imports: 8 Imported by: 0

README

Tachyon

Tachyon is a simple Fixture library for you Go test suite.

Tachyon loads your Yaml fixtures into your database in an unopinionated manner.

Tachyon is still a work in progress, so expect great changes to come!

Installation

The easy way:

go get github.com/ki4jnq/tachyon

However, it's recommended that you use a tool such as glide and vendor your dependencies.

Usage

import "github.com/ki4jnq/tachyon"

func TestSomethingBig(t *testing.T) {
	var db *sql.Db
	db := magicallyMakeDbInstance()

	// Read one fixture from testdata/users.yml
	f, err := tachyon.NewFixture("users")

	// Load the fixture into the database.
	err = f.Load(db)

	// ... Or, load it in a transaction
	tx, _ := db.Begin()
	err = f.LoadTx(tx)
	tx.Commit() // It's left up to you manage the transaction.

	// ... Or manage multiple fixtures at once
	fixtures, err := tachyon.ReadFixtures("users", "posts", "comments")

	// Load them all into the DB
	fixtures.Load(db)

	// And when you're done:
	fixtures.Clean(db)
}

Fixture files should be in the following format:

table: users
fields:
  - email
  - name

data:
  picard:
    email: "picard.cc@enterprise.net"
    name: "Jean Luc Picard"

  riker:
    email: "riker.xo@enterprise.net"
    name: "William Riker"

Place your fixtures in a testdata/ subdirectory relative to the root package path. Make sure that your fixtures have a .yml file extension.

TODO

Loading a list of fixtures creates 1 transaction per fixture, it should only create a single transaction, period.

Special Thanks

Special thanks to Tax Management Associates (TMA) for letting me have fun and call it "work".

Documentation

Index

Constants

View Source
const FixturePath = "testdata"

Variables

This section is empty.

Functions

This section is empty.

Types

type Fixture

type Fixture struct {
	Table   string            `yaml:"table"`
	Fields  []string          `yaml:"fields"`
	Records map[string]Record `yaml:"data"`
}

Fixture represents a test Fixture.

func NewFixture

func NewFixture(name string) (*Fixture, error)

func (*Fixture) Clean

func (f *Fixture) Clean(db *sql.DB) error

func (*Fixture) Load

func (f *Fixture) Load(db *sql.DB) error

Load inserts f's data into db inside of a transaction. If an error is encountered, the transaction is rolled back.

func (*Fixture) LoadTag

func (f *Fixture) LoadTag(tag string, db *sql.DB) error

func (*Fixture) LoadTx

func (f *Fixture) LoadTx(tx *sql.Tx) error

LoadTx inserts f's data within the transaction, but does not commit or rollback. The calling code must commit it manually.

type FixtureList

type FixtureList []*Fixture

func ReadFixtures

func ReadFixtures(names ...string) (FixtureList, error)

func (FixtureList) Clean

func (fl FixtureList) Clean(db *sql.DB) error

Clean deletes all records in affected tables in the reverse order that they were loaded. This helps ensure that any child records are deleted before the parent records they depend on are.

func (FixtureList) Load

func (fl FixtureList) Load(db *sql.DB) error

func (FixtureList) LoadTx

func (fl FixtureList) LoadTx(tx *sql.Tx) error

type Record

type Record map[string]string

Jump to

Keyboard shortcuts

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