modulepackage
0.1.0
Repository: https://github.com/bluegreenhq/codefixture.git
Documentation: pkg.go.dev
# README
codefixture
codefixture is a Go library designed to set up test fixtures through code.
Motivation
Writing complex integration tests is always necessary but challenging. This library assists in creating such tests programatically. codefixture doesn't rely on external files such as JSON, YAML or SQL, as it is more maintenance to define specific test conditions directly in the code.
Use Case
codefixture is useful when you want to conduct tests in the following scenarios:
- Integration tests (Round-trip tests)
- Application layer tests (use case layer)
- Tests involving database access
Installation
go get github.com/bluegreenhq/codefixture
Usage
- Import codefixture in your test file.
import "github.com/bluegreenhq/codefixture"
- Create a
FixtureBuilder
.
builder := codefixture.NewFixtureBuilder()
- Register writers with
FixtureBuilder
.
- Using Gorm (Note: this is an example. codefixture is not dependent on any DB libraries.)
conn := gorm.Open(...)
err := builder.RegisterWriter(&Person{}, func(m any) (any, error) {
m, ok := m.(*Person)
if !ok {
return nil, errors.New("invalid type")
}
res := conn.Create(m)
return m, res.Error
})
- Add models and relations to
FixtureBuilder
.
p, _ := builder.AddModel(&Person{Name: "John"})
g, _ := builder.AddModel(&Group{Name: "Family"})
builder.AddRelation(p, g, func(p, g any) {
p.(*Person).GroupID = g.(*Group).ID
})
- Build
Fixture
fromFixtureBuilder
.
fixture, err := builder.Build()
- Access your models from
Fixture
.
fmt.Printf("Person name: %s\n", fixture.GetModel(p).(*Person).Name)
fmt.Printf("Group name: %s\n", fixture.GetModel(g).(*Group).Name)
License
MIT
# Functions
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Type aliases
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author