grc721_metadata_test.gno

3.02 Kb ยท 106 lines
  1package grc721
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/demo/testutils"
  7	"gno.land/p/demo/uassert"
  8)
  9
 10func TestSetMetadata(t *testing.T) {
 11	// Create a new dummy NFT with metadata
 12	dummy := NewNFTWithMetadata(dummyNFTName, dummyNFTSymbol)
 13	if dummy == nil {
 14		t.Errorf("should not be nil")
 15	}
 16
 17	// Define addresses for testing purposes
 18	addr1 := testutils.TestAddress("alice")
 19	addr2 := testutils.TestAddress("bob")
 20
 21	// Define metadata attributes
 22	name := "test"
 23	description := "test"
 24	image := "test"
 25	imageData := "test"
 26	externalURL := "test"
 27	attributes := []Trait{}
 28	backgroundColor := "test"
 29	animationURL := "test"
 30	youtubeURL := "test"
 31
 32	// Set the original caller to addr1
 33	testing.SetOriginCaller(addr1) // addr1
 34
 35	// Mint a new token for addr1
 36	dummy.mint(addr1, TokenID("1"))
 37
 38	// Set metadata for token 1
 39	derr := dummy.SetTokenMetadata(TokenID("1"), Metadata{
 40		Name:            name,
 41		Description:     description,
 42		Image:           image,
 43		ImageData:       imageData,
 44		ExternalURL:     externalURL,
 45		Attributes:      attributes,
 46		BackgroundColor: backgroundColor,
 47		AnimationURL:    animationURL,
 48		YoutubeURL:      youtubeURL,
 49	})
 50
 51	// Check if there was an error setting metadata
 52	uassert.NoError(t, derr, "Should not result in error")
 53
 54	// Test case: Invalid token ID
 55	err := dummy.SetTokenMetadata(TokenID("3"), Metadata{
 56		Name:            name,
 57		Description:     description,
 58		Image:           image,
 59		ImageData:       imageData,
 60		ExternalURL:     externalURL,
 61		Attributes:      attributes,
 62		BackgroundColor: backgroundColor,
 63		AnimationURL:    animationURL,
 64		YoutubeURL:      youtubeURL,
 65	})
 66
 67	// Check if the error returned matches the expected error
 68	uassert.ErrorIs(t, err, ErrInvalidTokenId)
 69
 70	// Set the original caller to addr2
 71	testing.SetOriginCaller(addr2) // addr2
 72
 73	// Try to set metadata for token 1 from addr2 (should fail)
 74	cerr := dummy.SetTokenMetadata(TokenID("1"), Metadata{
 75		Name:            name,
 76		Description:     description,
 77		Image:           image,
 78		ImageData:       imageData,
 79		ExternalURL:     externalURL,
 80		Attributes:      attributes,
 81		BackgroundColor: backgroundColor,
 82		AnimationURL:    animationURL,
 83		YoutubeURL:      youtubeURL,
 84	})
 85
 86	// Check if the error returned matches the expected error
 87	uassert.ErrorIs(t, cerr, ErrCallerIsNotOwner)
 88
 89	// Set the original caller back to addr1
 90	testing.SetOriginCaller(addr1) // addr1
 91
 92	// Retrieve metadata for token 1
 93	dummyMetadata, err := dummy.TokenMetadata(TokenID("1"))
 94	uassert.NoError(t, err, "Metadata error")
 95
 96	// Check if metadata attributes match expected values
 97	uassert.Equal(t, image, dummyMetadata.Image)
 98	uassert.Equal(t, imageData, dummyMetadata.ImageData)
 99	uassert.Equal(t, externalURL, dummyMetadata.ExternalURL)
100	uassert.Equal(t, description, dummyMetadata.Description)
101	uassert.Equal(t, name, dummyMetadata.Name)
102	uassert.Equal(t, len(attributes), len(dummyMetadata.Attributes))
103	uassert.Equal(t, backgroundColor, dummyMetadata.BackgroundColor)
104	uassert.Equal(t, animationURL, dummyMetadata.AnimationURL)
105	uassert.Equal(t, youtubeURL, dummyMetadata.YoutubeURL)
106}