Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
habedi committed Jan 15, 2025
1 parent 45a7e6d commit 03819fd
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand All @@ -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
Expand Down
28 changes: 28 additions & 0 deletions db/db_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 03819fd

Please sign in to comment.