github.com/Finschia/finschia-sdk@v0.49.1/x/collection/genesis_test.go (about)

     1  package collection_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	codectypes "github.com/Finschia/finschia-sdk/codec/types"
     9  	"github.com/Finschia/finschia-sdk/crypto/keys/secp256k1"
    10  	sdk "github.com/Finschia/finschia-sdk/types"
    11  	"github.com/Finschia/finschia-sdk/x/collection"
    12  )
    13  
    14  func TestValidateGenesis(t *testing.T) {
    15  	addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
    16  	testCases := map[string]struct {
    17  		gs    *collection.GenesisState
    18  		valid bool
    19  	}{
    20  		"default genesis": {
    21  			collection.DefaultGenesisState(),
    22  			true,
    23  		},
    24  		"contract of invalid contract id": {
    25  			&collection.GenesisState{
    26  				Contracts: []collection.Contract{{
    27  					Name: "tibetian fox",
    28  					Meta: "Tibetian Fox",
    29  					Uri:  "file:///tibetian_fox.png",
    30  				}},
    31  			},
    32  			false,
    33  		},
    34  		"contract of invalid name": {
    35  			&collection.GenesisState{
    36  				Contracts: []collection.Contract{{
    37  					Id:   "deadbeef",
    38  					Name: string(make([]rune, 21)),
    39  					Meta: "Tibetian Fox",
    40  					Uri:  "file:///tibetian_fox.png",
    41  				}},
    42  			},
    43  			false,
    44  		},
    45  		"contract of invalid base img uri": {
    46  			&collection.GenesisState{
    47  				Contracts: []collection.Contract{{
    48  					Id:   "deadbeef",
    49  					Name: "tibetian fox",
    50  					Uri:  string(make([]rune, 1001)),
    51  					Meta: "Tibetian Fox",
    52  				}},
    53  			},
    54  			false,
    55  		},
    56  		"contract of invalid meta": {
    57  			&collection.GenesisState{
    58  				Contracts: []collection.Contract{{
    59  					Id:   "deadbeef",
    60  					Name: "tibetian fox",
    61  					Uri:  "file:///tibetian_fox.png",
    62  					Meta: string(make([]rune, 1001)),
    63  				}},
    64  			},
    65  			false,
    66  		},
    67  		"next class ids of invalid contract id": {
    68  			&collection.GenesisState{
    69  				NextClassIds: []collection.NextClassIDs{{
    70  					Fungible:    sdk.ZeroUint(),
    71  					NonFungible: sdk.OneUint(),
    72  				}},
    73  			},
    74  			false,
    75  		},
    76  		"contract classes of invalid contract id": {
    77  			&collection.GenesisState{
    78  				Classes: []collection.ContractClasses{{
    79  					Classes: []codectypes.Any{
    80  						*collection.TokenClassToAny(&collection.NFTClass{
    81  							Id:   "deadbeef",
    82  							Name: "tibetian fox",
    83  							Meta: "Tibetian Fox",
    84  						}),
    85  					},
    86  				}},
    87  			},
    88  			false,
    89  		},
    90  		"contract classes of empty classes": {
    91  			&collection.GenesisState{
    92  				Classes: []collection.ContractClasses{{
    93  					ContractId: "deadbeef",
    94  				}},
    95  			},
    96  			false,
    97  		},
    98  		"contract classes of invalid class": {
    99  			&collection.GenesisState{
   100  				Classes: []collection.ContractClasses{{
   101  					ContractId: "deadbeef",
   102  					Classes: []codectypes.Any{
   103  						*collection.TokenClassToAny(&collection.NFTClass{
   104  							Name: "tibetian fox",
   105  							Meta: "Tibetian Fox",
   106  						}),
   107  					},
   108  				}},
   109  			},
   110  			false,
   111  		},
   112  		"contract next token ids of invalid contract id": {
   113  			&collection.GenesisState{
   114  				NextTokenIds: []collection.ContractNextTokenIDs{{
   115  					TokenIds: []collection.NextTokenID{{
   116  						ClassId: "deadbeef",
   117  						Id:      sdk.ZeroUint(),
   118  					}},
   119  				}},
   120  			},
   121  			false,
   122  		},
   123  		"contract next token ids of empty classes": {
   124  			&collection.GenesisState{
   125  				NextTokenIds: []collection.ContractNextTokenIDs{{
   126  					ContractId: "deadbeef",
   127  				}},
   128  			},
   129  			false,
   130  		},
   131  		"contract next token ids of invalid class": {
   132  			&collection.GenesisState{
   133  				NextTokenIds: []collection.ContractNextTokenIDs{{
   134  					ContractId: "deadbeef",
   135  					TokenIds: []collection.NextTokenID{{
   136  						Id: sdk.ZeroUint(),
   137  					}},
   138  				}},
   139  			},
   140  			false,
   141  		},
   142  		"contract balances of invalid contract id": {
   143  			&collection.GenesisState{
   144  				Balances: []collection.ContractBalances{{
   145  					Balances: []collection.Balance{{
   146  						Address: addr.String(),
   147  						Amount:  collection.NewCoins(collection.NewFTCoin("00bab10c", sdk.OneInt())),
   148  					}},
   149  				}},
   150  			},
   151  			false,
   152  		},
   153  		"contract balances of empty balances": {
   154  			&collection.GenesisState{
   155  				Balances: []collection.ContractBalances{{
   156  					ContractId: "deadbeef",
   157  				}},
   158  			},
   159  			false,
   160  		},
   161  		"contract balances of invalid address": {
   162  			&collection.GenesisState{
   163  				Balances: []collection.ContractBalances{{
   164  					ContractId: "deadbeef",
   165  					Balances: []collection.Balance{{
   166  						Amount: collection.NewCoins(collection.NewFTCoin("00bab10c", sdk.OneInt())),
   167  					}},
   168  				}},
   169  			},
   170  			false,
   171  		},
   172  		"contract balances of invalid amount": {
   173  			&collection.GenesisState{
   174  				Balances: []collection.ContractBalances{{
   175  					ContractId: "deadbeef",
   176  					Balances: []collection.Balance{{
   177  						Address: addr.String(),
   178  					}},
   179  				}},
   180  			},
   181  			false,
   182  		},
   183  		"contract nfts of invalid contract id": {
   184  			&collection.GenesisState{
   185  				Nfts: []collection.ContractNFTs{{
   186  					Nfts: []collection.NFT{{
   187  						TokenId: collection.NewNFTID("deadbeef", 1),
   188  						Name:    "tibetian fox",
   189  						Meta:    "Tibetian Fox",
   190  					}},
   191  				}},
   192  			},
   193  			false,
   194  		},
   195  		"contract nfts of empty nfts": {
   196  			&collection.GenesisState{
   197  				Nfts: []collection.ContractNFTs{{
   198  					ContractId: "deadbeef",
   199  				}},
   200  			},
   201  			false,
   202  		},
   203  		"contract nfts of invalid class": {
   204  			&collection.GenesisState{
   205  				Nfts: []collection.ContractNFTs{{
   206  					ContractId: "deadbeef",
   207  					Nfts: []collection.NFT{{
   208  						Name: "tibetian fox",
   209  						Meta: "Tibetian Fox",
   210  					}},
   211  				}},
   212  			},
   213  			false,
   214  		},
   215  		"contract nfts of invalid name": {
   216  			&collection.GenesisState{
   217  				Nfts: []collection.ContractNFTs{{
   218  					ContractId: "deadbeef",
   219  					Nfts: []collection.NFT{{
   220  						TokenId: collection.NewNFTID("deadbeef", 1),
   221  						Name:    string(make([]rune, 21)),
   222  						Meta:    "Tibetian Fox",
   223  					}},
   224  				}},
   225  			},
   226  			false,
   227  		},
   228  		"contract nfts of invalid meta": {
   229  			&collection.GenesisState{
   230  				Nfts: []collection.ContractNFTs{{
   231  					ContractId: "deadbeef",
   232  					Nfts: []collection.NFT{{
   233  						TokenId: collection.NewNFTID("deadbeef", 1),
   234  						Name:    "tibetian fox",
   235  						Meta:    string(make([]rune, 1001)),
   236  					}},
   237  				}},
   238  			},
   239  			false,
   240  		},
   241  		"contract parents of invalid contract id": {
   242  			&collection.GenesisState{
   243  				Parents: []collection.ContractTokenRelations{{
   244  					Relations: []collection.TokenRelation{{
   245  						Self:  collection.NewNFTID("deadbeef", 1),
   246  						Other: collection.NewNFTID("fee1dead", 1),
   247  					}},
   248  				}},
   249  			},
   250  			false,
   251  		},
   252  		"contract parents of empty relations": {
   253  			&collection.GenesisState{
   254  				Parents: []collection.ContractTokenRelations{{
   255  					ContractId: "deadbeef",
   256  				}},
   257  			},
   258  			false,
   259  		},
   260  		"contract parents of invalid token": {
   261  			&collection.GenesisState{
   262  				Parents: []collection.ContractTokenRelations{{
   263  					ContractId: "deadbeef",
   264  					Relations: []collection.TokenRelation{{
   265  						Other: collection.NewNFTID("fee1dead", 1),
   266  					}},
   267  				}},
   268  			},
   269  			false,
   270  		},
   271  		"contract parents of invalid parent": {
   272  			&collection.GenesisState{
   273  				Parents: []collection.ContractTokenRelations{{
   274  					ContractId: "deadbeef",
   275  					Relations: []collection.TokenRelation{{
   276  						Self: collection.NewNFTID("deadbeef", 1),
   277  					}},
   278  				}},
   279  			},
   280  			false,
   281  		},
   282  		"contract authorizations of invalid contract id": {
   283  			&collection.GenesisState{
   284  				Authorizations: []collection.ContractAuthorizations{{
   285  					Authorizations: []collection.Authorization{{
   286  						Holder:   addr.String(),
   287  						Operator: addr.String(),
   288  					}},
   289  				}},
   290  			},
   291  			false,
   292  		},
   293  		"contract authorizations of empty authorizations": {
   294  			&collection.GenesisState{
   295  				Authorizations: []collection.ContractAuthorizations{{
   296  					ContractId: "deadbeef",
   297  				}},
   298  			},
   299  			false,
   300  		},
   301  		"contract authorizations of invalid holder": {
   302  			&collection.GenesisState{
   303  				Authorizations: []collection.ContractAuthorizations{{
   304  					ContractId: "deadbeef",
   305  					Authorizations: []collection.Authorization{{
   306  						Operator: addr.String(),
   307  					}},
   308  				}},
   309  			},
   310  			false,
   311  		},
   312  		"contract authorizations of invalid operator": {
   313  			&collection.GenesisState{
   314  				Authorizations: []collection.ContractAuthorizations{{
   315  					ContractId: "deadbeef",
   316  					Authorizations: []collection.Authorization{{
   317  						Holder: addr.String(),
   318  					}},
   319  				}},
   320  			},
   321  			false,
   322  		},
   323  		"contract grants of invalid contract id": {
   324  			&collection.GenesisState{
   325  				Grants: []collection.ContractGrants{{
   326  					Grants: []collection.Grant{{
   327  						Grantee:    addr.String(),
   328  						Permission: collection.PermissionMint,
   329  					}},
   330  				}},
   331  			},
   332  			false,
   333  		},
   334  		"contract grants of empty grants": {
   335  			&collection.GenesisState{
   336  				Grants: []collection.ContractGrants{{
   337  					ContractId: "deadbeef",
   338  				}},
   339  			},
   340  			false,
   341  		},
   342  		"contract grants of invalid grantee": {
   343  			&collection.GenesisState{
   344  				Grants: []collection.ContractGrants{{
   345  					ContractId: "deadbeef",
   346  					Grants: []collection.Grant{{
   347  						Permission: collection.PermissionMint,
   348  					}},
   349  				}},
   350  			},
   351  			false,
   352  		},
   353  		"contract grants of invalid permission": {
   354  			&collection.GenesisState{
   355  				Grants: []collection.ContractGrants{{
   356  					ContractId: "deadbeef",
   357  					Grants: []collection.Grant{{
   358  						Grantee: addr.String(),
   359  					}},
   360  				}},
   361  			},
   362  			false,
   363  		},
   364  		"contract supplies of invalid contract id": {
   365  			&collection.GenesisState{
   366  				Supplies: []collection.ContractStatistics{{
   367  					Statistics: []collection.ClassStatistics{{
   368  						ClassId: "deadbeef",
   369  						Amount:  sdk.OneInt(),
   370  					}},
   371  				}},
   372  			},
   373  			false,
   374  		},
   375  		"contract supplies of empty supplies": {
   376  			&collection.GenesisState{
   377  				Supplies: []collection.ContractStatistics{{
   378  					ContractId: "deadbeef",
   379  				}},
   380  			},
   381  			false,
   382  		},
   383  		"contract supplies of invalid class id": {
   384  			&collection.GenesisState{
   385  				Supplies: []collection.ContractStatistics{{
   386  					ContractId: "deadbeef",
   387  					Statistics: []collection.ClassStatistics{{
   388  						Amount: sdk.OneInt(),
   389  					}},
   390  				}},
   391  			},
   392  			false,
   393  		},
   394  		"contract supplies of invalid operator": {
   395  			&collection.GenesisState{
   396  				Supplies: []collection.ContractStatistics{{
   397  					ContractId: "deadbeef",
   398  					Statistics: []collection.ClassStatistics{{
   399  						ClassId: "deadbeef",
   400  						Amount:  sdk.ZeroInt(),
   401  					}},
   402  				}},
   403  			},
   404  			false,
   405  		},
   406  		"contract burnts of invalid contract id": {
   407  			&collection.GenesisState{
   408  				Burnts: []collection.ContractStatistics{{
   409  					Statistics: []collection.ClassStatistics{{
   410  						ClassId: "deadbeef",
   411  						Amount:  sdk.OneInt(),
   412  					}},
   413  				}},
   414  			},
   415  			false,
   416  		},
   417  		"contract burnts of empty burnts": {
   418  			&collection.GenesisState{
   419  				Burnts: []collection.ContractStatistics{{
   420  					ContractId: "deadbeef",
   421  				}},
   422  			},
   423  			false,
   424  		},
   425  		"contract burnts of invalid class id": {
   426  			&collection.GenesisState{
   427  				Burnts: []collection.ContractStatistics{{
   428  					ContractId: "deadbeef",
   429  					Statistics: []collection.ClassStatistics{{
   430  						Amount: sdk.OneInt(),
   431  					}},
   432  				}},
   433  			},
   434  			false,
   435  		},
   436  		"contract burnts of invalid operator": {
   437  			&collection.GenesisState{
   438  				Burnts: []collection.ContractStatistics{{
   439  					ContractId: "deadbeef",
   440  					Statistics: []collection.ClassStatistics{{
   441  						ClassId: "deadbeef",
   442  						Amount:  sdk.ZeroInt(),
   443  					}},
   444  				}},
   445  			},
   446  			false,
   447  		},
   448  		"should throw error when next token id is zero in genesis": {
   449  			&collection.GenesisState{
   450  				Params: collection.Params{},
   451  				NextTokenIds: []collection.ContractNextTokenIDs{
   452  					{ContractId: "deadbeef", TokenIds: []collection.NextTokenID{
   453  						{ClassId: "deadbeef", Id: sdk.NewUint(0)},
   454  					}},
   455  				},
   456  			},
   457  			false,
   458  		},
   459  	}
   460  
   461  	for name, tc := range testCases {
   462  		t.Run(name, func(t *testing.T) {
   463  			err := collection.ValidateGenesis(*tc.gs)
   464  			if !tc.valid {
   465  				require.Error(t, err)
   466  				return
   467  			}
   468  			require.NoError(t, err)
   469  		})
   470  	}
   471  }