Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testworx #19

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/teritori/worx/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/teritori/worx
22 changes: 22 additions & 0 deletions examples/gno.land/p/demo/teritori/worx/worx.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package worx

import "std"


type Worx struct {
Hours int
Metadata string
Address std.Address
Points int
Timestamp int64
}

func NewWorx(hours int, metadata string, addr std.Address, points int, timestamp int64) *Worx{
return &Worx{
Hours: hours,
Metadata: metadata,
Address: addr,
Points: points,
Timestamp: timestamp,
}
}
60 changes: 60 additions & 0 deletions examples/gno.land/p/demo/teritori/worx/worxs.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package worx

import (
"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"
"time"
)

type WorxKeeper struct {
worxs *avl.Tree
}

const dayToSeconds = 1

func NewWorxKeeper() *WorxKeeper {
return &WorxKeeper{
worxs: avl.NewTree(),
}
}

func (keeper *WorxKeeper) Store(worx *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 {
totalWorx := make([]*Worx, 0)
keeper.worxs.Iterate("", "", func(key string, value interface{}) bool {
worxByDay := value.([]*Worx)
totalWorx = append(totalWorx, worxByDay...)
return false
})

return totalWorx
}

func (keeper *WorxKeeper) GetFromDate(date int64) []*Worx {
start := ufmt.Sprintf("%d", date)
totalWorx := make([]*Worx, 0)
keeper.worxs.Iterate(start, "", func(key string, value interface{}) bool {
// here we account for string comparation "95" > "105" because comparing first character 9 > 1 (Comparation of lenght)
// and simply comparing same lenght string 25 > 12 this in order to stop tree for iterating over lower dates leaves
if len(start) > len(key) || start > key {
return true
}
worxByDay := value.([]*Worx)
totalWorx = append(totalWorx, worxByDay...)
return false
})

return totalWorx
}
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,
))
}
6 changes: 6 additions & 0 deletions examples/gno.land/r/demo/teritori/providers/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module gno.land/r/demo/teritori/social_follow

require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/demo/testutils v0.0.0-latest
)
23 changes: 23 additions & 0 deletions examples/gno.land/r/demo/teritori/providers/profile.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package provider

import (
"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"
)

type Profile struct{
data avl.Tree
}

func(p *Profile) ToString() string{
var data = ""
p.data.Iterate("", "", func(key string, value interface{}) bool {
data += ufmt.Sprintf("%s: %s\n",key, value)
return false
})
return data
}

func(p *Profile) SetField(fieldName string, value string) {
p.data.Set(fieldName, value)
}
66 changes: 66 additions & 0 deletions examples/gno.land/r/demo/teritori/providers/profiles.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package provider

import (
"std"
"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"
"gno.land/r/demo/teritori/registry"
)

var profiles avl.Tree

func init() {
registry.Register("profiles", RegisterHandler)
}


func Get(dataName string, addr std.Address) interface{} {
if dataName != "profile"{
panic("invalid dataname")
}
profile:=getProfile(addr)

return profile.ToString()
}

func SupportedTypes() interface{}{
return []interface{}{"profile"}
}

func UpsertProfile(field string, value string){
caller := std.GetOrigCaller()
profile := getProfile(caller)
profile.SetField(field, value)
profiles.Set(caller.String(), profile)
}


func getProfile(addr std.Address ) *Profile {
profile, found:=profiles.Get(addr.String())
if !found{
return &Profile{}
}

return profile.(*Profile)
}

func RegisterHandler(functionName string, args ...interface{}) interface{} {
switch functionName {
case "get":
if len(args) != 2{
panic("invalid number of arguments")
}
dataname := args[0].(string)
address := args[1].(std.Address)
return Get(dataname,address)
case "supportedTypes":
if len(args) != 0{
panic("invalid number of arguments")
}
dataname := args[0].(string)
address := args[1].(std.Address)
return SupportedTypes()
default:
panic("invalid function name")
}
}
33 changes: 33 additions & 0 deletions examples/gno.land/r/demo/teritori/providers/profiles_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package provider

import (
"std"
"testing"

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

func TestGetProfile(t *testing.T) {
user1 := testutils.TestAddress("user1")
std.TestSetOrigCaller(user1)
UpsertProfile("firstname", "John")
UpsertProfile("name", "Gopher")

result:=Get("profile",user1)
t.Log(result)
if !strings.Contains(result,"name: Gopher"){
t.Error("Bad")
}
}

func TestRegisterHandler(t *testing.T) {
user1 := testutils.TestAddress("user1")
std.TestSetOrigCaller(user1)
supportedTypes := RegisterHandler("supportedTypes")
if len(supportedTypes) != 1{
t.Error("Supported types is wrong")
}
}

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())
}
Loading
Loading