Browse Source

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.
Will Norris 12 years ago
parent
commit
5f1c20afed
2 changed files with 59 additions and 0 deletions
  1. +19
    -0
      github/github_test.go
  2. +40
    -0
      github/users_test.go

+ 19
- 0
github/github_test.go View File

@ -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)


+ 40
- 0
github/users_test.go View File

@ -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()


Loading…
Cancel
Save