-
Notifications
You must be signed in to change notification settings - Fork 5
/
pinned_test.go
181 lines (153 loc) Β· 3.89 KB
/
pinned_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
171
172
173
174
175
176
177
178
179
180
181
package pinned
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"
)
func TestVersionManagerAdd(t *testing.T) {
vm := &VersionManager{}
// Should fail if invalid date supplied.
err := vm.Add(&Version{
Date: "baddate",
})
if err == nil {
t.Fatal("Expected error when adding version with invalid date")
}
// Versions should be sorted in descending order.
vm.Add(&Version{
Date: "2016-01-02",
})
vm.Add(&Version{
Date: "2017-01-02",
})
versions := vm.Versions()
if versions[0] != "2017-01-02" || versions[1] != "2016-01-02" {
t.Fatal("Versions not sorted properly")
}
}
func TestVersionManagerLatest(t *testing.T) {
vm := &VersionManager{}
// If no versions, return nil Version.
v := vm.Latest()
if v != nil {
log.Fatal("Expected nil Version")
}
vm.Add(&Version{
Date: "2017-01-02",
})
vm.Add(&Version{
Date: "2018-01-02",
})
v = vm.Latest()
if v.Date != "2018-01-02" {
t.Fatalf("Expected 2018-01-02, instead got %s", v.Date)
}
}
func TestVersionManagerParse(t *testing.T) {
vm := &VersionManager{}
getRoute := func(v string) string {
route := "/users"
if v != "" {
route += fmt.Sprintf("?v=%s", v)
}
return route
}
// Should fail if no version supplied.
oldV := &Version{
Date: "2017-01-02",
}
vm.Add(oldV)
req := httptest.NewRequest(http.MethodGet, getRoute(""), nil)
_, err := vm.Parse(req)
if err != ErrNoVersionSupplied {
t.Fatalf("Expected ErrNoVersionSupplied, instead got %s", err)
}
// Should fail if invalid version supplied in query params.
req = httptest.NewRequest(http.MethodGet, getRoute("2000-01-02"), nil)
_, err = vm.Parse(req)
if err != ErrInvalidVersion {
t.Fatalf("Expected ErrInvalidVersion, instead got %s", err)
}
// Should fail if invalid version supplied in req header.
req = httptest.NewRequest(http.MethodGet, getRoute(""), nil)
req.Header.Set("Version", "2017-01-02")
_, err = vm.Parse(req)
if err != nil {
t.Fatalf("Expected no error, instead got %s", err)
}
// Should succeed if valid version supplied in query params.
req = httptest.NewRequest(http.MethodGet, getRoute("2017-01-02"), nil)
_, err = vm.Parse(req)
if err != nil {
t.Fatalf("Expected no error, instead got %s", err)
}
// Should succeed if valid version supplied in req header.
req = httptest.NewRequest(http.MethodGet, getRoute(""), nil)
req.Header.Set("Version", "2017-01-02")
_, err = vm.Parse(req)
if err != nil {
t.Fatalf("Expected no error, instead got %s", err)
}
// Should select more recent version if supplied in query params and header.
newV := &Version{
Date: "2018-01-02",
}
vm.Add(newV)
req = httptest.NewRequest(http.MethodGet, getRoute("2017-01-02"), nil)
req.Header.Set("Version", "2018-01-02")
v, err := vm.Parse(req)
if err != nil {
t.Fatalf("Expected no error, instead got %s", err)
}
if v.Date != "2018-01-02" {
t.Fatalf("Expected version 2018-01-02, instead got %s", v.Date)
}
// Should fail if version is deprecated.
oldV.Deprecated = true
req = httptest.NewRequest(http.MethodGet, getRoute("2017-01-02"), nil)
_, err = vm.Parse(req)
if err != ErrVersionDeprecated {
t.Fatalf("Expected ErrVersionDeprecated, instead got %s", err)
}
}
type TestObject struct {
B string
}
func (to *TestObject) Data() map[string]interface{} {
return map[string]interface{}{
"B": to.B,
}
}
func TestVersionManagerApply(t *testing.T) {
vm := &VersionManager{}
action := func(m map[string]interface{}) map[string]interface{} {
m["A"] = m["B"]
delete(m, "B")
return m
}
version := &Version{
Date: "2017-01-02",
}
vm.Add(version)
vm.Add(&Version{
Date: "2018-01-02",
Changes: []*Change{
{
Description: "Foobar.",
Actions: map[string]Action{
"TestObject": action,
},
},
},
})
to := &TestObject{"Foo"}
res, err := vm.Apply(version, to)
if err != nil {
t.Fatal(err)
}
if res["A"].(string) != "Foo" {
t.Fatalf("Expected map[A] = Foo, instead got %s", res["A"].(string))
}
}