mirror of https://github.com/aldy505/bob.git
test: refactored
This commit is contained in:
parent
cb9e7b01c4
commit
efa752f2ab
|
@ -178,7 +178,7 @@ func main() {
|
||||||
Available placeholder formats:
|
Available placeholder formats:
|
||||||
* `bob.Question` - `INSERT INTO "users" (name) VALUES (?)`
|
* `bob.Question` - `INSERT INTO "users" (name) VALUES (?)`
|
||||||
* `bob.Dollar` - `INSERT INTO "users" (name) VALUES ($1)`
|
* `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)`
|
* `bob.AtP` - `INSERT INTO "users" (name) VALUES (@p1)`
|
||||||
|
|
||||||
### With pgx (PostgreSQL)
|
### With pgx (PostgreSQL)
|
||||||
|
|
112
has_test.go
112
has_test.go
|
@ -7,23 +7,23 @@ import (
|
||||||
"github.com/aldy505/bob"
|
"github.com/aldy505/bob"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHas(t *testing.T) {
|
func TestHasTable(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()
|
if err != nil {
|
||||||
if err != nil {
|
t.Fatal(err.Error())
|
||||||
t.Fatal(err.Error())
|
}
|
||||||
}
|
|
||||||
|
|
||||||
result := "SELECT * FROM information_schema.tables WHERE table_name = ? AND table_schema = current_schema();"
|
result := "SELECT * FROM information_schema.tables WHERE table_name = ? AND table_schema = current_schema();"
|
||||||
if sql != result {
|
if sql != result {
|
||||||
t.Fatal("sql is not equal with result:", sql)
|
t.Fatal("sql is not equal with result:", sql)
|
||||||
}
|
}
|
||||||
argsResult := []interface{}{"users"}
|
argsResult := []interface{}{"users"}
|
||||||
if !reflect.DeepEqual(args, argsResult) {
|
if !reflect.DeepEqual(args, argsResult) {
|
||||||
t.Fatal("args is not equal with argsResult:", args)
|
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) {
|
t.Run("should be able to create a hasColumn query", func(t *testing.T) {
|
||||||
sql, args, err := bob.HasTable("users").HasColumn("name").ToSql()
|
sql, args, err := bob.HasTable("users").HasColumn("name").ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -57,45 +57,45 @@ func TestHas(t *testing.T) {
|
||||||
t.Fatal("args is not equal with argsResult:", args)
|
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()
|
func TestHas_Schema(t *testing.T) {
|
||||||
if err != nil {
|
sql, args, err := bob.HasTable("users").WithSchema("private").ToSql()
|
||||||
t.Fatal(err.Error())
|
if err != nil {
|
||||||
}
|
t.Fatal(err.Error())
|
||||||
|
}
|
||||||
result := "SELECT * FROM information_schema.tables WHERE table_name = ? AND table_schema = ?;"
|
|
||||||
if sql != result {
|
result := "SELECT * FROM information_schema.tables WHERE table_name = ? AND table_schema = ?;"
|
||||||
t.Fatal("sql is not equal with result:", sql)
|
if sql != result {
|
||||||
}
|
t.Fatal("sql is not equal with result:", sql)
|
||||||
|
}
|
||||||
argsResult := []interface{}{"users", "private"}
|
|
||||||
if !reflect.DeepEqual(args, argsResult) {
|
argsResult := []interface{}{"users", "private"}
|
||||||
t.Fatal("args is not equal with argsResult:", args)
|
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()
|
func TestHas_PlaceholderFormats(t *testing.T) {
|
||||||
if err != nil {
|
sql, args, err := bob.HasTable("users").HasColumn("name").PlaceholderFormat(bob.Dollar).ToSql()
|
||||||
t.Fatal(err.Error())
|
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 {
|
result := "SELECT * FROM information_schema.columns WHERE table_name = $1 AND column_name = $2 AND table_schema = current_schema();"
|
||||||
t.Fatal("sql is not equal with result:", sql)
|
if sql != result {
|
||||||
}
|
t.Fatal("sql is not equal with result:", sql)
|
||||||
|
}
|
||||||
argsResult := []interface{}{"users", "name"}
|
|
||||||
if !reflect.DeepEqual(args, argsResult) {
|
argsResult := []interface{}{"users", "name"}
|
||||||
t.Fatal("args is not equal with argsResult:", args)
|
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()
|
func TestHas_EmitError(t *testing.T) {
|
||||||
if err.Error() != "has statement should have a table name" {
|
_, _, err := bob.HasTable("").ToSql()
|
||||||
t.Fatal("error is different:", err.Error())
|
if err.Error() != "has statement should have a table name" {
|
||||||
}
|
t.Fatal("error is different:", err.Error())
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue