github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/grc/grc721/grc721_metadata_test.gno (about) 1 package grc721 2 3 import ( 4 "std" 5 "testing" 6 7 "gno.land/p/demo/testutils" 8 "gno.land/p/demo/users" 9 ) 10 11 func TestSetMetadata(t *testing.T) { 12 // Create a new dummy NFT with metadata 13 dummy := NewNFTWithMetadata(dummyNFTName, dummyNFTSymbol) 14 if dummy == nil { 15 t.Errorf("should not be nil") 16 } 17 18 // Define addresses for testing purposes 19 addr1 := testutils.TestAddress("alice") 20 addr2 := testutils.TestAddress("bob") 21 22 // Define metadata attributes 23 name := "test" 24 description := "test" 25 image := "test" 26 imageData := "test" 27 externalURL := "test" 28 attributes := []Trait{} 29 backgroundColor := "test" 30 animationURL := "test" 31 youtubeURL := "test" 32 33 // Set the original caller to addr1 34 std.TestSetOrigCaller(addr1) // addr1 35 36 // Mint a new token for addr1 37 dummy.mint(addr1, TokenID("1")) 38 39 // Set metadata for token 1 40 derr := dummy.SetTokenMetadata(TokenID("1"), Metadata{ 41 Name: name, 42 Description: description, 43 Image: image, 44 ImageData: imageData, 45 ExternalURL: externalURL, 46 Attributes: attributes, 47 BackgroundColor: backgroundColor, 48 AnimationURL: animationURL, 49 YoutubeURL: youtubeURL, 50 }) 51 52 // Check if there was an error setting metadata 53 if derr != nil { 54 t.Errorf("Should not result in error : %s", derr.Error()) 55 } 56 57 // Test case: Invalid token ID 58 err := dummy.SetTokenMetadata(TokenID("3"), Metadata{ 59 Name: name, 60 Description: description, 61 Image: image, 62 ImageData: imageData, 63 ExternalURL: externalURL, 64 Attributes: attributes, 65 BackgroundColor: backgroundColor, 66 AnimationURL: animationURL, 67 YoutubeURL: youtubeURL, 68 }) 69 70 // Check if the error returned matches the expected error 71 if err != ErrInvalidTokenId { 72 t.Errorf("Expected error %s, got %s", ErrInvalidTokenId, err) 73 } 74 75 // Set the original caller to addr2 76 std.TestSetOrigCaller(addr2) // addr2 77 78 // Try to set metadata for token 1 from addr2 (should fail) 79 cerr := dummy.SetTokenMetadata(TokenID("1"), Metadata{ 80 Name: name, 81 Description: description, 82 Image: image, 83 ImageData: imageData, 84 ExternalURL: externalURL, 85 Attributes: attributes, 86 BackgroundColor: backgroundColor, 87 AnimationURL: animationURL, 88 YoutubeURL: youtubeURL, 89 }) 90 91 // Check if the error returned matches the expected error 92 if cerr != ErrCallerIsNotOwner { 93 t.Errorf("Expected error %s, got %s", ErrCallerIsNotOwner, cerr) 94 } 95 96 // Set the original caller back to addr1 97 std.TestSetOrigCaller(addr1) // addr1 98 99 // Retrieve metadata for token 1 100 dummyMetadata, err := dummy.TokenMetadata(TokenID("1")) 101 if err != nil { 102 t.Errorf("Metadata error: %s", err.Error()) 103 } 104 105 // Check if metadata attributes match expected values 106 if dummyMetadata.Image != image { 107 t.Errorf("Expected Metadata's image %s, got %s", image, dummyMetadata.Image) 108 } 109 if dummyMetadata.ImageData != imageData { 110 t.Errorf("Expected Metadata's imageData %s, got %s", imageData, dummyMetadata.ImageData) 111 } 112 if dummyMetadata.ExternalURL != externalURL { 113 t.Errorf("Expected Metadata's externalURL %s, got %s", externalURL, dummyMetadata.ExternalURL) 114 } 115 if dummyMetadata.Description != description { 116 t.Errorf("Expected Metadata's description %s, got %s", description, dummyMetadata.Description) 117 } 118 if dummyMetadata.Name != name { 119 t.Errorf("Expected Metadata's name %s, got %s", name, dummyMetadata.Name) 120 } 121 if len(dummyMetadata.Attributes) != len(attributes) { 122 t.Errorf("Expected %d Metadata's attributes, got %d", len(attributes), len(dummyMetadata.Attributes)) 123 } 124 if dummyMetadata.BackgroundColor != backgroundColor { 125 t.Errorf("Expected Metadata's backgroundColor %s, got %s", backgroundColor, dummyMetadata.BackgroundColor) 126 } 127 if dummyMetadata.AnimationURL != animationURL { 128 t.Errorf("Expected Metadata's animationURL %s, got %s", animationURL, dummyMetadata.AnimationURL) 129 } 130 if dummyMetadata.YoutubeURL != youtubeURL { 131 t.Errorf("Expected Metadata's youtubeURL %s, got %s", youtubeURL, dummyMetadata.YoutubeURL) 132 } 133 }