Skip to content

Commit

Permalink
fix(test): pass in db location instead of hardcoding in function
Browse files Browse the repository at this point in the history
  • Loading branch information
musaubrian committed Jan 5, 2024
1 parent 0706c40 commit bdc4b0e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/db/**
test.db
11 changes: 9 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"log"
"os"
"path"

"github.com/cheynewallace/tabby"
"github.com/musaubrian/tinygo/internal/model"
Expand All @@ -28,11 +29,17 @@ func Execute() {
log.Fatal("Cannot create directory: ", err)
}

if err := model.SetupDB(); err != nil {
homePath, err := utils.GetPath()
if err != nil {
log.Fatal(err)
}
fullPath := path.Join(homePath, "tinygo.db")

if err := model.SetupDB(fullPath); err != nil {
log.Fatal("Db setup error ", err)
}

err := rootCmd.Execute()
err = rootCmd.Execute()
if err != nil {
os.Exit(1)
}
Expand Down
13 changes: 2 additions & 11 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
package model

import (
"path"

"github.com/musaubrian/tinygo/internal/utils"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
Expand All @@ -25,16 +22,10 @@ var db *gorm.DB
// SetupDb creates a connection to the db
// and initializes the table and columns
// Returns a possible error
func SetupDB() error {
func SetupDB(dbLoc string) error {
var err error

homePath, err := utils.GetPath()
if err != nil {
return err
}
fullPath := path.Join(homePath, "tinygo.db")

db, err = gorm.Open(sqlite.Open(fullPath), &gorm.Config{})
db, err = gorm.Open(sqlite.Open(dbLoc), &gorm.Config{})
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
)

func TestSetupDb(t *testing.T) {
err := model.SetupDB()

err := model.SetupDB("../../test.db")
if err != nil {
t.Error("Expected nil got an error")
}
Expand Down

0 comments on commit bdc4b0e

Please sign in to comment.