basic_nft_test.gno
10.09 Kb ยท 327 lines
1package grc721
2
3import (
4 "std"
5 "testing"
6
7 "gno.land/p/demo/testutils"
8 "gno.land/p/demo/uassert"
9)
10
11var (
12 dummyNFTName = "DummyNFT"
13 dummyNFTSymbol = "DNFT"
14)
15
16func TestNewBasicNFT(t *testing.T) {
17 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
18 uassert.True(t, dummy != nil, "should not be nil")
19}
20
21func TestName(t *testing.T) {
22 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
23 uassert.True(t, dummy != nil, "should not be nil")
24
25 name := dummy.Name()
26 uassert.Equal(t, dummyNFTName, name)
27}
28
29func TestSymbol(t *testing.T) {
30 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
31 uassert.True(t, dummy != nil, "should not be nil")
32
33 symbol := dummy.Symbol()
34 uassert.Equal(t, dummyNFTSymbol, symbol)
35}
36
37func TestTokenCount(t *testing.T) {
38 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
39 uassert.True(t, dummy != nil, "should not be nil")
40
41 count := dummy.TokenCount()
42 uassert.Equal(t, uint64(0), count)
43
44 dummy.mint("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm", TokenID("1"))
45 dummy.mint("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm", TokenID("2"))
46
47 count = dummy.TokenCount()
48 uassert.Equal(t, uint64(2), count)
49}
50
51func TestBalanceOf(t *testing.T) {
52 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
53 uassert.True(t, dummy != nil, "should not be nil")
54
55 addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
56 addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
57
58 balanceAddr1, err := dummy.BalanceOf(addr1)
59 uassert.NoError(t, err, "should not result in error")
60 uassert.Equal(t, uint64(0), balanceAddr1)
61
62 dummy.mint(addr1, TokenID("1"))
63 dummy.mint(addr1, TokenID("2"))
64 dummy.mint(addr2, TokenID("3"))
65
66 balanceAddr1, err = dummy.BalanceOf(addr1)
67 uassert.NoError(t, err, "should not result in error")
68
69 balanceAddr2, err := dummy.BalanceOf(addr2)
70 uassert.NoError(t, err, "should not result in error")
71
72 uassert.Equal(t, uint64(2), balanceAddr1)
73 uassert.Equal(t, uint64(1), balanceAddr2)
74}
75
76func TestOwnerOf(t *testing.T) {
77 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
78 uassert.True(t, dummy != nil, "should not be nil")
79
80 addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
81 addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
82
83 owner, err := dummy.OwnerOf(TokenID("invalid"))
84 uassert.Error(t, err, "should not result in error")
85
86 dummy.mint(addr1, TokenID("1"))
87 dummy.mint(addr2, TokenID("2"))
88
89 // Checking for token id "1"
90 owner, err = dummy.OwnerOf(TokenID("1"))
91 uassert.NoError(t, err, "should not result in error")
92 uassert.Equal(t, addr1.String(), owner.String())
93
94 // Checking for token id "2"
95 owner, err = dummy.OwnerOf(TokenID("2"))
96 uassert.NoError(t, err, "should not result in error")
97 uassert.Equal(t, addr2.String(), owner.String())
98}
99
100func TestIsApprovedForAll(t *testing.T) {
101 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
102 uassert.True(t, dummy != nil, "should not be nil")
103
104 addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
105 addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
106
107 isApprovedForAll := dummy.IsApprovedForAll(addr1, addr2)
108 uassert.False(t, isApprovedForAll)
109}
110
111func TestSetApprovalForAll(t *testing.T) {
112 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
113 uassert.True(t, dummy != nil, "should not be nil")
114
115 caller := std.PreviousRealm().Address()
116 addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
117
118 isApprovedForAll := dummy.IsApprovedForAll(caller, addr)
119 uassert.False(t, isApprovedForAll)
120
121 err := dummy.SetApprovalForAll(addr, true)
122 uassert.NoError(t, err, "should not result in error")
123
124 isApprovedForAll = dummy.IsApprovedForAll(caller, addr)
125 uassert.True(t, isApprovedForAll)
126}
127
128func TestGetApproved(t *testing.T) {
129 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
130 uassert.True(t, dummy != nil, "should not be nil")
131
132 _, err := dummy.GetApproved(TokenID("invalid"))
133 uassert.Error(t, err, "should result in error")
134}
135
136func TestApprove(t *testing.T) {
137 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
138 uassert.True(t, dummy != nil, "should not be nil")
139
140 caller := std.PreviousRealm().Address()
141 addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
142
143 dummy.mint(caller, TokenID("1"))
144
145 _, err := dummy.GetApproved(TokenID("1"))
146 uassert.Error(t, err, "should result in error")
147
148 err = dummy.Approve(addr, TokenID("1"))
149 uassert.NoError(t, err, "should not result in error")
150
151 approvedAddr, err := dummy.GetApproved(TokenID("1"))
152 uassert.NoError(t, err, "should result in error")
153 uassert.Equal(t, addr.String(), approvedAddr.String())
154}
155
156func TestTransferFrom(t *testing.T) {
157 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
158 uassert.True(t, dummy != nil, "should not be nil")
159
160 caller := std.PreviousRealm().Address()
161 addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
162
163 dummy.mint(caller, TokenID("1"))
164 dummy.mint(caller, TokenID("2"))
165
166 err := dummy.TransferFrom(caller, addr, TokenID("1"))
167 uassert.NoError(t, err, "should result in error")
168
169 // Check balance of caller after transfer
170 balanceOfCaller, err := dummy.BalanceOf(caller)
171 uassert.NoError(t, err, "should result in error")
172 uassert.Equal(t, uint64(1), balanceOfCaller)
173
174 // Check balance of addr after transfer
175 balanceOfAddr, err := dummy.BalanceOf(addr)
176 uassert.NoError(t, err, "should not result in error")
177 uassert.Equal(t, uint64(1), balanceOfAddr)
178
179 // Check Owner of transferred Token id
180 owner, err := dummy.OwnerOf(TokenID("1"))
181 uassert.NoError(t, err, "should result in error")
182 uassert.Equal(t, addr.String(), owner.String())
183}
184
185func TestSafeTransferFrom(t *testing.T) {
186 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
187 uassert.True(t, dummy != nil, "should not be nil")
188
189 caller := std.PreviousRealm().Address()
190 addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
191
192 dummy.mint(caller, TokenID("1"))
193 dummy.mint(caller, TokenID("2"))
194
195 err := dummy.SafeTransferFrom(caller, addr, TokenID("1"))
196 uassert.NoError(t, err, "should not result in error")
197
198 // Check balance of caller after transfer
199 balanceOfCaller, err := dummy.BalanceOf(caller)
200 uassert.NoError(t, err, "should not result in error")
201 uassert.Equal(t, uint64(1), balanceOfCaller)
202
203 // Check balance of addr after transfer
204 balanceOfAddr, err := dummy.BalanceOf(addr)
205 uassert.NoError(t, err, "should not result in error")
206 uassert.Equal(t, uint64(1), balanceOfAddr)
207
208 // Check Owner of transferred Token id
209 owner, err := dummy.OwnerOf(TokenID("1"))
210 uassert.NoError(t, err, "should not result in error")
211 uassert.Equal(t, addr.String(), owner.String())
212}
213
214func TestMint(t *testing.T) {
215 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
216 uassert.True(t, dummy != nil, "should not be nil")
217
218 addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
219 addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
220
221 err := dummy.Mint(addr1, TokenID("1"))
222 uassert.NoError(t, err, "should not result in error")
223 err = dummy.Mint(addr1, TokenID("2"))
224 uassert.NoError(t, err, "should not result in error")
225 err = dummy.Mint(addr2, TokenID("3"))
226 uassert.NoError(t, err, "should not result in error")
227
228 // Try minting duplicate token id
229 err = dummy.Mint(addr2, TokenID("1"))
230 uassert.Error(t, err, "should not result in error")
231
232 // Check Owner of Token id
233 owner, err := dummy.OwnerOf(TokenID("1"))
234 uassert.NoError(t, err, "should not result in error")
235 uassert.Equal(t, addr1.String(), owner.String())
236}
237
238func TestBurn(t *testing.T) {
239 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
240 uassert.True(t, dummy != nil, "should not be nil")
241
242 addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
243
244 dummy.mint(addr, TokenID("1"))
245 dummy.mint(addr, TokenID("2"))
246
247 err := dummy.Burn(TokenID("1"))
248 uassert.NoError(t, err, "should not result in error")
249
250 // Check Owner of Token id
251 _, err = dummy.OwnerOf(TokenID("1"))
252 uassert.Error(t, err, "should result in error")
253}
254
255func TestSetTokenURI(t *testing.T) {
256 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
257 uassert.True(t, dummy != nil, "should not be nil")
258
259 addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
260 addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
261 tokenURI := "http://example.com/token"
262
263 testing.SetOriginCaller(addr1) // addr1
264
265 dummy.mint(addr1, TokenID("1"))
266 _, derr := dummy.SetTokenURI(TokenID("1"), TokenURI(tokenURI))
267 uassert.NoError(t, derr, "should not result in error")
268
269 // Test case: Invalid token ID
270 _, err := dummy.SetTokenURI(TokenID("3"), TokenURI(tokenURI))
271 uassert.ErrorIs(t, err, ErrInvalidTokenId)
272
273 testing.SetOriginCaller(addr2) // addr2
274
275 _, cerr := dummy.SetTokenURI(TokenID("1"), TokenURI(tokenURI)) // addr2 trying to set URI for token 1
276 uassert.ErrorIs(t, cerr, ErrCallerIsNotOwner)
277
278 // Test case: Retrieving TokenURI
279 testing.SetOriginCaller(addr1) // addr1
280
281 dummyTokenURI, err := dummy.TokenURI(TokenID("1"))
282 uassert.NoError(t, err, "TokenURI error")
283 uassert.Equal(t, string(tokenURI), string(dummyTokenURI))
284}
285
286func TestIsApprovedOrOwner(t *testing.T) {
287 dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
288 uassert.True(t, dummy != nil, "should not be nil")
289
290 var (
291 owner = testutils.TestAddress("owner")
292 operator = testutils.TestAddress("operator")
293 approved = testutils.TestAddress("approved")
294 other = testutils.TestAddress("other")
295 )
296
297 tid := TokenID("1")
298
299 err := dummy.mint(owner, tid)
300 uassert.NoError(t, err)
301
302 // check owner
303 isApprovedOrOwner := dummy.isApprovedOrOwner(owner, tid)
304 uassert.True(t, isApprovedOrOwner, "owner should be approved")
305
306 // check operator
307 testing.SetOriginCaller(owner)
308 err = dummy.SetApprovalForAll(operator, true)
309 uassert.NoError(t, err)
310 isApprovedOrOwner = dummy.isApprovedOrOwner(operator, tid)
311 uassert.True(t, isApprovedOrOwner, "operator should be approved")
312
313 // check approved
314 testing.SetOriginCaller(owner)
315 err = dummy.Approve(approved, tid)
316 uassert.NoError(t, err)
317 isApprovedOrOwner = dummy.isApprovedOrOwner(approved, tid)
318 uassert.True(t, isApprovedOrOwner, "approved address should be approved")
319
320 // check other
321 isApprovedOrOwner = dummy.isApprovedOrOwner(other, tid)
322 uassert.False(t, isApprovedOrOwner, "other address should not be approved")
323
324 // check non-existent token
325 isApprovedOrOwner = dummy.isApprovedOrOwner(owner, TokenID("999"))
326 uassert.False(t, isApprovedOrOwner, "non-existent token should not be approved")
327}