Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

profile_test.gno

3.88 Kb · 141 lines
  1package profile
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/nt/testutils/v0"
  7	"gno.land/p/nt/uassert/v0"
  8)
  9
 10// Global addresses for test users
 11var (
 12	alice   = testutils.TestAddress("alice")
 13	bob     = testutils.TestAddress("bob")
 14	charlie = testutils.TestAddress("charlie")
 15	dave    = testutils.TestAddress("dave")
 16	eve     = testutils.TestAddress("eve")
 17	frank   = testutils.TestAddress("frank")
 18	user1   = testutils.TestAddress("user1")
 19	user2   = testutils.TestAddress("user2")
 20)
 21
 22func TestStringFields(cur realm, t *testing.T) {
 23	testing.SetRealm(testing.NewUserRealm(alice))
 24
 25	// Get before setting
 26	name := GetStringField(alice, DisplayName, "anon")
 27	uassert.Equal(t, "anon", name)
 28
 29	// Set new key
 30	updated := SetStringField(cross(cur), DisplayName, "Alice foo")
 31	uassert.Equal(t, updated, false)
 32	updated = SetStringField(cross(cur), Homepage, "https://example.com")
 33	uassert.Equal(t, updated, false)
 34
 35	// Update the key
 36	updated = SetStringField(cross(cur), DisplayName, "Alice foo")
 37	uassert.Equal(t, updated, true)
 38
 39	// Get after setting
 40	name = GetStringField(alice, DisplayName, "anon")
 41	homepage := GetStringField(alice, Homepage, "")
 42	bio := GetStringField(alice, Bio, "42")
 43
 44	uassert.Equal(t, "Alice foo", name)
 45	uassert.Equal(t, "https://example.com", homepage)
 46	uassert.Equal(t, "42", bio)
 47}
 48
 49func TestIntFields(cur realm, t *testing.T) {
 50	testing.SetRealm(testing.NewUserRealm(bob))
 51
 52	// Get before setting
 53	age := GetIntField(bob, Age, 25)
 54	uassert.Equal(t, 25, age)
 55
 56	// Set new key
 57	updated := SetIntField(cross(cur), Age, 30)
 58	uassert.Equal(t, updated, false)
 59
 60	// Update the key
 61	updated = SetIntField(cross(cur), Age, 30)
 62	uassert.Equal(t, updated, true)
 63
 64	// Get after setting
 65	age = GetIntField(bob, Age, 25)
 66	uassert.Equal(t, 30, age)
 67}
 68
 69func TestBoolFields(cur realm, t *testing.T) {
 70	testing.SetRealm(testing.NewUserRealm(charlie))
 71
 72	// Get before setting
 73	hiring := GetBoolField(charlie, AvailableForHiring, false)
 74	uassert.Equal(t, false, hiring)
 75
 76	// Set
 77	updated := SetBoolField(cross(cur), AvailableForHiring, true)
 78	uassert.Equal(t, updated, false)
 79
 80	// Update the key
 81	updated = SetBoolField(cross(cur), AvailableForHiring, true)
 82	uassert.Equal(t, updated, true)
 83
 84	// Get after setting
 85	hiring = GetBoolField(charlie, AvailableForHiring, false)
 86	uassert.Equal(t, true, hiring)
 87}
 88
 89func TestMultipleProfiles(cur realm, t *testing.T) {
 90	// Set profile for user1
 91	testing.SetRealm(testing.NewUserRealm(user1))
 92	updated := SetStringField(cross(cur), DisplayName, "User One")
 93	uassert.Equal(t, updated, false)
 94
 95	// Set profile for user2
 96	testing.SetRealm(testing.NewUserRealm(user2))
 97	updated = SetStringField(cross(cur), DisplayName, "User Two")
 98	uassert.Equal(t, updated, false)
 99
100	// Get profiles
101	testing.SetRealm(testing.NewUserRealm(user1)) // Switch back to user1
102	name1 := GetStringField(user1, DisplayName, "anon")
103	testing.SetRealm(testing.NewUserRealm(user2)) // Switch back to user2
104	name2 := GetStringField(user2, DisplayName, "anon")
105
106	uassert.Equal(t, "User One", name1)
107	uassert.Equal(t, "User Two", name2)
108}
109
110func TestArbitraryStringField(cur realm, t *testing.T) {
111	testing.SetRealm(testing.NewUserRealm(user1))
112
113	// Set arbitrary string field
114	updated := SetStringField(cross(cur), "MyEmail", "[email protected]")
115	uassert.Equal(t, updated, false)
116
117	val := GetStringField(user1, "MyEmail", "")
118	uassert.Equal(t, val, "[email protected]")
119}
120
121func TestArbitraryIntField(cur realm, t *testing.T) {
122	testing.SetRealm(testing.NewUserRealm(user1))
123
124	// Set arbitrary int field
125	updated := SetIntField(cross(cur), "MyIncome", 100_000)
126	uassert.Equal(t, updated, false)
127
128	val := GetIntField(user1, "MyIncome", 0)
129	uassert.Equal(t, val, 100_000)
130}
131
132func TestArbitraryBoolField(cur realm, t *testing.T) {
133	testing.SetRealm(testing.NewUserRealm(user1))
134
135	// Set arbitrary bool field
136	updated := SetBoolField(cross(cur), "IsWinner", true)
137	uassert.Equal(t, updated, false)
138
139	val := GetBoolField(user1, "IsWinner", false)
140	uassert.Equal(t, val, true)
141}