Skip to content

Commit

Permalink
✅ add internal/config/ngt test
Browse files Browse the repository at this point in the history
Signed-off-by: vankichi <kyukawa315@gmail.com>
  • Loading branch information
vankichi committed Jul 7, 2020
1 parent 38995d0 commit c6a2753
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 67 deletions.
1 change: 1 addition & 0 deletions internal/config/ngt.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type NGT struct {
EnableInMemoryMode bool `yaml:"enable_in_memory_mode" json:"enable_in_memory_mode"`
}

// Bind returns NGT object whose some string value is filed value or environment value.
func (n *NGT) Bind() *NGT {
n.IndexPath = GetActualValue(n.IndexPath)
n.DistanceType = GetActualValue(n.DistanceType)
Expand Down
188 changes: 121 additions & 67 deletions internal/config/ngt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package config

import (
"os"
"reflect"
"testing"

Expand All @@ -26,17 +27,19 @@ import (

func TestNGT_Bind(t *testing.T) {
type fields struct {
IndexPath string
Dimension int
BulkInsertChunkSize int
DistanceType string
ObjectType string
CreationEdgeSize int
SearchEdgeSize int
AutoIndexDurationLimit string
AutoIndexCheckDuration string
AutoIndexLength int
EnableInMemoryMode bool
IndexPath string
Dimension int
BulkInsertChunkSize int
DistanceType string
ObjectType string
CreationEdgeSize int
SearchEdgeSize int
AutoIndexDurationLimit string
AutoIndexCheckDuration string
AutoSaveIndexDuration string
AutoIndexLength int
InitialDelayMaxDuration string
EnableInMemoryMode bool
}
type want struct {
want *NGT
Expand All @@ -56,51 +59,100 @@ func TestNGT_Bind(t *testing.T) {
return nil
}
tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
fields: fields {
IndexPath: "",
Dimension: 0,
BulkInsertChunkSize: 0,
DistanceType: "",
ObjectType: "",
CreationEdgeSize: 0,
SearchEdgeSize: 0,
AutoIndexDurationLimit: "",
AutoIndexCheckDuration: "",
AutoIndexLength: 0,
EnableInMemoryMode: false,
},
want: want{},
checkFunc: defaultCheckFunc,
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
fields: fields {
IndexPath: "",
Dimension: 0,
BulkInsertChunkSize: 0,
DistanceType: "",
ObjectType: "",
CreationEdgeSize: 0,
SearchEdgeSize: 0,
AutoIndexDurationLimit: "",
AutoIndexCheckDuration: "",
AutoIndexLength: 0,
EnableInMemoryMode: false,
},
want: want{},
checkFunc: defaultCheckFunc,
}
}(),
*/
{
name: "return NGT when all fields contain no prefix/suffix symbol",
fields: fields{
IndexPath: "config/ngt",
Dimension: 1000,
BulkInsertChunkSize: 100,
DistanceType: "l2",
ObjectType: "float",
CreationEdgeSize: 3,
SearchEdgeSize: 5,
AutoIndexDurationLimit: "1h",
AutoIndexCheckDuration: "30m",
AutoSaveIndexDuration: "30m",
AutoIndexLength: 100,
InitialDelayMaxDuration: "1h",
EnableInMemoryMode: false,
},
want: want{
want: &NGT{
IndexPath: "config/ngt",
Dimension: 1000,
BulkInsertChunkSize: 100,
DistanceType: "l2",
ObjectType: "float",
CreationEdgeSize: 3,
SearchEdgeSize: 5,
AutoIndexDurationLimit: "1h",
AutoIndexCheckDuration: "30m",
AutoSaveIndexDuration: "30m",
AutoIndexLength: 100,
InitialDelayMaxDuration: "1h",
EnableInMemoryMode: false,
},
},
},
{
name: "return NGT with environment variable when it contains `_` as prefix and suffix",
fields: fields{
IndexPath: "_indexPath_",
Dimension: 1000,
BulkInsertChunkSize: 100,
DistanceType: "_distanceType_",
ObjectType: "_objectType_",
CreationEdgeSize: 3,
SearchEdgeSize: 5,
AutoIndexDurationLimit: "_autoIndexDurationLimit_",
AutoIndexCheckDuration: "_autoIndexCheckDuration_",
AutoSaveIndexDuration: "_autoSaveIndexDuration_",
AutoIndexLength: 100,
InitialDelayMaxDuration: "_initialDelayMaxDuration_",
EnableInMemoryMode: false,
},
beforeFunc: func() {
_ = os.Setenv("indexPath", "config/ngt")
_ = os.Setenv("distanceType", "l2")
_ = os.Setenv("objectType", "float")
_ = os.Setenv("autoIndexDurationLimit", "1h")
_ = os.Setenv("autoIndexCheckDuration", "30m")
_ = os.Setenv("autoSaveIndexDuration", "30m")
_ = os.Setenv("initialDelayMaxDuration", "1h")
},
afterFunc: func() {
_ = os.Unsetenv("indexPath")
_ = os.Unsetenv("distanceType")
_ = os.Unsetenv("objectType")
_ = os.Unsetenv("autoIndexDurationLimit")
_ = os.Unsetenv("autoIndexCheckDuration")
_ = os.Unsetenv("autoSaveIndexDuration")
_ = os.Unsetenv("initialDelayMaxDuration")
},
want: want{
want: &NGT{
IndexPath: "config/ngt",
Dimension: 1000,
BulkInsertChunkSize: 100,
DistanceType: "l2",
ObjectType: "float",
CreationEdgeSize: 3,
SearchEdgeSize: 5,
AutoIndexDurationLimit: "1h",
AutoIndexCheckDuration: "30m",
AutoSaveIndexDuration: "30m",
AutoIndexLength: 100,
InitialDelayMaxDuration: "1h",
EnableInMemoryMode: false,
},
},
},
{
name: "returns NGT when all fields are empty",
want: want{
want: new(NGT),
},
},
}

for _, test := range tests {
Expand All @@ -115,17 +167,19 @@ func TestNGT_Bind(t *testing.T) {
test.checkFunc = defaultCheckFunc
}
n := &NGT{
IndexPath: test.fields.IndexPath,
Dimension: test.fields.Dimension,
BulkInsertChunkSize: test.fields.BulkInsertChunkSize,
DistanceType: test.fields.DistanceType,
ObjectType: test.fields.ObjectType,
CreationEdgeSize: test.fields.CreationEdgeSize,
SearchEdgeSize: test.fields.SearchEdgeSize,
AutoIndexDurationLimit: test.fields.AutoIndexDurationLimit,
AutoIndexCheckDuration: test.fields.AutoIndexCheckDuration,
AutoIndexLength: test.fields.AutoIndexLength,
EnableInMemoryMode: test.fields.EnableInMemoryMode,
IndexPath: test.fields.IndexPath,
Dimension: test.fields.Dimension,
BulkInsertChunkSize: test.fields.BulkInsertChunkSize,
DistanceType: test.fields.DistanceType,
ObjectType: test.fields.ObjectType,
CreationEdgeSize: test.fields.CreationEdgeSize,
SearchEdgeSize: test.fields.SearchEdgeSize,
AutoIndexDurationLimit: test.fields.AutoIndexDurationLimit,
AutoIndexCheckDuration: test.fields.AutoIndexCheckDuration,
AutoSaveIndexDuration: test.fields.AutoSaveIndexDuration,
AutoIndexLength: test.fields.AutoIndexLength,
InitialDelayMaxDuration: test.fields.InitialDelayMaxDuration,
EnableInMemoryMode: test.fields.EnableInMemoryMode,
}

got := n.Bind()
Expand Down

0 comments on commit c6a2753

Please sign in to comment.