Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed Apr 21, 2024
1 parent 5c24dd8 commit bfdb290
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
5 changes: 5 additions & 0 deletions examples/gno.land/p/demo/teritori/worx/gno.mod
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
module gno.land/p/demo/teritori/worx

require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
)
49 changes: 46 additions & 3 deletions examples/gno.land/p/demo/teritori/worx/worxs.gno
Original file line number Diff line number Diff line change
@@ -1,13 +1,56 @@
package worx
import(
"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"
)

type WorxKeeper struct {
worxs []*Worx
worxs *avl.Tree
}
const dayToSeconds = 1

func NewWorxKeeper() *WorxKeeper{
return &WorxKeeper{
worxs: avl.NewTree(),
}
}
func (keeper *WorxKeeper) Store(worx *Worx) {
keeper.worxs = append(keeper.worxs, worx)
key := ufmt.Sprintf("%d", worx.Timestamp)
worxsInterface, exists := keeper.worxs.Get(key)

if exists {
worxsOnDay := worxsInterface.([]*Worx)
worxsOnDay = append(worxsOnDay, worx)
keeper.worxs.Set(key, worxsOnDay)
return
}
keeper.worxs.Set(key, []*Worx{worx})
}

func (keeper *WorxKeeper) Get() []*Worx {
return keeper.worxs
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,
))
}

0 comments on commit bfdb290

Please sign in to comment.