From 5f1c20afed08516e098c13fd0a94a74be0d573c3 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Thu, 5 Sep 2013 09:29:16 -0700 Subject: [PATCH] start of tests for json marshalling of resources ktoso kind of shamed me with his extensive resource tests in #49 :), so I'm finally starting to setup a structure for more general testing of this type. This change adds the `testJSONMarshal` helper function, and adds tests for the `User` type. This includes two checks: - check that an empty resource produces an empty JSON object. This effectively verifies that all fields include 'omitempty' so that we're not unintentionally sending additional data over the wire. - check that a full resource with every field populated produces the expected JSON. This verifies that the JSON field mappings are correct. In this case, it might be okay to use resource samples from the GitHub docs, though I do still prefer very simple field values since it makes tests easier to read. When these tests are added for each resource type, we can reduce all of our other tests to return bare minimal response bodies, since the resource fields are already being tested at that point. --- github/github_test.go | 19 +++++++++++++++++++ github/users_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/github/github_test.go b/github/github_test.go index b5c4827..bf4d87e 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -6,6 +6,7 @@ package github import ( + "bytes" "encoding/json" "fmt" "io/ioutil" @@ -78,6 +79,24 @@ func testURLParseError(t *testing.T, err error) { } } +// Helper function to test that a value is marshalled to JSON as expected. +func testJSONMarshal(t *testing.T, v interface{}, want string) { + j, err := json.Marshal(v) + if err != nil { + t.Errorf("Unable to marshal JSON for %v", v) + } + + w := new(bytes.Buffer) + err = json.Compact(w, []byte(want)) + if err != nil { + t.Errorf("String is not valid json: %s", want) + } + + if w.String() != string(j) { + t.Errorf("json.Marshal(%q) returned %s, want %s", v, j, w) + } +} + func TestNewClient(t *testing.T) { c := NewClient(nil) diff --git a/github/users_test.go b/github/users_test.go index dec8193..f7ff2af 100644 --- a/github/users_test.go +++ b/github/users_test.go @@ -13,6 +13,46 @@ import ( "testing" ) +func TestUser_marshall(t *testing.T) { + testJSONMarshal(t, &User{}, "{}") + + u := &User{ + Login: String("l"), + ID: Int(1), + URL: String("u"), + AvatarURL: String("a"), + GravatarID: String("g"), + Name: String("n"), + Company: String("c"), + Blog: String("b"), + Location: String("l"), + Email: String("e"), + Hireable: Bool(true), + PublicRepos: Int(1), + Followers: Int(1), + Following: Int(1), + CreatedAt: &referenceTime, + } + want := `{ + "login": "l", + "id": 1, + "url": "u", + "avatar_url": "a", + "gravatar_id": "g", + "name": "n", + "company": "c", + "blog": "b", + "location": "l", + "email": "e", + "hireable": true, + "public_repos": 1, + "followers": 1, + "following": 1, + "created_at": ` + referenceTimeStr + ` + }` + testJSONMarshal(t, u, want) +} + func TestUsersService_Get_authenticatedUser(t *testing.T) { setup() defer teardown()