bob/upsert_test.go

155 lines
5.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-31 04:44:21 +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-31 04:44:21 +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.
2021-07-31 04:44:21 +00:00
Upsert("users", bob.SQLite).
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").
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.
2021-07-31 04:44:21 +00:00
Upsert("users", bob.MSSQL).
2021-07-25 04:41:39 +00:00
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)
}
})
2021-07-25 06:58:44 +00:00
t.Run("should emit error without table name", func(t *testing.T) {
2021-07-31 04:44:21 +00:00
_, _, err := bob.Upsert("", bob.MySQL).ToSql()
2021-07-25 06:58:44 +00:00
if err == nil && err.Error() != "upsert statement must specify a table" {
t.Error(err)
}
})
t.Run("should emit error without columns", func(t *testing.T) {
2021-07-31 04:44:21 +00:00
_, _, err := bob.Upsert("users", bob.PostgreSQL).ToSql()
2021-07-25 06:58:44 +00:00
if err.Error() != "upsert statement must have at least one column" {
t.Error(err)
}
})
t.Run("should emit error without values", func(t *testing.T) {
2021-07-31 04:44:21 +00:00
_, _, err := bob.Upsert("users", bob.PostgreSQL).Columns("name", "email").ToSql()
2021-07-25 06:58:44 +00:00
if err.Error() != "upsert statements must have at least one set of values" {
t.Error(err)
}
})
t.Run("should emit error without replaces", func(t *testing.T) {
2021-07-31 04:44:21 +00:00
_, _, err := bob.Upsert("users", bob.PostgreSQL).Columns("name", "email").Values("James", "james@mail.com").ToSql()
2021-07-25 06:58:44 +00:00
if err.Error() != "upsert statement must have at least one key value pair to be replaced" {
t.Error(err)
}
})
t.Run("should emit error without key and value for mssql", func(t *testing.T) {
2021-07-31 04:44:21 +00:00
_, _, err := bob.Upsert("users", bob.MSSQL).Columns("name", "email").Values("James", "james@mail.com").Replace("name", "Thomas").ToSql()
2021-07-25 06:58:44 +00:00
if err.Error() != "unique key and value must be provided for MS SQL" {
t.Error(err)
}
})
t.Run("should emit error without key and value for mssql", func(t *testing.T) {
2021-07-31 04:44:21 +00:00
_, _, err := bob.Upsert("users", bob.SQLite).Columns("name", "email").Values("James", "james@mail.com").Replace("name", "Thomas").ToSql()
2021-07-25 06:58:44 +00:00
if err.Error() != "unique key must be provided for PostgreSQL and SQLite" {
t.Log(err.Error())
t.Error(err)
}
})
2021-07-25 07:01:07 +00:00
t.Run("should emit error if dialect not supported", func(t *testing.T) {
_, _, err := bob.Upsert("users", 100).Columns("name", "email").Values("James", "james@mail.com").Replace("name", "Thomas").ToSql()
if err.Error() != "provided database dialect is not supported" {
t.Log(err.Error())
t.Error(err)
}
})
2021-07-25 04:41:39 +00:00
}