From 5c6a59ac38b5579562039126287a80502a703e31 Mon Sep 17 00:00:00 2001 From: sentriz Date: Thu, 14 Sep 2023 00:21:01 +0100 Subject: [PATCH] add artistinfocache test --- artistinfocache/artistinfocache_test.go | 52 +++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 artistinfocache/artistinfocache_test.go diff --git a/artistinfocache/artistinfocache_test.go b/artistinfocache/artistinfocache_test.go new file mode 100644 index 0000000..29e923e --- /dev/null +++ b/artistinfocache/artistinfocache_test.go @@ -0,0 +1,52 @@ +package artistinfocache + +import ( + "context" + "net/http" + "sync/atomic" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.senan.xyz/gonic/db" + "go.senan.xyz/gonic/mockfs" + "go.senan.xyz/gonic/scrobble/lastfm" + "go.senan.xyz/gonic/scrobble/lastfm/mockclient" +) + +func TestInfoCache(t *testing.T) { + m := mockfs.New(t) + m.AddItems() + m.ScanAndClean() + + assert := assert.New(t) + + var artist db.Artist + assert.NoError(m.DB().Preload("Info").Find(&artist).Error) + assert.Greater(artist.ID, 0) + assert.Nil(artist.Info) + + var count atomic.Int32 + lastfmClient := lastfm.NewClientCustom(mockclient.New(t, func(w http.ResponseWriter, r *http.Request) { + switch method := r.URL.Query().Get("method"); method { + case "artist.getInfo": + count.Add(1) + w.Write(mockclient.ArtistGetInfoResponse) + case "artist.getTopTracks": + w.Write(mockclient.ArtistGetTopTracksResponse) + } + })) + + cache := New(m.DB(), lastfmClient) + _, err := cache.GetOrLookup(context.Background(), "", artist.ID) + require.NoError(t, err) + _, err = cache.GetOrLookup(context.Background(), "", artist.ID) + require.NoError(t, err) + + require.Equal(t, int32(1), count.Load()) + + assert.NoError(m.DB().Preload("Info").Find(&artist, "id=?", artist.ID).Error) + assert.Greater(artist.ID, 0) + assert.NotNil(artist.Info) + assert.Equal("Summary", artist.Info.Biography) +}