jldb/mdb/pfile/main_test.go

66 lines
1.1 KiB
Go
Raw Normal View History

2023-10-13 09:43:27 +00:00
package pfile
import (
"bytes"
crand "crypto/rand"
"path/filepath"
"testing"
2023-10-16 08:50:19 +00:00
"git.crumpington.com/public/jldb/lib/wal"
"git.crumpington.com/public/jldb/mdb/change"
2023-10-13 09:43:27 +00:00
)
func newForTesting(t *testing.T) (*File, *Index) {
t.Helper()
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "pagefile")
pf, err := Open(filePath)
if err != nil {
t.Fatal(err)
}
idx, err := NewIndex(pf)
if err != nil {
t.Fatal(err)
}
return pf, idx
}
func randBytes(size int) []byte {
buf := make([]byte, size)
if _, err := crand.Read(buf); err != nil {
panic(err)
}
return buf
}
func changesToRec(changes []change.Change) wal.Record {
buf := &bytes.Buffer{}
if err := change.Write(changes, buf); err != nil {
panic(err)
}
return wal.Record{
DataSize: int64(buf.Len()),
Reader: buf,
}
}
func TestChangesToRec(t *testing.T) {
changes := []change.Change{
{
CollectionID: 2,
ItemID: 3,
Store: true,
Data: []byte{2, 3, 4},
WritePageIDs: []uint64{0, 1},
ClearPageIDs: []uint64{2, 3},
},
}
rec := changesToRec(changes)
c2 := []change.Change{}
c2, _ = change.Read(c2, rec.Reader)
}