github.com/Tri-stone/burrow@v0.25.0/vent/sqldb/sqldb_test.go (about)

     1  // +build integration
     2  
     3  package sqldb_test
     4  
     5  import (
     6  	"fmt"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/hyperledger/burrow/vent/config"
    11  
    12  	"github.com/hyperledger/burrow/vent/sqlsol"
    13  	"github.com/hyperledger/burrow/vent/test"
    14  	"github.com/hyperledger/burrow/vent/types"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func testSynchronizeDB(t *testing.T, cfg *config.VentConfig) {
    19  	t.Run(fmt.Sprintf("%s: successfully creates database tables and synchronizes db", cfg.DBAdapter),
    20  		func(t *testing.T) {
    21  			goodJSON := test.GoodJSONConfFile(t)
    22  
    23  			byteValue := []byte(goodJSON)
    24  			tableStructure, err := sqlsol.NewProjectionFromBytes(byteValue)
    25  			require.NoError(t, err)
    26  
    27  			db, cleanUpDB := test.NewTestDB(t, cfg)
    28  			defer cleanUpDB()
    29  
    30  			err = db.Ping()
    31  			require.NoError(t, err)
    32  
    33  			err = db.SynchronizeDB(tableStructure.Tables)
    34  			require.NoError(t, err)
    35  		})
    36  }
    37  
    38  func testCleanDB(t *testing.T, cfg *config.VentConfig) {
    39  	t.Run(fmt.Sprintf("%s: successfully creates tables, updates chainID and drops all tables", cfg.DBAdapter),
    40  		func(t *testing.T) {
    41  			byteValue := []byte(test.GoodJSONConfFile(t))
    42  			tableStructure, err := sqlsol.NewProjectionFromBytes(byteValue)
    43  			require.NoError(t, err)
    44  
    45  			db, cleanUpDB := test.NewTestDB(t, cfg)
    46  			defer cleanUpDB()
    47  
    48  			err = db.Ping()
    49  			require.NoError(t, err)
    50  
    51  			err = db.SynchronizeDB(tableStructure.Tables)
    52  			require.NoError(t, err)
    53  
    54  			err = db.CleanTables("NEW_ID", "Version 1.0")
    55  			require.NoError(t, err)
    56  		})
    57  }
    58  
    59  func testSetBlock(t *testing.T, cfg *config.VentConfig) {
    60  	t.Run(fmt.Sprintf("%s: successfully inserts a block", cfg.DBAdapter),
    61  		func(t *testing.T) {
    62  			db, closeDB := test.NewTestDB(t, cfg)
    63  			defer closeDB()
    64  
    65  			errp := db.Ping()
    66  			require.NoError(t, errp)
    67  
    68  			// new
    69  			str, dat := getBlock()
    70  			err := db.SetBlock(str, dat)
    71  			require.NoError(t, err)
    72  
    73  			// read
    74  			_, err = db.GetLastBlockHeight()
    75  			require.NoError(t, err)
    76  
    77  			_, err = db.GetBlock(dat.BlockHeight)
    78  			require.NoError(t, err)
    79  
    80  			// alter
    81  			str, dat = getAlterBlock()
    82  			err = db.SetBlock(str, dat)
    83  			require.NoError(t, err)
    84  
    85  			//restore
    86  			ti := time.Now().Local().AddDate(10, 0, 0)
    87  			err = db.RestoreDB(ti, "RESTORED")
    88  			require.NoError(t, err)
    89  
    90  		})
    91  
    92  	t.Run(fmt.Sprintf("%s: successfully creates an empty table", cfg.DBAdapter), func(t *testing.T) {
    93  		db, closeDB := test.NewTestDB(t, cfg)
    94  		defer closeDB()
    95  
    96  		errp := db.Ping()
    97  		require.NoError(t, errp)
    98  
    99  		//table 1
   100  		tables := map[string]*types.SQLTable{
   101  			"AllDataTypesTable": {
   102  				Name: "AllDataTypesTable",
   103  				Columns: []*types.SQLTableColumn{
   104  					{Name: "test_id", Type: types.SQLColumnTypeSerial, Primary: true},
   105  					{Name: "col1", Type: types.SQLColumnTypeBool, Primary: false},
   106  					{Name: "col2", Type: types.SQLColumnTypeByteA, Primary: false},
   107  					{Name: "col3", Type: types.SQLColumnTypeInt, Primary: false},
   108  					{Name: "col4", Type: types.SQLColumnTypeText, Primary: false},
   109  					{Name: "col5", Type: types.SQLColumnTypeTimeStamp, Primary: false},
   110  					{Name: "col6", Type: types.SQLColumnTypeVarchar, Length: 100, Primary: false},
   111  				},
   112  			},
   113  		}
   114  
   115  		err := db.SynchronizeDB(tables)
   116  		require.NoError(t, err)
   117  	})
   118  }
   119  
   120  func getBlock() (types.EventTables, types.EventData) {
   121  	longtext := "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM"
   122  	longtext = fmt.Sprintf("%s %s %s %s %s", longtext, longtext, longtext, longtext, longtext)
   123  
   124  	//table 1
   125  	table1 := &types.SQLTable{
   126  		Name: "test_table1",
   127  		Columns: []*types.SQLTableColumn{
   128  			{Name: "test_id", Type: types.SQLColumnTypeInt, Primary: true},
   129  			{Name: "col1", Type: types.SQLColumnTypeVarchar, Length: 100, Primary: false},
   130  			{Name: "col2", Type: types.SQLColumnTypeVarchar, Length: 100, Primary: false},
   131  			{Name: "_height", Type: types.SQLColumnTypeVarchar, Length: 100, Primary: false},
   132  			{Name: "col4", Type: types.SQLColumnTypeText, Primary: false},
   133  			{Name: "colV", Type: types.SQLColumnTypeVarchar, Length: 400, Primary: false},
   134  			{Name: "colT", Type: types.SQLColumnTypeText, Length: 0, Primary: false},
   135  		},
   136  	}
   137  
   138  	//table 2
   139  	table2 := &types.SQLTable{
   140  		Name: "test_table2",
   141  		Columns: []*types.SQLTableColumn{
   142  			{Name: "_height", Type: types.SQLColumnTypeVarchar, Length: 100, Primary: true},
   143  			{Name: "sid_id", Type: types.SQLColumnTypeInt, Primary: true},
   144  			{Name: "field_1", Type: types.SQLColumnTypeVarchar, Length: 100, Primary: false},
   145  			{Name: "field_2", Type: types.SQLColumnTypeVarchar, Length: 100, Primary: false},
   146  		},
   147  	}
   148  
   149  	//table 3
   150  	table3 := &types.SQLTable{
   151  		Name: "test_table3",
   152  		Columns: []*types.SQLTableColumn{
   153  			{Name: "_height", Type: types.SQLColumnTypeVarchar, Length: 100, Primary: true},
   154  			{Name: "val", Type: types.SQLColumnTypeInt, Primary: false},
   155  		},
   156  	}
   157  
   158  	//table 4
   159  	table4 := &types.SQLTable{
   160  		Name: "test_table4",
   161  		Columns: []*types.SQLTableColumn{
   162  			{Name: "index", Type: types.SQLColumnTypeInt, Primary: true},
   163  			{Name: "time", Type: types.SQLColumnTypeTimeStamp, Primary: false},
   164  			{Name: "_height", Type: types.SQLColumnTypeVarchar, Length: 100, Primary: false},
   165  		},
   166  	}
   167  
   168  	str := make(types.EventTables)
   169  	str["1"] = table1
   170  	str["2"] = table2
   171  	str["3"] = table3
   172  	str["4"] = table4
   173  
   174  	//---------------------------------------data-------------------------------------
   175  	var dat types.EventData
   176  	dat.BlockHeight = 2134234
   177  	dat.Tables = make(map[string]types.EventDataTable)
   178  
   179  	var rows1 []types.EventDataRow
   180  	rows1 = append(rows1, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"test_id": "1", "col1": "text11", "col2": "text12", "_height": dat.BlockHeight, "col4": "14", "colV": longtext, "colT": longtext}})
   181  	rows1 = append(rows1, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"test_id": "2", "col1": "text21", "col2": "text22", "_height": dat.BlockHeight, "col4": "24", "colV": longtext, "colT": longtext}})
   182  	rows1 = append(rows1, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"test_id": "3", "col1": "text31", "col2": "text32", "_height": dat.BlockHeight, "col4": "34", "colV": longtext, "colT": longtext}})
   183  	rows1 = append(rows1, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"test_id": "4", "col1": "text41", "col3": "text43", "_height": dat.BlockHeight, "colV": longtext, "colT": longtext}})
   184  	rows1 = append(rows1, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"test_id": "1", "col1": "upd", "col2": "upd", "_height": dat.BlockHeight, "col4": "upd", "colV": longtext, "colT": longtext}})
   185  	dat.Tables["test_table1"] = rows1
   186  
   187  	var rows2 []types.EventDataRow
   188  	rows2 = append(rows2, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"_height": dat.BlockHeight, "sid_id": "1", "field_1": "A", "field_2": "B"}})
   189  	rows2 = append(rows2, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"_height": dat.BlockHeight, "sid_id": "2", "field_1": "C", "field_2": ""}})
   190  	rows2 = append(rows2, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"_height": dat.BlockHeight, "sid_id": "3", "field_1": "D", "field_2": "E"}})
   191  	rows2 = append(rows2, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"_height": dat.BlockHeight, "sid_id": "1", "field_1": "F"}})
   192  	rows2 = append(rows2, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"_height": dat.BlockHeight, "sid_id": "1", "field_2": "U"}})
   193  	dat.Tables["test_table2"] = rows2
   194  
   195  	var rows3 []types.EventDataRow
   196  	rows3 = append(rows3, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"_height": "0123456789ABCDEF1", "val": "1"}})
   197  	rows3 = append(rows3, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"_height": "0123456789ABCDEF2", "val": "2"}})
   198  	rows3 = append(rows3, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"_height": "0123456789ABCDEFX", "val": "-1"}})
   199  	rows3 = append(rows3, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"_height": dat.BlockHeight}})
   200  	dat.Tables["test_table3"] = rows3
   201  
   202  	var rows4 []types.EventDataRow
   203  	rows4 = append(rows4, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"_height": dat.BlockHeight, "time": "2006-01-01 15:04:05", "index": "1"}})
   204  	rows4 = append(rows4, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"_height": dat.BlockHeight, "time": "2006-01-02 15:04:05", "index": "2"}})
   205  	rows4 = append(rows4, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"_height": dat.BlockHeight, "time": "2006-01-03 15:04:05", "index": "3"}})
   206  	rows4 = append(rows4, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"_height": dat.BlockHeight, "time": "2006-01-03 15:04:05", "index": "4"}})
   207  	rows4 = append(rows4, types.EventDataRow{Action: types.ActionDelete, RowData: map[string]interface{}{"_height": dat.BlockHeight, "time": "2006-01-03 15:04:05", "index": "3"}})
   208  	dat.Tables["test_table4"] = rows4
   209  
   210  	return str, dat
   211  }
   212  
   213  func getAlterBlock() (types.EventTables, types.EventData) {
   214  	//table 3
   215  	table3 := &types.SQLTable{
   216  		Name: "test_table3",
   217  		Columns: []*types.SQLTableColumn{
   218  			{Name: "_height", Type: types.SQLColumnTypeVarchar, Length: 100, Primary: true},
   219  			{Name: "val", Type: types.SQLColumnTypeInt, Primary: false},
   220  			{Name: "val_alter", Type: types.SQLColumnTypeInt, Primary: false},
   221  		},
   222  	}
   223  
   224  	str := make(types.EventTables)
   225  	str["3"] = table3
   226  
   227  	//---------------------------------------data-------------------------------------
   228  	var dat types.EventData
   229  	dat.BlockHeight = 23423423
   230  	dat.Tables = make(map[string]types.EventDataTable)
   231  
   232  	var rows5 []types.EventDataRow
   233  	rows5 = append(rows5, types.EventDataRow{Action: types.ActionUpsert, RowData: map[string]interface{}{"_height": dat.BlockHeight, "val": "1", "val_alter": "1"}})
   234  	dat.Tables["test_table3"] = rows5
   235  
   236  	return str, dat
   237  }