From 56b0ed2434919e39879542dce90ac03b83741a32 Mon Sep 17 00:00:00 2001 From: Omar Sy Date: Sun, 21 Apr 2024 18:49:40 +0200 Subject: [PATCH] feat: add test --- examples/gno.land/Makefile | 70 +++++++++++++++++++ .../gno.land/p/demo/teritori/worx/worxs.gno | 14 +++- .../p/demo/teritori/worx/worxs_test.gno | 63 +++++++++++++++++ .../teritori/providers/random_pusher/gno.mod | 6 ++ .../providers/random_pusher/worx_dao.gno | 22 ++++++ .../r/demo/teritori/providers/worx_dao.gno | 18 ----- .../r/demo/teritori/worx_aggregator/admin.gno | 22 ++++++ .../teritori/worx_aggregator/aggregator.gno | 34 ++++++++- .../r/demo/teritori/worx_aggregator/gno.mod | 1 + .../r/demo/teritori/worx_aggregator/worx.gno | 41 ++++++----- .../teritori/worx_aggregator/worx_test.gno | 24 ++++++- 11 files changed, 276 insertions(+), 39 deletions(-) create mode 100644 examples/gno.land/Makefile create mode 100644 examples/gno.land/p/demo/teritori/worx/worxs_test.gno create mode 100644 examples/gno.land/r/demo/teritori/providers/random_pusher/gno.mod create mode 100644 examples/gno.land/r/demo/teritori/providers/random_pusher/worx_dao.gno delete mode 100644 examples/gno.land/r/demo/teritori/providers/worx_dao.gno create mode 100644 examples/gno.land/r/demo/teritori/worx_aggregator/admin.gno diff --git a/examples/gno.land/Makefile b/examples/gno.land/Makefile new file mode 100644 index 00000000000..8ca623a1296 --- /dev/null +++ b/examples/gno.land/Makefile @@ -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 diff --git a/examples/gno.land/p/demo/teritori/worx/worxs.gno b/examples/gno.land/p/demo/teritori/worx/worxs.gno index eaa2835133d..fb2dfae7e87 100644 --- a/examples/gno.land/p/demo/teritori/worx/worxs.gno +++ b/examples/gno.land/p/demo/teritori/worx/worxs.gno @@ -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 @@ -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 { diff --git a/examples/gno.land/p/demo/teritori/worx/worxs_test.gno b/examples/gno.land/p/demo/teritori/worx/worxs_test.gno new file mode 100644 index 00000000000..ec3efab0e82 --- /dev/null +++ b/examples/gno.land/p/demo/teritori/worx/worxs_test.gno @@ -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, + )) +} \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/providers/random_pusher/gno.mod b/examples/gno.land/r/demo/teritori/providers/random_pusher/gno.mod new file mode 100644 index 00000000000..0ca6bb1b4bb --- /dev/null +++ b/examples/gno.land/r/demo/teritori/providers/random_pusher/gno.mod @@ -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 +) \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/providers/random_pusher/worx_dao.gno b/examples/gno.land/r/demo/teritori/providers/random_pusher/worx_dao.gno new file mode 100644 index 00000000000..245f24a130b --- /dev/null +++ b/examples/gno.land/r/demo/teritori/providers/random_pusher/worx_dao.gno @@ -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()) +} \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/providers/worx_dao.gno b/examples/gno.land/r/demo/teritori/providers/worx_dao.gno deleted file mode 100644 index 612374f74dc..00000000000 --- a/examples/gno.land/r/demo/teritori/providers/worx_dao.gno +++ /dev/null @@ -1,18 +0,0 @@ -package provider - -import ( - "std" - "gno.land/r/demo/teritori/worx_aggregator" - "gno.land/p/demo/rand" -) - -var admin std.Address - -func init() { - admin = std.GetOrigCaller() -} - -func RandWorx(addr std.Address){ - r := rand.New() - worx_aggregator.Push(r.Intn(25), "", addr, r.Intn(100), std.GetHeight()) -} \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/admin.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/admin.gno new file mode 100644 index 00000000000..bbc740e2124 --- /dev/null +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/admin.gno @@ -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) +} diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/aggregator.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/aggregator.gno index 07c5727f76a..d0124779dbb 100644 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/aggregator.gno +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/aggregator.gno @@ -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 { @@ -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" + } +} \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/gno.mod b/examples/gno.land/r/demo/teritori/worx_aggregator/gno.mod index 701b2f6b6cd..63d70821808 100644 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/gno.mod +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/gno.mod @@ -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 ) diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno index 99432f3c1bd..e72e5075179 100644 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno @@ -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)) @@ -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") - } -} diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/worx_test.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/worx_test.gno index 21ce9b75639..70ed2873666 100644 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/worx_test.gno +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/worx_test.gno @@ -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 { @@ -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) +}