github.com/aergoio/aergo@v1.3.1/state/statedata_test.go (about)

     1  package state
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  var (
    10  	testKey  = []byte("test_key")
    11  	testData = []byte("test_data")
    12  	testOver = []byte("test_over")
    13  )
    14  
    15  func TestStateDataBasic(t *testing.T) {
    16  	initTest(t)
    17  	defer deinitTest()
    18  
    19  	// save data
    20  	if err := saveData(stateDB.store, testKey, &testData); err != nil {
    21  		t.Errorf("failed to save data: %v", err.Error())
    22  	}
    23  
    24  	// load data
    25  	data := []byte{}
    26  	if err := loadData(stateDB.store, testKey, &data); err != nil {
    27  		t.Errorf("failed to load data: %v", err.Error())
    28  	}
    29  	assert.NotNil(t, data)
    30  	assert.Equal(t, testData, data)
    31  }
    32  
    33  func TestStateDataNil(t *testing.T) {
    34  	initTest(t)
    35  	defer deinitTest()
    36  
    37  	// load data before saving
    38  	var data interface{}
    39  	assert.Nil(t, data)
    40  	if err := loadData(stateDB.store, testKey, &data); err != nil {
    41  		t.Errorf("failed to load data: %v", err.Error())
    42  	}
    43  	assert.Nil(t, data)
    44  }
    45  
    46  func TestStateDataEmpty(t *testing.T) {
    47  	initTest(t)
    48  	defer deinitTest()
    49  
    50  	// save empty data
    51  	var testEmpty []byte
    52  	if err := saveData(stateDB.store, testKey, &testEmpty); err != nil {
    53  		t.Errorf("failed to save nil data: %v", err.Error())
    54  	}
    55  
    56  	// load empty data
    57  	data := []byte{}
    58  	if err := loadData(stateDB.store, testKey, &data); err != nil {
    59  		t.Errorf("failed to load data: %v", err.Error())
    60  	}
    61  	assert.NotNil(t, data)
    62  	assert.Empty(t, data)
    63  }
    64  
    65  func TestStateDataOverwrite(t *testing.T) {
    66  	initTest(t)
    67  	defer deinitTest()
    68  
    69  	// save data
    70  	if err := saveData(stateDB.store, testKey, &testData); err != nil {
    71  		t.Errorf("failed to save data: %v", err.Error())
    72  	}
    73  
    74  	// save another data to same key
    75  	if err := saveData(stateDB.store, testKey, &testOver); err != nil {
    76  		t.Errorf("failed to overwrite data: %v", err.Error())
    77  	}
    78  
    79  	// load data
    80  	data := []byte{}
    81  	if err := loadData(stateDB.store, testKey, &data); err != nil {
    82  		t.Errorf("failed to load data: %v", err.Error())
    83  	}
    84  	assert.NotNil(t, data)
    85  	assert.Equal(t, testOver, data)
    86  }