Skip to content

Commit

Permalink
start of tests for json marshalling of resources
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
willnorris committed Sep 5, 2013
1 parent 1eaf383 commit 5f1c20a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
19 changes: 19 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package github

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -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)

Expand Down
40 changes: 40 additions & 0 deletions github/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

1 comment on commit 5f1c20a

@ktoso
Copy link
Contributor

@ktoso ktoso commented on 5f1c20a Sep 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That commit message humbles me deeply ;-)
I like the idea, it'll make the actual API tests smaller, +1 :-)

Please sign in to comment.