github.com/koko1123/flow-go-1@v0.29.6/engine/execution/state/delta/delta_test.go (about) 1 package delta_test 2 3 import ( 4 "sort" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 9 "github.com/koko1123/flow-go-1/engine/execution/state/delta" 10 "github.com/koko1123/flow-go-1/model/flow" 11 ) 12 13 func TestDelta_Get(t *testing.T) { 14 registerID1 := "fruit" 15 16 t.Run("ValueNotSet", func(t *testing.T) { 17 d := delta.NewDelta() 18 19 b, exists := d.Get(registerID1, "") 20 assert.Nil(t, b) 21 assert.False(t, exists) 22 }) 23 24 t.Run("ValueSet", func(t *testing.T) { 25 d := delta.NewDelta() 26 27 d.Set(registerID1, "", []byte("apple")) 28 29 b, exists := d.Get(registerID1, "") 30 assert.Equal(t, flow.RegisterValue("apple"), b) 31 assert.True(t, exists) 32 }) 33 } 34 35 func TestDelta_Set(t *testing.T) { 36 registerID1 := "fruit" 37 38 d := delta.NewDelta() 39 40 d.Set(registerID1, "", []byte("apple")) 41 42 b1, exists := d.Get(registerID1, "") 43 assert.Equal(t, []byte("apple"), b1) 44 assert.True(t, exists) 45 46 d.Set(registerID1, "", []byte("orange")) 47 48 b2, exists := d.Get(registerID1, "") 49 assert.Equal(t, []byte("orange"), b2) 50 assert.True(t, exists) 51 } 52 53 func TestDelta_MergeWith(t *testing.T) { 54 registerID1 := "fruit" 55 56 registerID2 := "vegetable" 57 58 t.Run("NoCollisions", func(t *testing.T) { 59 d1 := delta.NewDelta() 60 d2 := delta.NewDelta() 61 62 d1.Set(registerID1, "", []byte("apple")) 63 d2.Set(registerID2, "", []byte("carrot")) 64 65 d1.MergeWith(d2) 66 67 b1, _ := d1.Get(registerID1, "") 68 assert.Equal(t, flow.RegisterValue("apple"), b1) 69 70 b2, _ := d1.Get(registerID2, "") 71 assert.Equal(t, flow.RegisterValue("carrot"), b2) 72 }) 73 74 t.Run("OverwriteSetValue", func(t *testing.T) { 75 d1 := delta.NewDelta() 76 d2 := delta.NewDelta() 77 78 d1.Set(registerID1, "", flow.RegisterValue("apple")) 79 d2.Set(registerID1, "", flow.RegisterValue("orange")) 80 81 d1.MergeWith(d2) 82 83 b, _ := d1.Get(registerID1, "") 84 assert.Equal(t, flow.RegisterValue("orange"), b) 85 }) 86 87 t.Run("OverwriteDeletedValue", func(t *testing.T) { 88 d1 := delta.NewDelta() 89 d2 := delta.NewDelta() 90 91 d1.Set(registerID1, "", flow.RegisterValue("apple")) 92 d1.Set(registerID1, "", nil) 93 94 d2.Set(registerID1, "", flow.RegisterValue("orange")) 95 96 d1.MergeWith(d2) 97 98 b, _ := d1.Get(registerID1, "") 99 assert.Equal(t, flow.RegisterValue("orange"), b) 100 }) 101 102 t.Run("DeleteSetValue", func(t *testing.T) { 103 d1 := delta.NewDelta() 104 d2 := delta.NewDelta() 105 106 d1.Set(registerID1, "", flow.RegisterValue("apple")) 107 108 d2.Set(registerID1, "", nil) 109 110 d1.MergeWith(d2) 111 112 b, exists := d1.Get(registerID1, "") 113 assert.Nil(t, b) 114 assert.True(t, exists) 115 }) 116 } 117 118 func TestDelta_RegisterUpdatesAreSorted(t *testing.T) { 119 120 d := delta.NewDelta() 121 122 data := make(flow.RegisterEntries, 5) 123 124 data[0].Key = flow.NewRegisterID("a", "1") 125 data[1].Key = flow.NewRegisterID("b", "1") 126 data[2].Key = flow.NewRegisterID("c", "1") 127 data[3].Key = flow.NewRegisterID("d", "1") 128 data[4].Key = flow.NewRegisterID("d", "2") 129 130 data[0].Value = flow.RegisterValue("a") 131 data[1].Value = flow.RegisterValue("b") 132 data[2].Value = flow.RegisterValue("c") 133 data[3].Value = flow.RegisterValue("d") 134 data[4].Value = flow.RegisterValue("e") 135 136 sort.Sort(data) 137 138 // set in random order 139 d.Set(data[2].Key.Owner, data[2].Key.Key, data[2].Value) 140 d.Set(data[1].Key.Owner, data[1].Key.Key, data[1].Value) 141 d.Set(data[3].Key.Owner, data[3].Key.Key, data[3].Value) 142 d.Set(data[0].Key.Owner, data[0].Key.Key, data[0].Value) 143 d.Set(data[4].Key.Owner, data[4].Key.Key, data[4].Value) 144 145 retKeys, retValues := d.RegisterUpdates() 146 147 assert.Equal(t, data.IDs(), retKeys) 148 assert.Equal(t, data.Values(), retValues) 149 }