tdb/indicies_test.go
2020-04-15 23:53:50 -05:00

170 lines
5 KiB
Go

package tdb
import (
"errors"
"log"
"reflect"
"testing"
// "encoding/ascii85"
// "log"
// "strconv"
bolt "go.etcd.io/bbolt"
)
type testIndexish struct {
indexKeysResponses map[int64][][]byte
}
func (i *testIndexish) debugLog(s string) {
if debug {
log.Print(s)
}
}
func (i *testIndexish) debugLogf(f string, args ...interface{}) {
if debug {
log.Printf(f, args...)
}
}
func (i *testIndexish) count(tx *Tx) int { panic(errors.New("unimplemented")) }
func (i *testIndexish) initialize(tx *Tx) error { panic(errors.New("unimplemented")) }
func (i *testIndexish) validate(tx *Tx, val dbPtrValue) error { panic(errors.New("unimplemented")) }
func (i *testIndexish) update(tx *Tx, old, new dbPtrValue) { panic(errors.New("unimplemented")) }
func (i *testIndexish) updateRaw(tx *Tx, write, delete [][]byte) {
panic(errors.New("unimplemented"))
}
func (i *testIndexish) put(tx *Tx, val dbPtrValue) { panic(errors.New("unimplemented")) }
func (i *testIndexish) putRaw(tx *Tx, val [][]byte) { panic(errors.New("unimplemented")) }
func (i *testIndexish) delete(tx *Tx, val dbPtrValue) { panic(errors.New("unimplemented")) }
func (i *testIndexish) deleteRaw(tx *Tx, val [][]byte) { panic(errors.New("unimplemented")) }
func (i *testIndexish) bucket(tx *Tx) *bolt.Bucket { panic(errors.New("unimplemented")) }
func (i *testIndexish) iteratePrefixed(tx *Tx, prefix []byte, it KeyIterator) error {
panic(errors.New("unimplemented"))
}
func (i *testIndexish) indexedValues(val dbPtrValue) [][]byte { panic(errors.New("unimplemented")) }
func (i *testIndexish) keyValue(val dbPtrValue) []byte { panic(errors.New("unimplemented")) }
func (i *testIndexish) indexKeys(val dbPtrValue) [][]byte {
rv := reflect.Value(val)
if rv.Type().Kind() != reflect.Int {
panic(errors.New("unknowable response"))
}
if r, ok := i.indexKeysResponses[rv.Int()]; ok {
return r
}
panic(errors.New("unknown response"))
}
func (i *testIndexish) shouldUpdate(tx *Tx, oldVal, newVal dbPtrValue) (needsUpdate bool, oldKeys, newKeys, writes, deletes [][]byte) {
panic(errors.New("unimplemented"))
}
type indexishShouldUpdateTest struct {
old [][]byte
new [][]byte
needsUpdate bool
writes [][]byte
deletes [][]byte
}
var indexishShouldUpdateTests = []indexishShouldUpdateTest{
{ // 0
old: [][]byte{},
new: [][]byte{},
needsUpdate: false,
writes: [][]byte{},
deletes: [][]byte{},
},
{ // 1
old: [][]byte{[]byte("1")},
new: [][]byte{},
needsUpdate: true,
writes: [][]byte{},
deletes: [][]byte{[]byte("1")},
},
{ // 2
old: [][]byte{},
new: [][]byte{[]byte("2")},
needsUpdate: true,
writes: [][]byte{[]byte("2")},
deletes: [][]byte{},
},
{ // 3
old: [][]byte{[]byte("1")},
new: [][]byte{[]byte("2")},
needsUpdate: true,
writes: [][]byte{[]byte("2")},
deletes: [][]byte{[]byte("1")},
},
{ // 4
old: [][]byte{[]byte("1")},
new: [][]byte{[]byte("1")},
needsUpdate: false,
writes: [][]byte{},
deletes: [][]byte{},
},
{ // 5
old: [][]byte{[]byte("1"), []byte("2")},
new: [][]byte{[]byte("1"), []byte("2")},
needsUpdate: false,
writes: [][]byte{},
deletes: [][]byte{},
},
{ // 6
old: [][]byte{[]byte("1"), []byte("2")},
new: [][]byte{[]byte("2")},
needsUpdate: true,
writes: [][]byte{},
deletes: [][]byte{[]byte("1")},
},
{ // 7
old: [][]byte{[]byte("2")},
new: [][]byte{[]byte("1"), []byte("2")},
needsUpdate: true,
writes: [][]byte{[]byte("1")},
deletes: [][]byte{},
},
{ // 8
old: [][]byte{[]byte("1"), []byte("2")},
new: [][]byte{[]byte("2"), []byte("3")},
needsUpdate: true,
writes: [][]byte{[]byte("3")},
deletes: [][]byte{[]byte("1")},
},
}
func TestIndexishShouldUpdate(t *testing.T) {
ti := &testIndexish{}
oldVal := dbPtrValue(reflect.ValueOf(1))
newVal := dbPtrValue(reflect.ValueOf(2))
for i, tc := range indexishShouldUpdateTests {
ti.debugLogf("- Started test case %d", i)
ti.indexKeysResponses = map[int64][][]byte{
1: tc.old,
2: tc.new,
}
needsUpdate, _, _, writes, deletes := indexishShouldUpdate(ti, oldVal, newVal)
if needsUpdate != tc.needsUpdate {
ti.debugLog("! Incorrect needsUpdate")
t.Errorf("Incorrect needsUpdate (case %d)", i)
// continue
}
if len(writes) != len(tc.writes) {
ti.debugLogf("! Wrong # of writes (%d vs %d)", len(writes), len(tc.writes))
for _, write := range writes {
ti.debugLogf("%s", write)
}
t.Errorf("Wrong # of writes (case %d, %d vs %d)", i, len(writes), len(tc.writes))
// continue
}
if len(deletes) != len(tc.deletes) {
ti.debugLogf("! Wrong # of deletes (%d vs %d)", len(deletes), len(tc.deletes))
for _, delete := range deletes {
ti.debugLogf("%s", delete)
}
t.Errorf("Wrong # of deletes (case %d, %d vs %d)", i, len(deletes), len(tc.deletes))
// continue
}
}
}