Enable look-up by secondary index in cache

This commit is contained in:
derekwaynecarr
2015-02-04 15:47:47 -05:00
parent 25659cf1b3
commit 7a2d63048d
2 changed files with 144 additions and 0 deletions

View File

@@ -86,10 +86,47 @@ func doTestStore(t *testing.T, store Store) {
}
}
// Test public interface
func doTestIndex(t *testing.T, index Index) {
mkObj := func(id string, val string) testStoreObject {
return testStoreObject{id: id, val: val}
}
// Test Index
expected := map[string]util.StringSet{}
expected["b"] = util.NewStringSet("a", "c")
expected["f"] = util.NewStringSet("e")
expected["h"] = util.NewStringSet("g")
index.Add(mkObj("a", "b"))
index.Add(mkObj("c", "b"))
index.Add(mkObj("e", "f"))
index.Add(mkObj("g", "h"))
{
for k, v := range expected {
found := util.StringSet{}
indexResults, err := index.Index(mkObj("", k))
if err != nil {
t.Errorf("Unexpected error %v", err)
}
for _, item := range indexResults {
found.Insert(item.(testStoreObject).id)
}
items := v.List()
if !found.HasAll(items...) {
t.Errorf("missing items, index %s, expected %v but found %v", k, items, found.List())
}
}
}
}
func testStoreKeyFunc(obj interface{}) (string, error) {
return obj.(testStoreObject).id, nil
}
func testStoreIndexFunc(obj interface{}) (string, error) {
return obj.(testStoreObject).val, nil
}
type testStoreObject struct {
id string
val string
@@ -107,3 +144,7 @@ func TestUndeltaStore(t *testing.T) {
nop := func([]interface{}) {}
doTestStore(t, NewUndeltaStore(nop, testStoreKeyFunc))
}
func TestIndex(t *testing.T) {
doTestIndex(t, NewIndex(testStoreKeyFunc, testStoreIndexFunc))
}