From a8208ed47cba9a9b67af2d9c759e7cd1a8bea005 Mon Sep 17 00:00:00 2001 From: Jesse Schmidt Date: Mon, 15 Jul 2024 12:08:27 -0700 Subject: [PATCH] add tests, fix spelling --- cmd/flags/credentials_test.go | 80 +++++++++++++++++++++++++++++++++ cmd/flags/unixtime_test.go | 42 +++++++++++++++++ cmd/writers/indexList.go | 4 +- e2e_test.go | 49 ++++++++++---------- test_utils.go => tests/utils.go | 22 +++++---- 5 files changed, 159 insertions(+), 38 deletions(-) create mode 100644 cmd/flags/credentials_test.go create mode 100644 cmd/flags/unixtime_test.go rename test_utils.go => tests/utils.go (95%) diff --git a/cmd/flags/credentials_test.go b/cmd/flags/credentials_test.go new file mode 100644 index 0000000..f4473aa --- /dev/null +++ b/cmd/flags/credentials_test.go @@ -0,0 +1,80 @@ +//go:build unit + +package flags + +import ( + "asvec/tests" + "testing" +) + +func TestCredentialsFlag_Set(t *testing.T) { + // Test setting user and password + flag := CredentialsFlag{} + err := flag.Set("username:password") + if err != nil { + t.Errorf("Error setting credentials: %v", err) + } + + // Test setting user only + err = flag.Set("username") + if err != nil { + t.Errorf("Error setting user: %v", err) + } + + // Test setting password only + err = flag.Set(":password") + if err != nil { + t.Errorf("Error setting password: %v", err) + } + + // Test setting empty value + err = flag.Set("") + if err != nil { + t.Errorf("Error setting empty value: %v", err) + } +} + +func TestCredentialsFlag_Type(t *testing.T) { + flag := CredentialsFlag{} + expected := "[:]" + actual := flag.Type() + + if expected != actual { + t.Errorf("Expected type '%s', got '%s'", expected, actual) + } +} + +func TestCredentialsFlag_String(t *testing.T) { + // Test string representation with user and password + flag := CredentialsFlag{ + User: StringOptionalFlag{Val: tests.GetStrPtr("username")}, + Password: StringOptionalFlag{Val: tests.GetStrPtr("password")}, + } + str := flag.String() + expected := "username:password" + if str != expected { + t.Errorf("Expected string '%s', got '%s'", expected, str) + } + + // Test string representation with user only + flag = CredentialsFlag{ + User: StringOptionalFlag{Val: tests.GetStrPtr("username")}, + Password: StringOptionalFlag{}, + } + str = flag.String() + expected = "username:" + if str != expected { + t.Errorf("Expected string '%s', got '%s'", expected, str) + } + + // Test string representation with empty values + flag = CredentialsFlag{ + User: StringOptionalFlag{}, + Password: StringOptionalFlag{}, + } + str = flag.String() + expected = ":" + if str != expected { + t.Errorf("Expected string '%s', got '%s'", expected, str) + } +} diff --git a/cmd/flags/unixtime_test.go b/cmd/flags/unixtime_test.go new file mode 100644 index 0000000..a5f12c0 --- /dev/null +++ b/cmd/flags/unixtime_test.go @@ -0,0 +1,42 @@ +//go:build unit + +package flags + +import ( + "testing" + "time" +) + +func TestUnixTimestampFlag_Set(t *testing.T) { + var flag UnixTimestampFlag + testTimestamp := "1609459200" // Corresponds to 2021-01-01 00:00:00 UTC + expectedTime := time.Unix(1609459200, 0) + + err := flag.Set(testTimestamp) + if err != nil { + t.Errorf("Failed to set UnixTimestampFlag: %v", err) + } + + if !flag.Time().Equal(expectedTime) { + t.Errorf("Expected time %v, got %v", expectedTime, flag.Time()) + } +} + +func TestUnixTimestampFlag_String(t *testing.T) { + expectedString := "1609459200" + var flag UnixTimestampFlag + flag.Set(expectedString) + + if flag.String() != expectedString { + t.Errorf("Expected string representation %s, got %s", expectedString, flag.String()) + } +} + +func TestUnixTimestampFlag_Type(t *testing.T) { + var flag UnixTimestampFlag + expectedType := "unix-timestamp (sec)" + + if flag.Type() != expectedType { + t.Errorf("Expected type %s, got %s", expectedType, flag.Type()) + } +} diff --git a/cmd/writers/indexList.go b/cmd/writers/indexList.go index d131b5a..ea11c56 100644 --- a/cmd/writers/indexList.go +++ b/cmd/writers/indexList.go @@ -72,8 +72,8 @@ func (itw *IndexTableWriter) AppendIndexRow(index *protos.IndexDefinition, statu {"MaxMemQueueSize", v.HnswParams.GetMaxMemQueueSize()}, {"Batch Max Records", v.HnswParams.BatchingParams.GetMaxRecords()}, {"Batch Interval", convertMillisecondToDuration(uint64(v.HnswParams.BatchingParams.GetInterval()))}, - {"Catch Max Entires", v.HnswParams.CachingParams.GetMaxEntries()}, - {"Catch Expiry", convertMillisecondToDuration(v.HnswParams.CachingParams.GetExpiry())}, + {"Cache Max Entires", v.HnswParams.CachingParams.GetMaxEntries()}, + {"Cache Expiry", convertMillisecondToDuration(v.HnswParams.CachingParams.GetExpiry())}, {"Healer Max Scan Rate / Node", v.HnswParams.HealerParams.GetMaxScanRatePerNode()}, {"Healer Max Page Size", v.HnswParams.HealerParams.GetMaxScanPageSize()}, {"Healer Re-index %", convertFloatToPercentStr(v.HnswParams.HealerParams.GetReindexPercent())}, diff --git a/e2e_test.go b/e2e_test.go index df0963e..442ad31 100644 --- a/e2e_test.go +++ b/e2e_test.go @@ -4,6 +4,7 @@ package main import ( "asvec/cmd/flags" + "asvec/tests" "context" "crypto/tls" "crypto/x509" @@ -102,7 +103,7 @@ func TestCmdSuite(t *testing.T) { suiteFlags: []string{ "--log-level debug", "--timeout 10s", - createFlagStr(flags.TLSCaFile, "docker/tls/config/tls/ca.aerospike.com.crt"), + tests.CreateFlagStr(flags.TLSCaFile, "docker/tls/config/tls/ca.aerospike.com.crt"), }, avsTLSConfig: &tls.Config{ Certificates: nil, @@ -115,9 +116,9 @@ func TestCmdSuite(t *testing.T) { suiteFlags: []string{ "--log-level debug", "--timeout 10s", - createFlagStr(flags.TLSCaFile, "docker/mtls/config/tls/ca.aerospike.com.crt"), - createFlagStr(flags.TLSCertFile, "docker/mtls/config/tls/localhost.crt"), - createFlagStr(flags.TLSKeyFile, "docker/mtls/config/tls/localhost.key"), + tests.CreateFlagStr(flags.TLSCaFile, "docker/mtls/config/tls/ca.aerospike.com.crt"), + tests.CreateFlagStr(flags.TLSCertFile, "docker/mtls/config/tls/localhost.crt"), + tests.CreateFlagStr(flags.TLSKeyFile, "docker/mtls/config/tls/localhost.key"), }, avsTLSConfig: &tls.Config{ Certificates: certificates, @@ -130,9 +131,9 @@ func TestCmdSuite(t *testing.T) { suiteFlags: []string{ "--log-level debug", "--timeout 10s", - createFlagStr(flags.TLSCaFile, "docker/auth/config/tls/ca.aerospike.com.crt"), - createFlagStr(flags.AuthUser, "admin"), - createFlagStr(flags.AuthPassword, "admin"), + tests.CreateFlagStr(flags.TLSCaFile, "docker/auth/config/tls/ca.aerospike.com.crt"), + tests.CreateFlagStr(flags.AuthUser, "admin"), + tests.CreateFlagStr(flags.AuthPassword, "admin"), }, avsCreds: avs.NewCredntialsFromUserPass("admin", "admin"), avsTLSConfig: &tls.Config{ @@ -257,7 +258,7 @@ func (suite *CmdTestSuite) TestSuccessfulCreateIndexCmd() { "index0", "test", fmt.Sprintf("index create -y --host %s -n test -i index0 -d 256 -m SQUARED_EUCLIDEAN --vector-field vector0 --index-labels model=all-MiniLM-L6-v2,foo=bar", suite.avsHostPort.String()), - NewIndexDefinitionBuilder("index0", "test", 256, protos.VectorDistanceMetric_SQUARED_EUCLIDEAN, "vector0"). + tests.NewIndexDefinitionBuilder("index0", "test", 256, protos.VectorDistanceMetric_SQUARED_EUCLIDEAN, "vector0"). WithLabels(map[string]string{"model": "all-MiniLM-L6-v2", "foo": "bar"}). Build(), }, @@ -266,7 +267,7 @@ func (suite *CmdTestSuite) TestSuccessfulCreateIndexCmd() { "index1", "test", fmt.Sprintf("index create -y --host %s -n test -i index1 -d 256 -m SQUARED_EUCLIDEAN --vector-field vector1 --storage-namespace bar --storage-set testbar s", suite.avsHostPort.String()), - NewIndexDefinitionBuilder("index1", "test", 256, protos.VectorDistanceMetric_SQUARED_EUCLIDEAN, "vector1"). + tests.NewIndexDefinitionBuilder("index1", "test", 256, protos.VectorDistanceMetric_SQUARED_EUCLIDEAN, "vector1"). WithStorageNamespace("bar"). WithStorageSet("testbar"). Build(), @@ -276,7 +277,7 @@ func (suite *CmdTestSuite) TestSuccessfulCreateIndexCmd() { "index2", "test", fmt.Sprintf("index create -y --seeds %s -n test -i index2 -d 256 -m HAMMING --vector-field vector2 --hnsw-max-edges 10 --hnsw-ef 11 --hnsw-ef-construction 12 --hnsw-max-mem-queue-size 10", suite.avsHostPort.String()), - NewIndexDefinitionBuilder("index2", "test", 256, protos.VectorDistanceMetric_HAMMING, "vector2"). + tests.NewIndexDefinitionBuilder("index2", "test", 256, protos.VectorDistanceMetric_HAMMING, "vector2"). WithHnswM(10). WithHnswEf(11). WithHnswEfConstruction(12). @@ -288,7 +289,7 @@ func (suite *CmdTestSuite) TestSuccessfulCreateIndexCmd() { "index3", "test", fmt.Sprintf("index create -y --host %s -n test -i index3 -d 256 -m COSINE --vector-field vector3 --hnsw-batch-interval 50s --hnsw-batch-max-records 100", suite.avsHostPort.String()), - NewIndexDefinitionBuilder("index3", "test", 256, protos.VectorDistanceMetric_COSINE, "vector3"). + tests.NewIndexDefinitionBuilder("index3", "test", 256, protos.VectorDistanceMetric_COSINE, "vector3"). WithHnswBatchingMaxRecord(100). WithHnswBatchingInterval(50000). Build(), @@ -298,7 +299,7 @@ func (suite *CmdTestSuite) TestSuccessfulCreateIndexCmd() { "index4", "test", fmt.Sprintf("index create -y --host %s -n test -i index4 -d 256 -m COSINE --vector-field vector4 --hnsw-cache-max-entries 1000 --hnsw-cache-expiry 10s", suite.avsHostPort.String()), - NewIndexDefinitionBuilder("index4", "test", 256, protos.VectorDistanceMetric_COSINE, "vector4"). + tests.NewIndexDefinitionBuilder("index4", "test", 256, protos.VectorDistanceMetric_COSINE, "vector4"). WithHnswCacheExpiry(10000). WithHnswCacheMaxEntries(1000). Build(), @@ -308,7 +309,7 @@ func (suite *CmdTestSuite) TestSuccessfulCreateIndexCmd() { "index5", "test", fmt.Sprintf("index create -y --host %s -n test -i index5 -d 256 -m COSINE --vector-field vector5 --hnsw-healer-max-scan-rate-per-node 1000 --hnsw-healer-max-scan-page-size 1000 --hnsw-healer-reindex-percent 10.10 --hnsw-healer-schedule-delay 10s --hnsw-healer-parallelism 10", suite.avsHostPort.String()), - NewIndexDefinitionBuilder("index5", "test", 256, protos.VectorDistanceMetric_COSINE, "vector5"). + tests.NewIndexDefinitionBuilder("index5", "test", 256, protos.VectorDistanceMetric_COSINE, "vector5"). WithHnswHealerMaxScanRatePerNode(1000). WithHnswHealerMaxScanPageSize(1000). WithHnswHealerReindexPercent(10.10). @@ -321,7 +322,7 @@ func (suite *CmdTestSuite) TestSuccessfulCreateIndexCmd() { "index6", "test", fmt.Sprintf("index create -y --host %s -n test -i index6 -d 256 -m COSINE --vector-field vector6 --hnsw-merge-parallelism 10", suite.avsHostPort.String()), - NewIndexDefinitionBuilder("index6", "test", 256, protos.VectorDistanceMetric_COSINE, "vector6"). + tests.NewIndexDefinitionBuilder("index6", "test", 256, protos.VectorDistanceMetric_COSINE, "vector6"). WithHnswMergeParallelism(10). Build(), }, @@ -351,7 +352,7 @@ func (suite *CmdTestSuite) TestSuccessfulUpdateIndexCmd() { suite.avsClient.IndexCreate(context.Background(), "test", "successful-update", "field", uint32(256), protos.VectorDistanceMetric_COSINE, nil) ns := "test" index := "successful-update" - builder := NewIndexDefinitionBuilder(index, ns, 256, protos.VectorDistanceMetric_COSINE, "field") + builder := tests.NewIndexDefinitionBuilder(index, ns, 256, protos.VectorDistanceMetric_COSINE, "field") testCases := []struct { name string indexName string // index names must be unique for the suite @@ -586,7 +587,7 @@ func (suite *CmdTestSuite) TestSuccessfulListIndexCmd() { { "single index", []*protos.IndexDefinition{ - NewIndexDefinitionBuilder( + tests.NewIndexDefinitionBuilder( "list", "test", 256, protos.VectorDistanceMetric_COSINE, "vector", ).Build(), }, @@ -603,10 +604,10 @@ func (suite *CmdTestSuite) TestSuccessfulListIndexCmd() { { "double index with set", []*protos.IndexDefinition{ - NewIndexDefinitionBuilder( + tests.NewIndexDefinitionBuilder( "list1", "test", 256, protos.VectorDistanceMetric_COSINE, "vector", ).Build(), - NewIndexDefinitionBuilder( + tests.NewIndexDefinitionBuilder( "list2", "bar", 256, protos.VectorDistanceMetric_HAMMING, "vector", ).WithSet("barset").Build(), }, @@ -625,10 +626,10 @@ func (suite *CmdTestSuite) TestSuccessfulListIndexCmd() { { "double index with set and verbose", []*protos.IndexDefinition{ - NewIndexDefinitionBuilder( + tests.NewIndexDefinitionBuilder( "list1", "test", 256, protos.VectorDistanceMetric_COSINE, "vector", ).Build(), - NewIndexDefinitionBuilder( + tests.NewIndexDefinitionBuilder( "list2", "bar", 256, protos.VectorDistanceMetric_HAMMING, "vector", ).WithSet("barset").Build(), }, @@ -647,8 +648,8 @@ func (suite *CmdTestSuite) TestSuccessfulListIndexCmd() { │ │ │ │ │ │ │ │ │ │ │ MaxMemQueueSize │ 0 │ │ │ │ │ │ │ │ │ │ │ │ │ Batch Max Records │ 100000 │ │ │ │ │ │ │ │ │ │ │ │ │ Batch Interval │ 30s │ │ -│ │ │ │ │ │ │ │ │ │ │ Catch Max Entires │ 0 │ │ -│ │ │ │ │ │ │ │ │ │ │ Catch Expiry │ 0s │ │ +│ │ │ │ │ │ │ │ │ │ │ Cache Max Entires │ 0 │ │ +│ │ │ │ │ │ │ │ │ │ │ Cache Expiry │ 0s │ │ │ │ │ │ │ │ │ │ │ │ │ Healer Max Scan Rate / Node │ 0 │ │ │ │ │ │ │ │ │ │ │ │ │ Healer Max Page Size │ 0 │ │ │ │ │ │ │ │ │ │ │ │ │ Healer Re-index % │ 0.00% │ │ @@ -666,8 +667,8 @@ func (suite *CmdTestSuite) TestSuccessfulListIndexCmd() { │ │ │ │ │ │ │ │ │ │ │ MaxMemQueueSize │ 0 │ │ │ │ │ │ │ │ │ │ │ │ │ Batch Max Records │ 100000 │ │ │ │ │ │ │ │ │ │ │ │ │ Batch Interval │ 30s │ │ -│ │ │ │ │ │ │ │ │ │ │ Catch Max Entires │ 0 │ │ -│ │ │ │ │ │ │ │ │ │ │ Catch Expiry │ 0s │ │ +│ │ │ │ │ │ │ │ │ │ │ Cache Max Entires │ 0 │ │ +│ │ │ │ │ │ │ │ │ │ │ Cache Expiry │ 0s │ │ │ │ │ │ │ │ │ │ │ │ │ Healer Max Scan Rate / Node │ 0 │ │ │ │ │ │ │ │ │ │ │ │ │ Healer Max Page Size │ 0 │ │ │ │ │ │ │ │ │ │ │ │ │ Healer Re-index % │ 0.00% │ │ diff --git a/test_utils.go b/tests/utils.go similarity index 95% rename from test_utils.go rename to tests/utils.go index 8c333ec..380b755 100644 --- a/test_utils.go +++ b/tests/utils.go @@ -1,6 +1,4 @@ -//go:build integration - -package main +package tests import ( "fmt" @@ -8,22 +6,22 @@ import ( "github.com/aerospike/avs-client-go/protos" ) -func getStrPtr(str string) *string { +func GetStrPtr(str string) *string { ptr := str return &ptr } -func getUint32Ptr(i int) *uint32 { +func GetUint32Ptr(i int) *uint32 { ptr := uint32(i) return &ptr } -func getBoolPtr(b bool) *bool { +func GetBoolPtr(b bool) *bool { ptr := b return &ptr } -func createFlagStr(name, value string) string { +func CreateFlagStr(name, value string) string { return fmt.Sprintf("--%s %s", name, value) } @@ -175,12 +173,12 @@ func (idb *IndexDefinitionBuilder) Build() *protos.IndexDefinition { }, Params: &protos.IndexDefinition_HnswParams{ HnswParams: &protos.HnswParams{ - M: getUint32Ptr(16), - EfConstruction: getUint32Ptr(100), - Ef: getUint32Ptr(100), + M: GetUint32Ptr(16), + EfConstruction: GetUint32Ptr(100), + Ef: GetUint32Ptr(100), BatchingParams: &protos.HnswBatchingParams{ - MaxRecords: getUint32Ptr(100000), - Interval: getUint32Ptr(30000), + MaxRecords: GetUint32Ptr(100000), + Interval: GetUint32Ptr(30000), }, CachingParams: &protos.HnswCachingParams{}, HealerParams: &protos.HnswHealerParams{},