From 71d4c873bd12233b585637404115cfea9462c904 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Thu, 30 Apr 2026 00:56:09 +0200 Subject: add ctx arguments to most methods --- query_test.go | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) (limited to 'query_test.go') diff --git a/query_test.go b/query_test.go index 6f73642..05a3af2 100644 --- a/query_test.go +++ b/query_test.go @@ -16,6 +16,7 @@ import ( ) func TestInsertBasic(t *testing.T) { + ctx := t.Context() md := mock.NewDriver() db := sql.OpenDB(md) @@ -40,7 +41,7 @@ func TestInsertBasic(t *testing.T) { AndReturnColumns("id"). WithRow(int64(42 + idx)) } - must.Succeed(t, store.Insert(db, records...)) + must.Succeed(t, store.Insert(ctx, db, records...)) for idx, r := range records { assert.Equal(t, r.ID, int64(42+idx)) } @@ -49,6 +50,7 @@ func TestInsertBasic(t *testing.T) { } func TestUpdateBasic(t *testing.T) { + ctx := t.Context() md := mock.NewDriver() db := sql.OpenDB(md) @@ -72,12 +74,13 @@ func TestUpdateBasic(t *testing.T) { ExpectExecWithArgs(r.Name, r.ID). AndReturnRowsAffected(1) } - must.Succeed(t, store.Update(db, records...)) + must.Succeed(t, store.Update(ctx, db, records...)) }) } } func TestDeleteBasic(t *testing.T) { + ctx := t.Context() md := mock.NewDriver() db := sql.OpenDB(md) @@ -101,12 +104,13 @@ func TestDeleteBasic(t *testing.T) { ExpectExecWithArgs(r.ID). AndReturnRowsAffected(1) } - must.Succeed(t, store.Delete(db, records...)) + must.Succeed(t, store.Delete(ctx, db, records...)) }) } } func TestUpsertBasicWithAutoColumn(t *testing.T) { + ctx := t.Context() md := mock.NewDriver() db := sql.OpenDB(md) @@ -141,7 +145,7 @@ func TestUpsertBasicWithAutoColumn(t *testing.T) { {Name: "third needs insert"}, {ID: 4, Name: "fourth needs update"}, } - must.Succeed(t, store.Upsert(db, records...)) + must.Succeed(t, store.Upsert(ctx, db, records...)) assert.SliceDeepEqual(t, records, &basicRecord{ID: 1, Name: "first needs insert"}, @@ -152,6 +156,7 @@ func TestUpsertBasicWithAutoColumn(t *testing.T) { } func TestWriteQueriesNotPossible(t *testing.T) { + ctx := t.Context() md := mock.NewDriver() db := sql.OpenDB(md) @@ -165,21 +170,22 @@ func TestWriteQueriesNotPossible(t *testing.T) { ) r := basicRecord{Name: "foo"} - err := store.Insert(db, &r) + err := store.Insert(ctx, db, &r) assert.ErrEqual(t, err, "cannot execute Insert() because query could not be autogenerated") - err = store.Upsert(db, &r) + err = store.Upsert(ctx, db, &r) assert.ErrEqual(t, err, "cannot execute Insert() because query could not be autogenerated") r.ID = 42 - err = store.Update(db, r) + err = store.Update(ctx, db, r) assert.ErrEqual(t, err, "cannot execute Update() because query could not be autogenerated") - err = store.Delete(db, r) + err = store.Delete(ctx, db, r) assert.ErrEqual(t, err, "cannot execute Delete() because query could not be autogenerated") } func TestWriteQueriesFailDuringPrepare(t *testing.T) { + ctx := t.Context() md := mock.NewDriver() db := sql.OpenDB(md) @@ -201,7 +207,7 @@ func TestWriteQueriesFailDuringPrepare(t *testing.T) { recordsForInsert[idx] = &basicRecord{Name: "foo"} } - err := store.Insert(db, recordsForInsert...) + err := store.Insert(ctx, db, recordsForInsert...) baseError := `unexpected query: INSERT INTO "basic_records" ("name") VALUES (?) RETURNING "id"` if batchSize < oblast.PrepareThreshold { assert.ErrEqual(t, err, "while inserting record with idx = 0: "+baseError) @@ -209,7 +215,7 @@ func TestWriteQueriesFailDuringPrepare(t *testing.T) { assert.ErrEqual(t, err, "during Prepare(): "+baseError) } - err = store.Update(db, records...) + err = store.Update(ctx, db, records...) baseError = `unexpected query: UPDATE "basic_records" SET "name" = ? WHERE "id" = ?` if batchSize < oblast.PrepareThreshold { assert.ErrEqual(t, err, "while updating record with idx = 0: "+baseError) @@ -217,7 +223,7 @@ func TestWriteQueriesFailDuringPrepare(t *testing.T) { assert.ErrEqual(t, err, "during Prepare(): "+baseError) } - err = store.Delete(db, records...) + err = store.Delete(ctx, db, records...) baseError = `unexpected query: DELETE FROM "basic_records" WHERE "id" = ?` if batchSize < oblast.PrepareThreshold { assert.ErrEqual(t, err, "while deleting record with idx = 0: "+baseError) @@ -228,6 +234,7 @@ func TestWriteQueriesFailDuringPrepare(t *testing.T) { } func TestUpdateOrUpsertFailsOnMissingRecord(t *testing.T) { + ctx := t.Context() md := mock.NewDriver() db := sql.OpenDB(md) @@ -245,7 +252,7 @@ func TestUpdateOrUpsertFailsOnMissingRecord(t *testing.T) { md.ForQuery(`UPDATE "basic_records" SET "name" = ? WHERE "id" = ?`). ExpectExecWithArgs("changed", 42). AndReturnRowsAffected(0) - err := store.Update(db, basicRecord{ID: 42, Name: "changed"}) + err := store.Update(ctx, db, basicRecord{ID: 42, Name: "changed"}) assert.ErrEqual(t, err, "could not UPDATE record that does not exist in the database: id = 42") _, hasCorrectType := err.(oblast.MissingRecordError[basicRecord]) //nolint:errorlint // we explicitly do not want a wrapped error assert.Equal(t, hasCorrectType, true) @@ -255,13 +262,14 @@ func TestUpdateOrUpsertFailsOnMissingRecord(t *testing.T) { md.ForQuery(`UPDATE "basic_records" SET "name" = ? WHERE "id" = ?`). ExpectExecWithArgs("changed", 42). AndReturnRowsAffected(0) - err = store.Upsert(db, &basicRecord{ID: 42, Name: "changed"}) + err = store.Upsert(ctx, db, &basicRecord{ID: 42, Name: "changed"}) assert.ErrEqual(t, err, "could not UPDATE record that does not exist in the database: id = 42") _, hasCorrectType = err.(oblast.MissingRecordError[basicRecord]) //nolint:errorlint // we explicitly do not want a wrapped error assert.Equal(t, hasCorrectType, true) } func TestInsertFailsOnFilledAutoField(t *testing.T) { + ctx := t.Context() md := mock.NewDriver() db := sql.OpenDB(md) @@ -279,11 +287,12 @@ func TestInsertFailsOnFilledAutoField(t *testing.T) { ExpectQueryWithArgs("existing"). AndReturnColumns("id"). WithRow(42) - err := store.Insert(db, &basicRecord{ID: 23, Name: "third"}) + err := store.Insert(ctx, db, &basicRecord{ID: 23, Name: "third"}) assert.ErrEqual(t, err, `refusing to INSERT record with idx = 0 that already has non-zero values in its "auto" columns`) } func TestInsertAndUpsertWithNoAutoColumns(t *testing.T) { + ctx := t.Context() md := mock.NewDriver() db := sql.OpenDB(md) @@ -301,7 +310,7 @@ func TestInsertAndUpsertWithNoAutoColumns(t *testing.T) { md.ForQuery(`INSERT INTO "foo_bar_relations" ("foo_id", "bar_id") VALUES (?, ?)`). ExpectExecWithArgs(23, 42). AndReturnRowsAffected(1) - must.Succeed(t, store.Insert(db, &relation{23, 42})) + must.Succeed(t, store.Insert(ctx, db, &relation{23, 42})) // test Upsert() md.ForQuery(`INSERT INTO "foo_bar_relations" ("foo_id", "bar_id") VALUES (?, ?) ON CONFLICT ("foo_id", "bar_id") DO NOTHING`). @@ -310,10 +319,11 @@ func TestInsertAndUpsertWithNoAutoColumns(t *testing.T) { md.ForQuery(`INSERT INTO "foo_bar_relations" ("foo_id", "bar_id") VALUES (?, ?) ON CONFLICT ("foo_id", "bar_id") DO NOTHING`). ExpectExecWithArgs(3, 4). AndReturnRowsAffected(1) - must.Succeed(t, store.Upsert(db, &relation{1, 2}, &relation{3, 4})) + must.Succeed(t, store.Upsert(ctx, db, &relation{1, 2}, &relation{3, 4})) } func TestUpsertFailsOnMixedAutoFieldState(t *testing.T) { + ctx := t.Context() md := mock.NewDriver() db := sql.OpenDB(md) @@ -333,6 +343,6 @@ func TestUpsertFailsOnMixedAutoFieldState(t *testing.T) { Name: "foo", CreatedAt: time.Time{}, // this looks like we need to INSERT } - err := store.Upsert(db, &brokenRecord) + err := store.Upsert(ctx, db, &brokenRecord) assert.ErrEqual(t, err, `cannot decide whether to INSERT or UPDATE record with idx = 0: some "auto" columns are zero, others are not`) } -- cgit v1.2.3