refactor disk tests
This commit is contained in:
@@ -31,7 +31,7 @@ func TestCS3NonUniqueIndex_FakeSymlink(t *testing.T) {
|
||||
}
|
||||
|
||||
sut := NewNonUniqueIndexWithOptions(
|
||||
option.WithTypeName("test.Users.Cs3"),
|
||||
option.WithTypeName(GetTypeFQN(TestUser{})),
|
||||
option.WithIndexBy("UserName"),
|
||||
option.WithFilesDir(path.Join(cfg.Repo.Disk.Path, "/meta")),
|
||||
option.WithDataDir(cfg.Repo.Disk.Path),
|
||||
|
||||
@@ -50,7 +50,7 @@ func TestCS3UniqueIndex_FakeSymlink(t *testing.T) {
|
||||
}
|
||||
|
||||
sut := NewUniqueIndexWithOptions(
|
||||
option.WithTypeName("test.Users.Cs3"),
|
||||
option.WithTypeName(GetTypeFQN(TestUser{})),
|
||||
option.WithIndexBy("UserName"),
|
||||
option.WithFilesDir(path.Join(cfg.Repo.Disk.Path, "/meta")),
|
||||
option.WithDataDir(cfg.Repo.Disk.Path),
|
||||
@@ -102,7 +102,7 @@ func TestCS3UniqueIndexSearch(t *testing.T) {
|
||||
}
|
||||
|
||||
sut := NewUniqueIndexWithOptions(
|
||||
option.WithTypeName("test.Users.Cs3"),
|
||||
option.WithTypeName(GetTypeFQN(TestUser{})),
|
||||
option.WithIndexBy("UserName"),
|
||||
option.WithFilesDir(path.Join(cfg.Repo.Disk.Path, "/meta")),
|
||||
option.WithDataDir(cfg.Repo.Disk.Path),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package disk
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/owncloud/ocis/accounts/pkg/config"
|
||||
"github.com/owncloud/ocis/accounts/pkg/indexer/errors"
|
||||
"github.com/owncloud/ocis/accounts/pkg/indexer/index"
|
||||
@@ -13,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
func TestNonUniqueIndexAdd(t *testing.T) {
|
||||
sut, dataPath := getNonUniqueIdxSut(t, "Color")
|
||||
sut, dataPath := getNonUniqueIdxSut(t, TestPet{}, "Color")
|
||||
|
||||
ids, err := sut.Lookup("Green")
|
||||
assert.NoError(t, err)
|
||||
@@ -32,7 +33,7 @@ func TestNonUniqueIndexAdd(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNonUniqueIndexUpdate(t *testing.T) {
|
||||
sut, dataPath := getNonUniqueIdxSut(t, "Color")
|
||||
sut, dataPath := getNonUniqueIdxSut(t, TestPet{}, "Color")
|
||||
|
||||
err := sut.Update("goefe-789", "Green", "Black")
|
||||
assert.NoError(t, err)
|
||||
@@ -40,23 +41,25 @@ func TestNonUniqueIndexUpdate(t *testing.T) {
|
||||
err = sut.Update("xadaf-189", "Green", "Black")
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.DirExists(t, path.Join(dataPath, "index.disk/non_unique.test.Users.Disk.Color/Black"))
|
||||
assert.NoDirExists(t, path.Join(dataPath, "index.disk/non_unique.test.Users.Disk.Color/Green"))
|
||||
assert.DirExists(t, path.Join(dataPath, fmt.Sprintf("index.disk/non_unique.%v.Color/Black", GetTypeFQN(TestPet{}))))
|
||||
assert.NoDirExists(t, path.Join(dataPath, fmt.Sprintf("index.disk/non_unique.%v.Color/Green", GetTypeFQN(TestPet{}))))
|
||||
|
||||
_ = os.RemoveAll(dataPath)
|
||||
}
|
||||
|
||||
func TestNonUniqueIndexDelete(t *testing.T) {
|
||||
sut, dataPath := getNonUniqueIdxSut(t, "Color")
|
||||
assert.FileExists(t, path.Join(dataPath, "index.disk/non_unique.test.Users.Disk.Color/Green/goefe-789"))
|
||||
sut, dataPath := getNonUniqueIdxSut(t, TestPet{}, "Color")
|
||||
assert.FileExists(t, path.Join(dataPath, fmt.Sprintf("index.disk/non_unique.%v.Color/Green/goefe-789", GetTypeFQN(TestPet{}))))
|
||||
|
||||
err := sut.Remove("goefe-789", "")
|
||||
assert.NoError(t, err)
|
||||
assert.NoFileExists(t, path.Join(dataPath, "index.disk/non_unique.test.Users.Disk.Color/Green/goefe-789"))
|
||||
assert.NoFileExists(t, path.Join(dataPath, fmt.Sprintf("index.disk/non_unique.%v.Color/Green/goefe-789", GetTypeFQN(TestPet{}))))
|
||||
|
||||
_ = os.RemoveAll(dataPath)
|
||||
}
|
||||
|
||||
func TestNonUniqueIndexSearch(t *testing.T) {
|
||||
sut, dataPath := getNonUniqueIdxSut(t, "Email")
|
||||
sut, dataPath := getNonUniqueIdxSut(t, TestPet{}, "Email")
|
||||
|
||||
res, err := sut.Search("Gr*")
|
||||
|
||||
@@ -73,7 +76,8 @@ func TestNonUniqueIndexSearch(t *testing.T) {
|
||||
_ = os.RemoveAll(dataPath)
|
||||
}
|
||||
|
||||
func getNonUniqueIdxSut(t *testing.T, indexBy string) (index.Index, string) {
|
||||
// entity: used to get the fully qualified name for the index root path.
|
||||
func getNonUniqueIdxSut(t *testing.T, entity interface{}, indexBy string) (index.Index, string) {
|
||||
dataPath := WriteIndexTestData(t, TestData, "Id")
|
||||
cfg := config.Config{
|
||||
Repo: config.Repo{
|
||||
@@ -84,7 +88,7 @@ func getNonUniqueIdxSut(t *testing.T, indexBy string) (index.Index, string) {
|
||||
}
|
||||
|
||||
sut := NewNonUniqueIndexWithOptions(
|
||||
option.WithTypeName("test.Users.Disk"),
|
||||
option.WithTypeName(GetTypeFQN(entity)),
|
||||
option.WithIndexBy(indexBy),
|
||||
option.WithFilesDir(path.Join(cfg.Repo.Disk.Path, "pets")),
|
||||
option.WithDataDir(cfg.Repo.Disk.Path),
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
)
|
||||
|
||||
func TestUniqueLookupSingleEntry(t *testing.T) {
|
||||
uniq, dataDir := getUniqueIdxSut(t, "Email")
|
||||
uniq, dataDir := getUniqueIdxSut(t, "Email", TestUser{})
|
||||
filesDir := path.Join(dataDir, "users")
|
||||
|
||||
t.Log("existing lookup")
|
||||
@@ -33,7 +33,7 @@ func TestUniqueLookupSingleEntry(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUniqueUniqueConstraint(t *testing.T) {
|
||||
uniq, dataDir := getUniqueIdxSut(t, "Email")
|
||||
uniq, dataDir := getUniqueIdxSut(t, "Email", TestUser{})
|
||||
|
||||
_, err := uniq.Add("abcdefg-123", "mikey@example.com")
|
||||
assert.Error(t, err)
|
||||
@@ -43,7 +43,7 @@ func TestUniqueUniqueConstraint(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUniqueRemove(t *testing.T) {
|
||||
uniq, dataDir := getUniqueIdxSut(t, "Email")
|
||||
uniq, dataDir := getUniqueIdxSut(t, "Email", TestUser{})
|
||||
|
||||
err := uniq.Remove("", "mikey@example.com")
|
||||
assert.NoError(t, err)
|
||||
@@ -56,7 +56,7 @@ func TestUniqueRemove(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUniqueUpdate(t *testing.T) {
|
||||
uniq, dataDir := getUniqueIdxSut(t, "Email")
|
||||
uniq, dataDir := getUniqueIdxSut(t, "Email", TestUser{})
|
||||
|
||||
t.Log("successful update")
|
||||
err := uniq.Update("", "mikey@example.com", "mikey2@example.com")
|
||||
@@ -76,7 +76,7 @@ func TestUniqueUpdate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUniqueIndexSearch(t *testing.T) {
|
||||
sut, dataDir := getUniqueIdxSut(t, "Email")
|
||||
sut, dataDir := getUniqueIdxSut(t, "Email", TestUser{})
|
||||
|
||||
res, err := sut.Search("j*@example.com")
|
||||
|
||||
@@ -98,7 +98,7 @@ func TestErrors(t *testing.T) {
|
||||
assert.True(t, errors.IsNotFoundErr(&errors.NotFoundErr{}))
|
||||
}
|
||||
|
||||
func getUniqueIdxSut(t *testing.T, indexBy string) (index.Index, string) {
|
||||
func getUniqueIdxSut(t *testing.T, indexBy string, entityType interface{}) (index.Index, string) {
|
||||
dataPath := WriteIndexTestData(t, TestData, "Id")
|
||||
cfg := config.Config{
|
||||
Repo: config.Repo{
|
||||
@@ -109,7 +109,7 @@ func getUniqueIdxSut(t *testing.T, indexBy string) (index.Index, string) {
|
||||
}
|
||||
|
||||
sut := NewUniqueIndexWithOptions(
|
||||
option.WithTypeName("test.Users.Disk"),
|
||||
option.WithTypeName(GetTypeFQN(entityType)),
|
||||
option.WithIndexBy(indexBy),
|
||||
option.WithFilesDir(path.Join(cfg.Repo.Disk.Path, "users")),
|
||||
option.WithDataDir(cfg.Repo.Disk.Path),
|
||||
|
||||
@@ -20,10 +20,10 @@ func TestIndexer_AddWithUniqueIndex(t *testing.T) {
|
||||
},
|
||||
})
|
||||
|
||||
err := indexer.AddIndex(&User{}, "UserName", "Id", "users", "unique")
|
||||
err := indexer.AddIndex(&TestUser{}, "UserName", "Id", "users", "unique")
|
||||
assert.NoError(t, err)
|
||||
|
||||
u := &User{Id: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
|
||||
u := &TestUser{Id: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
|
||||
err = indexer.Add(u)
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -43,10 +43,10 @@ func TestIndexer_AddWithUniqueIndexCS3(t *testing.T) {
|
||||
},
|
||||
})
|
||||
|
||||
err := indexer.AddIndex(&User{}, "UserName", "Id", "users", "unique")
|
||||
err := indexer.AddIndex(&TestUser{}, "UserName", "Id", "users", "unique")
|
||||
assert.NoError(t, err)
|
||||
|
||||
u := &User{Id: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
|
||||
u := &TestUser{Id: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
|
||||
err = indexer.Add(u)
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -66,10 +66,10 @@ func TestIndexer_AddWithNonUniqueIndexCS3(t *testing.T) {
|
||||
},
|
||||
})
|
||||
|
||||
err := indexer.AddIndex(&User{}, "UserName", "Id", "users", "non_unique")
|
||||
err := indexer.AddIndex(&TestUser{}, "UserName", "Id", "users", "non_unique")
|
||||
assert.NoError(t, err)
|
||||
|
||||
u := &User{Id: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
|
||||
u := &TestUser{Id: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
|
||||
err = indexer.Add(u)
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -86,14 +86,14 @@ func TestIndexer_FindByWithUniqueIndex(t *testing.T) {
|
||||
},
|
||||
})
|
||||
|
||||
err := indexer.AddIndex(&User{}, "UserName", "Id", "users", "unique")
|
||||
err := indexer.AddIndex(&TestUser{}, "UserName", "Id", "users", "unique")
|
||||
assert.NoError(t, err)
|
||||
|
||||
u := &User{Id: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
|
||||
u := &TestUser{Id: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
|
||||
err = indexer.Add(u)
|
||||
assert.NoError(t, err)
|
||||
|
||||
res, err := indexer.FindBy(User{}, "UserName", "mikey")
|
||||
res, err := indexer.FindBy(TestUser{}, "UserName", "mikey")
|
||||
assert.NoError(t, err)
|
||||
t.Log(res)
|
||||
|
||||
@@ -195,14 +195,14 @@ func TestIndexer_UpdateWithUniqueIndex(t *testing.T) {
|
||||
},
|
||||
})
|
||||
|
||||
err := indexer.AddIndex(&User{}, "UserName", "Id", "users", "unique")
|
||||
err := indexer.AddIndex(&TestUser{}, "UserName", "Id", "users", "unique")
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = indexer.AddIndex(&User{}, "Email", "Id", "users", "unique")
|
||||
err = indexer.AddIndex(&TestUser{}, "Email", "Id", "users", "unique")
|
||||
assert.NoError(t, err)
|
||||
|
||||
user1 := &User{Id: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
|
||||
user2 := &User{Id: "hijklmn-456", UserName: "frank", Email: "frank@example.com"}
|
||||
user1 := &TestUser{Id: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
|
||||
user2 := &TestUser{Id: "hijklmn-456", UserName: "frank", Email: "frank@example.com"}
|
||||
|
||||
err = indexer.Add(user1)
|
||||
assert.NoError(t, err)
|
||||
@@ -210,33 +210,33 @@ func TestIndexer_UpdateWithUniqueIndex(t *testing.T) {
|
||||
err = indexer.Add(user2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = indexer.Update(user1, &User{
|
||||
err = indexer.Update(user1, &TestUser{
|
||||
Id: "abcdefg-123",
|
||||
UserName: "mikey-new",
|
||||
Email: "mikey@example.com",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
v, err1 := indexer.FindBy(&User{}, "UserName", "mikey-new")
|
||||
v, err1 := indexer.FindBy(&TestUser{}, "UserName", "mikey-new")
|
||||
assert.NoError(t, err1)
|
||||
assert.Len(t, v, 1)
|
||||
v, err2 := indexer.FindBy(&User{}, "UserName", "mikey")
|
||||
v, err2 := indexer.FindBy(&TestUser{}, "UserName", "mikey")
|
||||
assert.NoError(t, err2)
|
||||
assert.Len(t, v, 0)
|
||||
|
||||
err1 = indexer.Update(&User{
|
||||
err1 = indexer.Update(&TestUser{
|
||||
Id: "abcdefg-123",
|
||||
UserName: "mikey-new",
|
||||
Email: "mikey@example.com",
|
||||
}, &User{
|
||||
}, &TestUser{
|
||||
Id: "abcdefg-123",
|
||||
UserName: "mikey-newest",
|
||||
Email: "mikey-new@example.com",
|
||||
})
|
||||
assert.NoError(t, err1)
|
||||
fbUserName, err2 := indexer.FindBy(&User{}, "UserName", "mikey-newest")
|
||||
fbUserName, err2 := indexer.FindBy(&TestUser{}, "UserName", "mikey-newest")
|
||||
assert.NoError(t, err2)
|
||||
assert.Len(t, fbUserName, 1)
|
||||
fbEmail, err3 := indexer.FindBy(&User{}, "Email", "mikey-new@example.com")
|
||||
fbEmail, err3 := indexer.FindBy(&TestUser{}, "Email", "mikey-new@example.com")
|
||||
assert.NoError(t, err3)
|
||||
assert.Len(t, fbEmail, 1)
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
type TestUser struct {
|
||||
Id, UserName, Email string
|
||||
}
|
||||
|
||||
@@ -18,10 +18,10 @@ type TestPet struct {
|
||||
|
||||
var TestData = map[string][]interface{}{
|
||||
"users": {
|
||||
User{Id: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"},
|
||||
User{Id: "hijklmn-456", UserName: "frank", Email: "frank@example.com"},
|
||||
User{Id: "ewf4ofk-555", UserName: "jacky", Email: "jacky@example.com"},
|
||||
User{Id: "rulan54-777", UserName: "jones", Email: "jones@example.com"},
|
||||
TestUser{Id: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"},
|
||||
TestUser{Id: "hijklmn-456", UserName: "frank", Email: "frank@example.com"},
|
||||
TestUser{Id: "ewf4ofk-555", UserName: "jacky", Email: "jacky@example.com"},
|
||||
TestUser{Id: "rulan54-777", UserName: "jones", Email: "jones@example.com"},
|
||||
},
|
||||
"pets": {
|
||||
TestPet{Id: "rebef-123", Kind: "Dog", Color: "Brown", Name: "Waldo"},
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"path"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -21,3 +24,29 @@ func ValueOf(v interface{}, field string) string {
|
||||
|
||||
return f.String()
|
||||
}
|
||||
|
||||
func getType(v interface{}) (reflect.Value, error) {
|
||||
rv := reflect.ValueOf(v)
|
||||
for rv.Kind() == reflect.Ptr || rv.Kind() == reflect.Interface {
|
||||
rv = rv.Elem()
|
||||
}
|
||||
if !rv.IsValid() {
|
||||
return reflect.Value{}, errors.New("failed to read value via reflection")
|
||||
}
|
||||
|
||||
return rv, nil
|
||||
}
|
||||
|
||||
func GetTypeFQN(t interface{}) string {
|
||||
typ, _ := getType(t)
|
||||
typeName := path.Join(typ.Type().PkgPath(), typ.Type().Name())
|
||||
typeName = strings.ReplaceAll(typeName, "/", ".")
|
||||
return typeName
|
||||
}
|
||||
|
||||
func valueOf(v interface{}, field string) string {
|
||||
r := reflect.ValueOf(v)
|
||||
f := reflect.Indirect(r).FieldByName(field)
|
||||
|
||||
return f.String()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user