bob/create_test.go

106 lines
3.0 KiB
Go
Raw Normal View History

2021-06-24 07:43:28 +00:00
package bob_test
import (
"testing"
"github.com/aldy505/bob"
)
func TestCreate(t *testing.T) {
2021-07-21 08:35:57 +00:00
t.Run("should return correct sql string with all columns and types", func(t *testing.T) {
2021-07-21 07:50:05 +00:00
sql, _, err := bob.
CreateTable("users").
2021-07-21 08:35:57 +00:00
UUIDColumn("uuid").
StringColumn("string").
TextColumn("text").
DateColumn("date").
BooleanColumn("boolean").
IntegerColumn("integer").
IntColumn("int").
TimeStampColumn("timestamp").
TimeColumn("time").
2021-07-21 07:50:05 +00:00
DateColumn("date").
2021-07-21 08:35:57 +00:00
DateTimeColumn("datetime").
JSONColumn("json").
JSONBColumn("jsonb").
BlobColumn("blob").
AddColumn(bob.ColumnDef{Name: "custom", Type: "custom"}).
2021-07-21 07:50:05 +00:00
ToSql()
2021-06-24 07:43:28 +00:00
if err != nil {
t.Fatal(err.Error())
}
2021-07-21 08:35:57 +00:00
result := "CREATE TABLE \"users\" (\"uuid\" UUID, \"string\" VARCHAR(255), \"text\" TEXT, \"date\" DATE, \"boolean\" BOOLEAN, \"integer\" INTEGER, \"int\" INT, \"timestamp\" TIMESTAMP, \"time\" TIME, \"date\" DATE, \"datetime\" DATETIME, \"json\" JSON, \"jsonb\" JSONB, \"blob\" BLOB, \"custom\" custom);"
2021-06-24 07:43:28 +00:00
if sql != result {
t.Fatal("sql is not equal to result:", sql)
}
})
2021-07-21 08:35:57 +00:00
t.Run("should return correct sql with extras", func(t *testing.T) {
2021-06-24 07:43:28 +00:00
sql, _, err := bob.CreateTable("users").
2021-07-21 07:50:05 +00:00
UUIDColumn("id", "PRIMARY KEY").
StringColumn("name").
2021-07-21 08:35:57 +00:00
StringColumn("email", "NOT NULL", "UNIQUE").
2021-07-21 07:50:05 +00:00
TextColumn("password").
DateTimeColumn("date").
ToSql()
2021-07-21 07:50:05 +00:00
2021-06-24 07:43:28 +00:00
if err != nil {
t.Fatal(err.Error())
}
2021-07-21 08:35:57 +00:00
result := "CREATE TABLE \"users\" (\"id\" UUID PRIMARY KEY, \"name\" VARCHAR(255), \"email\" VARCHAR(255) NOT NULL UNIQUE, \"password\" TEXT, \"date\" DATETIME);"
2021-06-24 07:43:28 +00:00
if sql != result {
t.Fatal("sql is not equal to result:", sql)
}
})
2021-06-25 17:04:41 +00:00
t.Run("should be able to have a schema name", func(t *testing.T) {
2021-07-21 07:50:05 +00:00
sql, _, err := bob.
CreateTable("users").
WithSchema("private").
StringColumn("name").
TextColumn("password").
DateColumn("date").
ToSql()
2021-06-25 17:04:41 +00:00
if err != nil {
t.Fatal(err.Error())
}
2021-07-21 07:50:05 +00:00
result := "CREATE TABLE \"private\".\"users\" (\"name\" VARCHAR(255), \"password\" TEXT, \"date\" DATE);"
2021-06-25 17:04:41 +00:00
if sql != result {
t.Fatal("sql is not equal to result:", sql)
}
})
t.Run("should emit error on empty table name", func(t *testing.T) {
2021-07-21 07:50:05 +00:00
_, _, err := bob.
CreateTable("").
StringColumn("name").
ToSql()
if err.Error() != "create statements must specify a table" {
t.Fatal("should throw an error, it didn't:", err.Error())
}
})
2021-07-21 08:35:57 +00:00
t.Run("should emit error if no column were specified", func(t *testing.T) {
_, _, err := bob.
CreateTable("users").
ToSql()
if err.Error() != "a table should at least have one column" {
t.Fatal("should throw an error, it didn't:", err.Error())
}
})
2021-07-09 06:39:30 +00:00
t.Run("should emit create if not exists", func(t *testing.T) {
2021-07-21 07:50:05 +00:00
sql, _, err := bob.
CreateTableIfNotExists("users").
TextColumn("name").
ToSql()
2021-07-09 06:39:30 +00:00
if err != nil {
t.Fatal(err.Error())
}
2021-07-21 07:50:05 +00:00
result := "CREATE TABLE IF NOT EXISTS \"users\" (\"name\" TEXT);"
2021-07-09 06:39:30 +00:00
if sql != result {
t.Fatal("sql is not equal to result: ", sql)
}
})
2021-06-24 07:43:28 +00:00
}