test: more tests

This commit is contained in:
Reinaldy Rafli 2021-07-21 15:35:57 +07:00
parent ca2d51bc7a
commit a4dd89213f
1 changed files with 28 additions and 7 deletions

View File

@ -7,27 +7,39 @@ import (
)
func TestCreate(t *testing.T) {
t.Run("should return correct sql string with basic columns and types", func(t *testing.T) {
t.Run("should return correct sql string with all columns and types", func(t *testing.T) {
sql, _, err := bob.
CreateTable("users").
StringColumn("name").
TextColumn("password").
UUIDColumn("uuid").
StringColumn("string").
TextColumn("text").
DateColumn("date").
BooleanColumn("boolean").
IntegerColumn("integer").
IntColumn("int").
TimeStampColumn("timestamp").
TimeColumn("time").
DateColumn("date").
DateTimeColumn("datetime").
JSONColumn("json").
JSONBColumn("jsonb").
BlobColumn("blob").
AddColumn(bob.ColumnDef{Name: "custom", Type: "custom"}).
ToSql()
if err != nil {
t.Fatal(err.Error())
}
result := "CREATE TABLE \"users\" (\"name\" VARCHAR(255), \"password\" TEXT, \"date\" DATE);"
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);"
if sql != result {
t.Fatal("sql is not equal to result:", sql)
}
})
t.Run("should return correct sql with primary key and unique key", func(t *testing.T) {
t.Run("should return correct sql with extras", func(t *testing.T) {
sql, _, err := bob.CreateTable("users").
UUIDColumn("id", "PRIMARY KEY").
StringColumn("name").
StringColumn("email", "UNIQUE").
StringColumn("email", "NOT NULL", "UNIQUE").
TextColumn("password").
DateTimeColumn("date").
ToSql()
@ -35,7 +47,7 @@ func TestCreate(t *testing.T) {
if err != nil {
t.Fatal(err.Error())
}
result := "CREATE TABLE \"users\" (\"id\" UUID PRIMARY KEY, \"name\" VARCHAR(255), \"email\" VARCHAR(255) UNIQUE, \"password\" TEXT, \"date\" DATETIME);"
result := "CREATE TABLE \"users\" (\"id\" UUID PRIMARY KEY, \"name\" VARCHAR(255), \"email\" VARCHAR(255) NOT NULL UNIQUE, \"password\" TEXT, \"date\" DATETIME);"
if sql != result {
t.Fatal("sql is not equal to result:", sql)
}
@ -68,6 +80,15 @@ func TestCreate(t *testing.T) {
}
})
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())
}
})
t.Run("should emit create if not exists", func(t *testing.T) {
sql, _, err := bob.
CreateTableIfNotExists("users").