Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from lildude/weight-is-a-float
Browse files Browse the repository at this point in the history
User's weight may be a float
  • Loading branch information
lildude authored May 18, 2021
2 parents 4bd7d86 + 35c6bad commit 9b3a4c4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
8 changes: 4 additions & 4 deletions userinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (

// UserInfo is the information for the current user
type UserInfo struct {
Age int `json:"age"`
Weight int `json:"weight"`
Gender string `json:"gender"`
Email string `json:"email"`
Age int `json:"age"`
Weight float64 `json:"weight"`
Gender string `json:"gender"`
Email string `json:"email"`
}

// GetUserInfo returns the user information for the current user
Expand Down
73 changes: 50 additions & 23 deletions userinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,62 @@ package oura

import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestUserInfo(t *testing.T) {
mock := `{
"age": 27,
"weight": 80,
"gender": "male",
"email": "john.doe@the.domain"
}`

client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/userinfo", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
fmt.Fprint(w, mock)
})

got, _, err := client.GetUserInfo(context.Background())
assert.NoError(t, err, "should not return an error")

want := &UserInfo{}
json.Unmarshal([]byte(mock), want)
var infoTestCases = []struct {
name string
mock string
expected UserInfo
}{
{
name: "Regular info",
mock: `{
"age": 27,
"weight": 80,
"gender": "male",
"email": "john.doe@the.domain"
}`,
expected: UserInfo{
Age: 27,
Weight: 80.0,
Gender: "male",
Email: "john.doe@the.domain",
},
},
{
name: "Info w/ weight as float",
mock: `{
"age": 27,
"weight": 80.0,
"gender": "male",
"email": "john.doe@the.domain"
}`,
expected: UserInfo{
Age: 27,
Weight: 80.0,
Gender: "male",
Email: "john.doe@the.domain",
},
},
}

assert.ObjectsAreEqual(want, got)
func TestUserInfo(t *testing.T) {
for _, tc := range infoTestCases {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/userinfo", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
fmt.Fprint(w, tc.mock)
})

got, _, err := client.GetUserInfo(context.Background())
assert.NoError(t, err, tc.name+" should not return an error")
assert.ObjectsAreEqual(tc.expected, got)
}
}

0 comments on commit 9b3a4c4

Please sign in to comment.