Skip to content

Commit

Permalink
add tests, fix spelling
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Schmidt committed Jul 15, 2024
1 parent 808189b commit a8208ed
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 38 deletions.
80 changes: 80 additions & 0 deletions cmd/flags/credentials_test.go
Original file line number Diff line number Diff line change
@@ -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 := "<user>[:<pass>]"
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:<nil>"
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 = "<nil>:<nil>"
if str != expected {
t.Errorf("Expected string '%s', got '%s'", expected, str)
}
}
42 changes: 42 additions & 0 deletions cmd/flags/unixtime_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
4 changes: 2 additions & 2 deletions cmd/writers/indexList.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())},
Expand Down
49 changes: 25 additions & 24 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"asvec/cmd/flags"
"asvec/tests"
"context"
"crypto/tls"
"crypto/x509"
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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{
Expand Down Expand Up @@ -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(),
},
Expand All @@ -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(),
Expand All @@ -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).
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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).
Expand All @@ -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(),
},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -586,7 +587,7 @@ func (suite *CmdTestSuite) TestSuccessfulListIndexCmd() {
{
"single index",
[]*protos.IndexDefinition{
NewIndexDefinitionBuilder(
tests.NewIndexDefinitionBuilder(
"list", "test", 256, protos.VectorDistanceMetric_COSINE, "vector",
).Build(),
},
Expand All @@ -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(),
},
Expand All @@ -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(),
},
Expand All @@ -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% │ │
Expand All @@ -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% │ │
Expand Down
22 changes: 10 additions & 12 deletions test_utils.go → tests/utils.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
//go:build integration

package main
package tests

import (
"fmt"

"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)
}

Expand Down Expand Up @@ -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{},
Expand Down

0 comments on commit a8208ed

Please sign in to comment.