github.com/Finschia/finschia-sdk@v0.48.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 tc := tc 220 t.Run(tc.name, func(t *testing.T) { 221 err := tc.metadata.Validate() 222 223 if tc.expErr { 224 require.Error(t, err) 225 } else { 226 require.NoError(t, err) 227 } 228 }) 229 } 230 } 231 232 func TestMarshalJSONMetaData(t *testing.T) { 233 cdc := codec.NewLegacyAmino() 234 235 testCases := []struct { 236 name string 237 input []types.Metadata 238 strOutput string 239 }{ 240 {"nil metadata", nil, `null`}, 241 {"empty metadata", []types.Metadata{}, `[]`}, 242 { 243 "non-empty coins", 244 []types.Metadata{ 245 { 246 Description: "The native staking token of the Cosmos Hub.", 247 DenomUnits: []*types.DenomUnit{ 248 {"uatom", uint32(0), []string{"microatom"}}, // The default exponent value 0 is omitted in the json 249 {"matom", uint32(3), []string{"milliatom"}}, 250 {"atom", uint32(6), nil}, 251 }, 252 Base: "uatom", 253 Display: "atom", 254 }, 255 }, 256 `[{"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"}]`, 257 }, 258 } 259 260 for _, tc := range testCases { 261 tc := tc 262 t.Run(tc.name, func(t *testing.T) { 263 bz, err := cdc.MarshalJSON(tc.input) 264 require.NoError(t, err) 265 require.Equal(t, tc.strOutput, string(bz)) 266 267 var newMetadata []types.Metadata 268 require.NoError(t, cdc.UnmarshalJSON(bz, &newMetadata)) 269 270 if len(tc.input) == 0 { 271 require.Nil(t, newMetadata) 272 } else { 273 require.Equal(t, tc.input, newMetadata) 274 } 275 }) 276 } 277 }