storage/leveldb_test.go

71 lines
1.4 KiB
Go
Executable File

package storage
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"testing"
)
func TestLevelDBListLimitedAscending(t *testing.T) {
d, err := ioutil.TempDir(os.TempDir(), "leveldb.list.test.*")
if err != nil {
t.Fatal(err)
}
db, err := NewLevelDB(d)
if err != nil {
t.Fatal(err)
}
db.Set("key", []byte("hi"), "bad")
n := 20
for i := 0; i < n; i++ {
db.Set(fmt.Sprintf("%02d", i), []byte(strconv.Itoa(i)), "good")
}
if list, err := db.List([]string{}); err != nil {
t.Error(err)
} else if len(list) != 0 {
t.Error(len(list), list)
}
if list, err := db.List([]string{""}); err != nil {
t.Error(err)
} else if len(list) != n+1 {
t.Error(len(list), list)
}
if list, err := db.List([]string{"good"}); err != nil {
t.Error(err)
} else if len(list) != n {
t.Error(len(list), list)
}
if list, err := db.List([]string{"good"}, "10"); err != nil {
t.Error(err)
} else if len(list) != n-10 {
t.Error(len(list), list)
}
if list, err := db.List([]string{"good"}, "10", "15"); err != nil {
t.Error(err)
} else if len(list) != 15-10+1 {
t.Error(len(list), list)
}
if list, err := db.List([]string{"good"}, "10", "15", "2"); err != nil {
t.Error(err)
} else if len(list) != 2 {
t.Error(len(list), list)
}
if list, err := db.List([]string{"good"}, "10", "15", "2", "-"); err != nil {
t.Error(err)
} else if len(list) != 2 {
t.Error(len(list), list)
} else if list[1] > list[0] {
t.Errorf("not desc: %v", list)
}
}