From ad3481b3144dd68c0b9c31f3abbb9af3e5599e0b Mon Sep 17 00:00:00 2001 From: Joshua Raphael Date: Tue, 13 Aug 2024 10:49:52 -0700 Subject: [PATCH] write unit tests for user profile, add examples dir --- client/user_achievements.go | 6 +- client/user_profile.go | 6 +- client/user_profile_test.go | 113 +++++++++++++++++++++++++++++++++--- examples/getuserprofile.go | 25 ++++++++ http/response_test.go | 6 +- sandbox.go | 33 ----------- scripts/test.sh | 2 +- 7 files changed, 144 insertions(+), 47 deletions(-) create mode 100644 examples/getuserprofile.go delete mode 100644 sandbox.go diff --git a/client/user_achievements.go b/client/user_achievements.go index 1f4f0e4..3b03dc1 100644 --- a/client/user_achievements.go +++ b/client/user_achievements.go @@ -20,5 +20,9 @@ func (c *Client) GetUserRecentAchievements(username string, lookbackMinutes int) return nil, fmt.Errorf("calling endpoint: %w", err) } defer resp.Body.Close() - return raHttp.ResponseList[models.Achievement](resp) + achievements, err := raHttp.ResponseList[models.Achievement](resp) + if err != nil { + return nil, fmt.Errorf("parsing response list: %w", err) + } + return achievements, nil } diff --git a/client/user_profile.go b/client/user_profile.go index b529b0c..64cbb12 100644 --- a/client/user_profile.go +++ b/client/user_profile.go @@ -19,5 +19,9 @@ func (c *Client) GetUserProfile(username string) (*models.Profile, error) { return nil, fmt.Errorf("calling endpoint: %w", err) } defer resp.Body.Close() - return raHttp.ResponseObject[models.Profile](resp) + profile, err := raHttp.ResponseObject[models.Profile](resp) + if err != nil { + return nil, fmt.Errorf("parsing response object: %w", err) + } + return profile, nil } diff --git a/client/user_profile_test.go b/client/user_profile_test.go index 0bb614b..9108f56 100644 --- a/client/user_profile_test.go +++ b/client/user_profile_test.go @@ -1,20 +1,119 @@ package client_test import ( + "encoding/json" + "net/http" + "net/http/httptest" "testing" + "time" + "github.com/joshraphael/go-retroachievements/client" "github.com/joshraphael/go-retroachievements/models" + "github.com/stretchr/testify/require" ) -func TestGetUserProfile(t *testing.T) { - test := []struct { - name string - response models.Profile +func TestGetUserProfile(tt *testing.T) { + now, err := time.Parse(time.DateTime, "2024-03-02 17:27:03") + require.NoError(tt, err) + tests := []struct { + name string + username string + responseCode int + responseProfile models.Profile + responseError models.ErrorResponse + response func(profileBytes []byte, errorBytes []byte) []byte + assert func(t *testing.T, profile *models.Profile, err error) }{ { - name: "test", - response: models.Profile{}, + name: "error response", + username: "Test", + responseCode: http.StatusUnauthorized, + responseError: models.ErrorResponse{ + Message: "test", + Errors: []models.ErrorDetail{ + { + Status: http.StatusUnauthorized, + Code: "unauthorized", + Title: "Not Authorized", + }, + }, + }, + response: func(profileBytes []byte, errorBytes []byte) []byte { + return errorBytes + }, + assert: func(t *testing.T, profile *models.Profile, err error) { + require.Nil(t, profile) + require.EqualError(t, err, "parsing response object: error responses: [401] Not Authorized") + }, }, + { + name: "success", + username: "Test", + responseCode: http.StatusOK, + responseProfile: models.Profile{ + User: "xXxSnip3rxXx", + UserPic: "/some/resource.png", + MemberSince: models.DateTime{ + Time: now, + }, + RichPresenceMsg: "Playing Super Mario 64", + LastGameID: 5436, + ContribCount: 10, + ContribYield: 1, + TotalPoints: 1000, + TotalSoftcorePoints: 234, + TotalTruePoints: 512, + Permissions: 1, + Untracked: 0, + ID: 445526, + UserWallActive: true, + Motto: "Playing games", + }, + response: func(profileBytes []byte, errorBytes []byte) []byte { + return profileBytes + }, + assert: func(t *testing.T, profile *models.Profile, err error) { + require.NoError(t, err) + require.NotNil(t, profile) + require.Equal(t, "xXxSnip3rxXx", profile.User) + require.Equal(t, "/some/resource.png", profile.UserPic) + require.Equal(t, models.DateTime{ + Time: now, + }, profile.MemberSince) + require.Equal(t, "Playing Super Mario 64", profile.RichPresenceMsg) + require.Equal(t, 5436, profile.LastGameID) + require.Equal(t, 10, profile.ContribCount) + require.Equal(t, 1, profile.ContribYield) + require.Equal(t, 1000, profile.TotalPoints) + require.Equal(t, 234, profile.TotalSoftcorePoints) + require.Equal(t, 512, profile.TotalTruePoints) + require.Equal(t, 1, profile.Permissions) + require.Equal(t, 0, profile.Untracked) + require.Equal(t, 445526, profile.ID) + require.True(t, profile.UserWallActive) + require.Equal(t, "Playing games", profile.Motto) + }, + }, + } + for _, test := range tests { + tt.Run(test.name, func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + expectedPath := "/API/API_GetUserProfile.php" + if r.URL.Path != expectedPath { + t.Errorf("Expected to request '%s', got: %s", expectedPath, r.URL.Path) + } + w.WriteHeader(test.responseCode) + profileBytes, err := json.Marshal(test.responseProfile) + require.NoError(t, err) + errBytes, err := json.Marshal(test.responseError) + require.NoError(t, err) + w.Write(test.response(profileBytes, errBytes)) + })) + defer server.Close() + + client := client.New(server.URL, "some_secret") + profile, err := client.GetUserProfile(test.username) + test.assert(t, profile, err) + }) } - _ = test } diff --git a/examples/getuserprofile.go b/examples/getuserprofile.go new file mode 100644 index 0000000..aca06f2 --- /dev/null +++ b/examples/getuserprofile.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "os" + + "github.com/joshraphael/go-retroachievements/client" +) + +/* +Test script for getting user profile. Add RA_API_KEY to your env and use `go run getuserprofile.go` +*/ +func main() { + host := "https://retroachievements.org" + secret := os.Getenv("RA_API_KEY") + + raClient := client.New(host, secret) + + resp, err := raClient.GetUserProfile("jamiras") + if err != nil { + panic(err) + } + + fmt.Printf("%+v\n", resp) +} diff --git a/http/response_test.go b/http/response_test.go index c34533c..646284b 100644 --- a/http/response_test.go +++ b/http/response_test.go @@ -112,8 +112,7 @@ func TestResponseObject(tt *testing.T) { }, }, } - for i := range tests { - test := tests[i] + for _, test := range tests { tt.Run(test.name, func(t *testing.T) { objBytes, err := json.Marshal(test.objBody) require.NoError(t, err) @@ -220,8 +219,7 @@ func TestResponseList(tt *testing.T) { }, }, } - for i := range tests { - test := tests[i] + for _, test := range tests { tt.Run(test.name, func(t *testing.T) { listBytes, err := json.Marshal(test.listBody) require.NoError(t, err) diff --git a/sandbox.go b/sandbox.go deleted file mode 100644 index 1553797..0000000 --- a/sandbox.go +++ /dev/null @@ -1,33 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "os" - - "github.com/joshraphael/go-retroachievements/client" -) - -/* -Test script for various endpoints. Add RA_API_KEY to your env and use `go run sandbox.go` -*/ -func main() { - host := "https://retroachievements.org" - secret := os.Getenv("RA_API_KEY") - - raClient := client.New(host, secret) - - resp, err := raClient.GetUserProfile("HippopotamusRex") - if err != nil { - panic(err) - } - - //fmt.Printf("%+v\n", resp) - - b, err := json.Marshal(resp) - if err != nil { - fmt.Println(err) - return - } - fmt.Println(string(b)) -} diff --git a/scripts/test.sh b/scripts/test.sh index 830e924..f84eae8 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -9,7 +9,7 @@ OPEN_FILE="file://$DIR/../$OUT_HTML" set -e cd $DIR/.. go get -t ./... -go test ./... -coverpkg=./... -coverprofile ./$OUT_FILE.tmp +go test $(go list ./... | grep -v /examples) -coverpkg=./... -coverprofile ./$OUT_FILE.tmp cat $OUT_FILE.tmp | grep -v "mocks" > $OUT_FILE go tool cover -func ./$OUT_FILE go tool cover -html $OUT_FILE -o $OUT_HTML