Skip to content

Commit

Permalink
write unit tests for user profile, add examples dir
Browse files Browse the repository at this point in the history
  • Loading branch information
joshraphael committed Aug 13, 2024
1 parent 1645f30 commit ad3481b
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 47 deletions.
6 changes: 5 additions & 1 deletion client/user_achievements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion client/user_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
113 changes: 106 additions & 7 deletions client/user_profile_test.go
Original file line number Diff line number Diff line change
@@ -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))

Check failure on line 110 in client/user_profile_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `w.Write` is not checked (errcheck)
}))
defer server.Close()

client := client.New(server.URL, "some_secret")
profile, err := client.GetUserProfile(test.username)
test.assert(t, profile, err)
})
}
_ = test
}
25 changes: 25 additions & 0 deletions examples/getuserprofile.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 2 additions & 4 deletions http/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 0 additions & 33 deletions sandbox.go

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ad3481b

Please sign in to comment.