github.com/Finschia/finschia-sdk@v0.49.1/x/bank/types/metadata_test.go (about) 1 package types_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 8 "github.com/Finschia/finschia-sdk/codec" 9 "github.com/Finschia/finschia-sdk/x/bank/types" 10 ) 11 12 func TestMetadataValidate(t *testing.T) { 13 testCases := []struct { 14 name string 15 metadata types.Metadata 16 expErr bool 17 }{ 18 { 19 "non-empty coins", 20 types.Metadata{ 21 Name: "Cosmos Hub Atom", 22 Symbol: "ATOM", 23 Description: "The native staking token of the Cosmos Hub.", 24 DenomUnits: []*types.DenomUnit{ 25 {"uatom", uint32(0), []string{"microatom"}}, 26 {"matom", uint32(3), []string{"milliatom"}}, 27 {"atom", uint32(6), nil}, 28 }, 29 Base: "uatom", 30 Display: "atom", 31 }, 32 false, 33 }, 34 { 35 "base coin is display coin", 36 types.Metadata{ 37 Name: "Cosmos Hub Atom", 38 Symbol: "ATOM", 39 Description: "The native staking token of the Cosmos Hub.", 40 DenomUnits: []*types.DenomUnit{ 41 {"atom", uint32(0), []string{"ATOM"}}, 42 }, 43 Base: "atom", 44 Display: "atom", 45 }, 46 false, 47 }, 48 {"empty metadata", types.Metadata{}, true}, 49 { 50 "blank name", 51 types.Metadata{ 52 Name: "", 53 }, 54 true, 55 }, 56 { 57 "blank symbol", 58 types.Metadata{ 59 Name: "Cosmos Hub Atom", 60 Symbol: "", 61 }, 62 true, 63 }, 64 { 65 "invalid base denom", 66 types.Metadata{ 67 Name: "Cosmos Hub Atom", 68 Symbol: "ATOM", 69 Base: "", 70 }, 71 true, 72 }, 73 { 74 "invalid display denom", 75 types.Metadata{ 76 Name: "Cosmos Hub Atom", 77 Symbol: "ATOM", 78 Base: "uatom", 79 Display: "", 80 }, 81 true, 82 }, 83 { 84 "duplicate denom unit", 85 types.Metadata{ 86 Name: "Cosmos Hub Atom", 87 Symbol: "ATOM", 88 Description: "The native staking token of the Cosmos Hub.", 89 DenomUnits: []*types.DenomUnit{ 90 {"uatom", uint32(0), []string{"microatom"}}, 91 {"uatom", uint32(1), []string{"microatom"}}, 92 }, 93 Base: "uatom", 94 Display: "atom", 95 }, 96 true, 97 }, 98 { 99 "invalid denom unit", 100 types.Metadata{ 101 Name: "Cosmos Hub Atom", 102 Symbol: "ATOM", 103 Description: "The native staking token of the Cosmos Hub.", 104 DenomUnits: []*types.DenomUnit{ 105 {"", uint32(0), []string{"microatom"}}, 106 }, 107 Base: "uatom", 108 Display: "atom", 109 }, 110 true, 111 }, 112 { 113 "invalid denom unit alias", 114 types.Metadata{ 115 Name: "Cosmos Hub Atom", 116 Symbol: "ATOM", 117 Description: "The native staking token of the Cosmos Hub.", 118 DenomUnits: []*types.DenomUnit{ 119 {"uatom", uint32(0), []string{""}}, 120 }, 121 Base: "uatom", 122 Display: "atom", 123 }, 124 true, 125 }, 126 { 127 "duplicate denom unit alias", 128 types.Metadata{ 129 Name: "Cosmos Hub Atom", 130 Symbol: "ATOM", 131 Description: "The native staking token of the Cosmos Hub.", 132 DenomUnits: []*types.DenomUnit{ 133 {"uatom", uint32(0), []string{"microatom", "microatom"}}, 134 }, 135 Base: "uatom", 136 Display: "atom", 137 }, 138 true, 139 }, 140 { 141 "no base denom unit", 142 types.Metadata{ 143 Name: "Cosmos Hub Atom", 144 Symbol: "ATOM", 145 Description: "The native staking token of the Cosmos Hub.", 146 DenomUnits: []*types.DenomUnit{ 147 {"matom", uint32(3), []string{"milliatom"}}, 148 {"atom", uint32(6), nil}, 149 }, 150 Base: "uatom", 151 Display: "atom", 152 }, 153 true, 154 }, 155 { 156 "base denom exponent not zero", 157 types.Metadata{ 158 Name: "Cosmos Hub Atom", 159 Symbol: "ATOM", 160 Description: "The native staking token of the Cosmos Hub.", 161 DenomUnits: []*types.DenomUnit{ 162 {"uatom", uint32(1), []string{"microatom"}}, 163 {"matom", uint32(3), []string{"milliatom"}}, 164 {"atom", uint32(6), nil}, 165 }, 166 Base: "uatom", 167 Display: "atom", 168 }, 169 true, 170 }, 171 { 172 "invalid denom unit", 173 types.Metadata{ 174 Name: "Cosmos Hub Atom", 175 Symbol: "ATOM", 176 Description: "The native staking token of the Cosmos Hub.", 177 DenomUnits: []*types.DenomUnit{ 178 {"uatom", uint32(0), []string{"microatom"}}, 179 {"", uint32(3), []string{"milliatom"}}, 180 }, 181 Base: "uatom", 182 Display: "uatom", 183 }, 184 true, 185 }, 186 { 187 "no display denom unit", 188 types.Metadata{ 189 Name: "Cosmos Hub Atom", 190 Symbol: "ATOM", 191 Description: "The native staking token of the Cosmos Hub.", 192 DenomUnits: []*types.DenomUnit{ 193 {"uatom", uint32(0), []string{"microatom"}}, 194 }, 195 Base: "uatom", 196 Display: "atom", 197 }, 198 true, 199 }, 200 { 201 "denom units not sorted", 202 types.Metadata{ 203 Name: "Cosmos Hub Atom", 204 Symbol: "ATOM", 205 Description: "The native staking token of the Cosmos Hub.", 206 DenomUnits: []*types.DenomUnit{ 207 {"uatom", uint32(0), []string{"microatom"}}, 208 {"atom", uint32(6), nil}, 209 {"matom", uint32(3), []string{"milliatom"}}, 210 }, 211 Base: "uatom", 212 Display: "atom", 213 }, 214 true, 215 }, 216 } 217 218 for _, tc := range testCases { 219 t.Run(tc.name, func(t *testing.T) { 220 err := tc.metadata.Validate() 221 222 if tc.expErr { 223 require.Error(t, err) 224 } else { 225 require.NoError(t, err) 226 } 227 }) 228 } 229 } 230 231 func TestMarshalJSONMetaData(t *testing.T) { 232 cdc := codec.NewLegacyAmino() 233 234 testCases := []struct { 235 name string 236 input []types.Metadata 237 strOutput string 238 }{ 239 {"nil metadata", nil, `null`}, 240 {"empty metadata", []types.Metadata{}, `[]`}, 241 { 242 "non-empty coins", 243 []types.Metadata{ 244 { 245 Description: "The native staking token of the Cosmos Hub.", 246 DenomUnits: []*types.DenomUnit{ 247 {"uatom", uint32(0), []string{"microatom"}}, // The default exponent value 0 is omitted in the json 248 {"matom", uint32(3), []string{"milliatom"}}, 249 {"atom", uint32(6), nil}, 250 }, 251 Base: "uatom", 252 Display: "atom", 253 }, 254 }, 255 `[{"description":"The native staking token of the Cosmos Hub.","denom_units":[{"denom":"uatom","aliases":["microatom"]},{"denom":"matom","exponent":3,"aliases":["milliatom"]},{"denom":"atom","exponent":6}],"base":"uatom","display":"atom"}]`, 256 }, 257 } 258 259 for _, tc := range testCases { 260 t.Run(tc.name, func(t *testing.T) { 261 bz, err := cdc.MarshalJSON(tc.input) 262 require.NoError(t, err) 263 require.Equal(t, tc.strOutput, string(bz)) 264 265 var newMetadata []types.Metadata 266 require.NoError(t, cdc.UnmarshalJSON(bz, &newMetadata)) 267 268 if len(tc.input) == 0 { 269 require.Nil(t, newMetadata) 270 } else { 271 require.Equal(t, tc.input, newMetadata) 272 } 273 }) 274 } 275 }