Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bom-d-van/gogetter
A simple factory girl port with a simple database cleaner interface, in Golang.
https://github.com/bom-d-van/gogetter
Last synced: about 1 month ago
JSON representation
A simple factory girl port with a simple database cleaner interface, in Golang.
- Host: GitHub
- URL: https://github.com/bom-d-van/gogetter
- Owner: bom-d-van
- License: mit
- Created: 2013-12-15T06:13:11.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2014-01-06T02:16:43.000Z (almost 11 years ago)
- Last Synced: 2024-06-19T16:32:42.516Z (5 months ago)
- Language: Go
- Size: 199 KB
- Stars: 12
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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](http://godoc.org/github.com/bom-d-van/gogetter).
## Usage
```go
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)
}```