bob/upsert_test.go

104 lines
3.0 KiB
Go
Raw Normal View History

2021-07-25 04:41:39 +00:00
package bob_test
import (
"reflect"
"testing"
"github.com/aldy505/bob"
)
func TestUpsert(t *testing.T) {
2021-07-25 06:47:52 +00:00
t.Run("should be able to generate upsert query for mysql", func(t *testing.T) {
2021-07-25 04:41:39 +00:00
sql, args, err := bob.
2021-07-25 06:47:52 +00:00
Upsert("users", bob.Mysql).
2021-07-25 04:41:39 +00:00
Columns("name", "email").
Values("John Doe", "john@doe.com").
Replace("name", "John Does").
ToSql()
if err != nil {
t.Error(err)
}
2021-07-25 06:47:52 +00:00
desiredSql := "INSERT INTO \"users\" (\"name\", \"email\") VALUES (?, ?) ON DUPLICATE KEY UPDATE \"name\" = ?;"
2021-07-25 04:41:39 +00:00
desiredArgs := []interface{}{"John Doe", "john@doe.com", "John Does"}
if sql != desiredSql {
t.Error("sql is not the same as result: ", sql)
}
2021-07-25 06:47:52 +00:00
if !reflect.DeepEqual(args, desiredArgs) {
2021-07-25 04:41:39 +00:00
t.Error("args is not the same as result: ", args)
}
})
2021-07-25 06:47:52 +00:00
t.Run("should be able to generate upsert query for postgres", func(t *testing.T) {
2021-07-25 04:41:39 +00:00
sql, args, err := bob.
2021-07-25 06:47:52 +00:00
Upsert("users", bob.Postgresql).
2021-07-25 04:41:39 +00:00
Columns("name", "email").
Values("John Doe", "john@doe.com").
2021-07-25 06:47:52 +00:00
Key("email").
2021-07-25 04:41:39 +00:00
Replace("name", "John Does").
2021-07-25 06:47:52 +00:00
PlaceholderFormat(bob.Dollar).
2021-07-25 04:41:39 +00:00
ToSql()
if err != nil {
t.Error(err)
}
2021-07-25 06:47:52 +00:00
desiredSql := "INSERT INTO \"users\" (\"name\", \"email\") VALUES ($1, $2) ON CONFLICT (\"email\") DO UPDATE SET \"name\" = $3;"
2021-07-25 04:41:39 +00:00
desiredArgs := []interface{}{"John Doe", "john@doe.com", "John Does"}
if sql != desiredSql {
t.Error("sql is not the same as result: ", sql)
}
2021-07-25 06:47:52 +00:00
if !reflect.DeepEqual(args, desiredArgs) {
2021-07-25 04:41:39 +00:00
t.Error("args is not the same as result: ", args)
}
})
t.Run("should be able to generate upsert query for sqlite", func(t *testing.T) {
sql, args, err := bob.
Upsert("users", bob.Sqlite).
Columns("name", "email").
Values("John Doe", "john@doe.com").
2021-07-25 06:47:52 +00:00
Key("email").
2021-07-25 04:41:39 +00:00
Replace("name", "John Does").
PlaceholderFormat(bob.Question).
ToSql()
if err != nil {
t.Error(err)
}
2021-07-25 06:47:52 +00:00
desiredSql := "INSERT INTO \"users\" (\"name\", \"email\") VALUES (?, ?) ON CONFLICT (\"email\") DO UPDATE SET \"name\" = ?;"
2021-07-25 04:41:39 +00:00
desiredArgs := []interface{}{"John Doe", "john@doe.com", "John Does"}
if sql != desiredSql {
t.Error("sql is not the same as result: ", sql)
}
2021-07-25 06:47:52 +00:00
if !reflect.DeepEqual(args, desiredArgs) {
2021-07-25 04:41:39 +00:00
t.Error("args is not the same as result: ", args)
}
})
t.Run("should be able to generate upsert query for mssql", func(t *testing.T) {
sql, args, err := bob.
Upsert("users", bob.MSSql).
Columns("name", "email").
Values("John Doe", "john@doe.com").
Key("email", "john@doe.com").
Replace("name", "John Does").
PlaceholderFormat(bob.AtP).
ToSql()
if err != nil {
t.Error(err)
}
2021-07-25 06:47:52 +00:00
desiredSql := "IF NOT EXISTS (SELECT * FROM \"users\" WHERE \"email\" = @p1) INSERT INTO \"users\" (\"name\", \"email\") VALUES (@p2, @p3) ELSE UPDATE \"users\" SET \"name\" = @p4 WHERE \"email\" = @p5;"
desiredArgs := []interface{}{"john@doe.com", "John Doe", "john@doe.com", "John Does", "john@doe.com"}
2021-07-25 04:41:39 +00:00
if sql != desiredSql {
t.Error("sql is not the same as result: ", sql)
}
2021-07-25 06:47:52 +00:00
if !reflect.DeepEqual(args, desiredArgs) {
2021-07-25 04:41:39 +00:00
t.Error("args is not the same as result: ", args)
}
})
}