Repository navigation
Expand file tree
/
Copy pathgsstore_test.go
More file actions
169 lines (150 loc) · 4.06 KB
/
Copy pathgsstore_test.go
File metadata and controls
169 lines (150 loc) · 4.06 KB
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
package dstore
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGSStore_calculateStartOffset(t *testing.T) {
tests := []struct {
name string
baseURLPath string // always starts with '/'
prefix string // no / prefix
startingPoint string // must start with prefix
expectedOffset string
expectedPrefix string
expectedError string
}{
{
name: "mostly empty",
baseURLPath: "/",
prefix: "",
startingPoint: "",
expectedOffset: "",
expectedPrefix: "",
},
{
name: "starting point equals prefix",
baseURLPath: "/",
prefix: "data",
startingPoint: "data",
expectedOffset: "data",
expectedPrefix: "data",
},
{
name: "starting point equals prefix with baseurlpath",
baseURLPath: "/base",
prefix: "data",
startingPoint: "data",
expectedOffset: "base/data",
expectedPrefix: "base/data",
},
{
name: "starting point below prefix",
baseURLPath: "/",
prefix: "data",
startingPoint: "data/subdir/file.txt",
expectedOffset: "data/subdir/file.txt",
expectedPrefix: "data",
},
{
name: "prefix with trailing slash",
baseURLPath: "/",
prefix: "data/",
startingPoint: "data/file.txt",
expectedOffset: "data/file.txt",
expectedPrefix: "data/",
},
{
name: "starting point not matches prefix",
baseURLPath: "/",
prefix: "data/",
startingPoint: "datax/file.txt",
expectedError: "starting point \"datax/file.txt\" must start with prefix \"data/\"",
},
{
name: "starting point does not end with slash no baseurl",
baseURLPath: "/",
prefix: "errors.",
startingPoint: "errors.12345",
expectedOffset: "errors.12345",
expectedPrefix: "errors.",
},
{
name: "starting point does not end with slash",
baseURLPath: "/my/data",
prefix: "errors.",
startingPoint: "errors.12345",
expectedOffset: "my/data/errors.12345",
expectedPrefix: "my/data/errors.",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
query, err := getGSWalkQuery(tt.prefix, tt.startingPoint, "", tt.baseURLPath)
if tt.expectedError != "" {
if err == nil {
t.Errorf("Expected error %q, but got nil", tt.expectedError)
return
}
if err.Error() != tt.expectedError {
t.Errorf("Expected error %q, got %q", tt.expectedError, err.Error())
}
return
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if query.Prefix != tt.expectedPrefix {
t.Errorf("Expected prefix %q, got %q", tt.expectedPrefix, query.Prefix)
}
if query.StartOffset != tt.expectedOffset {
t.Errorf("Expected offset %q, got %q", tt.expectedOffset, query.StartOffset)
}
})
}
}
func TestGSStore_calculateEndOffset(t *testing.T) {
tests := []struct {
name string
prefix string
exclusiveEndPoint string
baseURLPath string
expectedEndOffset string
}{
{
name: "no end point",
prefix: "chain/",
baseURLPath: "/root",
expectedEndOffset: "",
},
{
name: "end point below the prefix",
prefix: "chain/",
exclusiveEndPoint: "chain/0000000100",
baseURLPath: "/root",
expectedEndOffset: "root/chain/0000000100",
},
{
name: "end point keeps its trailing slash, so the key without it stays in range",
prefix: "chain/",
exclusiveEndPoint: "chain/sub/",
baseURLPath: "/root",
expectedEndOffset: "root/chain/sub/",
},
{
name: "end point on a store without a path",
prefix: "",
exclusiveEndPoint: "sub/",
baseURLPath: "/",
expectedEndOffset: "sub/",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
query, err := getGSWalkQuery(tt.prefix, "", tt.exclusiveEndPoint, tt.baseURLPath)
require.NoError(t, err)
assert.Equal(t, tt.expectedEndOffset, query.EndOffset)
})
}
}