test: refactor & running fmt

This commit is contained in:
Reinaldy Rafli 2021-07-31 11:48:40 +07:00
parent 4a022225f2
commit b5df4ae1f0
10 changed files with 131 additions and 131 deletions

2
bob.go
View File

@ -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")

View File

@ -46,7 +46,7 @@ func (d *dropData) ToSql() (sqlStr string, args []interface{}, err error) {
sql.WriteString("IF EXISTS ") sql.WriteString("IF EXISTS ")
} }
sql.WriteString("\""+d.TableName+"\";") sql.WriteString("\"" + d.TableName + "\";")
sqlStr = sql.String() sqlStr = sql.String()
return return

View File

@ -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)

View File

@ -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()

View File

@ -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
} }

View File

@ -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)

View File

@ -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
} }

View File

@ -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)

View File

@ -106,12 +106,12 @@ func (d *upsertData) ToSql() (sqlStr string, args []interface{}, err error) {
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
@ -159,7 +159,7 @@ func (d *upsertData) ToSql() (sqlStr string, args []interface{}, err error) {
} }
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, ", "))
@ -176,9 +176,9 @@ func (d *upsertData) ToSql() (sqlStr string, args []interface{}, err error) {
// 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 {

View File

@ -7,8 +7,7 @@ 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").
@ -28,9 +27,9 @@ func TestUpsert(t *testing.T) {
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").
@ -52,9 +51,9 @@ func TestUpsert(t *testing.T) {
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").
@ -76,9 +75,9 @@ func TestUpsert(t *testing.T) {
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").
@ -100,8 +99,9 @@ func TestUpsert(t *testing.T) {
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" {