refactor: clean up test create table

This commit is contained in:
Reinaldy Rafli 2021-11-09 12:23:45 +07:00
parent 8f6a475880
commit 5af711e253
No known key found for this signature in database
GPG Key ID: CFDB9400255D8CB6
1 changed files with 71 additions and 71 deletions

View File

@ -6,67 +6,67 @@ import (
"github.com/aldy505/bob" "github.com/aldy505/bob"
) )
func TestCreate(t *testing.T) { func TestCreateTable(t *testing.T) {
t.Run("should return correct sql string with all columns and types", func(t *testing.T) { sql, _, err := bob.
sql, _, err := bob. CreateTable("users").
CreateTable("users"). UUIDColumn("uuid").
UUIDColumn("uuid"). StringColumn("string").
StringColumn("string"). TextColumn("text").
TextColumn("text"). DateColumn("date").
DateColumn("date"). BooleanColumn("boolean").
BooleanColumn("boolean"). IntegerColumn("integer").
IntegerColumn("integer"). IntColumn("int").
IntColumn("int"). TimeStampColumn("timestamp").
TimeStampColumn("timestamp"). TimeColumn("time").
TimeColumn("time"). DateColumn("date").
DateColumn("date"). DateTimeColumn("datetime").
DateTimeColumn("datetime"). JSONColumn("json").
JSONColumn("json"). JSONBColumn("jsonb").
JSONBColumn("jsonb"). BlobColumn("blob").
BlobColumn("blob"). RealColumn("real").
RealColumn("real"). FloatColumn("float").
FloatColumn("float"). AddColumn(bob.ColumnDef{Name: "custom", Type: "custom"}).
AddColumn(bob.ColumnDef{Name: "custom", Type: "custom"}). ToSql()
ToSql() if err != nil {
if err != nil { t.Fatal(err.Error())
t.Fatal(err.Error()) }
} 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, \"real\" REAL, \"float\" FLOAT, \"custom\" custom);"
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, \"real\" REAL, \"float\" FLOAT, \"custom\" custom);" if sql != result {
if sql != result { t.Fatal("sql is not equal to result:", sql)
t.Fatal("sql is not equal to result:", sql) }
} }
})
t.Run("should return correct sql with extras", func(t *testing.T) { func TestCreateTable_Extras(t *testing.T) {
sql, _, err := bob.CreateTable("users"). sql, _, err := bob.CreateTable("users").
UUIDColumn("id", "PRIMARY KEY"). UUIDColumn("id", "PRIMARY KEY").
StringColumn("email", "NOT NULL", "UNIQUE"). StringColumn("email", "NOT NULL", "UNIQUE").
ToSql() ToSql()
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
result := "CREATE TABLE \"users\" (\"id\" UUID PRIMARY KEY, \"email\" VARCHAR(255) NOT NULL UNIQUE);" result := "CREATE TABLE \"users\" (\"id\" UUID PRIMARY KEY, \"email\" VARCHAR(255) NOT NULL UNIQUE);"
if sql != result { if sql != result {
t.Fatal("sql is not equal to result:", sql) t.Fatal("sql is not equal to result:", sql)
} }
}) }
t.Run("should be able to have a schema name", func(t *testing.T) { func TestCreateTable_Schema(t *testing.T) {
sql, _, err := bob. sql, _, err := bob.
CreateTable("users"). CreateTable("users").
WithSchema("private"). WithSchema("private").
StringColumn("name"). StringColumn("name").
ToSql() ToSql()
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
result := "CREATE TABLE \"private\".\"users\" (\"name\" VARCHAR(255));" result := "CREATE TABLE \"private\".\"users\" (\"name\" VARCHAR(255));"
if sql != result { if sql != result {
t.Fatal("sql is not equal to result:", sql) t.Fatal("sql is not equal to result:", sql)
} }
}) }
func TestCreateTable_Error(t *testing.T) {
t.Run("should emit error on empty table name", func(t *testing.T) { t.Run("should emit error on empty table name", func(t *testing.T) {
_, _, err := bob. _, _, err := bob.
CreateTable(""). CreateTable("").
@ -85,18 +85,18 @@ func TestCreate(t *testing.T) {
t.Fatal("should throw an error, it didn't:", err.Error()) 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. func TestCreateTable_IfNotExists(t *testing.T) {
CreateTableIfNotExists("users"). sql, _, err := bob.
TextColumn("name"). CreateTableIfNotExists("users").
ToSql() TextColumn("name").
if err != nil { ToSql()
t.Fatal(err.Error()) if err != nil {
} t.Fatal(err.Error())
result := "CREATE TABLE IF NOT EXISTS \"users\" (\"name\" TEXT);" }
if sql != result { result := "CREATE TABLE IF NOT EXISTS \"users\" (\"name\" TEXT);"
t.Fatal("sql is not equal to result: ", sql) if sql != result {
} t.Fatal("sql is not equal to result: ", sql)
}) }
} }