Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Commit

Permalink
Refactor and update tests (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamplata authored Oct 17, 2020
1 parent 7df315a commit dcd2277
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 285 deletions.
101 changes: 17 additions & 84 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,102 +9,35 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"time"

"github.com/bndr/gotabulate"
"github.com/grahamplata/sixers/config"
"github.com/logrusorgru/aurora"
"github.com/grahamplata/sixers/utils"
)

// BuildURL takes the query parameters and returns a string to be used to retrieve data from the api
func BuildURL(seasonYearStart string, seasonYearEnd string) string {
// TODO currently paging is hard coded address this later
url := fmt.Sprintf("%s/?seasons[]=%s,%s&postseason=False&team_ids[]=%v&per_page=100", config.APIURL, seasonYearStart, seasonYearEnd, config.TeamID)
// urlBuilder takes the query parameters and returns a string to be used to retrieve data from the api
func urlBuilder(r Request) string {
url := fmt.Sprintf("%s/?seasons[]=%s,%s&postseason=False&team_ids[]=%v&per_page=100",
config.APIURL, r.Year, utils.DecrementString(r.Year), config.TeamID)
return url
}

// NextResponse ...
func NextResponse(response *http.Response) string {
// Fetch ...
func Fetch(r Request) Response {
var responseObject Response

responseData, _ := ioutil.ReadAll(response.Body)
json.Unmarshal(responseData, &responseObject)

for _, v := range responseObject.Data {
if v.Status != "Final" {
gameTime := strings.TrimRight(v.Time, " ")
resp := fmt.Sprintf("10,9 8 %s! There is a game currently @ %s %+s\n", config.SixersLogo, v.Status, gameTime)
return resp
}
url := urlBuilder(r)
response, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
return "Sorry, there are not any available games."
}

// RecordResponse ...
func RecordResponse(response *http.Response) string {
var gameCount, winRecord int
var responseObject Response

responseData, _ := ioutil.ReadAll(response.Body)
json.Unmarshal(responseData, &responseObject)

for _, v := range responseObject.Data {
if v.VisitorTeamScore != 0 || v.HomeTeamScore != 0 {
if v.HomeTeam.ID == config.TeamID {
if v.HomeTeamScore > v.VisitorTeamScore {
winRecord++
}
} else {
if v.HomeTeamScore < v.VisitorTeamScore {
winRecord++
}
}
gameCount++
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
wins := fmt.Sprintf("%s %d", aurora.Green("Wins:"), winRecord)
losses := fmt.Sprintf("%s %d", aurora.Red("Losses:"), (gameCount - winRecord))
pct := fmt.Sprintf("%s %.3f", aurora.Yellow("Win pct:"), (float64(winRecord) / float64(gameCount)))
return fmt.Sprintf("%s %s %s", wins, losses, pct)
}
json.Unmarshal(body, &responseObject)

// ScheduleResponse ...
func ScheduleResponse(response *http.Response) string {
var headers = []string{"Game", "Date", "Home", "Away", "Winner"}
var responseObject Response
var table [][]string

responseData, _ := ioutil.ReadAll(response.Body)
json.Unmarshal(responseData, &responseObject)

for i, v := range responseObject.Data {
var winner string
parsedTime, _ := time.Parse(time.RFC3339, v.Date)
formattedTime := parsedTime.Format(config.LayoutUS)
gameNum := strconv.Itoa(i + 1)
home := fmt.Sprintf("%s: %v", v.HomeTeam.Abbreviation, v.HomeTeamScore)
away := fmt.Sprintf("%s: %v", v.VisitorTeam.Abbreviation, v.VisitorTeamScore)

if v.VisitorTeamScore != 0 || v.HomeTeamScore != 0 {
if v.HomeTeam.ID == config.TeamID {
if v.HomeTeamScore > v.VisitorTeamScore {
winner = v.HomeTeam.FullName
}
} else {
if v.HomeTeamScore < v.VisitorTeamScore {
winner = v.HomeTeam.FullName
}
}
winner = v.VisitorTeam.FullName
}

game := []string{gameNum, formattedTime, home, away, winner}
table = append(table, game)
}
tabulate := gotabulate.Create(table)
tabulate.SetHeaders(headers)
return tabulate.Render("simple")
return responseObject
}
113 changes: 6 additions & 107 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,123 +5,22 @@ Copyright © 2019 Graham Plata <graham.plata@gmail.com>
*/

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"

"github.com/grahamplata/sixers/config"
)

var urlTests = []struct {
n string // input
m string // input
expected string // expected result
n Request // input
expected string // expected result
}{
{"a", "b", "https://www.balldontlie.io/api/v1/games/?seasons[]=a,b&postseason=False&team_ids[]=23&per_page=100"},
{"1", "2", "https://www.balldontlie.io/api/v1/games/?seasons[]=1,2&postseason=False&team_ids[]=23&per_page=100"},
{"21", "2009", "https://www.balldontlie.io/api/v1/games/?seasons[]=21,2009&postseason=False&team_ids[]=23&per_page=100"},
{"2019", "2020", "https://www.balldontlie.io/api/v1/games/?seasons[]=2019,2020&postseason=False&team_ids[]=23&per_page=100"},
{Request{Year: "2009"}, "https://www.balldontlie.io/api/v1/games/?seasons[]=2009,2008&postseason=False&team_ids[]=23&per_page=100"},
{Request{Year: "2020"}, "https://www.balldontlie.io/api/v1/games/?seasons[]=2020,2019&postseason=False&team_ids[]=23&per_page=100"},
}

func TestBuildURL(t *testing.T) {
for _, tt := range urlTests {
actual := BuildURL(tt.n, tt.m)
actual := urlBuilder(tt.n)
if actual != tt.expected {
t.Errorf("BuildURL(%s,%s): expected %s, actual %s", tt.n, tt.m, tt.expected, actual)
t.Errorf("BuildURL(%s): expected %s, actual %s", tt.n, tt.expected, actual)
}
}
}

var dummy = Response{
Data: []Game{
{
ID: 1,
Date: "2018-10-16T00:00:00.000Z",
HomeTeamScore: 123,
VisitorTeamScore: 122,
Season: 2020,
Period: 1,
Status: "Final",
Time: " ",
PostSeason: false,
HomeTeam: Team{
ID: 1,
Abbreviation: "BOS",
City: "Boston",
Conference: "East",
Division: "Atlantic",
FullName: "Boston Celtics",
Name: "Celtics",
},
VisitorTeam: Team{
ID: 23,
Abbreviation: "PHI",
City: "Philadelphia",
Conference: "East",
Division: "Atlantic",
FullName: "Philadelphia 76ers",
Name: "76ers",
},
},
},
}

func TestNextResponseNoGameTonight(t *testing.T) {
body, _ := json.Marshal(dummy)
q := &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: ioutil.NopCloser(bytes.NewReader(body)),
ContentLength: int64(len(body)),
Header: make(http.Header, 0),
}
actual := NextResponse(q)
if actual != "Sorry, there are not any available games." {
t.Errorf("NextResponse(stub): expected false, actual %s", actual)
}
}

func TestNextResponseGameTonight(t *testing.T) {
dummy.Data[0].Status = "8PM"
body, _ := json.Marshal(dummy)
q := &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: ioutil.NopCloser(bytes.NewReader(body)),
ContentLength: int64(len(body)),
Header: make(http.Header, 0),
}
actual := NextResponse(q)
gameTime := strings.TrimRight(dummy.Data[0].Time, " ")
if actual != fmt.Sprintf("10,9 8 %s! There is a game currently @ %s %+s\n", config.SixersLogo, dummy.Data[0].Status, gameTime) {
t.Errorf("NextResponse(stub): expected false, actual %s", actual)
}
}

func TestRecordResponse(t *testing.T) {
body, _ := json.Marshal(dummy)
q := &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: ioutil.NopCloser(bytes.NewReader(body)),
ContentLength: int64(len(body)),
Header: make(http.Header, 0),
}
actual := RecordResponse(q)
if actual == "1" {
t.Errorf("NextResponse(stub): expected 1, actual %s", actual)
}
}
5 changes: 5 additions & 0 deletions api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Copyright © 2019 Graham Plata <graham.plata@gmail.com>

package api

// A Request Struct to map request parameters to.
type Request struct {
Year string
}

// A Response Struct to map response to.
type Response struct {
Data []Game `json:"data"`
Expand Down
2 changes: 1 addition & 1 deletion cmd/next.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var nextCmd = &cobra.Command{
Short: "Fetches the next available sixers game date and time.",
Long: "Fetches the next available sixers game date and time.",
Run: func(cmd *cobra.Command, args []string) {
handlers.Next(cmd, args)
handlers.Next()
},
}

Expand Down
4 changes: 1 addition & 3 deletions cmd/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ import (
"github.com/spf13/cobra"
)

// recordCmd represents the record command
var recordCmd = &cobra.Command{
Use: "record",
Short: "Fetches the record for the current season.",
Long: "Fetches the record for the current season.",
Run: func(cmd *cobra.Command, args []string) {
handlers.Record(cmd, args)
handlers.Record(cmd)
},
}

func init() {
rootCmd.AddCommand(recordCmd)
// Here you will define your flags and configuration settings.
recordCmd.Flags().StringP("year", "y", "", "The year of the season you would like to query")
}
2 changes: 1 addition & 1 deletion cmd/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var scheduleCmd = &cobra.Command{
Short: "An at a glance view of the Sixers NBA season.",
Long: `An at a glance view of the Sixers NBA season.`,
Run: func(cmd *cobra.Command, args []string) {
handlers.Schedule(cmd, args)
handlers.Schedule(cmd)
},
}

Expand Down
Loading

0 comments on commit dcd2277

Please sign in to comment.