<commit_before>package blockchain

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"runtime/debug"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/tendermint/go-wire"
	"github.com/tendermint/tendermint/types"
	"github.com/tendermint/tmlibs/db"
	"github.com/tendermint/tmlibs/log"
)

func TestLoadBlockStoreStateJSON(t *testing.T) {
	db := db.NewMemDB()

	bsj := &BlockStoreStateJSON{Height: 1000}
	bsj.Save(db)

	retrBSJ := LoadBlockStoreStateJSON(db)

	assert.Equal(t, *bsj, retrBSJ, "expected the retrieved DBs to match")
}

func TestNewBlockStore(t *testing.T) {
	db := db.NewMemDB()
	db.Set(blockStoreKey, []byte(`{"height": 10000}`))
	bs := NewBlockStore(db)
	assert.Equal(t, bs.Height(), 10000, "failed to properly parse blockstore")

	panicCausers := []struct {
		data    []byte
		wantErr string
	}{
		{[]byte("artful-doger"), "not unmarshal bytes"},
		{[]byte(""), "unmarshal bytes"},
		{[]byte(" "), "unmarshal bytes"},
	}

	for i, tt := range panicCausers {
		// Expecting a panic here on trying to parse an invalid blockStore
		_, _, panicErr := doFn(func() (interface{}, error) {
			db.Set(blockStoreKey, tt.data)
			_ = NewBlockStore(db)
			return nil, nil
		})
		require.NotNil(t, panicErr, "#%d panicCauser: %q expected a panic", i, tt.data)
		assert.Contains(t, panicErr.Error(), tt.wantErr, "#%d data: %q", i, tt.data)
	}

	db.Set(blockStoreKey, nil)
	bs = NewBlockStore(db)
	assert.Equal(t, bs.Height(), 0, "expecting nil bytes to be unmarshaled alright")
}

func TestBlockStoreGetReader(t *testing.T) {
	db := db.NewMemDB()
	// Initial setup
	db.Set([]byte("Foo"), []byte("Bar"))
	db.Set([]byte("Foo1"), nil)

	bs := NewBlockStore(db)

	tests := [...]struct {
		key  []byte
		want []byte
	}{
		0: {key: []byte("Foo"), want: []byte("Bar")},
		1: {key: []byte("KnoxNonExistent"), want: nil},
		2: {key: []byte("Foo1"), want: nil},
	}

	for i, tt := range tests {
		r := bs.GetReader(tt.key)
		if r == nil {
			assert.Nil(t, tt.want, "#%d: expected a non-nil reader", i)
			continue
		}
		slurp, err := ioutil.ReadAll(r)
		if err != nil {
			t.Errorf("#%d: unexpected Read err: %v", i, err)
		} else {
			assert.Equal(t, slurp, tt.want, "#%d: mismatch", i)
		}
	}
}

func freshBlockStore() (*BlockStore, db.DB) {
	db := db.NewMemDB()
	return NewBlockStore(db), db
}

var (
	state, _ = makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer)))

	block       = makeBlock(1, state)
	partSet     = block.MakePartSet(2)
	part1       = partSet.GetPart(0)
	part2       = partSet.GetPart(1)
	seenCommit1 = &types.Commit{Precommits: []*types.Vote{{Height: 10}}}
)

func TestBlockStoreSaveLoadBlock(t *testing.T) {
	state, bs := makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer)))
	require.Equal(t, bs.Height(), 0, "initially the height should be zero")

	noBlockHeights := []int{0, -1, 100, 1000, 2}
	for i, height := range noBlockHeights {
		if g := bs.LoadBlock(height); g != nil {
			t.Errorf("#%d: height(%d) got a block; want nil", i, height)
		}
	}
	block := makeBlock(bs.Height()+1, state)

	validPartSet := block.MakePartSet(2)
	seenCommit := &types.Commit{Precommits: []*types.Vote{{Height: 10}}}
	bs.SaveBlock(block, partSet, seenCommit)
	require.Equal(t, bs.Height(), block.Header.Height, "expecting the new height to be changed")

	incompletePartSet := types.NewPartSetFromHeader(types.PartSetHeader{Total: 2})

	uncontiguousPartSet := types.NewPartSetFromHeader(types.PartSetHeader{Total: 0})
	uncontiguousPartSet.AddPart(part2, false)

	header1 := types.Header{
		Height:  1,
		NumTxs:  100,<commit_msg>blockchain: note about store tests needing simplification ...<commit_after>package blockchain

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"runtime/debug"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/tendermint/go-wire"
	"github.com/tendermint/tendermint/types"
	"github.com/tendermint/tmlibs/db"
	"github.com/tendermint/tmlibs/log"
)

func TestLoadBlockStoreStateJSON(t *testing.T) {
	db := db.NewMemDB()

	bsj := &BlockStoreStateJSON{Height: 1000}
	bsj.Save(db)

	retrBSJ := LoadBlockStoreStateJSON(db)

	assert.Equal(t, *bsj, retrBSJ, "expected the retrieved DBs to match")
}

func TestNewBlockStore(t *testing.T) {
	db := db.NewMemDB()
	db.Set(blockStoreKey, []byte(`{"height": 10000}`))
	bs := NewBlockStore(db)
	assert.Equal(t, bs.Height(), 10000, "failed to properly parse blockstore")

	panicCausers := []struct {
		data    []byte
		wantErr string
	}{
		{[]byte("artful-doger"), "not unmarshal bytes"},
		{[]byte(""), "unmarshal bytes"},
		{[]byte(" "), "unmarshal bytes"},
	}

	for i, tt := range panicCausers {
		// Expecting a panic here on trying to parse an invalid blockStore
		_, _, panicErr := doFn(func() (interface{}, error) {
			db.Set(blockStoreKey, tt.data)
			_ = NewBlockStore(db)
			return nil, nil
		})
		require.NotNil(t, panicErr, "#%d panicCauser: %q expected a panic", i, tt.data)
		assert.Contains(t, panicErr.Error(), tt.wantErr, "#%d data: %q", i, tt.data)
	}

	db.Set(blockStoreKey, nil)
	bs = NewBlockStore(db)
	assert.Equal(t, bs.Height(), 0, "expecting nil bytes to be unmarshaled alright")
}

func TestBlockStoreGetReader(t *testing.T) {
	db := db.NewMemDB()
	// Initial setup
	db.Set([]byte("Foo"), []byte("Bar"))
	db.Set([]byte("Foo1"), nil)

	bs := NewBlockStore(db)

	tests := [...]struct {
		key  []byte
		want []byte
	}{
		0: {key: []byte("Foo"), want: []byte("Bar")},
		1: {key: []byte("KnoxNonExistent"), want: nil},
		2: {key: []byte("Foo1"), want: nil},
	}

	for i, tt := range tests {
		r := bs.GetReader(tt.key)
		if r == nil {
			assert.Nil(t, tt.want, "#%d: expected a non-nil reader", i)
			continue
		}
		slurp, err := ioutil.ReadAll(r)
		if err != nil {
			t.Errorf("#%d: unexpected Read err: %v", i, err)
		} else {
			assert.Equal(t, slurp, tt.want, "#%d: mismatch", i)
		}
	}
}

func freshBlockStore() (*BlockStore, db.DB) {
	db := db.NewMemDB()
	return NewBlockStore(db), db
}

var (
	state, _ = makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer)))

	block       = makeBlock(1, state)
	partSet     = block.MakePartSet(2)
	part1       = partSet.GetPart(0)
	part2       = partSet.GetPart(1)
	seenCommit1 = &types.Commit{Precommits: []*types.Vote{{Height: 10}}}
)

// TODO: This test should be simplified ...

func TestBlockStoreSaveLoadBlock(t *testing.T) {
	state, bs := makeStateAndBlockStore(log.NewTMLogger(new(bytes.Buffer)))
	require.Equal(t, bs.Height(), 0, "initially the height should be zero")

	// check there are no blocks at various heights
	noBlockHeights := []int{0, -1, 100, 1000, 2}
	for i, height := range noBlockHeights {
		if g := bs.LoadBlock(height); g != nil {
			t.Errorf("#%d: height(%d) got a block; want nil", i, height)
		}
	}

	// save a block
	block := makeBlock(bs.Height()+1, state)
	validPartSet := block.MakePartSet(2)
	seenCommit := &types.Commit{Precommits: []*types.Vote{{Height: 10}}}
	bs.SaveBlock(block, partSet, seenCommit)
	require.Equal(t, bs.Height(), block.Header.Height, "expecting the new height to be changed")

	incompletePartSet := types.NewPartSetFromHeader(types.PartSetHeader{Total: 2})
	uncontiguousPartSet := types.NewPartSetFromHeader(types.PartSetHeader{Total: 0})
	uncontiguousPartSet.AddPart(part2, false)

	header1 := types.Header{
		Height:  1,
		NumTxs:  100,
{'fraction_non_alphanumeric': 0.17393675027262814, 'fraction_numerical': 0.014176663031624863, 'mean_word_length': 4.336, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3591574', 'n_tokens_mistral': 2992, 'n_tokens_neox': 2794, 'n_words': 625}