github.com/Finschia/finschia-sdk@v0.48.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  func TestFTClass(t *testing.T) {
    14  	nextIDs := collection.DefaultNextClassIDs("deadbeef")
    15  	testCases := map[string]struct {
    16  		id       string
    17  		name     string
    18  		meta     string
    19  		decimals int32
    20  		valid    bool
    21  	}{
    22  		"valid class": {
    23  			valid: true,
    24  		},
    25  		"invalid id": {
    26  			id: "invalid",
    27  		},
    28  		"invalid name": {
    29  			name: string(make([]rune, 21)),
    30  		},
    31  		"invalid meta": {
    32  			meta: string(make([]rune, 1001)),
    33  		},
    34  		"invalid decimals": {
    35  			decimals: 19,
    36  		},
    37  	}
    38  
    39  	for name, tc := range testCases {
    40  		t.Run(name, func(t *testing.T) {
    41  			var class collection.TokenClass
    42  			class = &collection.FTClass{
    43  				Id:       tc.id,
    44  				Decimals: tc.decimals,
    45  			}
    46  
    47  			if len(tc.id) == 0 {
    48  				class.SetId(&nextIDs)
    49  			}
    50  			class.SetName(tc.name)
    51  			class.SetMeta(tc.meta)
    52  
    53  			err := class.ValidateBasic()
    54  			if !tc.valid {
    55  				require.Error(t, err)
    56  				return
    57  			}
    58  			require.NoError(t, err)
    59  		})
    60  	}
    61  }
    62  
    63  func TestNFTClass(t *testing.T) {
    64  	nextIDs := collection.DefaultNextClassIDs("deadbeef")
    65  	testCases := map[string]struct {
    66  		name  string
    67  		meta  string
    68  		valid bool
    69  	}{
    70  		"valid class": {
    71  			valid: true,
    72  		},
    73  		"invalid name": {
    74  			name: string(make([]rune, 21)),
    75  		},
    76  		"invalid meta": {
    77  			meta: string(make([]rune, 1001)),
    78  		},
    79  	}
    80  
    81  	for name, tc := range testCases {
    82  		t.Run(name, func(t *testing.T) {
    83  			var class collection.TokenClass
    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: "deadbeef00000001:1,00bab10c00000000:10",
   148  			valid: true,
   149  			expected: collection.NewCoins(
   150  				collection.NewNFTCoin("deadbeef", 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 := "deadbeef"
   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  }