renamed exaples to playground, edited README
This commit is contained in:
84
playground/qry_insert_test.go
Normal file
84
playground/qry_insert_test.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package playground
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"code.patial.tech/go/pgm"
|
||||
"code.patial.tech/go/pgm/playground/db"
|
||||
"code.patial.tech/go/pgm/playground/db/user"
|
||||
)
|
||||
|
||||
func TestInsertQuery(t *testing.T) {
|
||||
got := db.User.Insert().
|
||||
Set(user.Email, "aa@aa.com").
|
||||
Set(user.Phone, 8889991234).
|
||||
Set(user.FirstName, "fname").
|
||||
Set(user.LastName, "lname").
|
||||
Returning(user.ID).
|
||||
String()
|
||||
|
||||
expected := "INSERT INTO users(email, phone, first_name, last_name) VALUES($1, $2, $3, $4) RETURNING id"
|
||||
if got != expected {
|
||||
t.Errorf("\nexpected: %q\ngot: %q", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertSetMap(t *testing.T) {
|
||||
got := db.User.Insert().
|
||||
SetMap(map[pgm.Field]any{
|
||||
user.Email: "aa@aa.com",
|
||||
user.Phone: 8889991234,
|
||||
user.FirstName: "fname",
|
||||
user.LastName: "lname",
|
||||
}).
|
||||
Returning(user.ID).
|
||||
String()
|
||||
|
||||
expected := "INSERT INTO users(email, phone, first_name, last_name) VALUES($1, $2, $3, $4) RETURNING id"
|
||||
if got != expected {
|
||||
t.Errorf("\nexpected: %q\ngot: %q", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertQuery2(t *testing.T) {
|
||||
got := db.User.Insert().
|
||||
Set(user.Email, "aa@aa.com").
|
||||
Set(user.Phone, 8889991234).
|
||||
OnConflict(user.ID).
|
||||
DoNothing().
|
||||
String()
|
||||
expected := "INSERT INTO users(email, phone) VALUES($1, $2) ON CONFLICT (id) DO NOTHING"
|
||||
if got != expected {
|
||||
t.Errorf("\nexpected: %q\ngot: %q", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkInsertQuery-12 1952412 605.3 ns/op 1144 B/op 18 allocs/op
|
||||
func BenchmarkInsertQuery(b *testing.B) {
|
||||
for b.Loop() {
|
||||
_ = db.User.Insert().
|
||||
Set(user.Email, "aa@aa.com").
|
||||
Set(user.Phone, 8889991234).
|
||||
Set(user.FirstName, "fname").
|
||||
Set(user.LastName, "lname").
|
||||
Returning(user.ID).
|
||||
String()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkInsertSetMap-12 1534039 777.1 ns/op 1480 B/op 20 allocs/op
|
||||
func BenchmarkInsertSetMap(b *testing.B) {
|
||||
for b.Loop() {
|
||||
_ = db.User.Insert().
|
||||
SetMap(map[pgm.Field]any{
|
||||
user.Email: "aa@aa.com",
|
||||
user.Phone: 8889991234,
|
||||
user.FirstName: "fname",
|
||||
user.LastName: "lname",
|
||||
}).
|
||||
Returning(user.ID).
|
||||
String()
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user