Skip to content

Commit

Permalink
radio: add tests for Song.Hydrate and Metadata
Browse files Browse the repository at this point in the history
fixed Metadata not handling whitespace on the edges of its input
  • Loading branch information
Wessie committed Apr 1, 2024
1 parent 0dc43a8 commit 14c69a1
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 4 deletions.
20 changes: 16 additions & 4 deletions radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,20 +537,32 @@ func (s *Song) UntilRequestable() time.Duration {
// Hydrate tries to fill Song with data from other fields, mostly useful
// for if we have a DatabaseTrack but want to create the Song fields
func (s *Song) Hydrate() {
// trim any whitespace from the metadata
s.Metadata = strings.TrimSpace(s.Metadata)
if !s.HasTrack() {
s.Hash = NewSongHash(s.Metadata)
return
// if our metadata is empty at this point, and we have a database track
// we lookup the artist and title of the track to create our metadata
if s.Metadata == "" && s.HasTrack() {
s.Metadata = Metadata(s.Artist, s.Title)
}

if s.Metadata == "" {
s.Metadata = Metadata(s.Artist, s.Title)
// no metadata to work with, to avoid a bogus hash creation down below
// we just exit early and don't update anything
return
}

// generate a hash from the metadata
s.Hash = NewSongHash(s.Metadata)
// and if our HashLink isn't set yet update that too
if s.HashLink.IsZero() {
s.HashLink = s.Hash
}
}

func Metadata(artist, title string) string {
artist = strings.TrimSpace(artist)
title = strings.TrimSpace(title)

if artist != "" {
return fmt.Sprintf("%s - %s", artist, title)
}
Expand Down
110 changes: 110 additions & 0 deletions radio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,116 @@ import (
"github.com/stretchr/testify/assert"
)

func TestSongHydrate(t *testing.T) {
t.Run("song with just metadata", func(t *testing.T) {
a := Song{
Metadata: "we test if this gets hydrated",
}
a.Hydrate()

assert.False(t, a.Hash.IsZero(), "Hash should not be zero after hydrate")
assert.False(t, a.HashLink.IsZero(), "HashLink should not be zero after hydrate")
})

t.Run("song with a HashLink already set", func(t *testing.T) {
a := Song{
Metadata: "we test if this gets hydrated",
}
a.Hydrate()

b := Song{
Metadata: "some other metadata",
HashLink: a.Hash, // set the HashLink to the other song
}
// hydrate should now not touch HashLink but still update Hash
b.Hydrate()
assert.False(t, b.Hash.IsZero(), "Hash should not be zero after hydrate")
assert.Equal(t, a.Hash, b.HashLink)
})

t.Run("song with no metadata, but does have DatabaseTrack", func(t *testing.T) {
c := Song{
DatabaseTrack: &DatabaseTrack{
Artist: "Hello",
Title: "World",
},
}
c.Hydrate()
assert.NotEmpty(t, c.Metadata)
assert.False(t, c.Hash.IsZero())
assert.False(t, c.HashLink.IsZero())
})

t.Run("song with no metadata and no DatabaseTrack", func(t *testing.T) {
a := Song{}
a.Hydrate()

assert.Empty(t, a.Metadata, "hydrate should not update Metadata if there is nothing")
assert.True(t, a.Hash.IsZero(), "hydrate should not update Hash if there is nothing")
assert.True(t, a.HashLink.IsZero(), "hydrate shoudl not update HashLink if there is nothing")
})
}

func TestMetadata(t *testing.T) {
cases := []struct {
name string
artist string
title string
expected string
}{
{
name: "simple",
artist: "hello",
title: "world",
expected: "hello - world",
},
{
name: "missing artist",
artist: "",
title: "hello world",
expected: "hello world",
},
{
name: "whitespace at start of artist",
artist: " hello",
title: "world",
expected: "hello - world",
},
{
name: "whitespace at end of artist",
artist: "hello ",
title: "world",
expected: "hello - world",
},
{
name: "whitespace at start of title",
artist: "hello",
title: " world",
expected: "hello - world",
},
{
name: "whitespace at end of title",
artist: "hello",
title: "world ",
expected: "hello - world",
},
{
name: "whitespace only artist",
artist: " ",
title: "hello world",
expected: "hello world",
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
res := Metadata(c.artist, c.title)

assert.Equal(t, c.expected, res)
})
}
}

func TestSongEqualTo(t *testing.T) {
tp := gopter.DefaultTestParameters()
tp.MinSuccessfulTests = 500
Expand Down

0 comments on commit 14c69a1

Please sign in to comment.