forked from yanatan16/golang-instagram
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencoding_test.go
More file actions
160 lines (155 loc) · 4.05 KB
/
encoding_test.go
File metadata and controls
160 lines (155 loc) · 4.05 KB
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
package instagram
import (
"encoding/json"
"reflect"
"testing"
"time"
"github.com/kr/pretty"
)
func TestMediaJSON(t *testing.T) {
tests := []struct {
desc string
json string
media Media
}{
{
desc: "zero value",
json: `{"type":"","id":"","users_in_photo":null,"filter":"","tags":null,"comments":{"count":0},"caption":{"id":"","text":"","from":{},"created_time":null},"likes":{"count":0},"link":"","user":{},"images":{},"videos":{},"carousel_media":null,"location":{"id":""},"user_has_liked":false,"created_time":null}`,
media: Media{},
},
{
desc: "round trip",
json: `{"type":"","id":"123","users_in_photo":null,"filter":"","tags":null,"comments":{"count":0},"caption":{"id":"","text":"","from":{},"created_time":null},"likes":{"count":0},"link":"","user":{},"images":{},"videos":{},"carousel_media":null,"location":{"id":""},"user_has_liked":false,"created_time":"1550173420"}`,
media: Media{
ID: "123",
CreatedTime: time.Date(2019, 2, 14, 19, 43, 40, 0, time.UTC),
},
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
gotJSON, err := json.Marshal(tt.media)
if err != nil {
t.Fatalf("Marshal: %v", err)
}
if got, want := string(gotJSON), tt.json; got != want {
t.Errorf("Marshal\ngot %s\nwant %s", got, want)
}
var got Media
if err := json.Unmarshal([]byte(tt.json), &got); err != nil {
t.Fatalf("Unmarshal: %v", err)
}
if got, want := got, tt.media; !reflect.DeepEqual(got, want) {
t.Errorf("Unmarshal got %#v want %#v", got, want)
pretty.Ldiff(t, got, want)
}
})
}
}
func TestCommentJSON(t *testing.T) {
tests := []struct {
desc string
json string
comment Comment
unmarshalOnly bool
}{
{
desc: "zero value",
json: `{"id":"","text":"","from":{},"created_time":null}`,
comment: Comment{},
},
{
desc: "null",
json: `null`,
comment: Comment{},
unmarshalOnly: true,
},
{
desc: "round trip",
json: `{"id":"123","text":"","from":{},"created_time":"1550173420"}`,
comment: Comment{
ID: "123",
CreatedTime: time.Date(2019, 2, 14, 19, 43, 40, 0, time.UTC),
},
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
if !tt.unmarshalOnly {
gotJSON, err := json.Marshal(tt.comment)
if err != nil {
t.Fatalf("Marshal: %v", err)
}
if got, want := string(gotJSON), tt.json; got != want {
t.Errorf("Marshal\ngot %s\nwant %s", got, want)
}
}
var got Comment
if err := json.Unmarshal([]byte(tt.json), &got); err != nil {
t.Fatalf("Unmarshal: %v", err)
}
if got, want := got, tt.comment; !reflect.DeepEqual(got, want) {
t.Errorf("Unmarshal got %#v want %#v", got, want)
pretty.Ldiff(t, got, want)
}
})
}
}
func TestLocationJSON(t *testing.T) {
tests := []struct {
desc string
json string
location Location
unmarshalOnly bool
}{
{
desc: "zero value",
json: `{"id":""}`,
location: Location{},
},
{
desc: "null",
json: `null`,
location: Location{},
unmarshalOnly: true,
},
{
desc: "round trip with string",
json: `{"id":"123","name":"florida"}`,
location: Location{
ID: "123",
Name: "florida",
},
},
{
desc: "round trip with number",
json: `{"id":123,"name":"florida"}`,
location: Location{
ID: "123",
Name: "florida",
},
unmarshalOnly: true,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
if !tt.unmarshalOnly {
gotJSON, err := json.Marshal(tt.location)
if err != nil {
t.Fatalf("Marshal: %v", err)
}
if got, want := string(gotJSON), tt.json; got != want {
t.Errorf("Marshal\ngot %s\nwant %s", got, want)
}
}
var got Location
if err := json.Unmarshal([]byte(tt.json), &got); err != nil {
t.Fatalf("Unmarshal: %v", err)
}
if got, want := got, tt.location; !reflect.DeepEqual(got, want) {
t.Errorf("Unmarshal got %#v want %#v", got, want)
pretty.Ldiff(t, got, want)
}
})
}
}