Selaa lähdekoodia

go-music: refformated code with go standards, fix bug on search funcion

Douglas Andreani 5 vuotta sitten
vanhempi
commit
3143d77bb3
2 muutettua tiedostoa jossa 77 lisäystä ja 41 poistoa
  1. 57 36
      go-music/mb.go
  2. 20 5
      go-music/mb_test.go

+ 57 - 36
go-music/mb.go

@@ -9,30 +9,39 @@ import (
 )
 
 const (
-	API_URI                = "https://musicbrainz.org/ws/2"
-	ARTIST_SEARCH_ENDPOINT = "artist?query"
+	apiURI               = "https://musicbrainz.org/ws/2"
+	artistSearchEndpoint = "artist?query"
 )
 
-type mb struct {
+//MB holds the http client and the search results
+type MB struct {
 	Client http.Client
-	artist mb_artist_search
+	artist artistSearch
 }
 
-func NewMusicBrainzSearch() mb {
-	var mb_search mb
+//NewMusicBrainzSearch returns a new Search object
+func NewMusicBrainzSearch() MB {
+
+	var mbSearch MB
 	timeout := time.Duration(10 * time.Second)
 
-	mb_search.Client = http.Client{Timeout: timeout}
-	return mb_search
+	mbSearch.Client = http.Client{Timeout: timeout}
+	return mbSearch
 }
 
-func (m mb) get_artist_name() string {
-	return m.artist.Artists[0].Name
+//GetArtistName get the artist with the highest score on the search
+func (m MB) GetArtistName() (string, error) {
+
+	if len(m.artist.Artists) == 0 {
+		return "", fmt.Errorf("could not find any artist")
+	}
+	return m.artist.Artists[0].Name, nil
 }
 
-func (m mb) search_for_artist(artist string) error {
+//SearchForArtist return a list of artists using `query` as parameter if none found returns error
+func (m *MB) SearchForArtist(query string) error {
 
-	uri := fmt.Sprintf("%s/%s=%s", API_URI, ARTIST_SEARCH_ENDPOINT, artist)
+	uri := fmt.Sprintf("%s/%s=%s", apiURI, artistSearchEndpoint, query)
 
 	req, err := http.NewRequest(
 		"GET",
@@ -61,12 +70,21 @@ func (m mb) search_for_artist(artist string) error {
 		return err
 	}
 
-	json.NewDecoder(resp.Body).Decode(&m.artist)
+	err = json.NewDecoder(resp.Body).Decode(&m.artist)
+	if err != nil {
+		fmt.Printf("could not decode body\n%v", err)
+		return err
+	}
+
+	if m.artist.Count == 0 {
+		return fmt.Errorf("cound not found artist: %s", query)
+	}
 	return nil
 }
 
-func (m mb) get_artist_tags() []mb_tag {
-	var t []mb_tag
+//GetArtistTags returns the tags in order of relevance of the current artist
+func (m MB) GetArtistTags() []Tags {
+	var t []Tags
 	for _, elem := range m.artist.Artists[0].Tags {
 		if int(elem.Count) > 0 {
 			t = append(t, elem)
@@ -76,14 +94,14 @@ func (m mb) get_artist_tags() []mb_tag {
 	return t
 }
 
-type mb_artist_search struct {
-	Created time.Time    `json:"created"`
-	Count   int          `json:"count"`
-	Offset  int          `json:"offset"`
-	Artists []mb_artists `json:"artists"`
+type artistSearch struct {
+	Created time.Time `json:"created"`
+	Count   int       `json:"count"`
+	Offset  int       `json:"offset"`
+	Artists []artists `json:"artists"`
 }
 
-type mb_area struct {
+type area struct {
 	ID       string `json:"id"`
 	Type     string `json:"type"`
 	TypeID   string `json:"type-id"`
@@ -94,7 +112,7 @@ type mb_area struct {
 	} `json:"life-span"`
 }
 
-type mb_alias struct {
+type alias struct {
 	SortName  string      `json:"sort-name"`
 	TypeID    string      `json:"type-id,omitempty"`
 	Name      string      `json:"name"`
@@ -105,15 +123,15 @@ type mb_alias struct {
 	EndDate   interface{} `json:"end-date"`
 }
 
-type mb_artists struct {
-	ID        string  `json:"id"`
-	Type      string  `json:"type,omitempty"`
-	TypeID    string  `json:"type-id,omitempty"`
-	Score     int     `json:"score"`
-	Name      string  `json:"name"`
-	SortName  string  `json:"sort-name"`
-	Country   string  `json:"country,omitempty"`
-	Area      mb_area `json:"area,omitempty"`
+type artists struct {
+	ID        string `json:"id"`
+	Type      string `json:"type,omitempty"`
+	TypeID    string `json:"type-id,omitempty"`
+	Score     int    `json:"score"`
+	Name      string `json:"name"`
+	SortName  string `json:"sort-name"`
+	Country   string `json:"country,omitempty"`
+	Area      area   `json:"area,omitempty"`
 	BeginArea struct {
 		ID       string `json:"id"`
 		Type     string `json:"type"`
@@ -129,16 +147,19 @@ type mb_artists struct {
 		Begin string      `json:"begin"`
 		Ended interface{} `json:"ended"`
 	} `json:"life-span,omitempty"`
-	Aliases        []mb_alias `json:"aliases,omitempty"`
-	Tags           []mb_tag   `json:"tags,omitempty"`
-	Disambiguation string     `json:"disambiguation,omitempty"`
+	Aliases        []alias `json:"aliases,omitempty"`
+	Tags           []Tags  `json:"tags,omitempty"`
+	Disambiguation string  `json:"disambiguation,omitempty"`
 }
 
-type mb_tag struct {
+//Tags represent Artist tags (eg. Rock, Punk, Late 2000s)
+type Tags struct {
 	Count int    `json:"count"`
 	Name  string `json:"name"`
 }
-type ByInverseCount []mb_tag
+
+//ByInverseCount return the tags in relevance order
+type ByInverseCount []Tags
 
 func (t ByInverseCount) Len() int           { return len(t) }
 func (t ByInverseCount) Less(i, j int) bool { return t[i].Count >= t[j].Count }

+ 20 - 5
go-music/mb_test.go

@@ -1,19 +1,34 @@
 package mb
 
 import (
+	"fmt"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
 )
 
-func test_mb_search(t *testing.T) {
+func TestShouldBeEqualToArtist(t *testing.T) {
 	m := NewMusicBrainzSearch()
-	_ = m.search_for_artist("Metallica")
-	assert.Equal(t, m.get_artist_name(), "Metallica")
+	err := m.SearchForArtist("Metallica")
+	if err != nil {
+		fmt.Printf("Error on search_for_artist with parameter Metallica\n")
+		return
+	}
+	name, err := m.GetArtistName()
+	if err != nil {
+		fmt.Printf("Error on get_artist_name: no artist found\n")
+	}
+	assert.Equal(t, name, "Metallica")
 }
 
-func test_error_should_be_nil(t *testing.T) {
+func TestIfArtistIsValidThenErrIsNil(t *testing.T) {
 	m := NewMusicBrainzSearch()
-	err := m.search_for_artist("Metallica")
+	err := m.SearchForArtist("Metallica")
 	assert.Nil(t, err, "err should be nil if found artist")
 }
+
+func TestErrShouldContainErrorIfArtistIsInvalid(t *testing.T) {
+	m := NewMusicBrainzSearch()
+	err := m.SearchForArtist("asdfkjahsdflkjasdhfoia")
+	assert.NotNil(t, err, "err should not be nil if found artist")
+}