2021-06-25 17:04:41 +00:00
|
|
|
package bob_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aldy505/bob"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO - do more test
|
|
|
|
|
|
|
|
func TestHas(t *testing.T) {
|
|
|
|
t.Run("should be able to create a hasTable query", func(t *testing.T) {
|
2021-07-09 06:41:34 +00:00
|
|
|
sql, args, err := bob.HasTable("users").ToSql()
|
2021-06-25 17:04:41 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
t.Fatal("args is not equal with argsResult:", args)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should be able to create a hasColumn query", func(t *testing.T) {
|
2021-07-09 06:41:34 +00:00
|
|
|
sql, args, err := bob.HasTable("users").HasColumn("name").ToSql()
|
2021-06-25 17:04:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
result := "SELECT * FROM information_schema.columns WHERE table_name = ? AND column_name = ? AND table_schema = current_schema();"
|
|
|
|
if sql != result {
|
|
|
|
t.Fatal("sql is not equal with result:", sql)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) != 2 {
|
|
|
|
t.Fatal("args is not equal with argsResult:", args)
|
|
|
|
}
|
|
|
|
})
|
2021-06-26 02:57:08 +00:00
|
|
|
|
|
|
|
t.Run("should be able to create a hasColumn query (but reversed)", func(t *testing.T) {
|
2021-07-09 06:41:34 +00:00
|
|
|
sql, args, err := bob.HasColumn("name").HasTable("users").ToSql()
|
2021-06-26 02:57:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
result := "SELECT * FROM information_schema.columns WHERE table_name = ? AND column_name = ? AND table_schema = current_schema();"
|
|
|
|
if sql != result {
|
|
|
|
t.Fatal("sql is not equal with result:", sql)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) != 2 {
|
|
|
|
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) {
|
2021-07-09 06:41:34 +00:00
|
|
|
sql, args, err := bob.HasTable("users").WithSchema("private").ToSql()
|
2021-06-26 02:57:08 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) != 2 {
|
|
|
|
t.Fatal("args is not equal with argsResult:", args)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should be able to have a different placeholder", func(t *testing.T) {
|
2021-07-09 06:41:34 +00:00
|
|
|
sql, args, err := bob.HasTable("users").HasColumn("name").PlaceholderFormat(bob.Dollar).ToSql()
|
2021-06-26 02:57:08 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) != 2 {
|
|
|
|
t.Fatal("args is not equal with argsResult:", args)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should expect an error for no table name", func(t *testing.T) {
|
2021-07-09 06:41:34 +00:00
|
|
|
_, _, err := bob.HasTable("").ToSql()
|
2021-06-26 02:57:08 +00:00
|
|
|
if err.Error() != "has statement should have a table name" {
|
|
|
|
t.Fatal("error is different:", err.Error())
|
|
|
|
}
|
|
|
|
})
|
2021-06-25 17:04:41 +00:00
|
|
|
}
|