Files
pgm/playground/qry_update_test.go

62 lines
1.4 KiB
Go
Raw Normal View History

package playground
2025-07-26 18:34:56 +05:30
import (
2025-11-16 11:37:02 +05:30
"context"
"strings"
2025-07-26 18:34:56 +05:30
"testing"
"code.patial.tech/go/pgm/playground/db"
"code.patial.tech/go/pgm/playground/db/user"
2025-07-26 18:34:56 +05:30
)
func TestUpdateQuery(t *testing.T) {
got := db.User.Update().
Set(user.FirstName, "ankit").
Set(user.MiddleName, "singh").
Set(user.LastName, "patial").
Where(
user.Email.Eq("aa@aa.com"),
).
Where(
2025-08-11 22:37:25 +05:30
user.StatusID.NotEq(1),
2025-07-26 18:34:56 +05:30
).
String()
expected := "UPDATE users SET first_name=$1, middle_name=$2, last_name=$3 WHERE users.email = $4 AND users.status_id != $5"
if got != expected {
t.Errorf("\nexpected: %q\ngot: %q", expected, got)
}
}
2025-11-16 11:37:02 +05:30
func TestUpdateQueryValidation(t *testing.T) {
// Test that UPDATE without Set() returns error
err := db.User.Update().
Where(user.Email.Eq("aa@aa.com")).
Exec(context.Background())
if err == nil {
t.Error("Expected error when calling Exec() without Set(), got nil")
}
if !strings.Contains(err.Error(), "no columns to update") {
t.Errorf("Expected error message to contain 'no columns to update', got: %v", err)
}
}
2025-07-26 18:34:56 +05:30
// BenchmarkUpdateQuery-12 2004985 592.2 ns/op 1176 B/op 20 allocs/op
func BenchmarkUpdateQuery(b *testing.B) {
for b.Loop() {
_ = db.User.Update().
Set(user.FirstName, "ankit").
Set(user.MiddleName, "singh").
Set(user.LastName, "patial").
Where(
user.Email.Eq("aa@aa.com"),
).
Where(
2025-08-11 22:37:25 +05:30
user.StatusID.NotEq(1),
2025-07-26 18:34:56 +05:30
).
String()
}
}