github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/operation/computation_result_test.go (about)

     1  package operation
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/dgraph-io/badger/v2"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/onflow/flow-go/engine/execution"
    12  	"github.com/onflow/flow-go/engine/execution/testutil"
    13  	"github.com/onflow/flow-go/model/flow"
    14  	"github.com/onflow/flow-go/storage"
    15  	"github.com/onflow/flow-go/utils/unittest"
    16  )
    17  
    18  func TestInsertAndUpdateAndRetrieveComputationResultUpdateStatus(t *testing.T) {
    19  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    20  		expected := testutil.ComputationResultFixture(t)
    21  		expectedId := expected.ExecutableBlock.ID()
    22  
    23  		t.Run("Update existing ComputationResult", func(t *testing.T) {
    24  			// insert as False
    25  			testUploadStatusVal := false
    26  
    27  			err := db.Update(InsertComputationResultUploadStatus(expectedId, testUploadStatusVal))
    28  			require.NoError(t, err)
    29  
    30  			var actualUploadStatus bool
    31  			err = db.View(GetComputationResultUploadStatus(expectedId, &actualUploadStatus))
    32  			require.NoError(t, err)
    33  
    34  			assert.Equal(t, testUploadStatusVal, actualUploadStatus)
    35  
    36  			// update to True
    37  			testUploadStatusVal = true
    38  			err = db.Update(UpdateComputationResultUploadStatus(expectedId, testUploadStatusVal))
    39  			require.NoError(t, err)
    40  
    41  			// check if value is updated
    42  			err = db.View(GetComputationResultUploadStatus(expectedId, &actualUploadStatus))
    43  			require.NoError(t, err)
    44  
    45  			assert.Equal(t, testUploadStatusVal, actualUploadStatus)
    46  		})
    47  
    48  		t.Run("Update non-existed ComputationResult", func(t *testing.T) {
    49  			testUploadStatusVal := true
    50  			randomFlowID := flow.Identifier{}
    51  			err := db.Update(UpdateComputationResultUploadStatus(randomFlowID, testUploadStatusVal))
    52  			require.Error(t, err)
    53  			require.Equal(t, err, storage.ErrNotFound)
    54  		})
    55  	})
    56  }
    57  
    58  func TestUpsertAndRetrieveComputationResultUpdateStatus(t *testing.T) {
    59  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    60  		expected := testutil.ComputationResultFixture(t)
    61  		expectedId := expected.ExecutableBlock.ID()
    62  
    63  		t.Run("Upsert ComputationResult", func(t *testing.T) {
    64  			// first upsert as false
    65  			testUploadStatusVal := false
    66  
    67  			err := db.Update(UpsertComputationResultUploadStatus(expectedId, testUploadStatusVal))
    68  			require.NoError(t, err)
    69  
    70  			var actualUploadStatus bool
    71  			err = db.View(GetComputationResultUploadStatus(expectedId, &actualUploadStatus))
    72  			require.NoError(t, err)
    73  
    74  			assert.Equal(t, testUploadStatusVal, actualUploadStatus)
    75  
    76  			// upsert to true
    77  			testUploadStatusVal = true
    78  			err = db.Update(UpsertComputationResultUploadStatus(expectedId, testUploadStatusVal))
    79  			require.NoError(t, err)
    80  
    81  			// check if value is updated
    82  			err = db.View(GetComputationResultUploadStatus(expectedId, &actualUploadStatus))
    83  			require.NoError(t, err)
    84  
    85  			assert.Equal(t, testUploadStatusVal, actualUploadStatus)
    86  		})
    87  	})
    88  }
    89  
    90  func TestRemoveComputationResultUploadStatus(t *testing.T) {
    91  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    92  		expected := testutil.ComputationResultFixture(t)
    93  		expectedId := expected.ExecutableBlock.ID()
    94  
    95  		t.Run("Remove ComputationResult", func(t *testing.T) {
    96  			testUploadStatusVal := true
    97  
    98  			err := db.Update(InsertComputationResultUploadStatus(expectedId, testUploadStatusVal))
    99  			require.NoError(t, err)
   100  
   101  			var actualUploadStatus bool
   102  			err = db.View(GetComputationResultUploadStatus(expectedId, &actualUploadStatus))
   103  			require.NoError(t, err)
   104  
   105  			assert.Equal(t, testUploadStatusVal, actualUploadStatus)
   106  
   107  			err = db.Update(RemoveComputationResultUploadStatus(expectedId))
   108  			require.NoError(t, err)
   109  
   110  			err = db.View(GetComputationResultUploadStatus(expectedId, &actualUploadStatus))
   111  			assert.NotNil(t, err)
   112  		})
   113  	})
   114  }
   115  
   116  func TestListComputationResults(t *testing.T) {
   117  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
   118  		expected := [...]*execution.ComputationResult{
   119  			testutil.ComputationResultFixture(t),
   120  			testutil.ComputationResultFixture(t),
   121  		}
   122  		t.Run("List all ComputationResult with status True", func(t *testing.T) {
   123  			expectedIDs := make(map[string]bool, 0)
   124  			// Store a list of ComputationResult instances first
   125  			for _, cr := range expected {
   126  				expectedId := cr.ExecutableBlock.ID()
   127  				expectedIDs[expectedId.String()] = true
   128  				err := db.Update(InsertComputationResultUploadStatus(expectedId, true))
   129  				require.NoError(t, err)
   130  			}
   131  
   132  			// Get the list of IDs of stored ComputationResult
   133  			crIDs := make([]flow.Identifier, 0)
   134  			err := db.View(GetBlockIDsByStatus(&crIDs, true))
   135  			require.NoError(t, err)
   136  			crIDsStrMap := make(map[string]bool, 0)
   137  			for _, crID := range crIDs {
   138  				crIDsStrMap[crID.String()] = true
   139  			}
   140  
   141  			assert.True(t, reflect.DeepEqual(crIDsStrMap, expectedIDs))
   142  		})
   143  	})
   144  }