herald.go/db/db_test.go

211 lines
3.8 KiB
Go
Raw Normal View History

package db
import (
2021-12-11 23:22:45 +01:00
"encoding/hex"
"fmt"
"log"
"testing"
2021-12-11 23:22:45 +01:00
"github.com/lbryio/hub/db/prefixes"
)
2021-12-11 23:22:45 +01:00
func TestReadUTXO2(t *testing.T) {
tests := []struct {
2021-12-12 18:40:51 +01:00
name string
want []uint64
wantTotal int
2021-12-11 23:22:45 +01:00
}{
{
2021-12-12 18:40:51 +01:00
name: "Read UTXO Key Values With Stop",
want: []uint64{2174594, 200000000, 20000000, 100000, 603510, 75000000, 100000, 962984, 25000000, 50000000},
wantTotal: 7,
2021-12-11 23:22:45 +01:00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-12-12 18:40:51 +01:00
db, err := GetDB("../resources/asdf.db")
2021-12-11 23:22:45 +01:00
if err != nil {
t.Errorf("err not nil: %+v\n", err)
}
2021-12-12 01:24:37 +01:00
defer db.Close()
2021-12-11 23:22:45 +01:00
utxoRow := &PrefixRow{
// KeyStruct: UTXOKey{},
// ValueStruct: UTXOValue{},
Prefix: prefixes.UTXO,
KeyPackFunc: nil,
ValuePackFunc: nil,
DB: db,
}
2021-12-12 18:40:51 +01:00
b, err := hex.DecodeString("000012")
2021-12-11 23:22:45 +01:00
if err != nil {
log.Println(err)
}
stopKey := &UTXOKey{
Prefix: prefixes.UTXO,
HashX: b,
TxNum: 0,
Nout: 0,
}
stop := UTXOKeyPackPartial(stopKey, 1)
2021-12-13 00:35:16 +01:00
options := &IterOptions{
FillCache: false,
Start: nil,
Stop: stop,
IncludeStart: true,
IncludeStop: false,
IncludeKey: true,
IncludeValue: true,
}
2021-12-11 23:22:45 +01:00
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] {
2021-12-12 18:40:51 +01:00
t.Errorf("got: %d, want: %d\n", got, tt.want[i])
2021-12-11 23:22:45 +01:00
}
i++
}
2021-12-12 18:40:51 +01:00
got := i
if got != tt.wantTotal {
t.Errorf("got: %d, want: %d\n", got, tt.want)
}
2021-12-11 23:22:45 +01:00
})
}
}
2021-12-12 18:40:51 +01:00
// func TestReadUTXO(t *testing.T) {
// tests := []struct {
// name string
// want int
// }{
// {
// name: "Read UTXO Key Values",
// want: 12,
// },
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// db, err := GetDB("../resources/asdf.db")
// if err != nil {
// t.Errorf("err not nil: %+v\n", err)
// }
// defer db.Close()
// 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) {
2021-12-11 23:22:45 +01:00
tests := []struct {
name string
want int
}{
{
2021-12-12 18:40:51 +01:00
name: "Open a rocksdb database",
2021-12-11 23:22:45 +01:00
want: 10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-12-12 18:40:51 +01:00
vals := OpenDB("../resources/tmp.db", "foo")
got := vals
2021-12-11 23:22:45 +01:00
if got != tt.want {
t.Errorf("got: %d, want: %d\n", got, tt.want)
}
})
}
}
2021-12-12 18:40:51 +01:00
func TestOpenDB2(t *testing.T) {
tests := []struct {
name string
2021-12-07 12:45:18 +01:00
want int
}{
{
name: "Open a rocksdb database",
2021-12-07 12:45:18 +01:00
want: 10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-12-12 18:40:51 +01:00
OpenDB("../resources/asdf.db", "u")
// got := vals
2021-12-12 18:40:51 +01:00
// if got != tt.want {
// t.Errorf("got: %d, want: %d\n", got, tt.want)
// }
})
}
}
2021-12-11 23:22:45 +01:00
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,
2021-12-12 01:24:37 +01:00
want: "*db.UTXOKey(hashX=41414141414141414141, tx_num=0, nout=0)",
2021-12-11 23:22:45 +01:00
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-12-12 01:24:37 +01:00
key := &UTXOKey{
2021-12-11 23:22:45 +01:00
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)
}
})
}
}