aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pkg/database/sql/main_test.go45
-rw-r--r--pkg/database/sql/settings_test.go43
-rw-r--r--pkg/database/sql/user_test.go30
3 files changed, 88 insertions, 30 deletions
diff --git a/pkg/database/sql/main_test.go b/pkg/database/sql/main_test.go
new file mode 100644
index 0000000..c373740
--- /dev/null
+++ b/pkg/database/sql/main_test.go
@@ -0,0 +1,45 @@
+//go:build integration
+
+package sql
+
+import (
+ "context"
+ "os"
+ "testing"
+
+ "gorm.io/driver/sqlite"
+ "gorm.io/gorm"
+ "gorm.io/gorm/logger"
+)
+
+var (
+ ctx context.Context
+)
+
+func setup(t *testing.T) (*gorm.DB, func()) {
+ t.Helper()
+
+ ctx = context.Background()
+
+ file, err := os.CreateTemp("", "img_user_*.db")
+ if err != nil {
+ t.Fatalf("Error creating tmp error: %s", err.Error())
+ }
+
+ db, err := gorm.Open(sqlite.Open(file.Name()), &gorm.Config{
+ Logger: logger.Default.LogMode(logger.Info),
+ })
+ if err != nil {
+ t.Fatalf("Error openning db, error %s", err.Error())
+ }
+
+ err = Migrate(db)
+ if err != nil {
+ t.Fatalf("Error migrating db, error %s", err.Error())
+ }
+
+ return db, func() {
+ //nolint:errcheck
+ os.Remove(file.Name())
+ }
+}
diff --git a/pkg/database/sql/settings_test.go b/pkg/database/sql/settings_test.go
new file mode 100644
index 0000000..f1c859d
--- /dev/null
+++ b/pkg/database/sql/settings_test.go
@@ -0,0 +1,43 @@
+//go:build integration
+
+package sql
+
+import (
+ "testing"
+
+ "git.sr.ht/~gabrielgio/img/pkg/database/repository"
+ "git.sr.ht/~gabrielgio/img/pkg/testkit"
+)
+
+func TestSaveLoad(t *testing.T) {
+ db, tearDown := setup(t)
+ defer tearDown()
+
+ settingsRepository := NewSettingsRespository(db)
+
+ // testing saving once
+ want := &repository.Settings{
+ ShowMode: true,
+ ShowOwner: true,
+ }
+
+ err := settingsRepository.Save(ctx, want)
+ testkit.TestFatalError(t, "Save", err)
+
+ got, err := settingsRepository.Load(ctx)
+ testkit.TestFatalError(t, "Save", err)
+ testkit.TestValue(t, "Save", want, got)
+
+ // testing saving a second time
+ want = &repository.Settings{
+ ShowMode: false,
+ ShowOwner: false,
+ }
+
+ err = settingsRepository.Save(ctx, want)
+ testkit.TestFatalError(t, "Save", err)
+
+ got, err = settingsRepository.Load(ctx)
+ testkit.TestFatalError(t, "Save", err)
+ testkit.TestValue(t, "Save", want, got)
+}
diff --git a/pkg/database/sql/user_test.go b/pkg/database/sql/user_test.go
index 01f6729..db43676 100644
--- a/pkg/database/sql/user_test.go
+++ b/pkg/database/sql/user_test.go
@@ -4,43 +4,13 @@ package sql
import (
"context"
- "os"
"testing"
"github.com/google/go-cmp/cmp"
- "gorm.io/driver/sqlite"
- "gorm.io/gorm"
- "gorm.io/gorm/logger"
"git.sr.ht/~gabrielgio/img/pkg/database/repository"
)
-func setup(t *testing.T) (*gorm.DB, func()) {
- t.Helper()
-
- file, err := os.CreateTemp("", "img_user_*.db")
- if err != nil {
- t.Fatalf("Error creating tmp error: %s", err.Error())
- }
-
- db, err := gorm.Open(sqlite.Open(file.Name()), &gorm.Config{
- Logger: logger.Default.LogMode(logger.Info),
- })
- if err != nil {
- t.Fatalf("Error openning db, error %s", err.Error())
- }
-
- err = Migrate(db)
- if err != nil {
- t.Fatalf("Error migrating db, error %s", err.Error())
- }
-
- return db, func() {
- //nolint:errcheck
- os.Remove(file.Name())
- }
-}
-
func TestCreate(t *testing.T) {
t.Parallel()
db, tearDown := setup(t)