-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdbview_test.go
73 lines (66 loc) · 1.65 KB
/
dbview_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
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"reflect"
"testing"
"golang.org/x/oscar/internal/storage"
"golang.org/x/oscar/internal/testutil"
"rsc.io/ordered"
)
var o = ordered.Encode
func TestParseOrdered(t *testing.T) {
for _, tc := range []struct {
in string
want []byte
}{
{"", nil},
{"1", o(1)},
{"moo", o("moo")},
{"1 , moo ", o(1, "moo")},
{"foo, bar, Inf", o("foo", "bar", ordered.Inf)},
{"-inf, 8", o(ordered.Rev(ordered.Inf), 8)},
} {
got := parseOrdered(tc.in)
if !bytes.Equal(got, tc.want) {
t.Errorf("%q: got %v, want %v", tc.in, got, tc.want)
}
}
}
func TestDBView(t *testing.T) {
g := &Gaby{
db: storage.MemDB(),
slog: testutil.Slogger(t),
}
for i := range 10 {
g.db.Set(o("x", i), o(i))
}
for _, tc := range []struct {
start, end []any
want *dbviewResult
}{
{[]any{"y"}, nil, nil},
{[]any{"x", 3}, nil, &dbviewResult{Items: []item{{`("x", 3)`, "(3)"}}}},
{[]any{"x", 3}, []any{"x", 5}, &dbviewResult{Items: []item{
{`("x", 3)`, "(3)"},
{`("x", 4)`, "(4)"},
{`("x", 5)`, "(5)"},
}}},
{[]any{"x", 8}, []any{ordered.Inf}, &dbviewResult{Items: []item{
{`("x", 8)`, "(8)"},
{`("x", 9)`, "(9)"},
}}},
} {
start := ordered.Encode(tc.start...)
end := ordered.Encode(tc.end...)
got, err := g.dbview(start, end, 100)
if err != nil {
t.Fatalf("start: %v, end: %v: %v", tc.start, tc.end, err)
}
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("start: %v, end: %v:\ngot %+v\nwant %+v", tc.start, tc.end, got, tc.want)
}
}
}