2014-01-09 06:54:52 +01:00
|
|
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
2013-08-22 17:40:59 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package ldb_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
2014-07-03 02:47:24 +02:00
|
|
|
|
2015-01-16 20:59:18 +01:00
|
|
|
"github.com/btcsuite/btcwire"
|
2014-07-03 02:47:24 +02:00
|
|
|
"github.com/conformal/btcdb"
|
2013-08-22 17:40:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// we need to test for empty databas and make certain it returns proper value
|
|
|
|
|
|
|
|
func TestEmptyDB(t *testing.T) {
|
2013-09-09 22:50:13 +02:00
|
|
|
|
2013-08-22 17:40:59 +02:00
|
|
|
dbname := "tstdbempty"
|
2013-09-25 22:18:35 +02:00
|
|
|
dbnamever := dbname + ".ver"
|
2013-08-22 17:40:59 +02:00
|
|
|
_ = os.RemoveAll(dbname)
|
2013-09-25 22:18:35 +02:00
|
|
|
_ = os.RemoveAll(dbnamever)
|
2013-08-22 17:40:59 +02:00
|
|
|
db, err := btcdb.CreateDB("leveldb", dbname)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to open test database %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dbname)
|
2013-09-25 22:18:35 +02:00
|
|
|
defer os.RemoveAll(dbnamever)
|
2013-08-22 17:40:59 +02:00
|
|
|
|
2013-09-10 20:17:24 +02:00
|
|
|
sha, height, err := db.NewestSha()
|
|
|
|
if !sha.IsEqual(&btcwire.ShaHash{}) {
|
|
|
|
t.Errorf("sha not zero hash")
|
|
|
|
}
|
|
|
|
if height != -1 {
|
|
|
|
t.Errorf("height not -1 %v", height)
|
|
|
|
}
|
|
|
|
|
2013-08-22 17:40:59 +02:00
|
|
|
// This is a reopen test
|
2014-07-07 16:50:50 +02:00
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
t.Errorf("Close: unexpected error: %v", err)
|
|
|
|
}
|
2013-08-22 17:40:59 +02:00
|
|
|
|
|
|
|
db, err = btcdb.OpenDB("leveldb", dbname)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to open test database %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-07 16:50:50 +02:00
|
|
|
defer func() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
t.Errorf("Close: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2013-08-22 17:40:59 +02:00
|
|
|
|
2013-09-10 20:17:24 +02:00
|
|
|
sha, height, err = db.NewestSha()
|
2013-09-10 17:44:26 +02:00
|
|
|
if !sha.IsEqual(&btcwire.ShaHash{}) {
|
2013-09-10 17:47:49 +02:00
|
|
|
t.Errorf("sha not zero hash")
|
2013-08-22 17:40:59 +02:00
|
|
|
}
|
|
|
|
if height != -1 {
|
|
|
|
t.Errorf("height not -1 %v", height)
|
|
|
|
}
|
|
|
|
}
|