This repository was archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_test.go
189 lines (179 loc) · 4.2 KB
/
store_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
182
183
184
185
186
187
188
189
package memorydb
import (
"strings"
"testing"
"gitlab.com/flimzy/testy"
)
func TestRandStr(t *testing.T) {
str := randStr()
if len(str) != 32 {
t.Errorf("Expected 32-char string, got %d", len(str))
}
}
func TestToCouchDoc(t *testing.T) {
type tcdTest struct {
Name string
Input interface{}
Expected couchDoc
Error string
}
tests := []tcdTest{
{
Name: "Map",
Input: map[string]interface{}{"foo": "bar"},
Expected: couchDoc{"foo": "bar"},
},
{
Name: "CouchDoc",
Input: couchDoc{"foo": "bar"},
Expected: couchDoc{"foo": "bar"},
},
{
Name: "Unmarshalable",
Input: make(chan int),
Error: "json: unsupported type: chan int",
},
{
Name: "Marshalable",
Input: map[string]string{"foo": "bar"},
Expected: couchDoc{"foo": "bar"},
},
}
for _, test := range tests {
func(test tcdTest) {
t.Run(test.Name, func(t *testing.T) {
result, err := toCouchDoc(test.Input)
var msg string
if err != nil {
msg = err.Error()
}
if msg != test.Error {
t.Errorf("Unexpected error: %s", msg)
}
if d := testy.DiffInterface(test.Expected, result); d != nil {
t.Error(d)
}
})
}(test)
}
}
func TestAddRevision(t *testing.T) {
d := &database{
docs: make(map[string]*document),
}
r := d.addRevision(couchDoc{"_id": "bar"})
if !strings.HasPrefix(r, "1-") {
t.Errorf("Expected initial revision to start with '1-', but got '%s'", r)
}
if len(r) != 34 {
t.Errorf("rev (%s) is %d chars long, expected 34", r, len(r))
}
r = d.addRevision(couchDoc{"_id": "bar"})
if !strings.HasPrefix(r, "2-") {
t.Errorf("Expected second revision to start with '2-', but got '%s'", r)
}
if len(r) != 34 {
t.Errorf("rev (%s) is %d chars long, expected 34", r, len(r))
}
t.Run("NoID", func(t *testing.T) {
r := func() (i interface{}) {
defer func() {
i = recover()
}()
d.addRevision(nil)
return nil
}()
if r == nil {
t.Errorf("addRevision without ID should panic")
}
})
t.Run("InvalidJSON", func(t *testing.T) {
r := func() (i interface{}) {
defer func() {
i = recover()
}()
d.addRevision(couchDoc{"_id": "foo", "invalid": make(chan int)})
return nil
}()
if r == nil {
t.Errorf("unmarshalable objects should panic")
}
})
}
func TestAddLocalRevision(t *testing.T) {
d := &database{
docs: make(map[string]*document),
}
r := d.addRevision(couchDoc{"_id": "_local/foo"})
if r != "1-0" {
t.Errorf("Expected local revision, got %s", r)
}
r = d.addRevision(couchDoc{"_id": "_local/foo"})
if r != "1-0" {
t.Errorf("Expected local revision, got %s", r)
}
}
func TestGetRevisionMissing(t *testing.T) {
d := &database{
docs: make(map[string]*document),
}
_, found := d.getRevision("foo", "bar")
if found {
t.Errorf("Should not have found missing revision")
}
}
func TestGetRevisionFound(t *testing.T) {
d := &database{
docs: make(map[string]*document),
}
r := d.addRevision(map[string]interface{}{"_id": "foo", "a": 1})
_ = d.addRevision(map[string]interface{}{"_id": "foo", "a": 2})
result, found := d.getRevision("foo", r)
if !found {
t.Errorf("Should have found revision")
}
expected := map[string]interface{}{"_id": "foo", "a": 1, "_rev": r}
if d := testy.DiffAsJSON(expected, result.data); d != nil {
t.Error(d)
}
}
func TestRev(t *testing.T) {
t.Run("Missing", func(t *testing.T) {
d := couchDoc{}
if d.Rev() != "" {
t.Errorf("Rev should be missing, but got %s", d.Rev())
}
})
t.Run("Set", func(t *testing.T) {
d := couchDoc{"_rev": "foo"}
if d.Rev() != "foo" {
t.Errorf("Rev should be foo, but got %s", d.Rev())
}
})
t.Run("NonString", func(t *testing.T) {
d := couchDoc{"_rev": true}
if d.Rev() != "" {
t.Errorf("Rev should be missing, but got %s", d.Rev())
}
})
}
func TestID(t *testing.T) {
t.Run("Missing", func(t *testing.T) {
d := couchDoc{}
if d.ID() != "" {
t.Errorf("ID should be missing, but got %s", d.ID())
}
})
t.Run("Set", func(t *testing.T) {
d := couchDoc{"_id": "foo"}
if d.ID() != "foo" {
t.Errorf("ID should be foo, but got %s", d.ID())
}
})
t.Run("NonString", func(t *testing.T) {
d := couchDoc{"_id": true}
if d.ID() != "" {
t.Errorf("ID should be missing, but got %s", d.ID())
}
})
}