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 56b0ed2
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 39 deletions.
70 changes: 70 additions & 0 deletions examples/gno.land/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
KEY = MyKey
BASE = teritori
REMOTE = http://127.0.0.1:26657
CHAIN_ID = dev

.PHONY: add_social_feeds_realm add_utf16_pkg add_ujson_pkg add_flags_index_pkg add_dao_interfaces_pkg add_social_feed all

add_agregator_realm:
gnokey maketx addpkg \
-deposit="1ugnot" \
-gas-fee="1ugnot" \
-gas-wanted="50000000" \
-broadcast="true" \
-chainid="${CHAIN_ID}" \
-remote="${REMOTE}" \
-pkgdir="./r/demo/teritori/worx_aggregator" \
-pkgpath="gno.land/r/${BASE}/worx_aggregator" \
${KEY}

add_random_worx_realm:
gnokey maketx addpkg \
-deposit="1ugnot" \
-gas-fee="1ugnot" \
-gas-wanted="50000000" \
-broadcast="true" \
-chainid="${CHAIN_ID}" \
-remote="${REMOTE}" \
-pkgdir="./r/demo/teritori/providers/random_pusher" \
-pkgpath="gno.land/r/${BASE}/providers/random_pusher" \
${KEY}


add_worx_pkg:
gnokey maketx addpkg \
-deposit="1ugnot" \
-gas-fee="1ugnot" \
-gas-wanted="50000000" \
-broadcast="true" \
-chainid="${CHAIN_ID}" \
-remote="${REMOTE}" \
-pkgdir="./p/demo/teritori/worx" \
-pkgpath="gno.land/p/${BASE}/worx" \
${KEY}


register_dataprovider:
gnokey maketx call \
-pkgpath "gno.land/r/${BASE}/worx_aggregator" \
-func="RegisterDataProvider" \
-gas-fee="1000000ugnot" \
-gas-wanted="3000000" \
-remote="${REMOTE}" \
-chainid="${CHAIN_ID}" \
-broadcast \
-args "gno.land/r/teritori/providers/random_pusher" \
${KEY}

push_worx:
gnokey maketx call \
-pkgpath "gno.land/r/${BASE}/providers/random_pusher" \
-func="RandWorx" \
-gas-fee="1000000ugnot" \
-gas-wanted="3000000" \
-remote="${REMOTE}" \
-chainid="${CHAIN_ID}" \
-broadcast \
-args "g14vxq5e5pt5sev7rkz2ej438scmxtylnzv5vnkw" \
${KEY}

all: add_utf16_pkg add_ujson_pkg add_flags_index_pkg add_dao_interfaces_pkg add_social_feeds_realm add_social_feed
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,
))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module gno.land/r/demo/teritori/random_pusher

require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/demo/testutils v0.0.0-latest
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package random_pusher

import (
"std"
"gno.land/r/teritori/worx_aggregator"
"math/rand"
)

var admin std.Address

func init() {
admin = std.GetOrigCaller()

}

var rSeed = rand.NewPCG(0, 0)

func RandWorx(addr std.Address){
r := rand.New(rSeed)

worx_aggregator.Push(r.IntN(25), "", addr, r.IntN(100), std.GetHeight())
}
18 changes: 0 additions & 18 deletions examples/gno.land/r/demo/teritori/providers/worx_dao.gno

This file was deleted.

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)
}
34 changes: 33 additions & 1 deletion examples/gno.land/r/demo/teritori/worx_aggregator/aggregator.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package worx_aggregator
import (
"std"

"gno.land/p/demo/teritori/worx"
"gno.land/p/teritori/worx"
"strings"

"gno.land/p/demo/ufmt"
)

func GetWorx(addr std.Address) []*worx.Worx {
Expand All @@ -15,3 +18,32 @@ func GetWorxFromDate(addr std.Address, date int64) []*worx.Worx {
keeper := getKeeper(addr)
return keeper.GetFromDate(date)
}

// Render renders the state of the realm
func Render(path string) string {
parts := strings.Split(path, "/")
c := len(parts)

switch {
case path == "":
res := "Registered Dataproviders \n"
registeredProviders.Iterate("", "", func(key string, value interface{}) bool {
res += key + "\n"
return false
})

return res
case c == 1:
// Render worx for this address
owner := std.Address(parts[0])
worxs := GetWorx(owner)
res := "user Worxs:\n"
for _, worx := range worxs {
res += ufmt.Sprintf(" Hours %d, Points %d, Timestamp %d \n",worx.Hours, worx.Points, worx.Timestamp)
}

return res
default:
return "404\n"
}
}
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/teritori/worx_aggregator/gno.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module gno.land/r/demo/teritori/worx_aggregator

require (
gno.land/p/demo/teritori/worx v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
)
41 changes: 24 additions & 17 deletions examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ import (
"std"

"gno.land/p/demo/avl"
"gno.land/p/demo/teritori/worx"
"gno.land/p/teritori/worx"
)

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)
providerPath := std.PrevRealm().PkgPath()
assertRegistered(providerPath)

keeper := getKeeper(addr)
keeper.Store(worx.NewWorx(hours, metadata, addr, points, timestamp))
Expand All @@ -31,28 +37,29 @@ func getKeeper(addr std.Address) *worx.WorxKeeper {
if ok {
return data.(*worx.WorxKeeper)
}
return &worx.WorxKeeper{}
return worx.NewWorxKeeper()
}

func getDataProviderKeeper(packagePath string) *worx.WorxKeeper {
data, ok := registeredProviders.Get(packagePath)
if ok {
return data.(*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)
}

func assertRegistered(addr std.Address) {
prevRealm := std.PrevRealm().Addr()
dataProviders, ok := registeredProviders.Get(string(prevRealm))
func assertRegistered(packagePath string) {
_, ok := registeredProviders.Get(packagePath)
if !ok {
panic("caller realm is not registered as provider")
}
}

func assertAdmin() {
if std.PrevRealm().Addr() != admin {
panic("unathorized")
}
}
Loading

0 comments on commit 56b0ed2

Please sign in to comment.