-
Notifications
You must be signed in to change notification settings - Fork 2
/
regression_test.go
76 lines (64 loc) · 2.05 KB
/
regression_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package statedb
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/cilium/statedb/index"
)
// Test_Regression_29324 tests that Get() on a index.String-based
// unique index only returns exact matches.
// https://github.com/cilium/cilium/issues/29324
func Test_Regression_29324(t *testing.T) {
type object struct {
ID string
Tag string
}
idIndex := Index[object, string]{
Name: "id",
FromObject: func(t object) index.KeySet {
return index.NewKeySet(index.String(t.ID))
},
FromKey: index.String,
Unique: true,
}
tagIndex := Index[object, string]{
Name: "tag",
FromObject: func(t object) index.KeySet {
return index.NewKeySet(index.String(t.Tag))
},
FromKey: index.String,
Unique: false,
}
db, _, _ := newTestDB(t)
table, err := NewTable[object]("objects", idIndex, tagIndex)
require.NoError(t, err)
require.NoError(t, db.RegisterTable(table))
wtxn := db.WriteTxn(table)
table.Insert(wtxn, object{"foo", "aa"})
table.Insert(wtxn, object{"foobar", "aaa"})
table.Insert(wtxn, object{"baz", "aaaa"})
wtxn.Commit()
// Exact match should only return "foo"
txn := db.ReadTxn()
iter := table.List(txn, idIndex.Query("foo"))
items := Collect(iter)
if assert.Len(t, items, 1, "Get(\"foo\") should return one match") {
assert.EqualValues(t, "foo", items[0].ID)
}
// Partial match on prefix should not return anything
iter = table.List(txn, idIndex.Query("foob"))
items = Collect(iter)
assert.Len(t, items, 0, "Get(\"foob\") should return nothing")
// Query on non-unique index should only return exact match
iter = table.List(txn, tagIndex.Query("aa"))
items = Collect(iter)
if assert.Len(t, items, 1, "Get(\"aa\") on tags should return one match") {
assert.EqualValues(t, "foo", items[0].ID)
}
// Partial match on prefix should not return anything on non-unique index
iter = table.List(txn, idIndex.Query("a"))
items = Collect(iter)
assert.Len(t, items, 0, "Get(\"a\") should return nothing")
}