Skip to content

Commit

Permalink
Add a simple test for Filter iterator
Browse files Browse the repository at this point in the history
Add a simple unit test to validate the Filter iterator semantic.

Signed-off-by: Fabio Falzoi <[email protected]>
  • Loading branch information
pippolo84 authored and joamaki committed Apr 3, 2024
1 parent 351ad8c commit 7f78035
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions iterator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

package statedb

import (
"testing"

"github.com/cilium/statedb/index"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFilter(t *testing.T) {
type testObject struct {
ID int
}

db, _ := NewDB(nil, NewExpVarMetrics(false))
idIndex := Index[*testObject, int]{
Name: "id",
FromObject: func(t *testObject) index.KeySet {
return index.NewKeySet(index.Int(t.ID))
},
FromKey: index.Int,
Unique: true,
}
table, _ := NewTable("test", idIndex)
require.NoError(t, db.RegisterTable(table))

txn := db.WriteTxn(table)
table.Insert(txn, &testObject{ID: 1})
table.Insert(txn, &testObject{ID: 2})
table.Insert(txn, &testObject{ID: 3})
table.Insert(txn, &testObject{ID: 4})
table.Insert(txn, &testObject{ID: 5})
txn.Commit()

iter, _ := table.All(db.ReadTxn())
filtered := CollectSet(
Map(
Filter(
iter,
func(obj *testObject) bool {
return obj.ID%2 == 0
},
),
func(obj *testObject) int {
return obj.ID
},
),
)
assert.Len(t, filtered, 2)
assert.True(t, filtered.Has(2))
assert.True(t, filtered.Has(4))
}

0 comments on commit 7f78035

Please sign in to comment.