mirror of https://github.com/aldy505/bob.git
test: refactor & running fmt
This commit is contained in:
parent
4a022225f2
commit
b5df4ae1f0
8
bob.go
8
bob.go
|
@ -8,8 +8,10 @@ import (
|
||||||
|
|
||||||
// ErrEmptyTable is a common database/sql error if a table is empty or no rows is returned by the query.
|
// ErrEmptyTable is a common database/sql error if a table is empty or no rows is returned by the query.
|
||||||
var ErrEmptyTable = errors.New("sql: no rows in result set")
|
var ErrEmptyTable = errors.New("sql: no rows in result set")
|
||||||
|
|
||||||
// ErrEmptyTable is a common pgx error if a table is empty or no rows is returned by the query.
|
// ErrEmptyTable is a common pgx error if a table is empty or no rows is returned by the query.
|
||||||
var ErrEmptyTablePgx = errors.New("no rows in result set")
|
var ErrEmptyTablePgx = errors.New("no rows in result set")
|
||||||
|
|
||||||
// ErrDialectNotSupported tells you whether the dialect is supported or not.
|
// ErrDialectNotSupported tells you whether the dialect is supported or not.
|
||||||
var ErrDialectNotSupported = errors.New("provided database dialect is not supported")
|
var ErrDialectNotSupported = errors.New("provided database dialect is not supported")
|
||||||
|
|
||||||
|
@ -147,7 +149,7 @@ func Truncate(table string) TruncateBuilder {
|
||||||
// Replace("age", 25).
|
// Replace("age", 25).
|
||||||
// PlaceholderFormat(bob.Question).
|
// PlaceholderFormat(bob.Question).
|
||||||
// ToSql()
|
// ToSql()
|
||||||
//
|
//
|
||||||
// // Another example for PostgreSQL:
|
// // Another example for PostgreSQL:
|
||||||
// sql, args, err = bob.
|
// sql, args, err = bob.
|
||||||
// Upsert("users", bob.PostgreSQL).
|
// Upsert("users", bob.PostgreSQL).
|
||||||
|
@ -157,7 +159,7 @@ func Truncate(table string) TruncateBuilder {
|
||||||
// Replace("age", 40).
|
// Replace("age", 40).
|
||||||
// PlaceholderFormat(bob.Dollar).
|
// PlaceholderFormat(bob.Dollar).
|
||||||
// ToSql()
|
// ToSql()
|
||||||
//
|
//
|
||||||
// // One more time, for MSSQL / SQL Server:
|
// // One more time, for MSSQL / SQL Server:
|
||||||
// sql, args, err = bob.
|
// sql, args, err = bob.
|
||||||
// Upsert("users", bob.MSSQL).
|
// Upsert("users", bob.MSSQL).
|
||||||
|
@ -169,4 +171,4 @@ func Truncate(table string) TruncateBuilder {
|
||||||
// ToSql()
|
// ToSql()
|
||||||
func Upsert(table string, dialect int) UpsertBuilder {
|
func Upsert(table string, dialect int) UpsertBuilder {
|
||||||
return BobStmtBuilder.Upsert(table, dialect)
|
return BobStmtBuilder.Upsert(table, dialect)
|
||||||
}
|
}
|
||||||
|
|
8
drop.go
8
drop.go
|
@ -11,7 +11,7 @@ type DropBuilder builder.Builder
|
||||||
|
|
||||||
type dropData struct {
|
type dropData struct {
|
||||||
TableName string
|
TableName string
|
||||||
IfExists bool
|
IfExists bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -45,9 +45,9 @@ func (d *dropData) ToSql() (sqlStr string, args []interface{}, err error) {
|
||||||
if d.IfExists {
|
if d.IfExists {
|
||||||
sql.WriteString("IF EXISTS ")
|
sql.WriteString("IF EXISTS ")
|
||||||
}
|
}
|
||||||
|
|
||||||
sql.WriteString("\""+d.TableName+"\";")
|
sql.WriteString("\"" + d.TableName + "\";")
|
||||||
|
|
||||||
sqlStr = sql.String()
|
sqlStr = sql.String()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDrop(t *testing.T) {
|
func TestDrop(t *testing.T) {
|
||||||
t.Run("should be able to create regular drop query", func (t *testing.T) {
|
t.Run("should be able to create regular drop query", func(t *testing.T) {
|
||||||
sql, _, err := bob.DropTable("users").ToSql()
|
sql, _, err := bob.DropTable("users").ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
@ -37,4 +37,4 @@ func TestDrop(t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,8 +7,6 @@ import (
|
||||||
"github.com/aldy505/bob"
|
"github.com/aldy505/bob"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO - do more test
|
|
||||||
|
|
||||||
func TestHas(t *testing.T) {
|
func TestHas(t *testing.T) {
|
||||||
t.Run("should be able to create a hasTable query", func(t *testing.T) {
|
t.Run("should be able to create a hasTable query", func(t *testing.T) {
|
||||||
sql, args, err := bob.HasTable("users").ToSql()
|
sql, args, err := bob.HasTable("users").ToSql()
|
||||||
|
|
|
@ -10,7 +10,7 @@ type RenameBuilder builder.Builder
|
||||||
|
|
||||||
type renameData struct {
|
type renameData struct {
|
||||||
From string
|
From string
|
||||||
To string
|
To string
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -38,6 +38,6 @@ func (d *renameData) ToSql() (sqlStr string, args []interface{}, err error) {
|
||||||
if len(d.From) == 0 || d.From == "" || len(d.To) == 0 || d.To == "" {
|
if len(d.From) == 0 || d.From == "" || len(d.To) == 0 || d.To == "" {
|
||||||
err = errors.New("rename statement must specify a table")
|
err = errors.New("rename statement must specify a table")
|
||||||
}
|
}
|
||||||
sqlStr = "RENAME TABLE \""+d.From+"\" TO \""+d.To+"\";"
|
sqlStr = "RENAME TABLE \"" + d.From + "\" TO \"" + d.To + "\";"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRename(t *testing.T) {
|
func TestRename(t *testing.T) {
|
||||||
t.Run("should be able to create rename query", func (t *testing.T) {
|
t.Run("should be able to create rename query", func(t *testing.T) {
|
||||||
sql, _, err := bob.RenameTable("users", "teachers").ToSql()
|
sql, _, err := bob.RenameTable("users", "teachers").ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
@ -25,4 +25,4 @@ func TestRename(t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,6 @@ func (d *truncateData) ToSql() (sqlStr string, args []interface{}, err error) {
|
||||||
if len(d.TableName) == 0 || d.TableName == "" {
|
if len(d.TableName) == 0 || d.TableName == "" {
|
||||||
err = errors.New("truncate statement must specify a table")
|
err = errors.New("truncate statement must specify a table")
|
||||||
}
|
}
|
||||||
sqlStr = "TRUNCATE \""+d.TableName+"\";"
|
sqlStr = "TRUNCATE \"" + d.TableName + "\";"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTruncate(t *testing.T) {
|
func TestTruncate(t *testing.T) {
|
||||||
t.Run("should be able to create truncate query", func (t *testing.T) {
|
t.Run("should be able to create truncate query", func(t *testing.T) {
|
||||||
sql, _, err := bob.Truncate("users").ToSql()
|
sql, _, err := bob.Truncate("users").ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
@ -25,4 +25,4 @@ func TestTruncate(t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
54
upsert.go
54
upsert.go
|
@ -11,12 +11,12 @@ import (
|
||||||
type UpsertBuilder builder.Builder
|
type UpsertBuilder builder.Builder
|
||||||
|
|
||||||
type upsertData struct {
|
type upsertData struct {
|
||||||
Dialect int
|
Dialect int
|
||||||
Into string
|
Into string
|
||||||
Columns []string
|
Columns []string
|
||||||
Values [][]interface{}
|
Values [][]interface{}
|
||||||
Key []interface{}
|
Key []interface{}
|
||||||
Replace [][]interface{}
|
Replace [][]interface{}
|
||||||
Placeholder string
|
Placeholder string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ func (u UpsertBuilder) Key(key ...interface{}) UpsertBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace sets the column and value respectively for the data to be changed on
|
// Replace sets the column and value respectively for the data to be changed on
|
||||||
// a specific row.
|
// a specific row.
|
||||||
func (u UpsertBuilder) Replace(column interface{}, value interface{}) UpsertBuilder {
|
func (u UpsertBuilder) Replace(column interface{}, value interface{}) UpsertBuilder {
|
||||||
return builder.Append(u, "Replace", []interface{}{column, value}).(UpsertBuilder)
|
return builder.Append(u, "Replace", []interface{}{column, value}).(UpsertBuilder)
|
||||||
}
|
}
|
||||||
|
@ -105,13 +105,13 @@ func (d *upsertData) ToSql() (sqlStr string, args []interface{}, err error) {
|
||||||
err = errors.New("unique key and value must be provided for MS SQL")
|
err = errors.New("unique key and value must be provided for MS SQL")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sql.WriteString("IF NOT EXISTS (SELECT * FROM \""+d.Into+"\" WHERE \""+d.Key[0].(string)+"\" = ?) ")
|
sql.WriteString("IF NOT EXISTS (SELECT * FROM \"" + d.Into + "\" WHERE \"" + d.Key[0].(string) + "\" = ?) ")
|
||||||
args = append(args, d.Key[1])
|
args = append(args, d.Key[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
sql.WriteString("INSERT INTO ")
|
sql.WriteString("INSERT INTO ")
|
||||||
sql.WriteString("\""+d.Into+"\"")
|
sql.WriteString("\"" + d.Into + "\"")
|
||||||
sql.WriteString(" ")
|
sql.WriteString(" ")
|
||||||
|
|
||||||
var columns []string
|
var columns []string
|
||||||
|
@ -134,7 +134,7 @@ func (d *upsertData) ToSql() (sqlStr string, args []interface{}, err error) {
|
||||||
}
|
}
|
||||||
values = append(values, "("+strings.Join(tempValues, ", ")+")")
|
values = append(values, "("+strings.Join(tempValues, ", ")+")")
|
||||||
}
|
}
|
||||||
|
|
||||||
sql.WriteString(strings.Join(values, ", "))
|
sql.WriteString(strings.Join(values, ", "))
|
||||||
sql.WriteString(" ")
|
sql.WriteString(" ")
|
||||||
|
|
||||||
|
@ -147,47 +147,47 @@ func (d *upsertData) ToSql() (sqlStr string, args []interface{}, err error) {
|
||||||
|
|
||||||
if d.Dialect == MySQL {
|
if d.Dialect == MySQL {
|
||||||
// INSERT INTO table (col) VALUES (values) ON DUPLICATE KEY UPDATE col = value
|
// INSERT INTO table (col) VALUES (values) ON DUPLICATE KEY UPDATE col = value
|
||||||
|
|
||||||
sql.WriteString("ON DUPLICATE KEY UPDATE ")
|
sql.WriteString("ON DUPLICATE KEY UPDATE ")
|
||||||
sql.WriteString(strings.Join(replaces, ", "))
|
sql.WriteString(strings.Join(replaces, ", "))
|
||||||
} else if d.Dialect == PostgreSQL || d.Dialect == SQLite {
|
} else if d.Dialect == PostgreSQL || d.Dialect == SQLite {
|
||||||
// INSERT INTO players (user_name, age) VALUES('steven', 32) ON CONFLICT(user_name) DO UPDATE SET age=excluded.age;
|
// INSERT INTO players (user_name, age) VALUES('steven', 32) ON CONFLICT(user_name) DO UPDATE SET age=excluded.age;
|
||||||
|
|
||||||
if len(d.Key) == 0 {
|
if len(d.Key) == 0 {
|
||||||
err = errors.New("unique key must be provided for PostgreSQL and SQLite")
|
err = errors.New("unique key must be provided for PostgreSQL and SQLite")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sql.WriteString("ON CONFLICT ")
|
sql.WriteString("ON CONFLICT ")
|
||||||
sql.WriteString("(\""+d.Key[0].(string)+"\") ")
|
sql.WriteString("(\"" + d.Key[0].(string) + "\") ")
|
||||||
sql.WriteString("DO UPDATE SET ")
|
sql.WriteString("DO UPDATE SET ")
|
||||||
sql.WriteString(strings.Join(replaces, ", "))
|
sql.WriteString(strings.Join(replaces, ", "))
|
||||||
|
|
||||||
} else if d.Dialect == MSSQL {
|
} else if d.Dialect == MSSQL {
|
||||||
// IF NOT EXISTS (SELECT * FROM dbo.Table1 WHERE ID = @ID)
|
// IF NOT EXISTS (SELECT * FROM dbo.Table1 WHERE ID = @ID)
|
||||||
// INSERT INTO dbo.Table1(ID, Name, ItemName, ItemCatName, ItemQty)
|
// INSERT INTO dbo.Table1(ID, Name, ItemName, ItemCatName, ItemQty)
|
||||||
// VALUES(@ID, @Name, @ItemName, @ItemCatName, @ItemQty)
|
// VALUES(@ID, @Name, @ItemName, @ItemCatName, @ItemQty)
|
||||||
// ELSE
|
// ELSE
|
||||||
// UPDATE dbo.Table1
|
// UPDATE dbo.Table1
|
||||||
// SET Name = @Name,
|
// SET Name = @Name,
|
||||||
// ItemName = @ItemName,
|
// ItemName = @ItemName,
|
||||||
// ItemCatName = @ItemCatName,
|
// ItemCatName = @ItemCatName,
|
||||||
// ItemQty = @ItemQty
|
// ItemQty = @ItemQty
|
||||||
// WHERE ID = @ID
|
// WHERE ID = @ID
|
||||||
|
|
||||||
sql.WriteString("ELSE ")
|
sql.WriteString("ELSE ")
|
||||||
sql.WriteString("UPDATE \""+d.Into+"\" SET ")
|
sql.WriteString("UPDATE \"" + d.Into + "\" SET ")
|
||||||
sql.WriteString(strings.Join(replaces, ", "))
|
sql.WriteString(strings.Join(replaces, ", "))
|
||||||
sql.WriteString(" WHERE \""+d.Key[0].(string)+"\" = ?")
|
sql.WriteString(" WHERE \"" + d.Key[0].(string) + "\" = ?")
|
||||||
args = append(args, d.Key[1])
|
args = append(args, d.Key[1])
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
err = ErrDialectNotSupported
|
err = ErrDialectNotSupported
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sql.WriteString(";")
|
sql.WriteString(";")
|
||||||
|
|
||||||
sqlStr = ReplacePlaceholder(sql.String(), d.Placeholder)
|
sqlStr = ReplacePlaceholder(sql.String(), d.Placeholder)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
168
upsert_test.go
168
upsert_test.go
|
@ -7,101 +7,101 @@ import (
|
||||||
"github.com/aldy505/bob"
|
"github.com/aldy505/bob"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUpsert(t *testing.T) {
|
func TestUpsert_MySQL(t *testing.T) {
|
||||||
t.Run("should be able to generate upsert query for mysql", func(t *testing.T) {
|
sql, args, err := bob.
|
||||||
sql, args, err := bob.
|
Upsert("users", bob.MySQL).
|
||||||
Upsert("users", bob.MySQL).
|
Columns("name", "email").
|
||||||
Columns("name", "email").
|
Values("John Doe", "john@doe.com").
|
||||||
Values("John Doe", "john@doe.com").
|
Replace("name", "John Does").
|
||||||
Replace("name", "John Does").
|
ToSql()
|
||||||
ToSql()
|
if err != nil {
|
||||||
if err != nil {
|
t.Error(err)
|
||||||
t.Error(err)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
desiredSql := "INSERT INTO \"users\" (\"name\", \"email\") VALUES (?, ?) ON DUPLICATE KEY UPDATE \"name\" = ?;"
|
desiredSql := "INSERT INTO \"users\" (\"name\", \"email\") VALUES (?, ?) ON DUPLICATE KEY UPDATE \"name\" = ?;"
|
||||||
desiredArgs := []interface{}{"John Doe", "john@doe.com", "John Does"}
|
desiredArgs := []interface{}{"John Doe", "john@doe.com", "John Does"}
|
||||||
|
|
||||||
if sql != desiredSql {
|
if sql != desiredSql {
|
||||||
t.Error("sql is not the same as result: ", sql)
|
t.Error("sql is not the same as result: ", sql)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(args, desiredArgs) {
|
if !reflect.DeepEqual(args, desiredArgs) {
|
||||||
t.Error("args is not the same as result: ", args)
|
t.Error("args is not the same as result: ", args)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
t.Run("should be able to generate upsert query for postgres", func(t *testing.T) {
|
func TestUpsert_PostgreSQL(t *testing.T) {
|
||||||
sql, args, err := bob.
|
sql, args, err := bob.
|
||||||
Upsert("users", bob.PostgreSQL).
|
Upsert("users", bob.PostgreSQL).
|
||||||
Columns("name", "email").
|
Columns("name", "email").
|
||||||
Values("John Doe", "john@doe.com").
|
Values("John Doe", "john@doe.com").
|
||||||
Key("email").
|
Key("email").
|
||||||
Replace("name", "John Does").
|
Replace("name", "John Does").
|
||||||
PlaceholderFormat(bob.Dollar).
|
PlaceholderFormat(bob.Dollar).
|
||||||
ToSql()
|
ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
desiredSql := "INSERT INTO \"users\" (\"name\", \"email\") VALUES ($1, $2) ON CONFLICT (\"email\") DO UPDATE SET \"name\" = $3;"
|
desiredSql := "INSERT INTO \"users\" (\"name\", \"email\") VALUES ($1, $2) ON CONFLICT (\"email\") DO UPDATE SET \"name\" = $3;"
|
||||||
desiredArgs := []interface{}{"John Doe", "john@doe.com", "John Does"}
|
desiredArgs := []interface{}{"John Doe", "john@doe.com", "John Does"}
|
||||||
|
|
||||||
if sql != desiredSql {
|
if sql != desiredSql {
|
||||||
t.Error("sql is not the same as result: ", sql)
|
t.Error("sql is not the same as result: ", sql)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(args, desiredArgs) {
|
if !reflect.DeepEqual(args, desiredArgs) {
|
||||||
t.Error("args is not the same as result: ", args)
|
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) {
|
func TestUpsert_SQLite(t *testing.T) {
|
||||||
sql, args, err := bob.
|
sql, args, err := bob.
|
||||||
Upsert("users", bob.SQLite).
|
Upsert("users", bob.SQLite).
|
||||||
Columns("name", "email").
|
Columns("name", "email").
|
||||||
Values("John Doe", "john@doe.com").
|
Values("John Doe", "john@doe.com").
|
||||||
Key("email").
|
Key("email").
|
||||||
Replace("name", "John Does").
|
Replace("name", "John Does").
|
||||||
PlaceholderFormat(bob.Question).
|
PlaceholderFormat(bob.Question).
|
||||||
ToSql()
|
ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
desiredSql := "INSERT INTO \"users\" (\"name\", \"email\") VALUES (?, ?) ON CONFLICT (\"email\") DO UPDATE SET \"name\" = ?;"
|
desiredSql := "INSERT INTO \"users\" (\"name\", \"email\") VALUES (?, ?) ON CONFLICT (\"email\") DO UPDATE SET \"name\" = ?;"
|
||||||
desiredArgs := []interface{}{"John Doe", "john@doe.com", "John Does"}
|
desiredArgs := []interface{}{"John Doe", "john@doe.com", "John Does"}
|
||||||
|
|
||||||
if sql != desiredSql {
|
if sql != desiredSql {
|
||||||
t.Error("sql is not the same as result: ", sql)
|
t.Error("sql is not the same as result: ", sql)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(args, desiredArgs) {
|
if !reflect.DeepEqual(args, desiredArgs) {
|
||||||
t.Error("args is not the same as result: ", args)
|
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) {
|
func TestUpsert_MSSQL(t *testing.T) {
|
||||||
sql, args, err := bob.
|
sql, args, err := bob.
|
||||||
Upsert("users", bob.MSSQL).
|
Upsert("users", bob.MSSQL).
|
||||||
Columns("name", "email").
|
Columns("name", "email").
|
||||||
Values("John Doe", "john@doe.com").
|
Values("John Doe", "john@doe.com").
|
||||||
Key("email", "john@doe.com").
|
Key("email", "john@doe.com").
|
||||||
Replace("name", "John Does").
|
Replace("name", "John Does").
|
||||||
PlaceholderFormat(bob.AtP).
|
PlaceholderFormat(bob.AtP).
|
||||||
ToSql()
|
ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
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;"
|
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"}
|
desiredArgs := []interface{}{"john@doe.com", "John Doe", "john@doe.com", "John Does", "john@doe.com"}
|
||||||
|
|
||||||
if sql != desiredSql {
|
if sql != desiredSql {
|
||||||
t.Error("sql is not the same as result: ", sql)
|
t.Error("sql is not the same as result: ", sql)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(args, desiredArgs) {
|
if !reflect.DeepEqual(args, desiredArgs) {
|
||||||
t.Error("args is not the same as result: ", args)
|
t.Error("args is not the same as result: ", args)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
func TestUpsert_EmitErrors(t *testing.T) {
|
||||||
t.Run("should emit error without table name", func(t *testing.T) {
|
t.Run("should emit error without table name", func(t *testing.T) {
|
||||||
_, _, err := bob.Upsert("", bob.MySQL).ToSql()
|
_, _, err := bob.Upsert("", bob.MySQL).ToSql()
|
||||||
if err == nil && err.Error() != "upsert statement must specify a table" {
|
if err == nil && err.Error() != "upsert statement must specify a table" {
|
||||||
|
@ -152,4 +152,4 @@ func TestUpsert(t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue