github.com/Finschia/finschia-sdk@v0.48.1/x/collection/keeper/nft_test.go (about)

     1  package keeper_test
     2  
     3  import (
     4  	"github.com/Finschia/finschia-sdk/x/collection"
     5  )
     6  
     7  func (s *KeeperTestSuite) TestAttach() {
     8  	testCases := map[string]struct {
     9  		contractID string
    10  		subject    string
    11  		target     string
    12  		err        error
    13  	}{
    14  		"valid request": {
    15  			contractID: s.contractID,
    16  			subject:    collection.NewNFTID(s.nftClassID, s.depthLimit+1),
    17  			target:     collection.NewNFTID(s.nftClassID, s.depthLimit),
    18  		},
    19  		"not owner of subject": {
    20  			contractID: s.contractID,
    21  			subject:    collection.NewNFTID(s.nftClassID, s.numNFTs+1),
    22  			target:     collection.NewNFTID(s.nftClassID, 1),
    23  			err:        collection.ErrTokenNotOwnedBy,
    24  		},
    25  		"target not found": {
    26  			contractID: s.contractID,
    27  			subject:    collection.NewNFTID(s.nftClassID, s.depthLimit+1),
    28  			target:     collection.NewNFTID(s.nftClassID, s.numNFTs*3+1),
    29  			err:        collection.ErrTokenNotExist,
    30  		},
    31  		"result exceeds the limit": {
    32  			contractID: s.contractID,
    33  			subject:    collection.NewNFTID(s.nftClassID, s.depthLimit+2),
    34  			target:     collection.NewNFTID(s.nftClassID, s.depthLimit),
    35  			err:        collection.ErrCompositionTooDeep,
    36  		},
    37  		"not owner of target": {
    38  			contractID: s.contractID,
    39  			subject:    collection.NewNFTID(s.nftClassID, s.depthLimit+1),
    40  			target:     collection.NewNFTID(s.nftClassID, s.numNFTs+1),
    41  			err:        collection.ErrTokenNotOwnedBy,
    42  		},
    43  	}
    44  
    45  	for name, tc := range testCases {
    46  		s.Run(name, func() {
    47  			ctx, _ := s.ctx.CacheContext()
    48  
    49  			err := s.keeper.Attach(ctx, tc.contractID, s.customer, tc.subject, tc.target)
    50  			s.Require().ErrorIs(err, tc.err)
    51  			if tc.err != nil {
    52  				return
    53  			}
    54  
    55  			parent, err := s.keeper.GetParent(ctx, tc.contractID, tc.subject)
    56  			s.Require().NoError(err)
    57  			s.Require().Equal(*parent, tc.target)
    58  		})
    59  	}
    60  }
    61  
    62  func (s *KeeperTestSuite) TestDetach() {
    63  	testCases := map[string]struct {
    64  		contractID string
    65  		subject    string
    66  		err        error
    67  	}{
    68  		"valid request": {
    69  			contractID: s.contractID,
    70  			subject:    collection.NewNFTID(s.nftClassID, 2),
    71  		},
    72  		"subject not found": {
    73  			contractID: s.contractID,
    74  			subject:    collection.NewNFTID(s.nftClassID, s.numNFTs*3+1),
    75  			err:        collection.ErrTokenNotExist,
    76  		},
    77  		"subject has no parent": {
    78  			contractID: s.contractID,
    79  			subject:    collection.NewNFTID(s.nftClassID, 1),
    80  			err:        collection.ErrTokenNotAChild,
    81  		},
    82  		"not owner of subject": {
    83  			contractID: s.contractID,
    84  			subject:    collection.NewNFTID(s.nftClassID, s.numNFTs+2),
    85  			err:        collection.ErrTokenNotOwnedBy,
    86  		},
    87  	}
    88  
    89  	for name, tc := range testCases {
    90  		s.Run(name, func() {
    91  			ctx, _ := s.ctx.CacheContext()
    92  
    93  			err := s.keeper.Detach(ctx, tc.contractID, s.customer, tc.subject)
    94  			s.Require().ErrorIs(err, tc.err)
    95  			if tc.err != nil {
    96  				return
    97  			}
    98  
    99  			parent, err := s.keeper.GetParent(ctx, tc.contractID, tc.subject)
   100  			s.Require().Error(err)
   101  			s.Require().Nil(parent)
   102  		})
   103  	}
   104  }
   105  
   106  func (s *KeeperTestSuite) TestGetNFT() {
   107  	testCases := map[string]struct {
   108  		tokenID string
   109  		err     error
   110  	}{
   111  		"valid request": {
   112  			tokenID: collection.NewNFTID(s.nftClassID, 1),
   113  		},
   114  		"not found (not existing nft id)": {
   115  			tokenID: collection.NewNFTID("deadbeef", 1),
   116  			err:     collection.ErrTokenNotExist,
   117  		},
   118  		"not found (not existing ft id)": {
   119  			tokenID: collection.NewFTID("00bab10c"),
   120  			err:     collection.ErrTokenNotExist,
   121  		},
   122  		"not found (existing nft class id)": {
   123  			tokenID: collection.NewNFTID(s.nftClassID, 0),
   124  			err:     collection.ErrTokenNotExist,
   125  		},
   126  		"not found (existing ft class id)": {
   127  			tokenID: collection.NewNFTID(s.ftClassID, 0),
   128  			err:     collection.ErrTokenNotNFT,
   129  		},
   130  	}
   131  
   132  	for name, tc := range testCases {
   133  		s.Run(name, func() {
   134  			token, err := s.keeper.GetNFT(s.ctx, s.contractID, tc.tokenID)
   135  			s.Require().ErrorIs(err, tc.err)
   136  			if tc.err != nil {
   137  				return
   138  			}
   139  			s.Require().NotNil(token)
   140  		})
   141  	}
   142  }