herald.go/db/db_test.go
2021-12-11 17:22:45 -05:00

170 lines
3 KiB
Go

package db
import (
"encoding/hex"
"fmt"
"log"
"testing"
"github.com/lbryio/hub/db/prefixes"
)
func TestReadUTXO2(t *testing.T) {
tests := []struct {
name string
want []uint64
}{
{
name: "Read 10 UTXO Key Values",
want: []uint64{2174594, 200000000, 20000000, 100000, 603510, 75000000, 100000, 962984, 25000000, 50000000},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db, err := GetDB("/mnt/d/data/wallet/lbry-rocksdb")
if err != nil {
t.Errorf("err not nil: %+v\n", err)
}
utxoRow := &PrefixRow{
// KeyStruct: UTXOKey{},
// ValueStruct: UTXOValue{},
Prefix: prefixes.UTXO,
KeyPackFunc: nil,
ValuePackFunc: nil,
DB: db,
}
b, err := hex.DecodeString("000012b")
if err != nil {
log.Println(err)
}
stopKey := &UTXOKey{
Prefix: prefixes.UTXO,
HashX: b,
TxNum: 0,
Nout: 0,
}
stop := UTXOKeyPackPartial(stopKey, 1)
options := NewIterateOptions().WithFillCache(false).WithStop(stop)
log.Println(options)
ch := utxoRow.Iter(options)
var i = 0
for kv := range ch {
log.Println(kv.Key)
log.Println(UTXOKeyUnpack(kv.Key))
log.Println(UTXOValueUnpack(kv.Value))
got := UTXOValueUnpack(kv.Value).Amount
if got != tt.want[i] {
t.Errorf("got: %d, want: %d\n", got, tt.want)
}
i++
if i >= 10 {
break
}
}
})
}
}
func TestReadUTXO(t *testing.T) {
tests := []struct {
name string
want int
}{
{
name: "Read 10 UTXO Key Values",
want: 10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db, err := GetDB("/mnt/d/data/wallet/lbry-rocksdb")
if err != nil {
t.Errorf("err not nil: %+v\n", err)
}
data := ReadPrefixN(db, prefixes.UTXO, tt.want)
got := len(data)
for _, kv := range data {
log.Println(UTXOKeyUnpack(kv.Key))
log.Println(UTXOValueUnpack(kv.Value))
}
if got != tt.want {
t.Errorf("got: %d, want: %d\n", got, tt.want)
}
})
}
}
// TestOpenDB test to see if we can open a db
func TestOpenDB(t *testing.T) {
tests := []struct {
name string
want int
}{
{
name: "Open a rocksdb database",
want: 10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
vals := OpenDB("../resources/tmp.db")
got := vals
if got != tt.want {
t.Errorf("got: %d, want: %d\n", got, tt.want)
}
})
}
}
func TestUTXOKey_String(t *testing.T) {
tests := []struct {
name string
prefix []byte
hashx []byte
txnum uint32
nout uint16
want string
}{
{
name: "Converts to string",
prefix: []byte("u"),
hashx: []byte("AAAAAAAAAA"),
txnum: 0,
nout: 0,
want: "db.UTXOKey(hashX=41414141414141414141, tx_num=0, nout=0)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
key := UTXOKey{
Prefix: tt.prefix,
HashX: tt.hashx,
TxNum: tt.txnum,
Nout: tt.nout,
}
got := fmt.Sprint(key)
log.Println(got)
if got != tt.want {
t.Errorf("got: %s, want: %s\n", got, tt.want)
}
})
}
}