github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/indexer/index/non_unique_test.go (about) 1 // Copyright 2018-2022 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package index_test 20 21 import ( 22 "fmt" 23 "os" 24 "path" 25 "testing" 26 27 "github.com/cs3org/reva/v2/pkg/storage/utils/indexer/errors" 28 "github.com/cs3org/reva/v2/pkg/storage/utils/indexer/index" 29 "github.com/cs3org/reva/v2/pkg/storage/utils/indexer/option" 30 . "github.com/cs3org/reva/v2/pkg/storage/utils/indexer/test" 31 "github.com/cs3org/reva/v2/pkg/storage/utils/metadata" 32 "github.com/stretchr/testify/assert" 33 ) 34 35 func TestNonUniqueIndexAdd(t *testing.T) { 36 sut, dataPath := getNonUniqueIdxSut(t, Pet{}, option.IndexByField("Color")) 37 38 ids, err := sut.Lookup("Green") 39 assert.NoError(t, err) 40 assert.Len(t, ids, 2) 41 assert.Contains(t, ids, "goefe-789") 42 assert.Contains(t, ids, "xadaf-189") 43 44 ids, err = sut.Lookup("White") 45 assert.NoError(t, err) 46 assert.EqualValues(t, []string{"wefwe-456"}, ids) 47 48 ids, err = sut.Lookup("Cyan") 49 assert.Error(t, err) 50 assert.Nil(t, ids) 51 52 _ = os.RemoveAll(dataPath) 53 54 } 55 56 func TestNonUniqueIndexUpdate(t *testing.T) { 57 sut, dataPath := getNonUniqueIdxSut(t, Pet{}, option.IndexByField("Color")) 58 59 err := sut.Update("goefe-789", "Green", "Black") 60 assert.NoError(t, err) 61 62 err = sut.Update("xadaf-189", "Green", "Black") 63 assert.NoError(t, err) 64 65 assert.DirExists(t, path.Join(dataPath, fmt.Sprintf("index.disk/non_unique.%v.Color/Black", GetTypeFQN(Pet{})))) 66 assert.NoDirExists(t, path.Join(dataPath, fmt.Sprintf("index.disk/non_unique.%v.Color/Green", GetTypeFQN(Pet{})))) 67 68 _ = os.RemoveAll(dataPath) 69 } 70 71 func TestNonUniqueIndexDelete(t *testing.T) { 72 sut, dataPath := getNonUniqueIdxSut(t, Pet{}, option.IndexByField("Color")) 73 assert.FileExists(t, path.Join(dataPath, fmt.Sprintf("index.disk/non_unique.%v.Color/Green/goefe-789", GetTypeFQN(Pet{})))) 74 75 err := sut.Remove("goefe-789", "Green") 76 assert.NoError(t, err) 77 assert.NoFileExists(t, path.Join(dataPath, fmt.Sprintf("index.disk/non_unique.%v.Color/Green/goefe-789", GetTypeFQN(Pet{})))) 78 assert.FileExists(t, path.Join(dataPath, fmt.Sprintf("index.disk/non_unique.%v.Color/Green/xadaf-189", GetTypeFQN(Pet{})))) 79 80 _ = os.RemoveAll(dataPath) 81 } 82 83 func TestNonUniqueIndexSearch(t *testing.T) { 84 sut, dataPath := getNonUniqueIdxSut(t, Pet{}, option.IndexByField("Email")) 85 86 res, err := sut.Search("Gr*") 87 88 assert.NoError(t, err) 89 assert.Len(t, res, 2) 90 91 assert.Equal(t, "goefe-789", path.Base(res[0])) 92 assert.Equal(t, "xadaf-189", path.Base(res[1])) 93 94 _, err = sut.Search("does-not-exist@example.com") 95 assert.Error(t, err) 96 assert.IsType(t, &errors.NotFoundErr{}, err) 97 98 _ = os.RemoveAll(dataPath) 99 } 100 101 // entity: used to get the fully qualified name for the index root path. 102 func getNonUniqueIdxSut(t *testing.T, entity interface{}, indexBy option.IndexBy) (index.Index, string) { 103 dataPath, _ := WriteIndexTestData(Data, "ID", "") 104 storage, err := metadata.NewDiskStorage(dataPath) 105 if err != nil { 106 t.Fatal(err) 107 } 108 109 sut := index.NewNonUniqueIndexWithOptions( 110 storage, 111 option.WithTypeName(GetTypeFQN(entity)), 112 option.WithIndexBy(indexBy), 113 option.WithFilesDir(path.Join(dataPath, "pets")), 114 ) 115 err = sut.Init() 116 if err != nil { 117 t.Fatal(err) 118 } 119 120 for _, u := range Data["pets"] { 121 pkVal := ValueOf(u, "ID") 122 idxByVal := ValueOf(u, "Color") 123 _, err := sut.Add(pkVal, idxByVal) 124 if err != nil { 125 t.Fatal(err) 126 } 127 } 128 129 return sut, dataPath 130 }