From 03819fda0cddff395ba527058b388df1450821ec Mon Sep 17 00:00:00 2001 From: Hassan Abedi Date: Thu, 16 Jan 2025 00:00:56 +0100 Subject: [PATCH] WIP --- .github/workflows/tests.yml | 5 ++--- db/db.go | 12 ++++++------ db/db_test.go | 28 ++++++++++++++++++++++++++++ go.mod | 4 ++++ go.sum | 1 + 5 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 db/db_test.go diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 4aec764..ca02bec 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -21,12 +21,11 @@ jobs: with: go-version: ${{ matrix.go-version }} - # Install dependencies and run tests + # Install dependencies and run the tests - name: Install Dependencies run: | - go mod tidy - go mod download sudo apt-get install -y make + make format continue-on-error: false - name: Run Tests diff --git a/db/db.go b/db/db.go index eb3c949..222139c 100644 --- a/db/db.go +++ b/db/db.go @@ -8,10 +8,10 @@ import ( "path/filepath" ) -// Private database variables +// Database variables var ( - db *gorm.DB - dbPath = filepath.Join(os.Getenv("HOME"), ".gogg/games.db") + db *gorm.DB // GORM database instance + Path = filepath.Join(os.Getenv("HOME"), ".gogg/games.db") // Default database path ) // InitDB initializes the database and creates the tables if they don't exist. @@ -36,8 +36,8 @@ func InitDB() error { // createDBDirectory checks if the database path exists and creates it if it doesn't. func createDBDirectory() error { - if _, err := os.Stat(filepath.Dir(dbPath)); os.IsNotExist(err) { - if err := os.MkdirAll(filepath.Dir(dbPath), 0755); err != nil { + if _, err := os.Stat(filepath.Dir(Path)); os.IsNotExist(err) { + if err := os.MkdirAll(filepath.Dir(Path), 0755); err != nil { log.Error().Err(err).Msg("Failed to create database directory") return err } @@ -48,7 +48,7 @@ func createDBDirectory() error { // openDatabase opens the database connection. func openDatabase() error { var err error - db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{}) + db, err = gorm.Open(sqlite.Open(Path), &gorm.Config{}) if err != nil { log.Error().Err(err).Msg("Failed to initialize database") return err diff --git a/db/db_test.go b/db/db_test.go new file mode 100644 index 0000000..620ce2e --- /dev/null +++ b/db/db_test.go @@ -0,0 +1,28 @@ +package db_test + +import ( + "github.com/habedi/gogg/db" + "github.com/stretchr/testify/assert" + "os" + "path/filepath" + "testing" +) + +// TestInitDB tests the InitDB function. +func TestInitDB(t *testing.T) { + tempDir := t.TempDir() + os.Setenv("HOME", tempDir) + db.Path = filepath.Join(tempDir, ".gogg/games.db") + err := db.InitDB() + assert.NoError(t, err, "InitDB should not return an error") + + // Check if the database file was created + _, statErr := os.Stat(db.Path) + assert.NoError(t, statErr, "Database file should exist") +} + +// TestCloseDB tests the CloseDB function. +func TestCloseDB(t *testing.T) { + err := db.CloseDB() + assert.NoError(t, err, "CloseDB should not return an error") +} diff --git a/go.mod b/go.mod index 3e4e93f..1b38193 100644 --- a/go.mod +++ b/go.mod @@ -17,12 +17,14 @@ require ( github.com/olekukonko/tablewriter v0.0.5 github.com/schollz/progressbar/v3 v3.18.0 github.com/spf13/cobra v1.8.1 + github.com/stretchr/testify v1.9.0 golang.org/x/term v0.28.0 ) require ( github.com/chromedp/cdproto v0.0.0-20250109193942-1ec2f6cf5d86 // indirect github.com/chromedp/sysutil v1.1.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/gobwas/httphead v0.1.0 // indirect github.com/gobwas/pool v0.2.1 // indirect github.com/gobwas/ws v1.4.0 // indirect @@ -36,8 +38,10 @@ require ( github.com/mattn/go-runewidth v0.0.16 // indirect github.com/mattn/go-sqlite3 v1.14.24 // indirect github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/spf13/pflag v1.0.5 // indirect golang.org/x/sys v0.29.0 // indirect golang.org/x/text v0.21.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 9ade7e3..992c0c2 100644 --- a/go.sum +++ b/go.sum @@ -74,6 +74,7 @@ golang.org/x/term v0.28.0 h1:/Ts8HFuMR2E6IP/jlo7QVLZHggjKQbhu/7H0LJFr3Gg= golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek= golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=