gnopensea_test.gno
5.23 Kb ยท 211 lines
1package gnopendao7
2
3import (
4 "testing"
5)
6
7// Test marketplace initialization values
8func TestMarketplaceInit(t *testing.T) {
9 // Check marketplace fee is set to default (2.5%)
10 fee := GetMarketplaceFee()
11 if fee != 250 {
12 t.Errorf("Expected marketplace fee to be 250, got %d", fee)
13 }
14}
15
16// Test marketplace fee getter
17func TestGetMarketplaceFee(t *testing.T) {
18 fee := GetMarketplaceFee()
19 if fee < 0 || fee > 1000 {
20 t.Errorf("Marketplace fee out of valid range (0-1000): %d", fee)
21 }
22}
23
24// Test active listings count (should not panic)
25func TestGetActiveListingsCount(t *testing.T) {
26 count := GetActiveListingsCount()
27 if count < 0 {
28 t.Errorf("Active listings count should not be negative: %d", count)
29 }
30}
31
32// Test total sales (should not panic)
33func TestGetTotalSales(t *testing.T) {
34 sales := GetTotalSales()
35 if sales < 0 {
36 t.Errorf("Total sales should not be negative: %d", sales)
37 }
38}
39
40// Test total volume (should not panic)
41func TestGetTotalVolume(t *testing.T) {
42 volume := GetTotalVolume()
43 if volume < 0 {
44 t.Errorf("Total volume should not be negative: %d", volume)
45 }
46}
47
48// Test total royalties paid (should not panic)
49func TestGetTotalRoyaltiesPaid(t *testing.T) {
50 royalties := GetTotalRoyaltiesPaid()
51 if royalties < 0 {
52 t.Errorf("Total royalties should not be negative: %d", royalties)
53 }
54}
55
56// Test marketplace address getter (should not panic)
57func TestGetMarketplaceAddress(t *testing.T) {
58 addr := GetMarketplaceAddress()
59 if addr == "" {
60 t.Error("Marketplace address should not be empty")
61 }
62}
63
64// Test balance getter (should not panic)
65func TestGetBalance(t *testing.T) {
66 balance := GetBalance()
67 if balance < 0 {
68 t.Errorf("Balance should not be negative: %d", balance)
69 }
70}
71
72// Test render home page (should not panic)
73func TestRenderHome(t *testing.T) {
74 output := Render("")
75 if len(output) == 0 {
76 t.Error("Home render should not be empty")
77 }
78
79 // Check for expected content
80 if !contains(output, "Marketplace") {
81 t.Error("Home render should contain 'Marketplace'")
82 }
83}
84
85// Test render stats page (should not panic)
86func TestRenderStats(t *testing.T) {
87 output := Render("stats")
88 if len(output) == 0 {
89 t.Error("Stats render should not be empty")
90 }
91
92 // Check for expected content
93 if !contains(output, "Statistics") {
94 t.Error("Stats render should contain 'Statistics'")
95 }
96}
97
98// Test render invalid path
99func TestRenderInvalidPath(t *testing.T) {
100 output := Render("invalid/path/test")
101 if output != "Page not found" {
102 t.Errorf("Expected 'Page not found', got '%s'", output)
103 }
104}
105
106// Test format price function
107func TestFormatPrice(t *testing.T) {
108 tests := []struct {
109 input int64
110 expected string
111 }{
112 {1000000, "1.00 GNOT"},
113 {5000000, "5.00 GNOT"},
114 {100, "0.00 GNOT"},
115 {0, "0.00 GNOT"},
116 }
117
118 for _, tt := range tests {
119 result := formatPrice(tt.input)
120 if result != tt.expected {
121 t.Errorf("formatPrice(%d) = %s, expected %s", tt.input, result, tt.expected)
122 }
123 }
124}
125
126// Test format fee function
127func TestFormatFee(t *testing.T) {
128 tests := []struct {
129 input int64
130 expected string
131 }{
132 {250, "2.50%"},
133 {500, "5.00%"},
134 {1000, "10.00%"},
135 {0, "0.00%"},
136 }
137
138 for _, tt := range tests {
139 result := formatFee(tt.input)
140 if result != tt.expected {
141 t.Errorf("formatFee(%d) = %s, expected %s", tt.input, result, tt.expected)
142 }
143 }
144}
145
146// Test format percentage function
147func TestFormatPercentage(t *testing.T) {
148 tests := []struct {
149 value int64
150 total int64
151 expected string
152 }{
153 {25, 100, "25.0%"},
154 {50, 100, "50.0%"},
155 {0, 100, "0.0%"},
156 {100, 100, "100.0%"},
157 {0, 0, "0%"}, // Division by zero case
158 }
159
160 for _, tt := range tests {
161 result := formatPercentage(tt.value, tt.total)
162 if result != tt.expected {
163 t.Errorf("formatPercentage(%d, %d) = %s, expected %s",
164 tt.value, tt.total, result, tt.expected)
165 }
166 }
167}
168
169// Test GetListing with invalid ID (should return zeros)
170func TestGetListingInvalid(t *testing.T) {
171 id, tokenId, price, seller, active, listedAt := GetListing(999999)
172
173 if id != 0 || tokenId != "" || price != 0 || seller != "" || active != false || listedAt != 0 {
174 t.Error("GetListing with invalid ID should return zero values")
175 }
176}
177
178// Test GetSale with invalid ID (should return zeros)
179func TestGetSaleInvalid(t *testing.T) {
180 listingId, tokenId, buyer, seller, price, marketFee, royaltyFee, royaltyReceiver, soldAt := GetSale(999999)
181
182 if listingId != 0 || tokenId != "" || buyer != "" || seller != "" ||
183 price != 0 || marketFee != 0 || royaltyFee != 0 || royaltyReceiver != "" || soldAt != 0 {
184 t.Error("GetSale with invalid ID should return zero values")
185 }
186}
187
188// Test GetRoyaltyBreakdown with invalid ID (should return zeros)
189func TestGetRoyaltyBreakdownInvalid(t *testing.T) {
190 sellerAmount, marketFee, royalty, royaltyReceiver := GetRoyaltyBreakdown(999999)
191
192 if sellerAmount != 0 || marketFee != 0 || royalty != 0 || royaltyReceiver != "" {
193 t.Error("GetRoyaltyBreakdown with invalid ID should return zero values")
194 }
195}
196
197// Helper function to check if string contains substring
198func contains(s, substr string) bool {
199 return len(s) >= len(substr) && (s == substr ||
200 len(s) > len(substr) && indexOf(s, substr) >= 0)
201}
202
203// Helper function to find substring index
204func indexOf(s, substr string) int {
205 for i := 0; i <= len(s)-len(substr); i++ {
206 if s[i:i+len(substr)] == substr {
207 return i
208 }
209 }
210 return -1
211}