Skip to content

Commit

Permalink
add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brianstrauch committed Jul 18, 2021
1 parent 9e0ccf1 commit 0ebcff2
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 46 deletions.
10 changes: 4 additions & 6 deletions internal/back/back_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ func TestBack(t *testing.T) {
ProgressMs: 0,
Item: spotify.Item{
Track: spotify.Track{
Meta: spotify.Meta{ID: "1"},
Name: "Song",
Artists: []spotify.Artist{
{Name: "Artist"},
},
Meta: spotify.Meta{ID: "1"},
Name: "Track",
Artists: []spotify.Artist{{Name: "Artist"}},
Duration: &spotify.Duration{Duration: time.Second},
},
Type: "track",
Expand All @@ -39,7 +37,7 @@ func TestBack(t *testing.T) {

status, err := back(api)
require.NoError(t, err)
require.Equal(t, " Song\r🎵\n Artist\r🎤\n 0:00 [ ] 0:01\r▶️\n", status)
require.Equal(t, " Track\r🎵\n Artist\r🎤\n 0:00 [ ] 0:01\r▶️\n", status)
}

func TestBack_ErrNoPrevious(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions internal/device/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"spotify/internal"
"strings"

"github.com/brianstrauch/spotify"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
Expand All @@ -32,7 +31,7 @@ func NewListCommand() *cobra.Command {
}
}

func List(api *spotify.API) (string, error) {
func List(api internal.APIInterface) (string, error) {
devices, err := api.GetDevices()
if err != nil {
return "", err
Expand Down
10 changes: 10 additions & 0 deletions internal/mock_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type APIInterface interface {
RemoveSavedTracks(ids ...string) error

GetPlayback() (*spotify.Playback, error)
GetDevices() ([]*spotify.Device, error)
Play(deviceID string, uris ...string) error
Pause(deviceID string) error
SkipToNextTrack() error
Expand Down Expand Up @@ -48,6 +49,15 @@ func (m *MockAPI) GetPlayback() (*spotify.Playback, error) {
return playback.(*spotify.Playback), err
}

func (m *MockAPI) GetDevices() ([]*spotify.Device, error) {
args := m.Called()

devices := args.Get(0)
err := args.Error(1)

return devices.([]*spotify.Device), err
}

func (m *MockAPI) Play(deviceID string, uris ...string) error {
args := m.Called(deviceID, uris)
return args.Error(0)
Expand Down
10 changes: 4 additions & 6 deletions internal/next/next_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ func TestNext(t *testing.T) {
ProgressMs: 0,
Item: spotify.Item{
Track: spotify.Track{
Meta: spotify.Meta{ID: "0"},
Name: "Song",
Artists: []spotify.Artist{
{Name: "Artist"},
},
Meta: spotify.Meta{ID: "0"},
Name: "Track",
Artists: []spotify.Artist{{Name: "Artist"}},
Duration: &spotify.Duration{Duration: time.Second},
},
Type: "track",
Expand All @@ -38,7 +36,7 @@ func TestNext(t *testing.T) {

status, err := next(api)
require.NoError(t, err)
require.Equal(t, " Song\r🎵\n Artist\r🎤\n 0:00 [ ] 0:01\r▶️\n", status)
require.Equal(t, " Track\r🎵\n Artist\r🎤\n 0:00 [ ] 0:01\r▶️\n", status)
}

func TestNext_ErrNoActiveDevice(t *testing.T) {
Expand Down
63 changes: 52 additions & 11 deletions internal/p/p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ func TestP_Play(t *testing.T) {
ProgressMs: 0,
Item: spotify.Item{
Track: spotify.Track{
Meta: spotify.Meta{ID: "0"},
Name: "Song",
Artists: []spotify.Artist{
{Name: "Artist"},
},
Meta: spotify.Meta{ID: "0"},
Name: "Track",
Artists: []spotify.Artist{{Name: "Artist"}},
Duration: &spotify.Duration{Duration: time.Second},
},
Type: "track",
Expand All @@ -38,7 +36,52 @@ func TestP_Play(t *testing.T) {

status, err := p(api, "", "")
require.NoError(t, err)
require.Equal(t, " Song\r🎵\n Artist\r🎤\n 0:00 [ ] 0:01\r▶️\n", status)
require.Equal(t, " Track\r🎵\n Artist\r🎤\n 0:00 [ ] 0:01\r▶️\n", status)
}

func TestP_Play_WithArgs(t *testing.T) {
api := new(internal.MockAPI)

uri := "uri"
name := "Track"

paging := &spotify.Paging{
Tracks: spotify.TrackPage{
Items: []*spotify.Track{
{
Meta: spotify.Meta{URI: uri},
Name: name,
Artists: []spotify.Artist{{Name: "Artist"}},
},
},
},
}

playback1 := &spotify.Playback{}
playback2 := &spotify.Playback{
IsPlaying: true,
ProgressMs: 0,
Item: spotify.Item{
Track: spotify.Track{
Meta: spotify.Meta{ID: "0"},
Name: name,
Artists: []spotify.Artist{{Name: "Artist"}},
Duration: &spotify.Duration{Duration: time.Second},
},
Type: "track",
},
}

query := "track"

api.On("Search", query, 1).Return(paging, nil)
api.On("Play", "", []string{uri}).Return(nil)
api.On("GetPlayback").Return(playback1, nil).Twice()
api.On("GetPlayback").Return(playback2, nil).Once()

status, err := p(api, query, "")
require.NoError(t, err)
require.Equal(t, " Track\r🎵\n Artist\r🎤\n 0:00 [ ] 0:01\r▶️\n", status)
}

func TestP_Pause(t *testing.T) {
Expand All @@ -49,10 +92,8 @@ func TestP_Pause(t *testing.T) {
ProgressMs: 0,
Item: spotify.Item{
Track: spotify.Track{
Name: "Song",
Artists: []spotify.Artist{
{Name: "Artist"},
},
Name: "Track",
Artists: []spotify.Artist{{Name: "Artist"}},
Duration: &spotify.Duration{Duration: time.Second},
},
Type: "track",
Expand All @@ -69,7 +110,7 @@ func TestP_Pause(t *testing.T) {

status, err := p(api, "", "")
require.NoError(t, err)
require.Equal(t, " Song\r🎵\n Artist\r🎤\n 0:00 [ ] 0:01\r\n", status)
require.Equal(t, " Track\r🎵\n Artist\r🎤\n 0:00 [ ] 0:01\r\n", status)
}

func TestP_ErrNoActiveDevice(t *testing.T) {
Expand Down
8 changes: 3 additions & 5 deletions internal/pause/pause_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ func TestPause(t *testing.T) {
ProgressMs: 0,
Item: spotify.Item{
Track: spotify.Track{
Name: "Song",
Artists: []spotify.Artist{
{Name: "Artist"},
},
Name: "Track",
Artists: []spotify.Artist{{Name: "Artist"}},
Duration: &spotify.Duration{Duration: time.Second},
},
Type: "track",
Expand All @@ -38,7 +36,7 @@ func TestPause(t *testing.T) {

status, err := Pause(api, "")
require.NoError(t, err)
require.Equal(t, " Song\r🎵\n Artist\r🎤\n 0:00 [ ] 0:01\r\n", status)
require.Equal(t, " Track\r🎵\n Artist\r🎤\n 0:00 [ ] 0:01\r\n", status)
}

func TestPause_ErrAlreadyPaused(t *testing.T) {
Expand Down
55 changes: 49 additions & 6 deletions internal/play/play_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ func TestPlay(t *testing.T) {
ProgressMs: 0,
Item: spotify.Item{
Track: spotify.Track{
Meta: spotify.Meta{ID: "0"},
Name: "Song",
Artists: []spotify.Artist{
{Name: "Artist"},
},
Meta: spotify.Meta{ID: "0"},
Name: "Track",
Artists: []spotify.Artist{{Name: "Artist"}},
Duration: &spotify.Duration{Duration: time.Second},
},
Type: "track",
Expand All @@ -39,7 +37,52 @@ func TestPlay(t *testing.T) {

status, err := Play(api, "", "")
require.NoError(t, err)
require.Equal(t, " Song\r🎵\n Artist\r🎤\n 0:00 [ ] 0:01\r▶️\n", status)
require.Equal(t, " Track\r🎵\n Artist\r🎤\n 0:00 [ ] 0:01\r▶️\n", status)
}

func TestPlay_WithArgs(t *testing.T) {
api := new(internal.MockAPI)

uri := "uri"
name := "Track"

paging := &spotify.Paging{
Tracks: spotify.TrackPage{
Items: []*spotify.Track{
{
Meta: spotify.Meta{URI: uri},
Name: name,
Artists: []spotify.Artist{{Name: "Artist"}},
},
},
},
}

playback1 := &spotify.Playback{}
playback2 := &spotify.Playback{
IsPlaying: true,
ProgressMs: 0,
Item: spotify.Item{
Track: spotify.Track{
Meta: spotify.Meta{ID: "0"},
Name: name,
Artists: []spotify.Artist{{Name: "Artist"}},
Duration: &spotify.Duration{Duration: time.Second},
},
Type: "track",
},
}

query := "track"

api.On("Search", query, 1).Return(paging, nil)
api.On("Play", "", []string{uri}).Return(nil)
api.On("GetPlayback").Return(playback1, nil).Twice()
api.On("GetPlayback").Return(playback2, nil).Once()

status, err := Play(api, query, "")
require.NoError(t, err)
require.Equal(t, " Track\r🎵\n Artist\r🎤\n 0:00 [ ] 0:01\r▶️\n", status)
}

func TestPlay_ErrAlreadyPlaying(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions internal/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Queue(api internal.APIInterface, query string) (string, error) {
}

func show(track *spotify.Track) string {
output := status.PrefixLineWithEmoji("🎵", track.Name)
output += status.PrefixLineWithEmoji("🎤", status.JoinArtists(track.Artists))
return output
out := status.PrefixLineWithEmoji("🎵", track.Name)
out += status.PrefixLineWithEmoji("🎤", status.JoinArtists(track.Artists))
return out
}
13 changes: 6 additions & 7 deletions internal/queue/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@ import (
func TestQueue(t *testing.T) {
api := new(internal.MockAPI)

query := "song"
var uri string
uri := "uri"

paging := &spotify.Paging{
Tracks: spotify.TrackPage{
Items: []*spotify.Track{
{
Meta: spotify.Meta{URI: uri},
Name: "Track",
Artists: []spotify.Artist{
{Name: "Artist"},
},
Meta: spotify.Meta{URI: uri},
Name: "Track",
Artists: []spotify.Artist{{Name: "Artist"}},
},
},
},
}

query := "track"

api.On("Search", query, 1).Return(paging, nil).Once()
api.On("Queue", uri).Return(nil)

Expand Down

0 comments on commit 0ebcff2

Please # to comment.