Skip to content

Commit

Permalink
test: error tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aldy505 committed Jul 25, 2021
1 parent c59e6ac commit 1b25a09
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
6 changes: 4 additions & 2 deletions upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ func (u UpsertBuilder) ToSql() (string, []interface{}, error) {

// ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (d *upsertData) ToSql() (sqlStr string, args []interface{}, err error) {
if len(d.Into) == 0 {
if len(d.Into) == 0 || d.Into == "" {
err = errors.New("upsert statements must specify a table")
return
}

if len(d.Columns) == 0 {
if len(d.Columns) == 0 || d.Columns[0] == "" {
err = errors.New("upsert statement must have at least one column")
return
}
Expand All @@ -97,7 +97,9 @@ func (d *upsertData) ToSql() (sqlStr string, args []interface{}, err error) {
if d.Dialect == MSSql {
if len(d.Key) == 0 {
err = errors.New("unique key and value must be provided for MS SQL")
return
}

sql.WriteString("IF NOT EXISTS (SELECT * FROM \""+d.Into+"\" WHERE \""+d.Key[0].(string)+"\" = ?) ")
args = append(args, d.Key[1])
}
Expand Down
43 changes: 43 additions & 0 deletions upsert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,47 @@ func TestUpsert(t *testing.T) {
t.Error("args is not the same as result: ", args)
}
})

t.Run("should emit error without table name", func(t *testing.T) {
_, _, err := bob.Upsert("", bob.Mysql).ToSql()
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) {
_, _, err := bob.Upsert("users", bob.Postgresql).ToSql()
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) {
_, _, err := bob.Upsert("users", bob.Postgresql).Columns("name", "email").ToSql()
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) {
_, _, err := bob.Upsert("users", bob.Postgresql).Columns("name", "email").Values("James", "james@mail.com").ToSql()
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) {
_, _, err := bob.Upsert("users", bob.MSSql).Columns("name", "email").Values("James", "james@mail.com").Replace("name", "Thomas").ToSql()
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) {
_, _, err := bob.Upsert("users", bob.Sqlite).Columns("name", "email").Values("James", "james@mail.com").Replace("name", "Thomas").ToSql()
if err.Error() != "unique key must be provided for PostgreSQL and SQLite" {
t.Log(err.Error())
t.Error(err)
}
})
}

0 comments on commit 1b25a09

Please sign in to comment.