Skip to content

Commit

Permalink
test: refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
aldy505 committed Jul 31, 2021
1 parent cb9e7b0 commit efa752f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func main() {
Available placeholder formats:
* `bob.Question` - `INSERT INTO "users" (name) VALUES (?)`
* `bob.Dollar` - `INSERT INTO "users" (name) VALUES ($1)`
* `bob.Colon` - `INSERT INTO "users" (name) VALUES (:1)` (Yes, I know this is kinda wrong. I'm thinking of removing it.)
* `bob.Colon` - `INSERT INTO "users" (name) VALUES (:1)`
* `bob.AtP` - `INSERT INTO "users" (name) VALUES (@p1)`

### With pgx (PostgreSQL)
Expand Down
108 changes: 54 additions & 54 deletions has_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ import (
"github.com/aldy505/bob"
)

func TestHas(t *testing.T) {
t.Run("should be able to create a hasTable query", func(t *testing.T) {
sql, args, err := bob.HasTable("users").ToSql()
if err != nil {
t.Fatal(err.Error())
}

result := "SELECT * FROM information_schema.tables WHERE table_name = ? AND table_schema = current_schema();"
if sql != result {
t.Fatal("sql is not equal with result:", sql)
}
argsResult := []interface{}{"users"}
if !reflect.DeepEqual(args, argsResult) {
t.Fatal("args is not equal with argsResult:", args)
}
})
func TestHasTable(t *testing.T) {
sql, args, err := bob.HasTable("users").ToSql()
if err != nil {
t.Fatal(err.Error())
}

result := "SELECT * FROM information_schema.tables WHERE table_name = ? AND table_schema = current_schema();"
if sql != result {
t.Fatal("sql is not equal with result:", sql)
}
argsResult := []interface{}{"users"}
if !reflect.DeepEqual(args, argsResult) {
t.Fatal("args is not equal with argsResult:", args)
}
}

func TestHasColumn(t *testing.T) {
t.Run("should be able to create a hasColumn query", func(t *testing.T) {
sql, args, err := bob.HasTable("users").HasColumn("name").ToSql()
if err != nil {
Expand Down Expand Up @@ -57,45 +57,45 @@ func TestHas(t *testing.T) {
t.Fatal("args is not equal with argsResult:", args)
}
})
}

t.Run("should be able to create a hasTable query with schema", func(t *testing.T) {
sql, args, err := bob.HasTable("users").WithSchema("private").ToSql()
if err != nil {
t.Fatal(err.Error())
}

result := "SELECT * FROM information_schema.tables WHERE table_name = ? AND table_schema = ?;"
if sql != result {
t.Fatal("sql is not equal with result:", sql)
}

argsResult := []interface{}{"users", "private"}
if !reflect.DeepEqual(args, argsResult) {
t.Fatal("args is not equal with argsResult:", args)
}
})

t.Run("should be able to have a different placeholder - dollar", func(t *testing.T) {
sql, args, err := bob.HasTable("users").HasColumn("name").PlaceholderFormat(bob.Dollar).ToSql()
if err != nil {
t.Fatal(err.Error())
}

result := "SELECT * FROM information_schema.columns WHERE table_name = $1 AND column_name = $2 AND table_schema = current_schema();"
if sql != result {
t.Fatal("sql is not equal with result:", sql)
}
func TestHas_Schema(t *testing.T) {
sql, args, err := bob.HasTable("users").WithSchema("private").ToSql()
if err != nil {
t.Fatal(err.Error())
}

result := "SELECT * FROM information_schema.tables WHERE table_name = ? AND table_schema = ?;"
if sql != result {
t.Fatal("sql is not equal with result:", sql)
}

argsResult := []interface{}{"users", "private"}
if !reflect.DeepEqual(args, argsResult) {
t.Fatal("args is not equal with argsResult:", args)
}
}

argsResult := []interface{}{"users", "name"}
if !reflect.DeepEqual(args, argsResult) {
t.Fatal("args is not equal with argsResult:", args)
}
})
func TestHas_PlaceholderFormats(t *testing.T) {
sql, args, err := bob.HasTable("users").HasColumn("name").PlaceholderFormat(bob.Dollar).ToSql()
if err != nil {
t.Fatal(err.Error())
}

result := "SELECT * FROM information_schema.columns WHERE table_name = $1 AND column_name = $2 AND table_schema = current_schema();"
if sql != result {
t.Fatal("sql is not equal with result:", sql)
}

argsResult := []interface{}{"users", "name"}
if !reflect.DeepEqual(args, argsResult) {
t.Fatal("args is not equal with argsResult:", args)
}
}

t.Run("should expect an error for no table name", func(t *testing.T) {
_, _, err := bob.HasTable("").ToSql()
if err.Error() != "has statement should have a table name" {
t.Fatal("error is different:", err.Error())
}
})
func TestHas_EmitError(t *testing.T) {
_, _, err := bob.HasTable("").ToSql()
if err.Error() != "has statement should have a table name" {
t.Fatal("error is different:", err.Error())
}
}

0 comments on commit efa752f

Please sign in to comment.