Skip to content

Commit

Permalink
roast: added hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Mar 12, 2024
1 parent 0ea409c commit da934d8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
52 changes: 47 additions & 5 deletions roast/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/256dpi/fire"
"github.com/256dpi/fire/axe"
"github.com/256dpi/fire/coal"
"github.com/256dpi/fire/stick"
)

// TODO: Support filters and sorters.
Expand Down Expand Up @@ -160,13 +161,18 @@ func (t *Tester) Invalidate() {
}

// List will list the provided model and validate the response if requested.
func (t *Tester) List(tt *testing.T, model coal.Model, response []coal.Model) Result {
func (t *Tester) List(tt *testing.T, model coal.Model, response []coal.Model, hooks ...func(res, cmp coal.Model)) Result {
// list resources
res, doc, err := t.DataClient.List(model)
assert.NoError(tt, err)

// validate response
if err == nil && response != nil {
for i := range response {
for _, cb := range hooks {
cb(res[i], response[i])
}
}
assert.Equal(tt, response, res)
}

Expand Down Expand Up @@ -195,13 +201,16 @@ func (t *Tester) ListError(tt *testing.T, model coal.Model, expected error) Resu
}

// Find will find the provided model and validate the response if requested.
func (t *Tester) Find(tt *testing.T, model coal.Model, response coal.Model) Result {
func (t *Tester) Find(tt *testing.T, model coal.Model, response coal.Model, hooks ...func(res, cmp coal.Model)) Result {
// find resource
res, doc, err := t.DataClient.Find(model)
assert.NoError(tt, err)

// validate response
if err == nil && response != nil {
for _, cb := range hooks {
cb(res, response)
}
assert.Equal(tt, response, res)
}

Expand Down Expand Up @@ -231,14 +240,17 @@ func (t *Tester) FindError(tt *testing.T, model coal.Model, expected error) Resu

// Create will create the provided model and validate the response and result
// if requested.
func (t *Tester) Create(tt *testing.T, model, response, result coal.Model) Result {
func (t *Tester) Create(tt *testing.T, model, response, result coal.Model, hooks ...func(res, cmp coal.Model)) Result {
// create resource
res, doc, err := t.DataClient.Create(model)
assert.NoError(tt, err)

// validate response
if err == nil && response != nil {
response.GetBase().DocID = res.ID()
for _, cb := range hooks {
cb(res, response)
}
assert.Equal(tt, response, res)
}

Expand All @@ -247,6 +259,9 @@ func (t *Tester) Create(tt *testing.T, model, response, result coal.Model) Resul
result.GetBase().DocID = res.ID()
actual := coal.GetMeta(model).Make()
t.Tester.Fetch(actual, res.ID())
for _, cb := range hooks {
cb(actual, result)
}
assert.Equal(tt, result, actual)
}

Expand Down Expand Up @@ -276,20 +291,26 @@ func (t *Tester) CreateError(tt *testing.T, model coal.Model, expected error) Re

// Update will update the provided model and validate the response and result
// if requested.
func (t *Tester) Update(tt *testing.T, model, response, result coal.Model) Result {
func (t *Tester) Update(tt *testing.T, model, response, result coal.Model, hooks ...func(res, cmp coal.Model)) Result {
// update resource
res, doc, err := t.DataClient.Update(model)
assert.NoError(tt, err)

// validate response
if err == nil && response != nil {
for _, cb := range hooks {
cb(res, response)
}
assert.Equal(tt, response, res)
}

// validate result
if err == nil && result != nil {
actual := coal.GetMeta(model).Make()
t.Tester.Fetch(actual, model.ID())
for _, cb := range hooks {
cb(actual, result)
}
assert.Equal(tt, result, actual)
}

Expand Down Expand Up @@ -318,7 +339,7 @@ func (t *Tester) UpdateError(tt *testing.T, model coal.Model, expected error) Re
}

// Delete will delete the provided model and validate the result.
func (t *Tester) Delete(tt *testing.T, model, result coal.Model) Result {
func (t *Tester) Delete(tt *testing.T, model, result coal.Model, hooks ...func(res, cmp coal.Model)) Result {
// delete resource
err := t.DataClient.Delete(model)
assert.NoError(tt, err)
Expand All @@ -327,6 +348,9 @@ func (t *Tester) Delete(tt *testing.T, model, result coal.Model) Result {
if err == nil && result != nil {
actual := coal.GetMeta(model).Make()
t.Tester.Fetch(actual, model.ID())
for _, fn := range hooks {
fn(actual, result)
}
assert.Equal(tt, result, actual)
}

Expand Down Expand Up @@ -478,3 +502,21 @@ func (t *Tester) Await(tt *testing.T, timeout time.Duration, fns ...func()) int

return n
}

// CopyHook will copy fields from the response to the result.
func CopyHook(fields ...string) func(res, cmp coal.Model) {
return func(res, cmp coal.Model) {
for _, field := range fields {
stick.MustSet(cmp, field, stick.MustGet(res, field))
}
}
}

// SkipHook will copy fields from the result to the response.
func SkipHook(fields ...string) func(res, cmp coal.Model) {
return func(res, cmp coal.Model) {
for _, field := range fields {
stick.MustSet(res, field, stick.MustGet(cmp, field))
}
}
}
16 changes: 16 additions & 0 deletions roast/tester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ func TestTester(t *testing.T) {
tt.Delete(t, post, nil)
}

func TestTesterHooks(t *testing.T) {
post1 := &fooModel{String: "String"}
post2 := &fooModel{}

CopyHook("String")(post1, post2)
assert.Equal(t, "String", post1.String)
assert.Equal(t, "String", post2.String)

post1 = &fooModel{String: "String"}
post2 = &fooModel{}

SkipHook("String")(post1, post2)
assert.Zero(t, post1.String)
assert.Zero(t, post2.String)
}

func TestTesterErrors(t *testing.T) {
tt := NewTester(Config{
Models: models.All(),
Expand Down

0 comments on commit da934d8

Please sign in to comment.