From b15db59a46557fed62a47845705eadf1d27e4994 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Thu, 29 Jun 2023 23:51:48 +0200 Subject: test: Add simple test for settings --- pkg/database/sql/main_test.go | 45 +++++++++++++++++++++++++++++++++++++++ pkg/database/sql/settings_test.go | 43 +++++++++++++++++++++++++++++++++++++ pkg/database/sql/user_test.go | 30 -------------------------- 3 files changed, 88 insertions(+), 30 deletions(-) create mode 100644 pkg/database/sql/main_test.go create mode 100644 pkg/database/sql/settings_test.go 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) -- cgit v1.2.3