From 1b25a09302850bfd0dc487710e9141bbee16e93c Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Sun, 25 Jul 2021 13:58:44 +0700 Subject: [PATCH] test: error tests --- upsert.go | 6 ++++-- upsert_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/upsert.go b/upsert.go index 2baf6b0..878b2c9 100644 --- a/upsert.go +++ b/upsert.go @@ -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 } @@ -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]) } diff --git a/upsert_test.go b/upsert_test.go index 03f77f4..d57083e 100644 --- a/upsert_test.go +++ b/upsert_test.go @@ -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) + } + }) } \ No newline at end of file