Search Apps Documentation Source Content File Folder Download Copy

hof_test.gno

2.73 Kb ยท 134 lines
  1package hof
  2
  3import (
  4	"std"
  5	"testing"
  6
  7	"gno.land/p/demo/testutils"
  8	"gno.land/p/demo/uassert"
  9	"gno.land/p/demo/urequire"
 10)
 11
 12const rlmPath = "gno.land/r/gnoland/home"
 13
 14var (
 15	admin      = Ownable.Owner()
 16	adminRealm = std.NewUserRealm(admin)
 17	alice      = testutils.TestAddress("alice")
 18)
 19
 20func TestRegister(t *testing.T) {
 21	// Test user realm register
 22	aliceRealm := std.NewUserRealm(alice)
 23	std.TestSetRealm(aliceRealm)
 24
 25	Register()
 26	uassert.False(t, itemExists(t, rlmPath))
 27
 28	// Test register while paused
 29	std.TestSetRealm(adminRealm)
 30	Pausable.Pause()
 31
 32	// Set legitimate caller
 33	std.TestSetRealm(std.NewCodeRealm(rlmPath))
 34
 35	Register()
 36	uassert.False(t, itemExists(t, rlmPath))
 37
 38	// Unpause
 39	std.TestSetRealm(adminRealm)
 40	Pausable.Unpause()
 41
 42	// Set legitimate caller
 43	std.TestSetRealm(std.NewCodeRealm(rlmPath))
 44	Register()
 45
 46	// Find registered items
 47	uassert.True(t, itemExists(t, rlmPath))
 48}
 49
 50func TestUpvote(t *testing.T) {
 51	raw, _ := exhibition.items.Get(rlmPath)
 52	item := raw.(*Item)
 53
 54	rawSorted, _ := exhibition.itemsSorted.Get(item.id.String())
 55	itemSorted := rawSorted.(*Item)
 56
 57	// 0 upvotes by default
 58	urequire.Equal(t, item.upvote.Size(), 0)
 59
 60	std.TestSetRealm(adminRealm)
 61
 62	urequire.NotPanics(t, func() {
 63		Upvote(rlmPath)
 64	})
 65
 66	// Check both trees for 1 upvote
 67	uassert.Equal(t, item.upvote.Size(), 1)
 68	uassert.Equal(t, itemSorted.upvote.Size(), 1)
 69
 70	// Check double upvote
 71	uassert.PanicsWithMessage(t, ErrDoubleUpvote.Error(), func() {
 72		Upvote(rlmPath)
 73	})
 74}
 75
 76func TestDownvote(t *testing.T) {
 77	raw, _ := exhibition.items.Get(rlmPath)
 78	item := raw.(*Item)
 79
 80	rawSorted, _ := exhibition.itemsSorted.Get(item.id.String())
 81	itemSorted := rawSorted.(*Item)
 82
 83	// 0 downvotes by default
 84	urequire.Equal(t, item.downvote.Size(), 0)
 85
 86	userRealm := std.NewUserRealm(alice)
 87	std.TestSetRealm(userRealm)
 88
 89	urequire.NotPanics(t, func() {
 90		Downvote(rlmPath)
 91	})
 92
 93	// Check both trees for 1 upvote
 94	uassert.Equal(t, item.downvote.Size(), 1)
 95	uassert.Equal(t, itemSorted.downvote.Size(), 1)
 96
 97	// Check double downvote
 98	uassert.PanicsWithMessage(t, ErrDoubleDownvote.Error(), func() {
 99		Downvote(rlmPath)
100	})
101}
102
103func TestDelete(t *testing.T) {
104	userRealm := std.NewUserRealm(admin)
105	std.TestSetRealm(userRealm)
106	std.TestSetOrigCaller(admin)
107
108	uassert.PanicsWithMessage(t, ErrNoSuchItem.Error(), func() {
109		Delete("nonexistentpkgpath")
110	})
111
112	i, _ := exhibition.items.Get(rlmPath)
113	id := i.(*Item).id
114
115	uassert.NotPanics(t, func() {
116		Delete(rlmPath)
117	})
118
119	uassert.False(t, exhibition.items.Has(rlmPath))
120	uassert.False(t, exhibition.itemsSorted.Has(id.String()))
121}
122
123func itemExists(t *testing.T, rlmPath string) bool {
124	t.Helper()
125
126	i, ok1 := exhibition.items.Get(rlmPath)
127	ok2 := false
128
129	if ok1 {
130		_, ok2 = exhibition.itemsSorted.Get(i.(*Item).id.String())
131	}
132
133	return ok1 && ok2
134}