Skip to content

Commit 5f1c20a

Browse files
committed
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.
1 parent 1eaf383 commit 5f1c20a

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

github/github_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package github
77

88
import (
9+
"bytes"
910
"encoding/json"
1011
"fmt"
1112
"io/ioutil"
@@ -78,6 +79,24 @@ func testURLParseError(t *testing.T, err error) {
7879
}
7980
}
8081

82+
// Helper function to test that a value is marshalled to JSON as expected.
83+
func testJSONMarshal(t *testing.T, v interface{}, want string) {
84+
j, err := json.Marshal(v)
85+
if err != nil {
86+
t.Errorf("Unable to marshal JSON for %v", v)
87+
}
88+
89+
w := new(bytes.Buffer)
90+
err = json.Compact(w, []byte(want))
91+
if err != nil {
92+
t.Errorf("String is not valid json: %s", want)
93+
}
94+
95+
if w.String() != string(j) {
96+
t.Errorf("json.Marshal(%q) returned %s, want %s", v, j, w)
97+
}
98+
}
99+
81100
func TestNewClient(t *testing.T) {
82101
c := NewClient(nil)
83102

github/users_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,46 @@ import (
1313
"testing"
1414
)
1515

16+
func TestUser_marshall(t *testing.T) {
17+
testJSONMarshal(t, &User{}, "{}")
18+
19+
u := &User{
20+
Login: String("l"),
21+
ID: Int(1),
22+
URL: String("u"),
23+
AvatarURL: String("a"),
24+
GravatarID: String("g"),
25+
Name: String("n"),
26+
Company: String("c"),
27+
Blog: String("b"),
28+
Location: String("l"),
29+
Email: String("e"),
30+
Hireable: Bool(true),
31+
PublicRepos: Int(1),
32+
Followers: Int(1),
33+
Following: Int(1),
34+
CreatedAt: &referenceTime,
35+
}
36+
want := `{
37+
"login": "l",
38+
"id": 1,
39+
"url": "u",
40+
"avatar_url": "a",
41+
"gravatar_id": "g",
42+
"name": "n",
43+
"company": "c",
44+
"blog": "b",
45+
"location": "l",
46+
"email": "e",
47+
"hireable": true,
48+
"public_repos": 1,
49+
"followers": 1,
50+
"following": 1,
51+
"created_at": ` + referenceTimeStr + `
52+
}`
53+
testJSONMarshal(t, u, want)
54+
}
55+
1656
func TestUsersService_Get_authenticatedUser(t *testing.T) {
1757
setup()
1858
defer teardown()

0 commit comments

Comments
 (0)