Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for JSON marshal of project types #14

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
module github.com/nstratos/go-myanimelist

go 1.16
go 1.18

require golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43

require (
github.com/golang/protobuf v1.4.2 // indirect
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.25.0 // indirect
)
30 changes: 30 additions & 0 deletions mal/mal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,36 @@ func testBody(t *testing.T, r *http.Request, want string) {
}
}

func testJSONMarshal[T any](t *testing.T, input T, want string) {
t.Helper()

gotOut, err := json.Marshal(input)
if err != nil {
t.Errorf("Cannot json.Marshal 'input' of type %T: %v\n\ninput value is: %#v", input, err, input)
}

// First we validate the wanted JSON.
if ok := json.Valid([]byte(want)); !ok {
t.Fatalf("Provided 'want' is not a valid JSON: %s", want)
}
// Then we unmarshal the wanted JSON to the same type as the input to verify
// that all fields decode correctly.
v := new(T)
if err := json.Unmarshal([]byte(want), v); err != nil {
t.Errorf("Cannot json.Unmarshal 'want' to type %T: %v\n\nwant string is: %s", input, err, want)
}
// Finally we marshal again so that the fields are sorted in order to
// compare the JSON strings.
wantOut, err := json.Marshal(v)
if err != nil {
t.Errorf("Cannot json.Marshal 'want' which has first been json.Unmarshal-ed to type %T: %v\n\nwant value is: %#v", v, err, v)
}

if got, want := string(gotOut), string(wantOut); got != want {
t.Errorf("json.Marshal(%#v) ->\n\nhave: %s\n\nwant: %s", input, got, want)
}
}

func testMethod(t *testing.T, r *http.Request, want string) {
if want != r.Method {
t.Errorf("Request method = %v, want %v", r.Method, want)
Expand Down
62 changes: 62 additions & 0 deletions mal/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"reflect"
"testing"
"time"
)

func TestUserServiceMyInfo(t *testing.T) {
Expand Down Expand Up @@ -49,3 +50,64 @@ func TestUserServiceMyInfoError(t *testing.T) {
}
testErrorResponse(t, err, ErrorResponse{Err: "not_found"})
}

func TestUserMarshal(t *testing.T) {
testJSONMarshal(t, &User{}, "{}")
u := &User{
ID: 1,
Name: "foo",
Picture: "https://api-cdn.myanimelist.net/images/userimages/1.jpg?t=1653811800",
Gender: "baz",
Birthday: "1990-01-01",
Location: "bar",
JoinedAt: time.Date(2015, 8, 20, 11, 11, 55, 0, time.UTC),
AnimeStatistics: AnimeStatistics{
NumItemsWatching: 1,
NumItemsCompleted: 1,
NumItemsOnHold: 1,
NumItemsDropped: 1,
NumItemsPlanToWatch: 1,
NumItems: 1,
NumDaysWatched: 1.1,
NumDaysWatching: 1.1,
NumDaysCompleted: 1.1,
NumDaysOnHold: 1.1,
NumDaysDropped: 1.1,
NumDays: 1.1,
NumEpisodes: 1,
NumTimesRewatched: 1,
MeanScore: 1.1,
},
TimeZone: "America/Guyana",
IsSupporter: true,
}
want := `{
"id": 1,
"name": "foo",
"picture": "https://api-cdn.myanimelist.net/images/userimages/1.jpg?t=1653811800",
"gender": "baz",
"birthday": "1990-01-01",
"location": "bar",
"joined_at": "2015-08-20T11:11:55Z",
"anime_statistics": {
"num_items_watching": 1,
"num_items_completed": 1,
"num_items_on_hold": 1,
"num_items_dropped": 1,
"num_items_plan_to_watch": 1,
"num_items": 1,
"num_days_watched": 1.1,
"num_days_watching": 1.1,
"num_days_completed": 1.1,
"num_days_on_hold": 1.1,
"num_days_dropped": 1.1,
"num_days": 1.1,
"num_episodes": 1,
"num_times_rewatched": 1,
"mean_score": 1.1
},
"time_zone": "America/Guyana",
"is_supporter": true
}`
testJSONMarshal(t, u, want)
}