@@ -1145,6 +1145,49 @@ func TestSlogTracerBatchQuery(t *testing.T) {
11451145 }
11461146}
11471147
1148+ func TestAfterScan (t * testing.T ) {
1149+ truncateAllTables (t )
1150+
1151+ ctx := t .Context ()
1152+
1153+ hookTable := pgkit.Table [AccountWithHook , * AccountWithHook , int64 ]{DB : DB , Name : "accounts" , IDColumn : "id" }
1154+ failTable := pgkit.Table [AccountWithFailingHook , * AccountWithFailingHook , int64 ]{DB : DB , Name : "accounts" , IDColumn : "id" }
1155+ plainTable := pgkit.Table [Account , * Account , int64 ]{DB : DB , Name : "accounts" , IDColumn : "id" }
1156+
1157+ // Seed data.
1158+ require .NoError (t , hookTable .Save (ctx , & AccountWithHook {Account : Account {Name : "alice" }}))
1159+ require .NoError (t , hookTable .Save (ctx , & AccountWithHook {Account : Account {Name : "bob" }}))
1160+
1161+ t .Run ("Get" , func (t * testing.T ) {
1162+ acc , err := hookTable .Get (ctx , sq.Eq {"name" : "alice" }, nil )
1163+ require .NoError (t , err )
1164+ assert .Equal (t , "ALICE" , acc .UpperName )
1165+ })
1166+
1167+ t .Run ("List" , func (t * testing.T ) {
1168+ accs , err := hookTable .List (ctx , nil , []string {"name" })
1169+ require .NoError (t , err )
1170+ require .Len (t , accs , 2 )
1171+ assert .Equal (t , "ALICE" , accs [0 ].UpperName )
1172+ assert .Equal (t , "BOB" , accs [1 ].UpperName )
1173+ })
1174+
1175+ t .Run ("NoHook" , func (t * testing.T ) {
1176+ // Account does not implement AfterScanner — should work unchanged.
1177+ acc , err := plainTable .Get (ctx , sq.Eq {"name" : "alice" }, nil )
1178+ require .NoError (t , err )
1179+ assert .Equal (t , "alice" , acc .Name )
1180+ })
1181+
1182+ t .Run ("ErrorPropagation" , func (t * testing.T ) {
1183+ _ , err := failTable .Get (ctx , sq.Eq {"name" : "alice" }, nil )
1184+ require .ErrorContains (t , err , "after scan boom" )
1185+
1186+ _ , err = failTable .List (ctx , nil , nil )
1187+ require .ErrorContains (t , err , "after scan boom" )
1188+ })
1189+ }
1190+
11481191func getTracer (opts []tracer.Option ) (* bytes.Buffer , * tracer.LogTracer ) {
11491192 buf := & bytes.Buffer {}
11501193 handler := slog .NewJSONHandler (buf , nil )
0 commit comments