forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
158 lines (141 loc) · 3.8 KB
/
util_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
package goshopify
import (
"net/url"
"testing"
"time"
)
func TestShopFullName(t *testing.T) {
cases := []struct {
in, expected string
}{
{"myshop", "myshop.myshopify.com"},
{"myshop.", "myshop.myshopify.com"},
{" myshop", "myshop.myshopify.com"},
{"myshop ", "myshop.myshopify.com"},
{"myshop \n", "myshop.myshopify.com"},
{"myshop.myshopify.com", "myshop.myshopify.com"},
}
for _, c := range cases {
actual := ShopFullName(c.in)
if actual != c.expected {
t.Errorf("ShopFullName(%s): expected %s, actual %s", c.in, c.expected, actual)
}
}
}
func TestShopShortName(t *testing.T) {
cases := []struct {
in, expected string
}{
{"myshop", "myshop"},
{"myshop.", "myshop"},
{" myshop", "myshop"},
{"myshop ", "myshop"},
{"myshop \n", "myshop"},
{"myshop.myshopify.com", "myshop"},
{".myshop.myshopify.com.", "myshop"},
}
for _, c := range cases {
actual := ShopShortName(c.in)
if actual != c.expected {
t.Errorf("ShopShortName(%s): expected %s, actual %s", c.in, c.expected, actual)
}
}
}
func TestShopBaseUrl(t *testing.T) {
cases := []struct {
in, expected string
}{
{"myshop", "https://myshop.myshopify.com"},
{"myshop.", "https://myshop.myshopify.com"},
{" myshop", "https://myshop.myshopify.com"},
{"myshop ", "https://myshop.myshopify.com"},
{"myshop \n", "https://myshop.myshopify.com"},
{"myshop.myshopify.com", "https://myshop.myshopify.com"},
}
for _, c := range cases {
actual := ShopBaseUrl(c.in)
if actual != c.expected {
t.Errorf("ShopBaseUrl(%s): expected %s, actual %s", c.in, c.expected, actual)
}
}
}
func TestMetafieldPathPrefix(t *testing.T) {
cases := []struct {
resource string
resourceID int64
expected string
}{
{"", 0, "metafields"},
{"products", 123, "products/123/metafields"},
}
for _, c := range cases {
actual := MetafieldPathPrefix(c.resource, c.resourceID)
if actual != c.expected {
t.Errorf("MetafieldPathPrefix(%s, %d): expected %s, actual %s", c.resource, c.resourceID, c.expected, actual)
}
}
}
func TestFulfillmentPathPrefix(t *testing.T) {
cases := []struct {
resource string
resourceID int64
expected string
}{
{"", 0, "fulfillments"},
{"orders", 123, "orders/123/fulfillments"},
}
for _, c := range cases {
actual := FulfillmentPathPrefix(c.resource, c.resourceID)
if actual != c.expected {
t.Errorf("FulfillmentPathPrefix(%s, %d): expected %s, actual %s", c.resource, c.resourceID, c.expected, actual)
}
}
}
func TestOnlyDateMarshal(t *testing.T) {
cases := []struct {
in OnlyDate
expected string
}{
{OnlyDate{time.Date(2023, 03, 31, 0, 0, 0, 0, time.Local)}, "\"2023-03-31\""},
{OnlyDate{}, "\"0001-01-01\""},
}
for _, c := range cases {
actual, _ := c.in.MarshalJSON()
if string(actual) != c.expected {
t.Errorf("MarshalJSON(%s): expected %s, actual %s", c.in.String(), c.expected, string(actual))
}
}
}
func TestOnlyDateUnmarshal(t *testing.T) {
cases := []struct {
in string
expected OnlyDate
}{
{"\"2023-03-31\"", OnlyDate{time.Date(2023, 03, 31, 0, 0, 0, 0, time.Local)}},
{"\"0001-01-01\"", OnlyDate{}},
{"\"\"", OnlyDate{}},
}
for _, c := range cases {
newDate := OnlyDate{}
_ = newDate.UnmarshalJSON([]byte(c.in))
if newDate.String() != c.expected.String() {
t.Errorf("UnmarshalJSON(%s): expected %s, actual %s", c.in, newDate.String(), c.expected.String())
}
}
}
func TestOnlyDateEncode(t *testing.T) {
cases := []struct {
in OnlyDate
expected string
}{
{OnlyDate{time.Date(2023, 03, 31, 0, 0, 0, 0, time.Local)}, "\"2023-03-31\""},
{OnlyDate{}, "\"0001-01-01\""},
}
for _, c := range cases {
urlVal := url.Values{}
_ = c.in.EncodeValues("date", &urlVal)
if urlVal.Get("date") != c.expected {
t.Errorf("EncodeValues(%s): expected %s, actual %s", c.in.String(), c.expected, urlVal.Get("date"))
}
}
}