Skip to content

Commit

Permalink
feat: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
omarsy committed Apr 21, 2024
1 parent b5f8306 commit fcacc64
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
22 changes: 22 additions & 0 deletions examples/gno.land/r/demo/teritori/worx_aggregator/admin.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package worx_aggregator

import (
"std"
)

var adminAddr std.Address

func assertIsAdmin() {
if std.PrevRealm().Addr() != adminAddr {
panic("restricted area")
}
}

func setAdminAddress(address std.Address) {
adminAddr = address
}

func SetAdminAddress(address std.Address) {
assertIsAdmin()
setAdminAddress(address)
}
21 changes: 10 additions & 11 deletions examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import (
)

var (
admin std.Address
registeredProviders avl.Tree
worxByAddress avl.Tree
registeredProviders *avl.Tree
worxByAddress *avl.Tree
)

func init() {
registeredProviders = avl.NewTree()
worxByAddress = avl.NewTree()
setAdminAddress(std.PrevRealm().Addr())
}

func Push(hours int, metadata string, addr std.Address, points int, timestamp int64) {
assertRegistered(addr)

Expand All @@ -35,9 +40,9 @@ func getKeeper(addr std.Address) *worx.WorxKeeper {
}

func RegisterDataProvider(addr std.Address) {
assertAdmin()
assertIsAdmin()
_, ok := registeredProviders.Get(string(addr))
if !ok {
if ok {
panic("Data provider already registered")
}
registeredProviders.Set(string(addr), 0)
Expand All @@ -50,9 +55,3 @@ func assertRegistered(addr std.Address) {
panic("caller realm is not registered as provider")
}
}

func assertAdmin() {
if std.PrevRealm().Addr() != admin {
panic("unathorized")
}
}
24 changes: 23 additions & 1 deletion examples/gno.land/r/demo/teritori/worx_aggregator/worx_test.gno
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package worx_aggregator

import (
"std"
"testing"

"gno.land/p/demo/avl"
"gno.land/p/demo/testutils"
)

func TestPushCallerNotRegistered(t * testing.T){
func TestPushCallerNotRegistered(t *testing.T) {
user1 := testutils.TestAddress("user1")
defer func() {
if v := recover(); v == nil {
Expand All @@ -14,3 +17,22 @@ func TestPushCallerNotRegistered(t * testing.T){
}()
Push(1, "", user1, 3, 10)
}

func TestHappyPath(t *testing.T) {
registeredProviders = avl.NewTree()
worxByAddress = avl.NewTree()

realm := testutils.TestAddress("realm")
admin := testutils.TestAddress("admin")
user1 := testutils.TestAddress("user1")

setAdminAddress(admin)

std.TestSetOrigCaller(admin)

RegisterDataProvider(realm)

std.TestSetOrigCaller(realm)

Push(1, "", user1, 3, 10)
}

0 comments on commit fcacc64

Please sign in to comment.