Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
107 lines
3.8 KiB
Go
107 lines
3.8 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/providers"
|
|
)
|
|
|
|
func TestProviderRepoSaveGetList(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewProviderRepo(db)
|
|
|
|
if _, found, err := repo.Get(ctx, providers.LastFM); err != nil || found {
|
|
t.Fatalf("never configured: found=%v err=%v", found, err)
|
|
}
|
|
if rows, err := repo.List(ctx); err != nil || len(rows) != 0 {
|
|
t.Fatalf("empty list: %v err=%v", rows, err)
|
|
}
|
|
|
|
at := time.Unix(1_700_000_000, 0)
|
|
want := providers.Settings{Provider: providers.LastFM, Enabled: true, APIKey: "0123456789abcdef0123456789abcdef", Contact: "x", UpdatedAt: at}
|
|
if err := repo.Save(ctx, want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, found, err := repo.Get(ctx, providers.LastFM)
|
|
if err != nil || !found {
|
|
t.Fatalf("Get: found=%v err=%v", found, err)
|
|
}
|
|
if got.Provider != want.Provider || !got.Enabled || got.APIKey != want.APIKey || got.Contact != "x" || !got.UpdatedAt.Equal(at) {
|
|
t.Errorf("got %+v, want %+v", got, want)
|
|
}
|
|
|
|
// Saving again updates the same row.
|
|
want.Enabled, want.APIKey, want.Contact = false, "", ""
|
|
want.UpdatedAt = at.Add(time.Hour)
|
|
if err := repo.Save(ctx, want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _, _ = repo.Get(ctx, providers.LastFM)
|
|
if got.Enabled || got.APIKey != "" || got.Contact != "" || !got.UpdatedAt.Equal(at.Add(time.Hour)) {
|
|
t.Errorf("after the second save: %+v", got)
|
|
}
|
|
if n := count(t, db, "SELECT COUNT(*) FROM provider_settings"); n != 1 {
|
|
t.Errorf("rows = %d, want 1: a save must update, not duplicate", n)
|
|
}
|
|
|
|
_ = repo.Save(ctx, providers.Settings{Provider: providers.MusicBrainz, Enabled: true, UpdatedAt: at})
|
|
rows, err := repo.List(ctx)
|
|
if err != nil || len(rows) != 2 {
|
|
t.Fatalf("list: %v err=%v", rows, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
// A provider that was never configured must not appear as enabled: the table
|
|
// default is "off".
|
|
func TestProviderTableDefaultsToOff(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
if _, err := db.Exec("INSERT INTO provider_settings (provider, updated_at) VALUES ('lastfm', 1)"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, found, err := NewProviderRepo(db).Get(context.Background(), providers.LastFM)
|
|
if err != nil || !found || got.Enabled || got.APIKey != "" || got.Contact != "" {
|
|
t.Fatalf("defaults: %+v found=%v err=%v", got, found, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestProviderRepoKeepsTheAddressAndTheLidarrOptions(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewProviderRepo(db)
|
|
at := time.Unix(1_700_000_000, 0)
|
|
want := providers.Settings{
|
|
Provider: providers.Lidarr, Enabled: true, APIKey: "0123456789abcdef0123456789abcdef", URL: "http://orion:8686/lidarr",
|
|
Lidarr: providers.LidarrOptions{QualityProfileID: 2, MetadataProfileID: 1, RootFolderPath: "/música/New", Monitor: "latest", SearchMissing: false},
|
|
UpdatedAt: at,
|
|
}
|
|
if err := repo.Save(ctx, want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, found, err := repo.Get(ctx, providers.Lidarr)
|
|
if err != nil || !found || got.URL != want.URL || got.Lidarr != want.Lidarr {
|
|
t.Fatalf("got %+v found=%v err=%v, want %+v", got, found, err, want)
|
|
}
|
|
want.Lidarr.SearchMissing = true
|
|
want.URL = ""
|
|
if err := repo.Save(ctx, want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _, _ = repo.Get(ctx, providers.Lidarr); got.URL != "" || !got.Lidarr.SearchMissing {
|
|
t.Errorf("after the second save: %+v", got)
|
|
}
|
|
// A provider that never had them reads them as empty.
|
|
if err := repo.Save(ctx, providers.Settings{Provider: providers.LastFM, UpdatedAt: at}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _, _ := repo.Get(ctx, providers.LastFM); got.URL != "" || got.Lidarr != (providers.LidarrOptions{}) {
|
|
t.Errorf("last.fm = %+v", got)
|
|
}
|
|
})
|
|
}
|