-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodel_test.go
162 lines (122 loc) · 3.48 KB
/
model_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package goparse
import (
"testing"
"encoding/json"
"strings"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestModel(t *testing.T) {
Convey("Given JSON strings", t, func() {
Convey("When decoding as User", func() {
s := `{
"sessionToken":"2qx3lA4So1jDzW1Dgj2Fq3sCC",
"objectId":"5J9g2dDJTF",
"username":"test",
"phone":"090-1234-5678",
"createdAt":"2014-09-29T08:31:59.030Z",
"updatedAt":"2014-10-29T08:31:59.030Z"
}`
var u User
err := json.NewDecoder(strings.NewReader(s)).Decode(&u)
So(err, ShouldEqual, nil)
Convey("It has a SessionToken", func() {
So(u.SessionToken, ShouldEqual, "2qx3lA4So1jDzW1Dgj2Fq3sCC")
})
Convey("It has an ObjectId", func() {
So(u.ObjectID, ShouldEqual, "5J9g2dDJTF")
})
Convey("It has an UserName", func() {
So(u.UserName, ShouldEqual, "test")
})
Convey("It has a Phone", func() {
So(u.Phone, ShouldEqual, "090-1234-5678")
})
Convey("CreatedAt equals provided time", func() {
t, _ := time.Parse(time.RFC3339, "2014-09-29T08:31:59.030Z")
So(u.CreatedAt, ShouldHappenOnOrBefore, t)
})
Convey("UpdatedAt equals provided time", func() {
t, _ := time.Parse(time.RFC3339, "2014-10-29T08:31:59.030Z")
So(u.UpdatedAt, ShouldHappenOnOrBefore, t)
})
})
Convey("When decoding as Facebook", func() {
s := `{
"id": "abcdefg",
"access_token": "hijklmn",
"expiration_date": "2014-10-29T08:31:59.030Z"
}`
var f Facebook
err := json.NewDecoder(strings.NewReader(s)).Decode(&f)
So(err, ShouldEqual, nil)
Convey("It has an Id", func() {
So(f.ID, ShouldEqual, "abcdefg")
})
Convey("It has an AccessToken", func() {
So(f.AccessToken, ShouldEqual, "hijklmn")
})
Convey("Expiration is provided date", func() {
t, _ := time.Parse(time.RFC3339, "2014-10-29T08:31:59.030Z")
So(f.Expiration, ShouldHappenOnOrBefore, t)
})
})
Convey("When decoding as Twitter", func() {
s := `{
"id": "abcdefg",
"screen_name": "hijklmn",
"consumer_key": "123abc",
"consumer_secret": "345def",
"auth_token": "678ghi",
"auth_token_secret": "901jkl"
}`
var t Twitter
err := json.NewDecoder(strings.NewReader(s)).Decode(&t)
So(err, ShouldEqual, nil)
Convey("It has an Id", func() {
So(t.ID, ShouldEqual, "abcdefg")
})
Convey("It has a ScreenName", func() {
So(t.ScreenName, ShouldEqual, "hijklmn")
})
Convey("It has a ConsumerKey", func() {
So(t.ConsumerKey, ShouldEqual, "123abc")
})
Convey("It has a ConsumerSecret", func() {
So(t.ConsumerSecret, ShouldEqual, "345def")
})
Convey("It has an AuthToken", func() {
So(t.AuthToken, ShouldEqual, "678ghi")
})
Convey("It has an AuthTokenSecret", func() {
So(t.AuthTokenSecret, ShouldEqual, "901jkl")
})
})
Convey("When decoding as Anonymous", func() {
s := `{
"id": "abcdefg"
}`
var a Anonymous
err := json.NewDecoder(strings.NewReader(s)).Decode(&a)
So(err, ShouldEqual, nil)
Convey("It has an Id", func() {
So(a.ID, ShouldEqual, "abcdefg")
})
})
Convey("When deocding as Error", func() {
s := `{
"code": 105,
"error": "invalid field name: bl!ng"
}`
var a Error
err := json.NewDecoder(strings.NewReader(s)).Decode(&a)
So(err, ShouldEqual, nil)
Convey("It has a Code", func() {
So(a.Code, ShouldEqual, 105)
})
Convey("It has a Message", func() {
So(a.Message, ShouldEqual, "invalid field name: bl!ng")
})
})
})
}