github.com/Finschia/finschia-sdk@v0.49.1/x/collection/collection_test.go (about) 1 package collection_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 sdk "github.com/Finschia/finschia-sdk/types" 10 "github.com/Finschia/finschia-sdk/x/collection" 11 ) 12 13 const TestContractID = "deadbeef" 14 15 func TestFTClass(t *testing.T) { 16 nextIDs := collection.DefaultNextClassIDs(TestContractID) 17 testCases := map[string]struct { 18 id string 19 name string 20 meta string 21 decimals int32 22 valid bool 23 }{ 24 "valid class": { 25 valid: true, 26 }, 27 "invalid id": { 28 id: "invalid", 29 }, 30 "invalid name": { 31 name: string(make([]rune, 21)), 32 }, 33 "invalid meta": { 34 meta: string(make([]rune, 1001)), 35 }, 36 "invalid decimals": { 37 decimals: 19, 38 }, 39 } 40 41 for name, tc := range testCases { 42 t.Run(name, func(t *testing.T) { 43 class := &collection.FTClass{ 44 Id: tc.id, 45 Decimals: tc.decimals, 46 } 47 48 if len(tc.id) == 0 { 49 class.SetId(&nextIDs) 50 } 51 class.SetName(tc.name) 52 class.SetMeta(tc.meta) 53 54 err := class.ValidateBasic() 55 if !tc.valid { 56 require.Error(t, err) 57 return 58 } 59 require.NoError(t, err) 60 }) 61 } 62 } 63 64 func TestNFTClass(t *testing.T) { 65 nextIDs := collection.DefaultNextClassIDs(TestContractID) 66 testCases := map[string]struct { 67 name string 68 meta string 69 valid bool 70 }{ 71 "valid class": { 72 valid: true, 73 }, 74 "invalid name": { 75 name: string(make([]rune, 21)), 76 }, 77 "invalid meta": { 78 meta: string(make([]rune, 1001)), 79 }, 80 } 81 82 for name, tc := range testCases { 83 t.Run(name, func(t *testing.T) { 84 class := &collection.NFTClass{} 85 class.SetId(&nextIDs) 86 class.SetName(tc.name) 87 class.SetMeta(tc.meta) 88 89 err := class.ValidateBasic() 90 if !tc.valid { 91 require.Error(t, err) 92 return 93 } 94 require.NoError(t, err) 95 }) 96 } 97 } 98 99 func TestParseCoin(t *testing.T) { 100 testCases := map[string]struct { 101 input string 102 valid bool 103 expected collection.Coin 104 }{ 105 "valid coin": { 106 input: "00bab10c00000000:10", 107 valid: true, 108 expected: collection.NewFTCoin("00bab10c", sdk.NewInt(10)), 109 }, 110 "invalid expression": { 111 input: "oobabloc00000000:10", 112 }, 113 "invalid amount": { 114 input: "00bab10c00000000:" + fmt.Sprintf("1%0127d", 0), 115 }, 116 } 117 118 for name, tc := range testCases { 119 t.Run(name, func(t *testing.T) { 120 parsed, err := collection.ParseCoin(tc.input) 121 if !tc.valid { 122 require.Error(t, err) 123 return 124 } 125 require.NoError(t, err) 126 127 require.Equal(t, tc.expected, *parsed) 128 require.Equal(t, tc.input, parsed.String()) 129 }) 130 } 131 } 132 133 func TestParseCoins(t *testing.T) { 134 testCases := map[string]struct { 135 input string 136 valid bool 137 expected collection.Coins 138 }{ 139 "valid single coins": { 140 input: "00bab10c00000000:10", 141 valid: true, 142 expected: collection.NewCoins( 143 collection.NewFTCoin("00bab10c", sdk.NewInt(10)), 144 ), 145 }, 146 "valid multiple coins": { 147 input: fmt.Sprintf("%s00000001:1,00bab10c00000000:10", TestContractID), 148 valid: true, 149 expected: collection.NewCoins( 150 collection.NewNFTCoin(TestContractID, 1), 151 collection.NewFTCoin("00bab10c", sdk.NewInt(10)), 152 ), 153 }, 154 "empty string": {}, 155 "invalid coin": { 156 input: "oobabloc00000000:10", 157 }, 158 } 159 160 for name, tc := range testCases { 161 t.Run(name, func(t *testing.T) { 162 parsed, err := collection.ParseCoins(tc.input) 163 if !tc.valid { 164 require.Error(t, err) 165 return 166 } 167 require.NoError(t, err) 168 169 require.Equal(t, tc.expected, parsed) 170 require.Equal(t, tc.input, parsed.String()) 171 }) 172 } 173 } 174 175 func TestDefaultNextClassIDs(t *testing.T) { 176 contractID := TestContractID 177 require.Equal(t, collection.NextClassIDs{ 178 ContractId: contractID, 179 Fungible: sdk.NewUint(1), 180 NonFungible: sdk.NewUint(1 << 28).Incr(), // "10000000 + 1" 181 }, 182 collection.DefaultNextClassIDs(contractID), 183 ) 184 }