Skip to content

Commit

Permalink
feat: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
omarsy authored and Villaquiranm committed Aug 18, 2024
1 parent 296d1f5 commit a5cef66
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 15 deletions.
14 changes: 12 additions & 2 deletions examples/gno.land/p/demo/teritori/worx/worxs.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package worx
import (
"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"
"time"
)

type WorxKeeper struct {
worxs []*Worx
worxs *avl.Tree
}

const dayToSeconds = 1
Expand All @@ -18,7 +19,16 @@ func NewWorxKeeper() *WorxKeeper {
}

func (keeper *WorxKeeper) Store(worx *Worx) {
keeper.worxs = append(keeper.worxs, worx)
storekey := ufmt.Sprintf("%d", time.Now().Unix())
worxs, ok:= keeper.worxs.Get(storekey)
if ok {
worxsOnDay := worxs.([]*Worx)
worxsOnDay = append(worxsOnDay, worx)
keeper.worxs.Set(storekey, worxsOnDay)
return
}

keeper.worxs.Set(storekey, []*Worx{worx})
}

func (keeper *WorxKeeper) Get() []*Worx {
Expand Down
63 changes: 63 additions & 0 deletions examples/gno.land/p/demo/teritori/worx/worxs_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package worx

import (
"testing"
"gno.land/p/demo/rand"
"gno.land/p/demo/testutils"
"gno.land/p/demo/ufmt"
"std"
)

func TestAddGet(t *testing.T) {
keeper := NewWorxKeeper()
user1 := testutils.TestAddress("user1")
if len(keeper.Get()) != 0{
t.Fatalf("Keeper is not empty initialized")
}

fillRandomWorx(keeper, 10123423, user1)
if len(keeper.Get()) != 1{
t.Fatalf("Keeper Worx was not added to keeper 1")
}

fillRandomWorx(keeper, 10123423, user1)
if len(keeper.Get()) != 2{
t.Fatalf("Keeper Worx was not added to keeper 2")
}
}

func TestGetFromDate(t *testing.T) {
keeper := NewWorxKeeper()
user1 := testutils.TestAddress("user1")
if len(keeper.Get()) != 0{
t.Fatalf("Keeper is not empty initialized")
}

for i:=0; i < 100; i++ {
fillRandomWorx(keeper, int64(i*10), user1)
}

if len(keeper.Get()) != 100{
t.Fatalf("Keeper Worx was not totally added")
}

if len(keeper.GetFromDate(1003)) != 0 {
t.Fatalf("Get From Date Should have found 0 registers")
}

if len(keeper.GetFromDate(903)) != 9 {
t.Fatalf("Get From Date Should have found 9 registers")
}

}

func fillRandomWorx(keeper *WorxKeeper, timestamp int64, address std.Address){
r := rand.New()
keeper.Store(NewWorx(
r.Intn(25),
"somekey",
address,
r.Intn(100),
timestamp,
))
}
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)
}
23 changes: 11 additions & 12 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 @@ -31,13 +36,13 @@ func getKeeper(addr std.Address) *worx.WorxKeeper {
if ok {
return data.(*worx.WorxKeeper)
}
return &worx.WorxKeeper{}
return worx.NewWorxKeeper()
}

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 a5cef66

Please sign in to comment.