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