Add failing test for indexer on autoincrement index

This commit is contained in:
Benedikt Kulmann
2020-10-15 15:36:06 +02:00
committed by A.Unger
parent d578a2603a
commit 6d39cedf08
6 changed files with 99 additions and 63 deletions
@@ -127,11 +127,16 @@ func (idx AutoincrementIndex) Lookup(v string) ([]string, error) {
}
func (idx AutoincrementIndex) Add(id, v string) (string, error) {
next, err := idx.next()
if err != nil {
return "", err
var newName string
if v == "" {
next, err := idx.next()
if err != nil {
return "", err
}
newName = path.Join(idx.indexRootDir, strconv.Itoa(next))
} else {
newName = path.Join(idx.indexRootDir, v)
}
newName := path.Join(idx.indexRootDir, strconv.Itoa(next))
if err := idx.createSymlink(id, newName); err != nil {
if os.IsExist(err) {
return "", &idxerrs.AlreadyExistsErr{TypeName: idx.typeName, Key: idx.indexBy, Value: v}
@@ -12,19 +12,7 @@ import (
func TestAutoincrementIndexAdd(t *testing.T) {
dataDir := WriteIndexTestDataCS3(t, Data, "ID")
cfg := config.Config{
Repo: config.Repo{
Disk: config.Disk{
Path: "",
},
CS3: config.CS3{
ProviderAddr: "0.0.0.0:9215",
DataURL: "http://localhost:9216",
DataPrefix: "data",
JWTSecret: "Pive-Fumkiu4",
},
},
}
cfg := generateConfig()
sut := NewAutoincrementIndex(
option.WithTypeName(GetTypeFQN(User{})),
@@ -49,19 +37,7 @@ func TestAutoincrementIndexAdd(t *testing.T) {
func BenchmarkAutoincrementIndexAdd(b *testing.B) {
dataDir := WriteIndexBenchmarkDataCS3(b, Data, "ID")
cfg := config.Config{
Repo: config.Repo{
Disk: config.Disk{
Path: "",
},
CS3: config.CS3{
ProviderAddr: "0.0.0.0:9215",
DataURL: "http://localhost:9216",
DataPrefix: "data",
JWTSecret: "Pive-Fumkiu4",
},
},
}
cfg := generateConfig()
sut := NewAutoincrementIndex(
option.WithTypeName(GetTypeFQN(User{})),
@@ -85,3 +61,19 @@ func BenchmarkAutoincrementIndexAdd(b *testing.B) {
_ = os.RemoveAll(dataDir)
}
func generateConfig() config.Config {
return config.Config{
Repo: config.Repo{
Disk: config.Disk{
Path: "",
},
CS3: config.CS3{
ProviderAddr: "0.0.0.0:9215",
DataURL: "http://localhost:9216",
DataPrefix: "data",
JWTSecret: "Pive-Fumkiu4",
},
},
}
}
@@ -101,7 +101,12 @@ func (idx Autoincrement) Lookup(v string) ([]string, error) {
func (idx Autoincrement) Add(id, v string) (string, error) {
oldName := filepath.Join(idx.filesDir, id)
newName := filepath.Join(idx.indexRootDir, strconv.Itoa(idx.next()))
var newName string
if v == "" {
newName = filepath.Join(idx.indexRootDir, strconv.Itoa(idx.next()))
} else {
newName = filepath.Join(idx.indexRootDir, v)
}
err := os.Symlink(oldName, newName)
if errors.Is(err, os.ErrExist) {
return "", &idxerrs.AlreadyExistsErr{TypeName: idx.typeName, Key: idx.indexBy, Value: v}
+64 -30
View File
@@ -7,31 +7,12 @@ import (
. "github.com/owncloud/ocis/accounts/pkg/indexer/test"
"github.com/stretchr/testify/assert"
"os"
"path"
"testing"
)
func TestIndexer_AddWithUniqueIndex(t *testing.T) {
dataDir := WriteIndexTestData(t, Data, "ID")
indexer := CreateIndexer(&config.Config{
Repo: config.Repo{
Disk: config.Disk{
Path: dataDir,
},
},
})
err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique")
assert.NoError(t, err)
u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
_, err = indexer.Add(u)
assert.NoError(t, err)
_ = os.RemoveAll(dataDir)
}
func TestIndexer_AddWithUniqueIndexCS3(t *testing.T) {
dir := WriteIndexTestDataCS3(t, Data, "ID")
func TestIndexer_CS3_AddWithUniqueIndex(t *testing.T) {
dataDir := WriteIndexTestDataCS3(t, Data, "ID")
indexer := CreateIndexer(&config.Config{
Repo: config.Repo{
CS3: config.CS3{
@@ -50,10 +31,10 @@ func TestIndexer_AddWithUniqueIndexCS3(t *testing.T) {
_, err = indexer.Add(u)
assert.NoError(t, err)
_ = os.RemoveAll(dir)
_ = os.RemoveAll(dataDir)
}
func TestIndexer_AddWithNonUniqueIndexCS3(t *testing.T) {
func TestIndexer_CS3_AddWithNonUniqueIndex(t *testing.T) {
dataDir := WriteIndexTestDataCS3(t, Data, "ID")
indexer := CreateIndexer(&config.Config{
Repo: config.Repo{
@@ -76,7 +57,7 @@ func TestIndexer_AddWithNonUniqueIndexCS3(t *testing.T) {
_ = os.RemoveAll(dataDir)
}
func TestIndexer_FindByWithUniqueIndex(t *testing.T) {
func TestIndexer_Disk_FindByWithUniqueIndex(t *testing.T) {
dataDir := WriteIndexTestData(t, Data, "ID")
indexer := CreateIndexer(&config.Config{
Repo: config.Repo{
@@ -100,7 +81,27 @@ func TestIndexer_FindByWithUniqueIndex(t *testing.T) {
_ = os.RemoveAll(dataDir)
}
func TestIndexer_AddWithNonUniqueIndex(t *testing.T) {
func TestIndexer_Disk_AddWithUniqueIndex(t *testing.T) {
dataDir := WriteIndexTestData(t, Data, "ID")
indexer := CreateIndexer(&config.Config{
Repo: config.Repo{
Disk: config.Disk{
Path: dataDir,
},
},
})
err := indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique")
assert.NoError(t, err)
u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
_, err = indexer.Add(u)
assert.NoError(t, err)
_ = os.RemoveAll(dataDir)
}
func TestIndexer_Disk_AddWithNonUniqueIndex(t *testing.T) {
dataDir := WriteIndexTestData(t, Data, "ID")
indexer := CreateIndexer(&config.Config{
Repo: config.Repo{
@@ -126,9 +127,42 @@ func TestIndexer_AddWithNonUniqueIndex(t *testing.T) {
assert.NoError(t, err)
t.Log(res)
_ = os.RemoveAll(dataDir)
}
func TestIndexer_DeleteWithNonUniqueIndex(t *testing.T) {
func TestIndexer_Disk_AddWithAutoincrementIndex(t *testing.T) {
dataDir := WriteIndexTestData(t, Data, "ID")
indexer := CreateIndexer(&config.Config{
Repo: config.Repo{
Disk: config.Disk{
Path: dataDir,
},
},
})
err := indexer.AddIndex(&User{}, "UID", "ID", "users", "autoincrement")
assert.NoError(t, err)
res1, err := indexer.Add(Data["users"][0])
assert.NoError(t, err)
assert.Equal(t, "UID", res1[0].Field)
assert.Equal(t, "0", path.Base(res1[0].Value))
res2, err := indexer.Add(Data["users"][1])
assert.NoError(t, err)
assert.Equal(t, "UID", res2[0].Field)
assert.Equal(t, "1", path.Base(res2[0].Value))
resFindBy, err := indexer.FindBy(User{}, "UID", "1")
assert.NoError(t, err)
assert.Equal(t, "hijklmn-456", resFindBy[0])
t.Log(resFindBy)
_ = os.RemoveAll(dataDir)
}
func TestIndexer_Disk_DeleteWithNonUniqueIndex(t *testing.T) {
dataDir := WriteIndexTestData(t, Data, "ID")
indexer := CreateIndexer(&config.Config{
Repo: config.Repo{
@@ -156,7 +190,7 @@ func TestIndexer_DeleteWithNonUniqueIndex(t *testing.T) {
_ = os.RemoveAll(dataDir)
}
func TestIndexer_SearchWithNonUniqueIndex(t *testing.T) {
func TestIndexer_Disk_SearchWithNonUniqueIndex(t *testing.T) {
dataDir := WriteIndexTestData(t, Data, "ID")
indexer := CreateIndexer(&config.Config{
Repo: config.Repo{
@@ -185,7 +219,7 @@ func TestIndexer_SearchWithNonUniqueIndex(t *testing.T) {
_ = os.RemoveAll(dataDir)
}
func TestIndexer_UpdateWithUniqueIndex(t *testing.T) {
func TestIndexer_Disk_UpdateWithUniqueIndex(t *testing.T) {
dataDir := WriteIndexTestData(t, Data, "ID")
indexer := CreateIndexer(&config.Config{
Repo: config.Repo{
@@ -243,7 +277,7 @@ func TestIndexer_UpdateWithUniqueIndex(t *testing.T) {
_ = os.RemoveAll(dataDir)
}
func TestIndexer_UpdateWithNonUniqueIndex(t *testing.T) {
func TestIndexer_Disk_UpdateWithNonUniqueIndex(t *testing.T) {
dataDir := WriteIndexTestData(t, Data, "ID")
indexer := CreateIndexer(&config.Config{
Repo: config.Repo{
+1 -1
View File
@@ -21,7 +21,7 @@ type Options struct {
ProviderAddr string
}
// WithEntity sets the JWTSecret field.
// WithEntity sets the Entity field.
func WithEntity(val interface{}) Option {
return func(o *Options) {
o.Entity = val
+1 -1
View File
@@ -11,7 +11,7 @@ import (
// CreateTmpDir creates a temporary dir for tests data.
func CreateTmpDir(t *testing.T) string {
name, err := ioutil.TempDir("/var/tmp", "testfiles-*")
name, err := ioutil.TempDir("/var/tmp", "testfiles-")
if err != nil {
t.Fatal(err)
}