Search Apps Documentation Source Content File Folder Download Copy

home_test.gno

8.45 Kb ยท 278 lines
  1package home
  2
  3import (
  4	"std"
  5	"strings"
  6	"testing"
  7
  8	"gno.land/p/demo/avl"
  9	"gno.land/p/demo/testutils"
 10)
 11
 12func TestUpdateAboutMe(t *testing.T) {
 13	var owner = std.Address("g1sd5ezmxt4rwpy52u6wl3l3y085n8x0p6nllxm8")
 14	std.TestSetOrigCaller(owner)
 15
 16	profile.aboutMe = []string{}
 17
 18	UpdateAboutMe("This is my new bio.|I love coding!")
 19
 20	expected := []string{"This is my new bio.", "I love coding!"}
 21
 22	if len(profile.aboutMe) != len(expected) {
 23		t.Fatalf("expected aboutMe to have length %d, got %d", len(expected), len(profile.aboutMe))
 24	}
 25
 26	for i := range profile.aboutMe {
 27		if profile.aboutMe[i] != expected[i] {
 28			t.Fatalf("expected aboutMe[%d] to be %s, got %s", i, expected[i], profile.aboutMe[i])
 29		}
 30	}
 31}
 32
 33func TestUpdateCities(t *testing.T) {
 34	var owner = std.Address("g1sd5ezmxt4rwpy52u6wl3l3y085n8x0p6nllxm8")
 35	std.TestSetOrigCaller(owner)
 36
 37	travel.cities = []City{}
 38
 39	newCities := []City{
 40		{Name: "Berlin", URL: "https://example.com/berlin.jpg"},
 41		{Name: "Vienna", URL: "https://example.com/vienna.jpg"},
 42	}
 43
 44	UpdateCities(newCities)
 45
 46	if len(travel.cities) != 2 {
 47		t.Fatalf("expected 2 cities, got %d", len(travel.cities))
 48	}
 49
 50	if travel.cities[0].Name != "Berlin" || travel.cities[1].Name != "Vienna" {
 51		t.Fatalf("expected cities to be updated to Berlin and Vienna, got %+v", travel.cities)
 52	}
 53}
 54
 55func TestUpdateJarLink(t *testing.T) {
 56	var owner = std.Address("g1sd5ezmxt4rwpy52u6wl3l3y085n8x0p6nllxm8")
 57	std.TestSetOrigCaller(owner)
 58
 59	travel.jarLink = ""
 60
 61	UpdateJarLink("https://example.com/jar")
 62
 63	if travel.jarLink != "https://example.com/jar" {
 64		t.Fatalf("expected jarLink to be https://example.com/jar, got %s", travel.jarLink)
 65	}
 66}
 67
 68func TestUpdateMaxSponsors(t *testing.T) {
 69	var owner = std.Address("g1sd5ezmxt4rwpy52u6wl3l3y085n8x0p6nllxm8")
 70	std.TestSetOrigCaller(owner)
 71
 72	sponsorship.maxSponsors = 0
 73
 74	UpdateMaxSponsors(10)
 75
 76	if sponsorship.maxSponsors != 10 {
 77		t.Fatalf("expected maxSponsors to be 10, got %d", sponsorship.maxSponsors)
 78	}
 79
 80	defer func() {
 81		if r := recover(); r == nil {
 82			t.Fatalf("expected panic for setting maxSponsors to 0")
 83		}
 84	}()
 85	UpdateMaxSponsors(0)
 86}
 87
 88func TestAddCities(t *testing.T) {
 89	var owner = std.Address("g1sd5ezmxt4rwpy52u6wl3l3y085n8x0p6nllxm8")
 90	std.TestSetOrigCaller(owner)
 91
 92	travel.cities = []City{}
 93
 94	AddCities(City{Name: "Berlin", URL: "https://example.com/berlin.jpg"})
 95
 96	if len(travel.cities) != 1 {
 97		t.Fatalf("expected 1 city, got %d", len(travel.cities))
 98	}
 99	if travel.cities[0].Name != "Berlin" || travel.cities[0].URL != "https://example.com/berlin.jpg" {
100		t.Fatalf("expected city to be Berlin, got %+v", travel.cities[0])
101	}
102
103	AddCities(
104		City{Name: "Paris", URL: "https://example.com/paris.jpg"},
105		City{Name: "Tokyo", URL: "https://example.com/tokyo.jpg"},
106	)
107
108	if len(travel.cities) != 3 {
109		t.Fatalf("expected 3 cities, got %d", len(travel.cities))
110	}
111	if travel.cities[1].Name != "Paris" || travel.cities[2].Name != "Tokyo" {
112		t.Fatalf("expected cities to be Paris and Tokyo, got %+v", travel.cities[1:])
113	}
114}
115
116func TestAddAboutMeRows(t *testing.T) {
117	var owner = std.Address("g1sd5ezmxt4rwpy52u6wl3l3y085n8x0p6nllxm8")
118	std.TestSetOrigCaller(owner)
119
120	profile.aboutMe = []string{}
121
122	AddAboutMeRows("I love exploring new technologies!")
123
124	if len(profile.aboutMe) != 1 {
125		t.Fatalf("expected 1 aboutMe row, got %d", len(profile.aboutMe))
126	}
127	if profile.aboutMe[0] != "I love exploring new technologies!" {
128		t.Fatalf("expected first aboutMe row to be 'I love exploring new technologies!', got %s", profile.aboutMe[0])
129	}
130
131	AddAboutMeRows("Travel is my passion!", "Always learning.")
132
133	if len(profile.aboutMe) != 3 {
134		t.Fatalf("expected 3 aboutMe rows, got %d", len(profile.aboutMe))
135	}
136	if profile.aboutMe[1] != "Travel is my passion!" || profile.aboutMe[2] != "Always learning." {
137		t.Fatalf("expected aboutMe rows to be 'Travel is my passion!' and 'Always learning.', got %+v", profile.aboutMe[1:])
138	}
139}
140
141func TestDonate(t *testing.T) {
142	var user = testutils.TestAddress("user")
143	std.TestSetOrigCaller(user)
144
145	sponsorship.sponsors = avl.NewTree()
146	sponsorship.DonationsCount = 0
147	sponsorship.sponsorsCount = 0
148	travel.currentCityIndex = 0
149
150	coinsSent := std.NewCoins(std.NewCoin("ugnot", 500))
151	std.TestSetOrigSend(coinsSent, std.NewCoins())
152	Donate()
153
154	existingAmount, exists := sponsorship.sponsors.Get(string(user))
155	if !exists {
156		t.Fatalf("expected sponsor to be added, but it was not found")
157	}
158
159	if existingAmount.(std.Coins).AmountOf("ugnot") != 500 {
160		t.Fatalf("expected donation amount to be 500ugnot, got %d", existingAmount.(std.Coins).AmountOf("ugnot"))
161	}
162
163	if sponsorship.DonationsCount != 1 {
164		t.Fatalf("expected DonationsCount to be 1, got %d", sponsorship.DonationsCount)
165	}
166
167	if sponsorship.sponsorsCount != 1 {
168		t.Fatalf("expected sponsorsCount to be 1, got %d", sponsorship.sponsorsCount)
169	}
170
171	if travel.currentCityIndex != 1 {
172		t.Fatalf("expected currentCityIndex to be 1, got %d", travel.currentCityIndex)
173	}
174
175	coinsSent = std.NewCoins(std.NewCoin("ugnot", 300))
176	std.TestSetOrigSend(coinsSent, std.NewCoins())
177	Donate()
178
179	existingAmount, exists = sponsorship.sponsors.Get(string(user))
180	if !exists {
181		t.Fatalf("expected sponsor to exist after second donation, but it was not found")
182	}
183
184	if existingAmount.(std.Coins).AmountOf("ugnot") != 800 {
185		t.Fatalf("expected total donation amount to be 800ugnot, got %d", existingAmount.(std.Coins).AmountOf("ugnot"))
186	}
187
188	if sponsorship.DonationsCount != 2 {
189		t.Fatalf("expected DonationsCount to be 2 after second donation, got %d", sponsorship.DonationsCount)
190	}
191
192	if travel.currentCityIndex != 2 {
193		t.Fatalf("expected currentCityIndex to be 2 after second donation, got %d", travel.currentCityIndex)
194	}
195}
196
197func TestGetTopSponsors(t *testing.T) {
198	var user = testutils.TestAddress("user")
199	std.TestSetOrigCaller(user)
200
201	sponsorship.sponsors = avl.NewTree()
202	sponsorship.sponsorsCount = 0
203
204	sponsorship.sponsors.Set("g1address1", std.NewCoins(std.NewCoin("ugnot", 300)))
205	sponsorship.sponsors.Set("g1address2", std.NewCoins(std.NewCoin("ugnot", 500)))
206	sponsorship.sponsors.Set("g1address3", std.NewCoins(std.NewCoin("ugnot", 200)))
207	sponsorship.sponsorsCount = 3
208
209	topSponsors := GetTopSponsors()
210
211	if len(topSponsors) != 3 {
212		t.Fatalf("expected 3 sponsors, got %d", len(topSponsors))
213	}
214
215	if topSponsors[0].Address.String() != "g1address2" || topSponsors[0].Amount.AmountOf("ugnot") != 500 {
216		t.Fatalf("expected top sponsor to be g1address2 with 500ugnot, got %s with %dugnot", topSponsors[0].Address.String(), topSponsors[0].Amount.AmountOf("ugnot"))
217	}
218
219	if topSponsors[1].Address.String() != "g1address1" || topSponsors[1].Amount.AmountOf("ugnot") != 300 {
220		t.Fatalf("expected second sponsor to be g1address1 with 300ugnot, got %s with %dugnot", topSponsors[1].Address.String(), topSponsors[1].Amount.AmountOf("ugnot"))
221	}
222
223	if topSponsors[2].Address.String() != "g1address3" || topSponsors[2].Amount.AmountOf("ugnot") != 200 {
224		t.Fatalf("expected third sponsor to be g1address3 with 200ugnot, got %s with %dugnot", topSponsors[2].Address.String(), topSponsors[2].Amount.AmountOf("ugnot"))
225	}
226}
227
228func TestGetTotalDonations(t *testing.T) {
229	var user = testutils.TestAddress("user")
230	std.TestSetOrigCaller(user)
231
232	sponsorship.sponsors = avl.NewTree()
233	sponsorship.sponsorsCount = 0
234
235	sponsorship.sponsors.Set("g1address1", std.NewCoins(std.NewCoin("ugnot", 300)))
236	sponsorship.sponsors.Set("g1address2", std.NewCoins(std.NewCoin("ugnot", 500)))
237	sponsorship.sponsors.Set("g1address3", std.NewCoins(std.NewCoin("ugnot", 200)))
238	sponsorship.sponsorsCount = 3
239
240	totalDonations := GetTotalDonations()
241
242	if totalDonations != 1000 {
243		t.Fatalf("expected total donations to be 1000ugnot, got %dugnot", totalDonations)
244	}
245}
246
247func TestRender(t *testing.T) {
248	travel.currentCityIndex = 0
249	travel.cities = []City{
250		{Name: "Venice", URL: "https://example.com/venice.jpg"},
251		{Name: "Paris", URL: "https://example.com/paris.jpg"},
252	}
253
254	output := Render("")
255
256	expectedCity := "Venice"
257	if !strings.Contains(output, expectedCity) {
258		t.Fatalf("expected output to contain city name '%s', got %s", expectedCity, output)
259	}
260
261	expectedURL := "https://example.com/venice.jpg"
262	if !strings.Contains(output, expectedURL) {
263		t.Fatalf("expected output to contain city URL '%s', got %s", expectedURL, output)
264	}
265
266	travel.currentCityIndex = 1
267	output = Render("")
268
269	expectedCity = "Paris"
270	if !strings.Contains(output, expectedCity) {
271		t.Fatalf("expected output to contain city name '%s', got %s", expectedCity, output)
272	}
273
274	expectedURL = "https://example.com/paris.jpg"
275	if !strings.Contains(output, expectedURL) {
276		t.Fatalf("expected output to contain city URL '%s', got %s", expectedURL, output)
277	}
278}