storage: test sqlite in-memory
This commit is contained in:
@@ -5,11 +5,10 @@ import "database/sql"
|
||||
func createBlockTable(conn *sql.DB) error {
|
||||
tableCreation := `
|
||||
CREATE TABLE IF NOT EXISTS blocks (
|
||||
height INTEGER,
|
||||
height INTEGER PRIMARY KEY,
|
||||
hash TEXT,
|
||||
has_sapling_tx BOOL,
|
||||
compact_encoding BLOB,
|
||||
PRIMARY KEY (height, hash)
|
||||
compact_encoding BLOB
|
||||
);
|
||||
`
|
||||
_, err := conn.Exec(tableCreation)
|
||||
|
||||
@@ -2,13 +2,10 @@ package storage
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
protobuf "github.com/golang/protobuf/proto"
|
||||
@@ -17,24 +14,6 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
conn, err := sql.Open("sqlite3", "testdata/blocks.db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = createBlockTable(conn)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
ret := m.Run()
|
||||
conn.Close()
|
||||
//os.Remove("testdata/blocks.db")
|
||||
os.Exit(ret)
|
||||
}
|
||||
|
||||
func TestFillDB(t *testing.T) {
|
||||
type compactTest struct {
|
||||
BlockHeight int `json:"block"`
|
||||
@@ -54,11 +33,15 @@ func TestFillDB(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
conn, err := sql.Open("sqlite3", "testdata/blocks.db")
|
||||
conn, err := sql.Open("sqlite3", ":memory:")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
err = createBlockTable(conn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, test := range compactTests {
|
||||
blockData, _ := hex.DecodeString(test.Full)
|
||||
@@ -73,13 +56,23 @@ func TestFillDB(t *testing.T) {
|
||||
hash := hex.EncodeToString(block.GetHash())
|
||||
hasSapling := block.HasSaplingTransactions()
|
||||
marshaled, _ := protobuf.Marshal(block.ToCompact())
|
||||
compactB64 := base64.RawStdEncoding.EncodeToString(marshaled)
|
||||
|
||||
insertBlock := "INSERT INTO blocks (height, hash, has_sapling_tx, compact_encoding) values (?, ?, ?, ?)"
|
||||
_, err := conn.Exec(insertBlock, height, hash, hasSapling, compactB64)
|
||||
_, err := conn.Exec(insertBlock, height, hash, hasSapling, marshaled)
|
||||
if err != nil {
|
||||
t.Error(errors.Wrap(err, fmt.Sprintf("storing compact block %d", height)))
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
var count int
|
||||
countBlocks := "SELECT count(*) FROM blocks"
|
||||
conn.QueryRow(countBlocks).Scan(&count)
|
||||
if err != nil {
|
||||
t.Error(errors.Wrap(err, fmt.Sprintf("counting compact blocks")))
|
||||
}
|
||||
|
||||
if count != len(compactTests) {
|
||||
t.Errorf("Wrong row count, want %d got %d", len(compactTests), count)
|
||||
}
|
||||
}
|
||||
|
||||
BIN
storage/testdata/blocks.db
vendored
BIN
storage/testdata/blocks.db
vendored
Binary file not shown.
Reference in New Issue
Block a user