Quellcode durchsuchen

go-music: refactor code and add testing scenarios

Douglas R Andreani vor 5 Jahren
Ursprung
Commit
6d1f266d5f
3 geänderte Dateien mit 120 neuen und 79 gelöschten Zeilen
  1. 2 0
      go-music/go.mod
  2. 98 79
      go-music/mb.go
  3. 20 0
      go-music/mb_test.go

+ 2 - 0
go-music/go.mod

@@ -1,3 +1,5 @@
 module go-music
 
 go 1.14
+
+require github.com/stretchr/testify v1.5.1

+ 98 - 79
go-music/mb.go

@@ -1,22 +1,42 @@
-package main
+package mb
 
 import (
-	"net/http"
-	"fmt"
-	"os"
-	"time"
 	"encoding/json"
+	"fmt"
+	"net/http"
 	"sort"
+	"time"
 )
 
-func main(){
+const (
+	API_URI                = "https://musicbrainz.org/ws/2"
+	ARTIST_SEARCH_ENDPOINT = "artist?query"
+)
+
+type mb struct {
+	Client http.Client
+	artist mb_artist_search
+}
 
-	timeout := time.Duration( 10 * time.Second )
-	client := http.Client { Timeout: timeout }
+func NewMusicBrainzSearch() mb {
+	var mb_search mb
+	timeout := time.Duration(10 * time.Second)
+
+	mb_search.Client = http.Client{Timeout: timeout}
+	return mb_search
+}
+
+func (m mb) get_artist_name() string {
+	return m.artist.Artists[0].Name
+}
+
+func (m mb) search_for_artist(artist string) error {
+
+	uri := fmt.Sprintf("%s/%s=%s", API_URI, ARTIST_SEARCH_ENDPOINT, artist)
 
 	req, err := http.NewRequest(
 		"GET",
-		"https://musicbrainz.org/ws/2/artist?query=Metallica",
+		uri,
 		nil)
 
 	req.Header.Set(
@@ -25,34 +45,29 @@ func main(){
 	req.Header.Set("Content-Type", "application/json")
 	req.Header.Set("Accept", "application/json")
 
-	if (err != nil) {
-		fmt.Println("error creating request\n%v", err)
-		os.Exit(1)
+	if err != nil {
+		fmt.Printf("error creating request\n%v", err)
+		return err
 	}
 
-	resp, err := client.Do(req)
-	if (err != nil) {
-		fmt.Println("error opening request\n%v", err)
-		os.Exit(1)
+	resp, err := m.Client.Do(req)
+	if err != nil {
+		fmt.Printf("error opening request\n%v", err)
+		return err
 	}
 	defer resp.Body.Close()
-	if (err != nil) {
-		fmt.Println("could not read body\n%v", err)
-		os.Exit(1)
+	if err != nil {
+		fmt.Printf("could not read body\n%v", err)
+		return err
 	}
-	var result mb_artist_search
-	json.NewDecoder(resp.Body).Decode(&result)
 
-	fmt.Println(result.Artists[0].Name)
-
-	for _, element := range get_artist_tags(&result){
-		fmt.Printf("\ntag: %2d: %s", element.Count, element.Name)
-	}
+	json.NewDecoder(resp.Body).Decode(&m.artist)
+	return nil
 }
 
-func get_artist_tags (res *mb_artist_search) []mb_tag{
+func (m mb) get_artist_tags() []mb_tag {
 	var t []mb_tag
-	for _, elem := range res.Artists[0].Tags  {
+	for _, elem := range m.artist.Artists[0].Tags {
 		if int(elem.Count) > 0 {
 			t = append(t, elem)
 		}
@@ -61,66 +76,70 @@ func get_artist_tags (res *mb_artist_search) []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 mb_area struct {
+	ID       string `json:"id"`
+	Type     string `json:"type"`
+	TypeID   string `json:"type-id"`
+	Name     string `json:"name"`
+	SortName string `json:"sort-name"`
+	LifeSpan struct {
+		Ended interface{} `json:"ended"`
+	} `json:"life-span"`
+}
 
+type mb_alias struct {
+	SortName  string      `json:"sort-name"`
+	TypeID    string      `json:"type-id,omitempty"`
+	Name      string      `json:"name"`
+	Locale    interface{} `json:"locale"`
+	Type      string      `json:"type"`
+	Primary   interface{} `json:"primary"`
+	BeginDate interface{} `json:"begin-date"`
+	EndDate   interface{} `json:"end-date"`
+}
 
-type mb_artist_search struct {
-	Created time.Time `json:"created"`
-	Count   int       `json:"count"`
-	Offset  int       `json:"offset"`
-	Artists []struct {
+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"`
+	BeginArea struct {
 		ID       string `json:"id"`
-		Type     string `json:"type,omitempty"`
-		TypeID   string `json:"type-id,omitempty"`
-		Score    int    `json:"score"`
+		Type     string `json:"type"`
+		TypeID   string `json:"type-id"`
 		Name     string `json:"name"`
 		SortName string `json:"sort-name"`
-		Country  string `json:"country,omitempty"`
-		Area     struct {
-			ID       string `json:"id"`
-			Type     string `json:"type"`
-			TypeID   string `json:"type-id"`
-			Name     string `json:"name"`
-			SortName string `json:"sort-name"`
-			LifeSpan struct {
-				Ended interface{} `json:"ended"`
-			} `json:"life-span"`
-		} `json:"area,omitempty"`
-		BeginArea struct {
-			ID       string `json:"id"`
-			Type     string `json:"type"`
-			TypeID   string `json:"type-id"`
-			Name     string `json:"name"`
-			SortName string `json:"sort-name"`
-			LifeSpan struct {
-				Ended interface{} `json:"ended"`
-			} `json:"life-span"`
-		} `json:"begin-area,omitempty"`
-		Isnis    []string `json:"isnis,omitempty"`
 		LifeSpan struct {
-			Begin string      `json:"begin"`
 			Ended interface{} `json:"ended"`
-		} `json:"life-span,omitempty"`
-		Aliases []struct {
-			SortName  string      `json:"sort-name"`
-			TypeID    string      `json:"type-id,omitempty"`
-			Name      string      `json:"name"`
-			Locale    interface{} `json:"locale"`
-			Type      string      `json:"type"`
-			Primary   interface{} `json:"primary"`
-			BeginDate interface{} `json:"begin-date"`
-			EndDate   interface{} `json:"end-date"`
-		} `json:"aliases,omitempty"`
-		Tags []mb_tag   `json:"tags,omitempty"`
-		Disambiguation string `json:"disambiguation,omitempty"`
-	} `json:"artists"`
+		} `json:"life-span"`
+	} `json:"begin-area,omitempty"`
+	Isnis    []string `json:"isnis,omitempty"`
+	LifeSpan 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"`
 }
 
-type mb_tag struct{
-	Count int `json:"count"`
-	Name string `json:"name"`
+type mb_tag struct {
+	Count int    `json:"count"`
+	Name  string `json:"name"`
 }
 type ByInverseCount []mb_tag
-func (t ByInverseCount) Len() int { return len(t) }
-func (t ByInverseCount) Less (i, j int) bool { return t[i].Count >= t[j].Count }
-func (t ByInverseCount) Swap (i,j int) { t[i], t[j] = t[j], t[i] }
+
+func (t ByInverseCount) Len() int           { return len(t) }
+func (t ByInverseCount) Less(i, j int) bool { return t[i].Count >= t[j].Count }
+func (t ByInverseCount) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }

+ 20 - 0
go-music/mb_test.go

@@ -0,0 +1,20 @@
+package mb
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func test_mb_search(t *testing.T) {
+	m := NewMusicBrainzSearch()
+	m.search_for_artist("Metallica")
+	assert.Equal(t, m.get_artist_name(), "Metallica")
+
+}
+
+func test_error_should_be_nil(t *testing.T) {
+	m := NewMusicBrainzSearch()
+	err := m.search_for_artist("Metallica")
+	assert.Nil(t, err, "err should be nil if found artist")
+}