-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathclient_test.go
More file actions
137 lines (125 loc) · 3.9 KB
/
client_test.go
File metadata and controls
137 lines (125 loc) · 3.9 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
package sunlight
import (
"context"
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"filippo.io/torchwood"
"golang.org/x/mod/sumdb/tlog"
)
type testTileReader struct {
t *testing.T
noDataTiles, noNamesTiles bool
}
var _ torchwood.TileReader = &testTileReader{}
func (tr *testTileReader) ReadTiles(ctx context.Context, tiles []tlog.Tile) (data [][]byte, err error) {
for _, t := range tiles {
if t.L == -1 && tr.noDataTiles {
return nil, fmt.Errorf("refusing to read tile %s due to noDataTiles setting", TilePath(t))
}
if t.L == -2 && tr.noNamesTiles {
return nil, fmt.Errorf("refusing to read tile %s due to noNamesTiles setting", TilePath(t))
}
path := TilePath(t)
path = filepath.Join("testdata", "navigli2025h2", path)
b, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read tile %s: %w", path, err)
}
data = append(data, b)
}
return data, nil
}
func (tr *testTileReader) SaveTiles(tiles []tlog.Tile, data [][]byte) {}
func (tr *testTileReader) ReadEndpoint(ctx context.Context, endpoint string) ([]byte, error) {
return nil, fmt.Errorf("testTileReader: ReadEndpoint not implemented")
}
func NewTestClient(t *testing.T, config *ClientConfig) (*Client, *testTileReader) {
tileReader := &testTileReader{t: t}
client, err := torchwood.NewClient(tileReader, torchwood.WithCutEntry(cutEntry))
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
return &Client{c: client, r: tileReader, cc: config}, tileReader
}
const partialCount = 256*3 + 10
const allCount = 256 * 4
func TestUnauthenticatedTrimmedEntries(t *testing.T) {
t.Run("names", func(t *testing.T) {
t.Parallel()
client, tr := NewTestClient(t, &ClientConfig{})
tr.noNamesTiles = true
testUnauthenticatedTrimmedEntries(t, client)
})
t.Run("data", func(t *testing.T) {
t.Parallel()
client, tr := NewTestClient(t, &ClientConfig{})
tr.noDataTiles = true
testUnauthenticatedTrimmedEntries(t, client)
})
}
func testUnauthenticatedTrimmedEntries(t *testing.T, client *Client) {
var allEntries []*TrimmedEntry
var index int64
for i, e := range client.UnauthenticatedTrimmedEntries(t.Context(), 0, allCount) {
if i != index {
t.Errorf("expected entry index %d, got %d", index, i)
}
allEntries = append(allEntries, e)
index++
}
if err := client.Err(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(allEntries) != allCount {
t.Fatalf("expected %d entries, got %d", allCount, len(allEntries))
}
compareRange := func(t *testing.T, start, end int64) {
var gotEntries []*TrimmedEntry
index := start
for i, e := range client.UnauthenticatedTrimmedEntries(t.Context(), start, end) {
if i != index {
t.Errorf("expected entry index %d, got %d", index, i)
}
gotEntries = append(gotEntries, e)
index++
}
if err := client.Err(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(gotEntries) != int(end-start) {
t.Fatalf("expected %d entries, got %d", end-start, len(gotEntries))
}
if !reflect.DeepEqual(allEntries[start:end], gotEntries) {
t.Errorf("entries from %d to %d do not match expected entries", start, end)
}
}
if testing.Short() {
for _, n := range []int64{0, 1, 255, 256, 257, partialCount - 1} {
t.Run(fmt.Sprintf("%d:%d", n, allCount), func(t *testing.T) {
compareRange(t, n, allCount)
})
t.Run(fmt.Sprintf("%d:%d", n, partialCount), func(t *testing.T) {
compareRange(t, n, partialCount)
})
}
for _, n := range []int64{partialCount, partialCount + 1, allCount - 1} {
t.Run(fmt.Sprintf("%d:%d", n, allCount), func(t *testing.T) {
compareRange(t, n, allCount)
})
}
return
}
for n := range allCount {
t.Run(fmt.Sprintf("%d:%d", n, allCount), func(t *testing.T) {
compareRange(t, int64(n), allCount)
})
}
for n := range partialCount {
t.Run(fmt.Sprintf("%d:%d", n, partialCount), func(t *testing.T) {
compareRange(t, int64(n), partialCount)
})
}
}