Skip to content

Commit

Permalink
test(db-driver): unit test validateDbDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
Shin-Thant committed Mar 3, 2024
1 parent 008ef90 commit ff6ceac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Migrate(args []string) {
action := args[ACTION]
version := ""

if !validateDbDriver(driver) {
if !ValidateDbDriver(driver) {
log.Fatalf("Invalid database driver. Supported databases are: %s\n", DB_DRIVERS.String())
}
driver = GetSQLDriver(driver)
Expand Down Expand Up @@ -190,7 +190,7 @@ func startMigrate(migrationFiles []migrationFile, db *sql.DB, action string) {

var DB_DRIVERS ValidData = []string{"mysql", "postgres", "sqlite3"}

func validateDbDriver(inputDriver string) bool {
func ValidateDbDriver(inputDriver string) bool {
for _, name := range DB_DRIVERS {
if name == inputDriver {
return true
Expand Down
27 changes: 27 additions & 0 deletions migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gograte_test

import (
"testing"

"github.com/Shin-Thant/gograte"
)

func TestValidateDbDriver(t *testing.T) {
// validate supported drivers
inputDrivers := []string{"sqlite3", "postgres", "mysql"}
for _, driver := range inputDrivers {
result := gograte.ValidateDbDriver(driver)
if !result {
t.Errorf("Expected true but got false for driver: %s", driver)
}
}

// validate unsupported drivers
unsupportedDrivers := []string{"sqlite", "pgx", "mysqlx", "mssql"}
for _, driver := range unsupportedDrivers {
result := gograte.ValidateDbDriver(driver)
if result {
t.Errorf("Expected false but got true for driver: %s", driver)
}
}
}
2 changes: 1 addition & 1 deletion status.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Status(args []string) {
log.Fatalf("Invalid number of arguments.")
}

if !validateDbDriver(driver) {
if !ValidateDbDriver(driver) {
log.Fatalf("Invalid database driver. Supported databases are: %s\n", DB_DRIVERS.String())
}
validatedURL, err := validateDbURL(dbURL)
Expand Down

0 comments on commit ff6ceac

Please sign in to comment.