-
Notifications
You must be signed in to change notification settings - Fork 19
/
query_test.go
95 lines (82 loc) · 1.74 KB
/
query_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
package compactindexsized
import (
"bytes"
"errors"
"math/rand"
"testing"
"github.com/rpcpool/yellowstone-faithful/indexmeta"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type failReader struct{ err error }
func (rd failReader) ReadAt([]byte, int64) (int, error) {
return 0, rd.err
}
func TestOpen_ReadFail(t *testing.T) {
err := errors.New("oh no!")
db, dbErr := Open(failReader{err})
require.Nil(t, db)
require.Same(t, err, dbErr)
}
func TestOpen_InvalidMagic(t *testing.T) {
var buf [32]byte
rand.Read(buf[:])
buf[1] = '.' // make test deterministic
db, dbErr := Open(bytes.NewReader(buf[:]))
require.Nil(t, db)
require.EqualError(t, dbErr, ErrInvalidMagic.Error())
}
func TestOpen_HeaderOnly(t *testing.T) {
buf := concatBytes(
// Magic
[]byte{'c', 'o', 'm', 'p', 'i', 's', 'z', 'd'},
// header size
i32tob(30),
// FileSize
[]byte{0x37, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
// NumBuckets
[]byte{0x42, 0x00, 0x00, 0x00},
// Version
[]byte{0x01},
// Meta: how many key-value pairs
[]byte{2},
// First key-value pair
// Key length
[]byte{3},
// Key
[]byte("foo"),
// Value length
[]byte{3},
// Value
[]byte("bar"),
// Second key-value pair
// Key length
[]byte{3},
// Key
[]byte("foo"),
// Value length
[]byte{3},
// Value
[]byte("baz"),
)
db, dbErr := Open(bytes.NewReader(buf[:]))
require.NoError(t, dbErr)
require.NotNil(t, db)
assert.NotNil(t, db.Stream)
assert.Equal(t, &Header{
ValueSize: 0x1337,
NumBuckets: 0x42,
Metadata: &indexmeta.Meta{
KeyVals: []indexmeta.KV{
{
Key: []byte("foo"),
Value: []byte("bar"),
},
{
Key: []byte("foo"),
Value: []byte("baz"),
},
},
},
}, db.Header)
}