Skip to content

Commit

Permalink
Fix validation of field name passed to Preload
Browse files Browse the repository at this point in the history
  • Loading branch information
youpy committed Nov 29, 2024
1 parent 82c2792 commit 9ef345a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions preload.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (p preload) execute(ctx context.Context, entities any, field string, querie
if (mp.argEntities == nil || reflect.DeepEqual(mp.argEntities, entities)) &&
(mp.argEntitiesType == "" || mp.argEntitiesType == reflect.TypeOf(entities).String()) &&
matchQuery(mp.argQuery, query) &&
mp.argField == field &&
mp.assert.call(ctx) {

if mp.result != nil {
Expand Down
14 changes: 14 additions & 0 deletions preload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,17 @@ func TestPreload_String(t *testing.T) {
assert.Equal(t, "Preload(ctx, &reltest.Book{ID: 1}, \"users\", rel.Where(where.Eq(\"status\", \"active\")))", mockPreload.String())
assert.Equal(t, "ExpectPreload(\"users\", rel.Where(where.Eq(\"status\", \"active\"))).ForType(\"*reltest.Book\")", mockPreload.ExpectString())
}

func TestPreload_invalidField(t *testing.T) {
var (
repo = New()
authorID = 1
result = Book{ID: 2, Title: "Rel for dummies", AuthorID: &authorID}
author = Author{ID: 1, Name: "Kia"}
)

repo.ExpectPreload("xxx").Result(author)
assert.Panics(t, func() {
repo.MustPreload(context.TODO(), &result, "author")
})
}

0 comments on commit 9ef345a

Please sign in to comment.