home_test.gno
2.87 Kb ยท 98 lines
1package home
2
3import (
4 "testing"
5
6 "gno.land/p/demo/testutils"
7 "gno.land/p/demo/uassert"
8 "gno.land/r/mouss/config"
9)
10
11var (
12 user1 = testutils.TestAddress("user1")
13 user2 = testutils.TestAddress("user2")
14 mainAddr = config.OwnableMain.Owner()
15)
16
17func TestProfile(t *testing.T) {
18 uassert.NotEmpty(t, profile.AboutMe, "AboutMe should not be empty")
19 uassert.NotEmpty(t, profile.Avatar, "Avatar should not be empty")
20 uassert.NotEmpty(t, profile.Email, "Email should not be empty")
21 uassert.NotEmpty(t, profile.Github, "Github should not be empty")
22 uassert.NotEmpty(t, profile.LinkedIn, "LinkedIn should not be empty")
23}
24
25func TestAddRecipe(t *testing.T) {
26 testing.SetOriginCaller(user1)
27 name := "Test Recipe"
28 origin := "Test Origin"
29 ingredients := "Ingredient 1\nIngredient 2"
30 instructions := "Step 1\nStep 2"
31 tips := "Test Tips"
32
33 result := AddRecipe(name, origin, ingredients, instructions, tips)
34 uassert.Equal(t, "Recipe added successfully", result)
35 uassert.Equal(t, 1, recipes.Size())
36 value, exist := recipes.Get(name)
37 uassert.True(t, exist)
38 recipe := value.(*Recipe)
39 uassert.Equal(t, name, recipe.Name)
40 uassert.Equal(t, origin, recipe.Origin)
41 uassert.Equal(t, ingredients, recipe.Ingredients)
42 uassert.Equal(t, instructions, recipe.Instructions)
43 uassert.Equal(t, tips, recipe.Tips)
44 uassert.Equal(t, user1, recipe.Author)
45
46 // Verify recipe is correctly stored in AVL tree with matching fields
47 var found bool
48 recipes.Iterate("", "", func(key string, value interface{}) bool {
49 if key == name {
50 found = true
51 foundRecipe := value.(*Recipe)
52 uassert.Equal(t, recipe.Name, foundRecipe.Name)
53 uassert.Equal(t, recipe.Origin, foundRecipe.Origin)
54 uassert.Equal(t, recipe.Ingredients, foundRecipe.Ingredients)
55 uassert.Equal(t, recipe.Instructions, foundRecipe.Instructions)
56 uassert.Equal(t, recipe.Tips, foundRecipe.Tips)
57 uassert.Equal(t, recipe.Author, foundRecipe.Author)
58 return true
59 }
60 return false
61 })
62 uassert.Equal(t, true, found)
63}
64
65func TestFollow(t *testing.T) {
66 // Test user following admin's profile
67 testing.SetOriginCaller(user1)
68 err := Follow()
69 uassert.NoError(t, err, "user should be able to follow admin's profile")
70
71 // Test admin trying to follow themselves
72 testing.SetOriginCaller(mainAddr)
73 err = Follow()
74 uassert.Error(t, err, "you cannot follow yourself")
75
76 // Test following same address twice
77 testing.SetOriginCaller(user1)
78 err = Follow()
79 uassert.Error(t, err, "should not be able to follow same address twice")
80
81 // Test multiple users following admin
82 testing.SetOriginCaller(user2)
83 err = Follow()
84 uassert.NoError(t, err, "another user should be able to follow admin's profile")
85}
86
87func TestUnfollow(t *testing.T) {
88 // Test successful unfollow
89 testing.SetOriginCaller(user1)
90 err := Unfollow()
91 uassert.NoError(t, err)
92 uassert.False(t, profile.Followers.Has(user1))
93
94 // Test unfollowing when not following
95 err = Unfollow()
96 uassert.Error(t, err)
97
98}