diff --git a/preload.go b/preload.go index 92d0aa5..ed3a281 100644 --- a/preload.go +++ b/preload.go @@ -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 { diff --git a/preload_test.go b/preload_test.go index b4737ec..5997e9e 100644 --- a/preload_test.go +++ b/preload_test.go @@ -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") + }) +}