-
Notifications
You must be signed in to change notification settings - Fork 3
/
subscriptions_test.go
114 lines (107 loc) · 2.4 KB
/
subscriptions_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
package chartmogul
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
)
func TestConnectSubscriptions(t *testing.T) {
expected := map[string]interface{}{
"subscriptions": []interface{}{
map[string]interface{}{
"data_source_uuid": "ds_uuid1",
"external_id": "ext_id1",
},
map[string]interface{}{
"data_source_uuid": "ds_uuid2",
"external_id": "ext_id2",
},
},
}
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(202)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{}")) //nolint
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error(err)
return
}
var incoming interface{}
err = json.Unmarshal(body, &incoming)
if err != nil {
t.Error(err)
return
}
if !reflect.DeepEqual(expected, incoming) {
spew.Dump(expected, incoming)
t.Error("Doesn't equal expected value")
return
}
}))
defer server.Close()
SetURL(server.URL + "/v/%v")
tested := &API{
ApiKey: "token",
}
err := tested.ConnectSubscriptions("cus_uuid", []Subscription{
{
ExternalID: "ext_id1",
DataSourceUUID: "ds_uuid1",
},
{
ExternalID: "ext_id2",
DataSourceUUID: "ds_uuid2",
},
})
if err != nil {
spew.Dump(err)
t.Fatal("Expected to retry")
}
}
func TestCancelSubscriptionParams(t *testing.T) {
emptySlice := []string{}
notEmptySlice := []string{"some-date"}
testCases := map[string]struct {
param *CancelSubscriptionParams
exp string
}{
"clearing cancellation history": {
param: &CancelSubscriptionParams{
CancellationDates: &emptySlice,
},
exp: `{"cancellation_dates":[]}`,
},
"setting cancellation history": {
param: &CancelSubscriptionParams{
CancellationDates: ¬EmptySlice,
},
exp: `{"cancellation_dates":["some-date"]}`,
},
"not using cancellation history": {
param: &CancelSubscriptionParams{
CancelledAt: "some-date",
},
exp: `{"cancelled_at":"some-date"}`,
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
got, err := json.Marshal(tc.param)
if err != nil {
spew.Dump(err)
t.Error("Expected not error")
}
if string(got) != tc.exp {
spew.Dump(tc.exp, string(got))
t.Error("Doesn't equal expected value")
}
})
}
}