gogetter

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

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

Go to latest
Published: Jan 6, 2014 License: MIT Imports: 6 Imported by: 0

README

gogetter

A simple factory girl port with a simple database cleaner interface, in Golang.

gogetter is designed to be a testing tool.

Serious Api is here.

Usage

type User struct {
	Id    bson.ObjectId `bson:"_id"`
	Name  string
}

func init() {
	gogetter.SetGoal("User", func() gogetter.Dream {
		return User{
			Id: bson.NewObjectId(),
			Name: "name",
		}
	})
}

func TestUser(t *testing.T) {
	// Grow is Build in factory girl
	userI, err := gogetter.Grow("User")
	user := user.(User)
	user.Name == "name" // true

	// Use asterisk operator (*)
	userI, err := gogetter.Grow("*User", gogetter.Lession{
		"Name": "Custom Name",
	})
	user := userI.(*User)
	user.Name == "Custom Name" // true

	// Use Lesson to override default values
	userI, err := gogetter.Grow("User", gogetter.Lession{
		"Name": "Custom Name",
	})
	user := userI.(User)
	user.Name == "Custom Name" // true

	// Use Multiple Lessons
	usersI, err := gogetter.Grow("User", gogetter.Lession{
		"Name": "Custom Name",
	}, gogetter.Lession{
		"Name": "Another Lession",
	})
	users := usersI.([]User)
	users[0].Name == "Custom Name" // true
	users[1].Name == "Another Lession" // true

	// Realize is Create in factory girl, it will insert record(s) in a provided database
	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	gogetter.SetDefaultGetterDb(session.DB("gogetter"))
	userI, err := gogetter.Realize("User")
	user := user.(User)
	user.Name == "name" // true
	// By Default, gogetter will use the lowercase, plural, and also replacing
	// spaces with underscores of the name defined with SetGoal
	users := []User{}
	session.DB("gogetter").C("users").FindId(user.Id).All(&users)
	users[0].Id == user.Id // true

	// Everything work in Grow work with Realize, except it will make changes in a database
	gogetter.Realize("User", Lession{...}, Lession{...})
	gogetter.Realize("*User", Lession{...}, Lession{...})
	...

	// Use AllInVain or Apocalypse to destory objects, i.e., remove records from databases
	gogetter.AllInVain("Users") // Will destory all "Users" just created
	gogetter.AllInVain("Users", user1, user2) // Only destory user1 and user2
	gogetter.Apocalypse("Users") // Equals to gogetter.AllInVain("Users")
	gogetter.Apocalypse("Users", "Another Goals") // Will Destroy all records of both "Users" and "Another Goals"
	gogetter.Apocalypse() // Will destroy every records

	// Of course, in most serious cases, you could use your own gogetter instead of the default one
	getter := gogetter.NewGoGetter(yourDb)
}


Documentation

Overview

gogetter is a simple sample data manager, a simple factory_girl (https://github.com/thoughtbot/factory_girl) port with a simple database cleaner interface, and designed to be a testing tool.

Supported Database

1. Mongo (Using labix.org/v2/mgo)

<under construction>

2. MySql (Using github.com/coopernurse/gorp)

3. Postgres (Using github.com/coopernurse/gorp)

Index

Constants

This section is empty.

Variables

View Source
var ErrGetterNotExist = errors.New("Getter Not Exist")

Functions

func AllInVain

func AllInVain(name string, dreams ...Dream) (err error)

See (gg *GoGetter) AllInVain.

func Apocalypse

func Apocalypse(names ...string) (err error)

See (gg *GoGetter) Apocalypse.

func AscendGoal

func AscendGoal(child, parent string, lesson func() Lesson)

By default, GetTableName will use parent's table name.

func GetTableName

func GetTableName(name string) (table string, err error)

func SetDefaultGetterDb

func SetDefaultGetterDb(db Database)

func SetDefaultTableId

func SetDefaultTableId(name string)

Table Id is used when gogetter is trying remove records from table, using a simple sql/mongo statement to remove the data. Default Table Id is "Id", its value must be comparable via reflect.DeepEqual.

func SetGoal

func SetGoal(name string, goal Goal)

SetGoal will save the Goal globally, then all gogetter values could share the same set of goals.

Note:
1. Leading asterisk (*) in name is saved for gogetter.
2. The return value of goal must be a Struct, map or anything else is not supported.

func SetTableName

func SetTableName(name, table string)

Setting table name is optional, if table name is not specifically setted, gogetter will use the pluralization and lower case form of the name as table name, it will also replace all spaces with underscores.

Types

type Database

type Database interface {
	Create(table string, data ...interface{}) (err error)
	Remove(table string, idField string, ids ...interface{}) (err error)
}

type Dream

type Dream interface{}

func Grow

func Grow(name string, lessons ...Lesson) (dreams Dream, err error)

See (gg *GoGetter) Grow.

func Realize

func Realize(name string, lessons ...Lesson) (dreams Dream, err error)

Realize is similar to Grow, except for inserting records/docs in a provided database.

type GoGetter

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

func NewGoGetter

func NewGoGetter(db Database) *GoGetter

func (*GoGetter) AllInVain

func (gg *GoGetter) AllInVain(name string, dreams ...Dream) (err error)

Destroy data created by Grow/Realize (Do not use leading * in name with this function).

Usage:

gogetter.AllInVain("Users") // Will destory all "Users"
gogetter.AllInVain("Users", user1, user2) // Only destory user1 and user2
gogetter.AllInVain("users", users) // users is a slice of User

func (*GoGetter) Apocalypse

func (gg *GoGetter) Apocalypse(names ...string) (err error)

Apocalypse is designed as a handy method to replace AllInVain in cases like simply wipe out all data created by Grow/Realize.

Usage:

gogetter.Apocalypse("Users") // Will remove all "Users" data
gogetter.Apocalypse("Users", "Another Goals") // Will remove all "Users" and "Another Goals" data
gogetter.Apocalypse() // Will destroy all data

func (*GoGetter) Grow

func (gg *GoGetter) Grow(name string, lessons ...Lesson) (dreams Dream, err error)

Could use a leading asterisk (*) in name to get pointer value.

TODO:
1. Support anonymous type, e,g, custom struct, map, etc
2. Tags specification in custom structs, provided that gogetter will support struct

func (*GoGetter) Realize

func (gg *GoGetter) Realize(name string, lessons ...Lesson) (dreams Dream, err error)

Grow and Create a Record in Database

type Goal

type Goal func() Dream

func GetGoal

func GetGoal(name string) Goal

type Lesson

type Lesson map[string]Dream

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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