-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsearch_test.go
170 lines (163 loc) · 4.43 KB
/
search_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
163
164
165
166
167
168
169
170
package kanka
import (
"net/http"
"os"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
const testSearch string = "test_data/search.json"
func TestClient_Search(t *testing.T) {
rslt := []*Result{
{
ID: 1194,
EntityID: 80025,
Name: "Shop",
Image: "https://kanka.io/images/defaults/attribute_templates.jpg",
ImageThumb: "https://kanka.io/images/defaults/attribute_templates_thumb.jpg",
HasCustomImage: false,
Type: "attribute_template",
Tooltip: "Shop",
URL: "https://kanka.io/campaign/5272/attribute_templates/1194",
IsAttributesPrivate: 0,
IsPrivate: false,
CreatedBy: 5600,
UpdatedBy: 5600,
},
{
ID: 26141,
EntityID: 80918,
Name: "The Rope Shop",
Image: "https://kanka.io/images/defaults/locations.jpg",
ImageThumb: "https://kanka.io/images/defaults/locations_thumb.jpg",
HasCustomImage: false,
Type: "location",
Tooltip: "",
URL: "https://kanka.io/campaign/5272/locations/26141",
IsAttributesPrivate: 0,
IsPrivate: false,
CreatedBy: 5600,
UpdatedBy: 5600,
},
{
ID: 912,
EntityID: 443499,
Name: "At The Magic Shop",
Image: "https://kanka.io/images/defaults/conversations.jpg",
ImageThumb: "https://kanka.io/images/defaults/conversations_thumb.jpg",
HasCustomImage: false,
Type: "conversation",
Tooltip: "At The Magic Shop",
URL: "https://kanka.io/campaign/5272/conversations/912",
IsAttributesPrivate: 0,
IsPrivate: false,
CreatedBy: 5600,
UpdatedBy: 5600,
},
}
n := time.Now()
now := &n
type args struct {
campID int
qry string
sync *time.Time
}
tests := []struct {
name string
status int
file string
args args
want []*Result
wantErr bool
}{
{
name: "StatusOK, valid response, valid args",
status: http.StatusOK,
file: testSearch,
args: args{campID: 5272, qry: "shop", sync: now},
want: rslt,
wantErr: false,
},
{
name: "Status OK, valid response, invalid campID",
status: http.StatusOK,
file: testSearch,
args: args{campID: -123, qry: "shop", sync: now},
want: nil,
wantErr: true,
},
{
name: "Status OK, valid response, invalid qry",
status: http.StatusOK,
file: testSearch,
args: args{campID: 5272, qry: "", sync: now},
want: nil,
wantErr: true,
},
{
name: "Status OK, valid response, invalid args",
status: http.StatusOK,
file: testSearch,
args: args{campID: -123, qry: "", sync: now},
want: nil,
wantErr: true,
},
{
name: "Status OK, empty response, valid args",
status: http.StatusOK,
file: testFileEmpty,
args: args{campID: 5272, qry: "shop", sync: now},
want: nil,
wantErr: true,
},
{
name: "Status OK, empty response, invalid args",
status: http.StatusOK,
file: testFileEmpty,
args: args{campID: -123, qry: "", sync: now},
want: nil,
wantErr: true,
},
{
name: "StatusUnauthorized, valid args",
status: http.StatusUnauthorized,
file: testFileEmpty,
args: args{campID: 5272, qry: "shop", sync: now},
want: nil,
wantErr: true,
},
{
name: "StatusForbidden, valid args",
status: http.StatusForbidden,
file: testFileEmpty,
args: args{campID: 5272, qry: "shop", sync: now},
want: nil,
wantErr: true,
},
{
name: "StatusNotFound, valid args",
status: http.StatusNotFound,
file: testFileEmpty,
args: args{campID: 5272, qry: "shop", sync: now},
want: nil,
wantErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
f, err := os.Open(test.file)
if err != nil {
t.Fatal(err)
}
defer f.Close()
c, _ := testClient(test.status, f)
got, err := c.Search(test.args.campID, test.args.qry, test.args.sync)
if (err != nil) != test.wantErr {
t.Fatalf("got err?: <%t>, want err?: <%t>\nerror: <%v>", (err != nil), test.wantErr, err)
}
if diff := cmp.Diff(got, test.want); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}