diff --git a/library-le/client.go b/library-le/client.go index ec3730f..3714df1 100644 --- a/library-le/client.go +++ b/library-le/client.go @@ -63,7 +63,8 @@ func (libClient Client) FindAvailabelGames(branchCode int, platform string) []do } defer response.Body.Close() - games, parseResultErr := parseSearchResult(response, branchCode) + games, parseResultErr := parseSearchResult(response.Body) + //Add branchCode to games? if parseResultErr != nil { log.Fatalln(parseResultErr) return nil diff --git a/library-le/searchresult.go b/library-le/searchresult.go index da4363d..a6af116 100644 --- a/library-le/searchresult.go +++ b/library-le/searchresult.go @@ -1,8 +1,8 @@ package libraryle import ( + "io" "log" - "net/http" "github.com/PuerkitoBio/goquery" "github.com/gunni1/leipzig-library-game-stock-api/domain" @@ -14,10 +14,10 @@ const ( availabilitySelector string = "span[class^=textgruen]" ) -// Takes a http.Response from a webopac search and +// Takes a html as reader from a webopac search and // try to parse it to an array of games that are listed as available. -func parseSearchResult(searchResult *http.Response, branchCode int) ([]domain.Game, error) { - doc, docErr := goquery.NewDocumentFromReader(searchResult.Body) +func parseSearchResult(searchResult io.Reader) ([]domain.Game, error) { + doc, docErr := goquery.NewDocumentFromReader(searchResult) if docErr != nil { log.Fatal("Could not create document from response.") return nil, docErr @@ -26,7 +26,7 @@ func parseSearchResult(searchResult *http.Response, branchCode int) ([]domain.Ga doc.Find(resultItemSelector).Each(func(i int, resultItem *goquery.Selection) { title := resultItem.Find(gameTitleSelector).Text() if isAvailable(resultItem.Parent()) { - games = append(games, domain.Game{Title: title, Branch: BranchCodes[branchCode]}) + games = append(games, domain.Game{Title: title}) } }) return games, nil diff --git a/library-le/searchresult_test.go b/library-le/searchresult_test.go index 412d726..3885474 100644 --- a/library-le/searchresult_test.go +++ b/library-le/searchresult_test.go @@ -1,9 +1,33 @@ package libraryle import ( + "bufio" + "log" + "os" "testing" + + "github.com/gunni1/leipzig-library-game-stock-api/domain" + . "github.com/stretchr/testify/assert" ) func TestAvailability(t *testing.T) { + file, fileErr := os.Open("searchresult_example.html") + if fileErr != nil { + log.Fatal(fileErr) + } + fileReader := bufio.NewReader(file) + games, _ := parseSearchResult(fileReader) + + True(t, hasElement(games, "Spiel2")) + False(t, hasElement(games, "Spiel1")) + +} +func hasElement(games []domain.Game, title string) bool { + for _, game := range games { + if game.Title == title { + return true + } + } + return false }