github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/indexer/index/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 "os" 23 "path" 24 "testing" 25 26 "github.com/cs3org/reva/v2/pkg/storage/utils/indexer/errors" 27 "github.com/cs3org/reva/v2/pkg/storage/utils/indexer/index" 28 "github.com/cs3org/reva/v2/pkg/storage/utils/indexer/option" 29 . "github.com/cs3org/reva/v2/pkg/storage/utils/indexer/test" 30 "github.com/cs3org/reva/v2/pkg/storage/utils/metadata" 31 "github.com/stretchr/testify/assert" 32 ) 33 34 func TestUniqueLookupSingleEntry(t *testing.T) { 35 uniq, dataDir := getUniqueIdxSut(t, option.IndexByField("Email"), User{}) 36 filesDir := path.Join(dataDir, "users") 37 38 t.Log("existing lookup") 39 resultPath, err := uniq.Lookup("mikey@example.com") 40 assert.NoError(t, err) 41 42 assert.Equal(t, []string{path.Join(filesDir, "abcdefg-123")}, resultPath) 43 44 t.Log("non-existing lookup") 45 resultPath, err = uniq.Lookup("doesnotExists@example.com") 46 assert.Error(t, err) 47 assert.IsType(t, &errors.NotFoundErr{}, err) 48 assert.Empty(t, resultPath) 49 50 _ = os.RemoveAll(dataDir) 51 52 } 53 54 func TestUniqueUniqueConstraint(t *testing.T) { 55 uniq, dataDir := getUniqueIdxSut(t, option.IndexByField("Email"), User{}) 56 57 _, err := uniq.Add("abcdefg-123", "mikey@example.com") 58 assert.Error(t, err) 59 assert.IsType(t, &errors.AlreadyExistsErr{}, err) 60 61 _ = os.RemoveAll(dataDir) 62 } 63 64 func TestUniqueRemove(t *testing.T) { 65 uniq, dataDir := getUniqueIdxSut(t, option.IndexByField("Email"), User{}) 66 67 err := uniq.Remove("", "mikey@example.com") 68 assert.NoError(t, err) 69 70 _, err = uniq.Lookup("mikey@example.com") 71 assert.Error(t, err) 72 assert.IsType(t, &errors.NotFoundErr{}, err) 73 74 _ = os.RemoveAll(dataDir) 75 } 76 77 func TestUniqueUpdate(t *testing.T) { 78 uniq, dataDir := getUniqueIdxSut(t, option.IndexByField("Email"), User{}) 79 80 t.Log("successful update") 81 err := uniq.Update("", "mikey@example.com", "mikey2@example.com") 82 assert.NoError(t, err) 83 84 t.Log("failed update because not found") 85 err = uniq.Update("", "nonexisting@example.com", "something2@example.com") 86 assert.Error(t, err) 87 assert.IsType(t, &errors.NotFoundErr{}, err) 88 89 _ = os.RemoveAll(dataDir) 90 } 91 92 func TestUniqueIndexSearch(t *testing.T) { 93 sut, dataDir := getUniqueIdxSut(t, option.IndexByField("Email"), User{}) 94 95 res, err := sut.Search("j*@example.com") 96 97 assert.NoError(t, err) 98 assert.Len(t, res, 2) 99 100 assert.Equal(t, "ewf4ofk-555", path.Base(res[0])) 101 assert.Equal(t, "rulan54-777", path.Base(res[1])) 102 103 _, err = sut.Search("does-not-exist@example.com") 104 assert.Error(t, err) 105 assert.IsType(t, &errors.NotFoundErr{}, err) 106 107 _ = os.RemoveAll(dataDir) 108 } 109 110 func getUniqueIdxSut(t *testing.T, indexBy option.IndexBy, entityType interface{}) (index.Index, string) { 111 dataPath, _ := WriteIndexTestData(Data, "ID", "") 112 storage, err := metadata.NewDiskStorage(dataPath) 113 if err != nil { 114 t.Fatal(err) 115 } 116 117 sut := index.NewUniqueIndexWithOptions( 118 storage, 119 option.WithTypeName(GetTypeFQN(entityType)), 120 option.WithIndexBy(indexBy), 121 option.WithFilesDir(path.Join(dataPath, "users")), 122 ) 123 err = sut.Init() 124 if err != nil { 125 t.Fatal(err) 126 } 127 128 for _, u := range Data["users"] { 129 pkVal := ValueOf(u, "ID") 130 idxByVal := ValueOf(u, "Email") 131 _, err := sut.Add(pkVal, idxByVal) 132 if err != nil { 133 t.Fatal(err) 134 } 135 } 136 137 return sut, dataPath 138 }