Skip to content

Commit 310b5b5

Browse files
authored
Fix validation of field name passed to Preload (#73)
1 parent d5c40b1 commit 310b5b5

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

preload.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func (p preload) execute(ctx context.Context, entities any, field string, querie
2727
if (mp.argEntities == nil || reflect.DeepEqual(mp.argEntities, entities)) &&
2828
(mp.argEntitiesType == "" || mp.argEntitiesType == reflect.TypeOf(entities).String()) &&
2929
matchQuery(mp.argQuery, query) &&
30+
mp.argField == field &&
3031
mp.assert.call(ctx) {
3132

3233
if mp.result != nil {

preload_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,17 @@ func TestPreload_String(t *testing.T) {
241241
assert.Equal(t, "Preload(ctx, &reltest.Book{ID: 1}, \"users\", rel.Where(where.Eq(\"status\", \"active\")))", mockPreload.String())
242242
assert.Equal(t, "ExpectPreload(\"users\", rel.Where(where.Eq(\"status\", \"active\"))).ForType(\"*reltest.Book\")", mockPreload.ExpectString())
243243
}
244+
245+
func TestPreload_invalidField(t *testing.T) {
246+
var (
247+
repo = New()
248+
authorID = 1
249+
result = Book{ID: 2, Title: "Rel for dummies", AuthorID: &authorID}
250+
author = Author{ID: 1, Name: "Kia"}
251+
)
252+
253+
repo.ExpectPreload("xxx").Result(author)
254+
assert.Panics(t, func() {
255+
repo.MustPreload(context.TODO(), &result, "author")
256+
})
257+
}

0 commit comments

Comments
 (0)