-
Notifications
You must be signed in to change notification settings - Fork 8
/
client_test.go
92 lines (76 loc) · 2.59 KB
/
client_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
package gopaapi5
import (
"context"
"net/http"
"testing"
"time"
"github.com/utekaravinash/gopaapi5/api"
)
func TestNewClient(t *testing.T) {
client, err := NewClient("accessKey", "secretKey", "associateTag", api.UnitedStates)
if err != nil {
t.Errorf("Error getting api client instance")
}
_, err1 := NewClient("", "", "", api.UnitedStates)
_, err2 := NewClient("accessKey", "", "", api.UnitedStates)
_, err3 := NewClient("accessKey", "secretKey", "", api.UnitedStates)
_, err4 := NewClient("accessKey", "secretKey", "associateTag", api.Locale("Fake Locale"))
ctx := context.Background()
err5 := client.executeOperation(ctx, api.GetItems, api.GetItemsParams{}, nil)
err6 := client.executeOperation(ctx, api.GetItems, nil, nil)
err7 := client.executeOperation(ctx, api.GetItems, api.GetItemsParams{Resources: []api.Resource{api.VariationSummaryVariationDimension}}, nil)
req := &request{
operation: api.GetItems,
payload: map[string]interface{}{},
path: "paapi5/getitems",
client: client,
dateTime: testRequestTime,
}
err = req.build()
if err != nil {
t.Errorf("%v", err)
}
tests := []struct {
name string
expected interface{}
actual interface{}
}{
{"AccessKey", "accessKey", client.AccessKey},
{"SecretKey", "secretKey", client.SecretKey},
{"AssociateTag", "associateTag", client.AssociateTag},
{"Empty AccessKey", "Empty access key", err1.Error()},
{"Empty SecretKey", "Empty secret key", err2.Error()},
{"Empty AssociateTag", "Empty associate tag", err3.Error()},
{"Invalid Locale", "Invalid locale", err4.Error()},
{"NoItemIds", "One or more item ids required", err5.Error()},
{"NilParameters", "Nil parameters", err6.Error()},
{"InvalidResource", `Invalid resource "VariationSummary.VariationDimension" for operation "GetItems"`, err7.Error()},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.expected != test.actual {
t.Errorf("Expected: %v, Actual: %v", test.expected, test.actual)
}
})
}
}
func TestSetHttpClient(t *testing.T) {
client, _ := NewClient("accessKey", "secretKey", "associateTag", api.UnitedStates)
client.SetHttpClient(&http.Client{Timeout: time.Hour * 1})
err := client.SetHttpClient(nil)
tests := []struct {
name string
expected interface{}
actual interface{}
}{
{"Custom Client", time.Hour * 1, client.httpClient.Timeout},
{"Nil HTTP Client", ErrNilHttpClient, err},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.expected != test.actual {
t.Errorf("Expected: %v, Actual: %v", test.expected, test.actual)
}
})
}
}