github.com/Finschia/finschia-sdk@v0.48.1/x/upgrade/types/plan_test.go (about) 1 package types_test 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 tmproto "github.com/tendermint/tendermint/proto/tendermint/types" 10 11 "github.com/Finschia/ostracon/libs/log" 12 13 codectypes "github.com/Finschia/finschia-sdk/codec/types" 14 sdk "github.com/Finschia/finschia-sdk/types" 15 "github.com/Finschia/finschia-sdk/x/upgrade/types" 16 ) 17 18 func mustParseTime(s string) time.Time { 19 t, err := time.Parse(time.RFC3339, s) 20 if err != nil { 21 panic(err) 22 } 23 return t 24 } 25 26 func TestPlanString(t *testing.T) { 27 cases := map[string]struct { 28 p types.Plan 29 expect string 30 }{ 31 "with height": { 32 p: types.Plan{ 33 Name: "by height", 34 Info: "https://foo.bar/baz", 35 Height: 7890, 36 }, 37 expect: "Upgrade Plan\n Name: by height\n Height: 7890\n Info: https://foo.bar/baz.", 38 }, 39 "neither": { 40 p: types.Plan{ 41 Name: "almost-empty", 42 }, 43 expect: "Upgrade Plan\n Name: almost-empty\n Height: 0\n Info: .", 44 }, 45 } 46 47 for name, tc := range cases { 48 tc := tc // copy to local variable for scopelint 49 t.Run(name, func(t *testing.T) { 50 s := tc.p.String() 51 require.Equal(t, tc.expect, s) 52 }) 53 } 54 } 55 56 func TestPlanValid(t *testing.T) { 57 cases := map[string]struct { 58 p types.Plan 59 valid bool 60 }{ 61 "proper by height": { 62 p: types.Plan{ 63 Name: "all-good", 64 Height: 123450000, 65 }, 66 valid: true, 67 }, 68 "no name": { 69 p: types.Plan{ 70 Height: 123450000, 71 }, 72 }, 73 "IBC upgrade": { 74 p: types.Plan{ 75 Height: 123450000, 76 UpgradedClientState: &codectypes.Any{}, 77 }, 78 }, 79 "no due at": { 80 p: types.Plan{ 81 Name: "missing", 82 Info: "important", 83 }, 84 }, 85 "negative height": { 86 p: types.Plan{ 87 Name: "minus", 88 Height: -12345, 89 }, 90 }, 91 } 92 93 for name, tc := range cases { 94 tc := tc // copy to local variable for scopelint 95 t.Run(name, func(t *testing.T) { 96 err := tc.p.ValidateBasic() 97 if tc.valid { 98 assert.NoError(t, err) 99 } else { 100 assert.Error(t, err) 101 } 102 }) 103 } 104 } 105 106 func TestShouldExecute(t *testing.T) { 107 cases := map[string]struct { 108 p types.Plan 109 ctxTime time.Time 110 ctxHeight int64 111 expected bool 112 }{ 113 "past height": { 114 p: types.Plan{ 115 Name: "do-good", 116 Height: 1234, 117 }, 118 ctxTime: mustParseTime("2019-07-08T11:32:00Z"), 119 ctxHeight: 1000, 120 expected: false, 121 }, 122 "on height": { 123 p: types.Plan{ 124 Name: "do-good", 125 Height: 1234, 126 }, 127 ctxTime: mustParseTime("2019-07-08T11:32:00Z"), 128 ctxHeight: 1234, 129 expected: true, 130 }, 131 "future height": { 132 p: types.Plan{ 133 Name: "do-good", 134 Height: 1234, 135 }, 136 ctxTime: mustParseTime("2019-07-08T11:32:00Z"), 137 ctxHeight: 1235, 138 expected: true, 139 }, 140 } 141 142 for name, tc := range cases { 143 tc := tc // copy to local variable for scopelint 144 t.Run(name, func(t *testing.T) { 145 ctx := sdk.NewContext(nil, tmproto.Header{Height: tc.ctxHeight, Time: tc.ctxTime}, false, log.NewNopLogger()) 146 should := tc.p.ShouldExecute(ctx) 147 assert.Equal(t, tc.expected, should) 148 }) 149 } 150 }