Start interface tests that require state.

This commit is contained in:
Dave Collins 2013-10-13 20:24:03 -05:00
parent 82d1898b12
commit ff429203c4

View file

@ -10,6 +10,14 @@ import (
"testing"
)
var (
// ignoreDbTypes are types which should be ignored when running tests
// that iterate all supported DB types. This allows some tests to add
// bogus drivers for testing purposes while still allowing other tests
// to easily iterate all supported drivers.
ignoreDbTypes = map[string]bool{"createopenfail": true}
)
// testNewestShaEmpty ensures the NewestSha returns the values expected by
// the interface contract.
func testNewestShaEmpty(t *testing.T, db btcdb.Db) {
@ -157,3 +165,79 @@ func TestCreateOpenUnsupported(t *testing.T) {
return
}
}
// testInterface tests performs tests for the various interfaces of btcdb which
// require state in the database for the given database type.
func testInterface(t *testing.T, dbType string) {
db, teardown, err := setupDB(dbType, "interface")
if err != nil {
t.Errorf("Failed to create test database %v", err)
return
}
defer teardown()
// Load up a bunch of test blocks.
blocks, err := loadBlocks(t)
if err != nil {
t.Errorf("Unable to load blocks from test data %v: %v",
blockDataFile, err)
return
}
t.Logf("Loaded %d blocks", len(blocks))
for height := int64(1); height < int64(len(blocks)); height++ {
block := blocks[height]
// Ensure there are no errors inserting each block into the
// database.
newHeight, err := db.InsertBlock(block)
if err != nil {
t.Errorf("InsertBlock: failed to insert block %v err %v",
height, err)
return
}
if newHeight != height {
t.Errorf("InsertBlock: height mismatch got: %v, want: %v",
newHeight, height)
return
}
}
// TODO(davec): Need to figure out how to handle the special checks
// required for the duplicate transactions allowed by blocks 91842 and
// 91880 on the main network due to the old miner + Satoshi client bug.
// TODO(davec): Add tests for the following functions:
/*
Close()
DropAfterBlockBySha(*btcwire.ShaHash) (err error)
ExistsSha(sha *btcwire.ShaHash) (exists bool)
FetchBlockBySha(sha *btcwire.ShaHash) (blk *btcutil.Block, err error)
FetchBlockShaByHeight(height int64) (sha *btcwire.ShaHash, err error)
FetchHeightRange(startHeight, endHeight int64) (rshalist []btcwire.ShaHash, err error)
ExistsTxSha(sha *btcwire.ShaHash) (exists bool)
FetchTxBySha(txsha *btcwire.ShaHash) ([]*TxListReply, error)
FetchTxByShaList(txShaList []*btcwire.ShaHash) []*TxListReply
FetchUnSpentTxByShaList(txShaList []*btcwire.ShaHash) []*TxListReply
- InsertBlock(block *btcutil.Block) (height int64, err error)
InvalidateBlockCache()
InvalidateCache()
InvalidateTxCache()
NewIterateBlocks() (pbi BlockIterator, err error)
NewestSha() (sha *btcwire.ShaHash, height int64, err error)
RollbackClose()
SetDBInsertMode(InsertMode)
Sync()
*/
}
// TestInterface performs tests for the various interfaces of btcdb which
// require state in the database for each supported database type (those loaded
// in common_test.go that is).
func TestInterface(t *testing.T) {
for _, dbType := range btcdb.SupportedDBs() {
if _, exists := ignoreDbTypes[dbType]; !exists {
testInterface(t, dbType)
}
}
}