diff --git a/tests/delete_test.go b/tests/delete_test.go index 6f87161..ed79b87 100644 --- a/tests/delete_test.go +++ b/tests/delete_test.go @@ -1,11 +1,13 @@ package tests import ( + "context" . "github.com/go-jet/jet" "github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/model" . "github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/table" "gotest.tools/assert" "testing" + "time" ) func TestDeleteWithWhere(t *testing.T) { @@ -60,3 +62,38 @@ func initForDeleteTest(t *testing.T) { assertExec(t, stmt, 2) } + +func TestDeleteQueryContext(t *testing.T) { + initForDeleteTest(t) + + deleteStmt := Link. + DELETE(). + WHERE(Link.Name.IN(String("Gmail"), String("Outlook"))) + + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond) + defer cancel() + + time.Sleep(10 * time.Microsecond) + + dest := []model.Link{} + err := deleteStmt.QueryContext(ctx, db, &dest) + + assert.Error(t, err, "context deadline exceeded") +} + +func TestDeleteExecContext(t *testing.T) { + initForDeleteTest(t) + + deleteStmt := Link. + DELETE(). + WHERE(Link.Name.IN(String("Gmail"), String("Outlook"))) + + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond) + defer cancel() + + time.Sleep(10 * time.Microsecond) + + _, err := deleteStmt.ExecContext(ctx, db) + + assert.Error(t, err, "context deadline exceeded") +} diff --git a/tests/insert_test.go b/tests/insert_test.go index 8c6f17d..17ffca0 100644 --- a/tests/insert_test.go +++ b/tests/insert_test.go @@ -1,11 +1,13 @@ package tests import ( + "context" . "github.com/go-jet/jet" "github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/model" . "github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/table" "gotest.tools/assert" "testing" + "time" ) func TestInsertValues(t *testing.T) { @@ -245,3 +247,37 @@ RETURNING link.id AS "link.id", assert.NilError(t, err) assert.Equal(t, len(youtubeLinks), 2) } + +func TestInsertWithQueryContext(t *testing.T) { + cleanUpLinkTable(t) + + stmt := Link.INSERT(). + VALUES(1100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT). + RETURNING(Link.AllColumns) + + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond) + defer cancel() + + time.Sleep(10 * time.Microsecond) + + dest := []model.Link{} + err := stmt.QueryContext(ctx, db, &dest) + + assert.Error(t, err, "context deadline exceeded") +} + +func TestInsertWithExecContext(t *testing.T) { + cleanUpLinkTable(t) + + stmt := Link.INSERT(). + VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT) + + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond) + defer cancel() + + time.Sleep(10 * time.Microsecond) + + _, err := stmt.ExecContext(ctx, db) + + assert.Error(t, err, "context deadline exceeded") +} diff --git a/tests/update_test.go b/tests/update_test.go index d8e1f0e..768231b 100644 --- a/tests/update_test.go +++ b/tests/update_test.go @@ -1,11 +1,13 @@ package tests import ( + "context" . "github.com/go-jet/jet" "github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/model" . "github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/table" "gotest.tools/assert" "testing" + "time" ) func TestUpdateValues(t *testing.T) { @@ -241,6 +243,43 @@ WHERE link.id = 201; assertExecErr(t, stmt, "pq: number of columns does not match number of values") } +func TestUpdateQueryContext(t *testing.T) { + setupLinkTableForUpdateTest(t) + + updateStmt := Link. + UPDATE(Link.Name, Link.URL). + SET("Bong", "http://bong.com"). + WHERE(Link.Name.EQ(String("Bing"))) + + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond) + defer cancel() + + time.Sleep(10 * time.Microsecond) + + dest := []model.Link{} + err := updateStmt.QueryContext(ctx, db, &dest) + + assert.Error(t, err, "context deadline exceeded") +} + +func TestUpdateExecContext(t *testing.T) { + setupLinkTableForUpdateTest(t) + + updateStmt := Link. + UPDATE(Link.Name, Link.URL). + SET("Bong", "http://bong.com"). + WHERE(Link.Name.EQ(String("Bing"))) + + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond) + defer cancel() + + time.Sleep(10 * time.Microsecond) + + _, err := updateStmt.ExecContext(ctx, db) + + assert.Error(t, err, "context deadline exceeded") +} + func setupLinkTableForUpdateTest(t *testing.T) { cleanUpLinkTable(t)