2025-07-26 20:16:50 +05:30
|
|
|
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"
|
|
|
|
|
|
2025-07-26 20:16:50 +05:30
|
|
|
"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-11-16 16:21:35 +05:30
|
|
|
// BenchmarkUpdateQuery-12 2334889 503.6 ns/op 1112 B/op 17 allocs/op
|
2025-07-26 18:34:56 +05:30
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|