storage: test sqlite in-memory

This commit is contained in:
George Tankersley
2018-11-20 21:45:28 -05:00
parent 0dee0b425e
commit 77c3f771e0
3 changed files with 19 additions and 27 deletions

View File

@@ -5,11 +5,10 @@ import "database/sql"
func createBlockTable(conn *sql.DB) error { func createBlockTable(conn *sql.DB) error {
tableCreation := ` tableCreation := `
CREATE TABLE IF NOT EXISTS blocks ( CREATE TABLE IF NOT EXISTS blocks (
height INTEGER, height INTEGER PRIMARY KEY,
hash TEXT, hash TEXT,
has_sapling_tx BOOL, has_sapling_tx BOOL,
compact_encoding BLOB, compact_encoding BLOB
PRIMARY KEY (height, hash)
); );
` `
_, err := conn.Exec(tableCreation) _, err := conn.Exec(tableCreation)

View File

@@ -2,13 +2,10 @@ package storage
import ( import (
"database/sql" "database/sql"
"encoding/base64"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os"
"testing" "testing"
protobuf "github.com/golang/protobuf/proto" protobuf "github.com/golang/protobuf/proto"
@@ -17,24 +14,6 @@ import (
"github.com/pkg/errors" "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) { func TestFillDB(t *testing.T) {
type compactTest struct { type compactTest struct {
BlockHeight int `json:"block"` BlockHeight int `json:"block"`
@@ -54,11 +33,15 @@ func TestFillDB(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
conn, err := sql.Open("sqlite3", "testdata/blocks.db") conn, err := sql.Open("sqlite3", ":memory:")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer conn.Close() defer conn.Close()
err = createBlockTable(conn)
if err != nil {
t.Fatal(err)
}
for _, test := range compactTests { for _, test := range compactTests {
blockData, _ := hex.DecodeString(test.Full) blockData, _ := hex.DecodeString(test.Full)
@@ -73,13 +56,23 @@ func TestFillDB(t *testing.T) {
hash := hex.EncodeToString(block.GetHash()) hash := hex.EncodeToString(block.GetHash())
hasSapling := block.HasSaplingTransactions() hasSapling := block.HasSaplingTransactions()
marshaled, _ := protobuf.Marshal(block.ToCompact()) marshaled, _ := protobuf.Marshal(block.ToCompact())
compactB64 := base64.RawStdEncoding.EncodeToString(marshaled)
insertBlock := "INSERT INTO blocks (height, hash, has_sapling_tx, compact_encoding) values (?, ?, ?, ?)" 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 { if err != nil {
t.Error(errors.Wrap(err, fmt.Sprintf("storing compact block %d", height))) t.Error(errors.Wrap(err, fmt.Sprintf("storing compact block %d", height)))
continue 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)
}
} }

Binary file not shown.