Skip to content

Commit

Permalink
chore: fixup
Browse files Browse the repository at this point in the history
Signed-off-by: moul <[email protected]>
  • Loading branch information
moul committed Dec 10, 2024
1 parent 8cce34c commit fd8645c
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/gno.land/p/demo/avl/pager/pager.gno
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Item struct {
}

// NewPager creates a new Pager with default values.
func NewPager(tree avl.TreeInterface, defaultPageSize int, reversed bool) *Pager {
func NewPager(tree avl.ITree, defaultPageSize int, reversed bool) *Pager {
return &Pager{
Tree: tree,
PageQueryParam: "page",
Expand Down
103 changes: 103 additions & 0 deletions examples/gno.land/p/moul/collection/collection_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,106 @@ func TestIndexKeyGeneration(t *testing.T) {
})
}
}

func TestGetIndex(t *testing.T) {
c := New()
c.AddIndex("name", func(v interface{}) string {
return v.(*Person).Name
}, UniqueIndex)

tests := []struct {
name string
indexName string
wantNil bool
}{
{
name: "Get existing index",
indexName: "name",
wantNil: false,
},
{
name: "Get _id index",
indexName: IDIndex,
wantNil: false,
},
{
name: "Get non-existent index",
indexName: "invalid",
wantNil: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tree := c.GetIndex(tt.indexName)
if (tree == nil) != tt.wantNil {
t.Errorf("GetIndex() got nil = %v, want nil = %v", tree == nil, tt.wantNil)
}
})
}
}

func TestAddIndexPanic(t *testing.T) {
c := New()
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic when adding _id index")
}
}()

c.AddIndex(IDIndex, func(v interface{}) string {
return ""
}, DefaultIndex)
}

func TestCaseInsensitiveGet(t *testing.T) {
c := New()
c.AddIndex("email", func(v interface{}) string {
return v.(*Person).Email
}, UniqueIndex|CaseInsensitiveIndex)

p := &Person{Email: "[email protected]"}
id := c.Set(p)

tests := []struct {
name string
key string
wantObj bool
}{
{
name: "Exact match",
key: "[email protected]",
wantObj: true,
},
{
name: "Different case",
key: "[email protected]",
wantObj: true,
},
{
name: "Non-existent",
key: "[email protected]",
wantObj: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
obj, gotId := c.Get("email", tt.key)
if (obj != nil) != tt.wantObj {
t.Errorf("Get() got object = %v, want object = %v", obj != nil, tt.wantObj)
}
if tt.wantObj && gotId != id {
t.Errorf("Get() got id = %v, want id = %v", gotId, id)
}
})
}
}

func TestGetInvalidID(t *testing.T) {
c := New()
obj, id := c.Get(IDIndex, "not-a-valid-id")
if obj != nil || id != 0 {
t.Errorf("Get() with invalid ID format got (obj=%v, id=%v), want (nil, 0)", obj, id)
}
}

0 comments on commit fd8645c

Please sign in to comment.