grc721_royalty_test.gno

1.92 Kb ยท 69 lines
 1package grc721
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/testutils"
 7	"gno.land/p/nt/uassert"
 8)
 9
10func TestSetTokenRoyalty(t *testing.T) {
11	dummy := NewNFTWithRoyalty(dummyNFTName, dummyNFTSymbol)
12	uassert.True(t, dummy != nil, "should not be nil")
13
14	addr1 := testutils.TestAddress("alice")
15	addr2 := testutils.TestAddress("bob")
16
17	paymentAddress := testutils.TestAddress("john")
18	percentage := int64(10) // 10%
19
20	salePrice := int64(1000)
21	expectRoyaltyAmount := int64(100)
22
23	testing.SetOriginCaller(addr1) // addr1
24
25	dummy.mint(addr1, TokenID("1"))
26
27	derr := dummy.SetTokenRoyalty(TokenID("1"), RoyaltyInfo{
28		PaymentAddress: paymentAddress,
29		Percentage:     percentage,
30	})
31	uassert.NoError(t, derr, "Should not result in error")
32
33	// Test case: Invalid token ID
34	_ = dummy.SetTokenRoyalty(TokenID("3"), RoyaltyInfo{
35		PaymentAddress: paymentAddress,
36		Percentage:     percentage,
37	})
38	uassert.ErrorIs(t, derr, ErrInvalidTokenId)
39
40	testing.SetOriginCaller(addr2) // addr2
41
42	cerr := dummy.SetTokenRoyalty(TokenID("1"), RoyaltyInfo{
43		PaymentAddress: paymentAddress,
44		Percentage:     percentage,
45	})
46	uassert.ErrorIs(t, cerr, ErrCallerIsNotOwner)
47
48	// Test case: Invalid payment address
49	aerr := dummy.SetTokenRoyalty(TokenID("4"), RoyaltyInfo{
50		PaymentAddress: address("###"), // invalid address
51		Percentage:     percentage,
52	})
53	uassert.ErrorIs(t, aerr, ErrInvalidRoyaltyPaymentAddress)
54
55	// Test case: Invalid percentage
56	perr := dummy.SetTokenRoyalty(TokenID("5"), RoyaltyInfo{
57		PaymentAddress: paymentAddress,
58		Percentage:     int64(200), // over maxRoyaltyPercentage
59	})
60	uassert.ErrorIs(t, perr, ErrInvalidRoyaltyPercentage)
61
62	// Test case: Retrieving Royalty Info
63	testing.SetOriginCaller(addr1) // addr1
64
65	dummyPaymentAddress, dummyRoyaltyAmount, rerr := dummy.RoyaltyInfo(TokenID("1"), salePrice)
66	uassert.NoError(t, rerr, "RoyaltyInfo error")
67	uassert.Equal(t, paymentAddress, dummyPaymentAddress)
68	uassert.Equal(t, expectRoyaltyAmount, dummyRoyaltyAmount)
69}