forked from colinmarc/sequencefile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
117 lines (101 loc) · 3.22 KB
/
reader_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
package sequencefile
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type fileSpec struct {
path string
compression Compression
codec CompressionCodec
classname string
}
var files = []fileSpec{
{
"testdata/uncompressed.sequencefile",
NoCompression,
0,
"",
},
{
"testdata/record_compressed_gzip.sequencefile",
RecordCompression,
GzipCompression,
GzipClassName,
},
{
"testdata/record_compressed_snappy.sequencefile",
RecordCompression,
SnappyCompression,
SnappyClassName,
},
{
"testdata/record_compressed_zlib.sequencefile",
RecordCompression,
ZlibCompression,
ZlibClassName,
},
{
"testdata/block_compressed_gzip.sequencefile",
BlockCompression,
GzipCompression,
GzipClassName,
},
{
"testdata/block_compressed_snappy.sequencefile",
BlockCompression,
SnappyCompression,
SnappyClassName,
},
{
"testdata/block_compressed_zlib.sequencefile",
BlockCompression,
ZlibCompression,
ZlibClassName,
},
}
func TestReadFile(t *testing.T) {
for _, spec := range files {
t.Run(spec.path, func(t *testing.T) {
file, err := os.Open(spec.path)
require.NoError(t, err)
r := NewReader(file)
err = r.ReadHeader()
require.NoError(t, err, "reading the header should succeed")
testFileSpec(t, r, spec)
})
}
}
func testFileSpec(t *testing.T, r *Reader, spec fileSpec) {
assert.Equal(t, 6, r.Header.Version, "The version should be set")
assert.Equal(t, "org.apache.hadoop.io.BytesWritable", r.Header.KeyClassName, "The key class name should be set")
assert.Equal(t, "org.apache.hadoop.io.BytesWritable", r.Header.ValueClassName, "The value class name should be set")
assert.Equal(t, map[string]string{}, r.Header.Metadata, "The metadata should be set")
assert.Equal(t, spec.compression, r.Header.Compression, "The compression should be set")
assert.Equal(t, spec.codec, r.Header.CompressionCodec, "The compression codec should be set")
assert.Equal(t, spec.classname, r.Header.CompressionCodecClassName, "The compression codec should be set")
file := r.reader.(*os.File)
offset1, _ := file.Seek(0, os.SEEK_CUR)
ok := r.Scan()
require.NoError(t, r.Err(), "ScanKey should succeed")
require.True(t, ok, "ScanKey should succeed")
assert.Equal(t, "Alice", string(BytesWritable(r.Key())), "The key should be correct")
assert.Equal(t, "Practice", string(BytesWritable(r.Value())), "The value should be correct")
ok = r.Scan()
require.NoError(t, r.Err(), "Scan should succeed")
require.True(t, ok, "Scan should succeed")
assert.Equal(t, "Bob", string(BytesWritable(r.Key())), "The key should be correct")
assert.Equal(t, "Hope", string(BytesWritable(r.Value())), "The value should be correct")
// EOF
ok = r.Scan()
require.NoError(t, r.Err(), "Scan at the end of the file should fail without an error")
require.False(t, ok, "Scan at the end of the file should fail without an error")
file.Seek(offset1, os.SEEK_SET)
r.Reset()
ok = r.Scan()
require.NoError(t, r.Err(), "Scan should succeed")
require.True(t, ok, "Scan should succeed")
assert.Equal(t, "Alice", string(BytesWritable(r.Key())), "The key should be correct")
assert.Equal(t, "Practice", string(BytesWritable(r.Value())), "The value should be correct")
}