diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-29 23:51:48 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-29 23:51:48 +0200 |
commit | b15db59a46557fed62a47845705eadf1d27e4994 (patch) | |
tree | d9a2ba48cc12289c3a3628c37b2e7a4b54c34f3e | |
parent | 7a414da9a802d5eeee911b3536790a061e1d7503 (diff) | |
download | lens-b15db59a46557fed62a47845705eadf1d27e4994.tar.gz lens-b15db59a46557fed62a47845705eadf1d27e4994.tar.bz2 lens-b15db59a46557fed62a47845705eadf1d27e4994.zip |
test: Add simple test for settings
-rw-r--r-- | pkg/database/sql/main_test.go | 45 | ||||
-rw-r--r-- | pkg/database/sql/settings_test.go | 43 | ||||
-rw-r--r-- | pkg/database/sql/user_test.go | 30 |
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) |