test(create): added more test on errors

This commit is contained in:
Reinaldy Rafli 2021-06-24 15:15:57 +07:00
parent 6c36822ecc
commit 2d720a1cde
1 changed files with 32 additions and 1 deletions

View File

@ -28,9 +28,40 @@ func TestCreate(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
result := "CREATE TABLE `users` (`id` uuid, `name` varchar(255), `password` text, `date` date); ALTER TABLE `users` ADD PRIMARY KEY (`id`); ALTER TABLE `users` ADD UNIQUE (`email`)" result := "CREATE TABLE `users` (`id` uuid, `name` varchar(255), `email` varchar(255), `password` text, `date` date); ALTER TABLE `users` ADD PRIMARY KEY (`id`); ALTER TABLE `users` ADD UNIQUE (`email`);"
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 emit error on unmatched column and types length", func(t *testing.T) {
_, _, err := bob.CreateTable("users").
Columns("id", "name", "email", "password", "date").
Types("uuid", "varchar(255)", "varchar(255)", "date").
ToSql()
if err.Error() != "columns and types should have equal length" {
t.Fatal("should throw an error, it didn't:", err.Error())
}
})
t.Run("should emit error on empty table name", func(t *testing.T) {
_, _, err := bob.CreateTable("").Columns("name").Types("text").ToSql()
if err.Error() != "create statements must specify a table" {
t.Fatal("should throw an error, it didn't:", err.Error())
}
})
t.Run("should emit error for primary key not in columns", func(t *testing.T) {
_, _, err := bob.CreateTable("").Columns("name").Types("text").Primary("id").ToSql()
if err.Error() != "supplied primary column name doesn't exists on columns" {
t.Fatal("should throw an error, it didn't:", err.Error())
}
})
t.Run("should emit error for unique key not in columns", func(t *testing.T) {
_, _, err := bob.CreateTable("").Columns("name").Types("text").Unique("id").ToSql()
if err.Error() != "supplied unique column name doesn't exists on columns" {
t.Fatal("should throw an error, it didn't:", err.Error())
}
})
} }