From bafcb64a1a0b8168a477664ab719b486a5077a22 Mon Sep 17 00:00:00 2001 From: Daniele Briggi Date: Thu, 13 Jun 2024 16:05:30 +0000 Subject: [PATCH] feat(compression): enabled by default --- cli/sqlc.go | 3 +- connection.go | 32 ++++---- test/{commons_test.go => commons.go} | 16 ++++ test/server_test.go | 14 ++++ test/unit/connection_test.go | 108 ++++++++++++++++++++++++++- 5 files changed, 154 insertions(+), 19 deletions(-) rename test/{commons_test.go => commons.go} (53%) diff --git a/cli/sqlc.go b/cli/sqlc.go index aa7ec7f..97d6521 100644 --- a/cli/sqlc.go +++ b/cli/sqlc.go @@ -317,7 +317,7 @@ func parseParameters() (Parameter, error) { p["--host"] = getFirstNoneEmptyString([]string{dropError(p.String("--host")), "localhost"}) p["--port"] = getFirstNoneEmptyString([]string{dropError(p.String("--port")), "8860"}) p["--timeout"] = getFirstNoneEmptyString([]string{dropError(p.String("--timeout")), "10"}) - p["--compress"] = getFirstNoneEmptyString([]string{dropError(p.String("--compress")), "NO"}) + p["--compress"] = getFirstNoneEmptyString([]string{dropError(p.String("--compress")), sqlitecloud.CompressModeLZ4}) p["--tls"] = getFirstNoneEmptyString([]string{dropError(p.String("--tls")), "YES"}) p["--separator"] = getFirstNoneEmptyString([]string{dropError(p.String("--separator")), dropError(sqlitecloud.GetDefaultSeparatorForOutputFormat(outputformat)), "|"}) p["--maxdata"] = getFirstNoneEmptyString([]string{dropError(p.String("--maxdata")), "0"}) @@ -420,6 +420,7 @@ func main() { Password: parameter.Password, Database: parameter.Database, Timeout: time.Duration(parameter.Timeout) * time.Second, + Compression: parameter.Compress == sqlitecloud.CompressModeLZ4, CompressMode: parameter.Compress, Zerotext: parameter.Zerotext, Memory: parameter.Memory, diff --git a/connection.go b/connection.go index 6c7db29..d8efdf5 100644 --- a/connection.go +++ b/connection.go @@ -113,8 +113,8 @@ func ParseConnectionString(ConnectionString string) (config *SQCloudConfig, err config.Password, _ = u.User.Password() config.Database = strings.TrimPrefix(u.Path, "/") config.Timeout = 0 - config.Compression = false - config.CompressMode = CompressModeNo + config.Compression = true // enabled by default + config.CompressMode = CompressModeLZ4 config.Zerotext = false config.Memory = false config.Create = false @@ -146,14 +146,18 @@ func ParseConnectionString(ConnectionString string) (config *SQCloudConfig, err } case "compress": - config.CompressMode = strings.ToUpper(lastLiteral) - if config.CompressMode == CompressModeLZ4 { - config.Compression = true + mode := strings.ToUpper(lastLiteral) + if mode == CompressModeNo { + config.CompressMode = CompressModeNo + config.Compression = false + } else if enable, err := parseBool(mode, config.Compression); err == nil && !enable { + config.CompressMode = CompressModeNo + config.Compression = false } case "compression": - if b, err := parseBool(lastLiteral, config.Compression); err == nil && b { - config.Compression = true - config.CompressMode = CompressModeLZ4 + if enable, err := parseBool(lastLiteral, config.Compression); err == nil && !enable { + config.Compression = false + config.CompressMode = CompressModeNo } case "zerotext": if b, err := parseBool(lastLiteral, config.Zerotext); err == nil { @@ -239,12 +243,12 @@ func (this *SQCloud) CheckConnectionParameter() error { return fmt.Errorf("Invalid hostname (%s)", this.Host) } - ip := net.ParseIP(this.Host) - if ip == nil { - if _, err := net.LookupHost(this.Host); err != nil { - return errors.New(fmt.Sprintf("Can't resolve hostname (%s)", this.Host)) - } - } + // ip := net.ParseIP(this.Host) + // if ip == nil { + // if _, err := net.LookupHost(this.Host); err != nil { + // return errors.New(fmt.Sprintf("Can't resolve hostname (%s)", this.Host)) + // } + // } if this.Port == 0 { this.Port = 8860 diff --git a/test/commons_test.go b/test/commons.go similarity index 53% rename from test/commons_test.go rename to test/commons.go index ca4fd5b..c172604 100644 --- a/test/commons_test.go +++ b/test/commons.go @@ -4,8 +4,10 @@ import ( "flag" "log" "os" + "testing" "github.com/joho/godotenv" + sqlitecloud "github.com/sqlitecloud/go-sdk" ) func init() { @@ -25,3 +27,17 @@ func contains[T comparable](s []T, e T) bool { } return false } + +func setupDatabase(t *testing.T) (*sqlitecloud.SQCloud, func()) { + connectionString, _ := os.LookupEnv("SQLITE_CONNECTION_STRING") + apikey, _ := os.LookupEnv("SQLITE_API_KEY") + + db, err := sqlitecloud.Connect(connectionString + "?apikey=" + apikey) + if err != nil { + t.Fatal("Connection error: ", err.Error()) + } + + return db, func() { + db.Close() + } +} diff --git a/test/server_test.go b/test/server_test.go index 353aeb7..85dd9dc 100644 --- a/test/server_test.go +++ b/test/server_test.go @@ -24,6 +24,7 @@ import ( "testing" sqlitecloud "github.com/sqlitecloud/go-sdk" + "github.com/stretchr/testify/assert" ) const testDbnameServer = "test-gosdk-server-db.sqlite" @@ -213,3 +214,16 @@ func TestServer(t *testing.T) { // } // fmt.Printf( "ok.\r\n" ) } + +func TestCompressionEnabledByDefault(t *testing.T) { + db, cleaup := setupDatabase(t) + defer cleaup() + + result, _ := db.Select("GET CLIENT KEY COMPRESSION") + value, err := result.GetString() + if err != nil { + t.Fatal(err.Error()) + } + + assert.Equal(t, "1", value) +} diff --git a/test/unit/connection_test.go b/test/unit/connection_test.go index 4d795de..8c58256 100644 --- a/test/unit/connection_test.go +++ b/test/unit/connection_test.go @@ -20,7 +20,8 @@ func TestParseConnectionString(t *testing.T) { Password: "", Database: "mydatabase", Timeout: time.Duration(11) * time.Second, - CompressMode: "YES", + Compression: true, + CompressMode: sqlitecloud.CompressModeLZ4, Secure: true, TlsInsecureSkipVerify: false, Pem: "", @@ -50,7 +51,8 @@ func TestParseConnectionStringWithAPIKey(t *testing.T) { Password: "pass", Database: "dbname", Timeout: time.Duration(11) * time.Second, - CompressMode: "TRUE", + Compression: true, + CompressMode: sqlitecloud.CompressModeLZ4, Secure: true, TlsInsecureSkipVerify: false, Pem: "", @@ -104,8 +106,8 @@ func TestParseConnectionStringWithParameters(t *testing.T) { { param: "compression", configParam: "Compression", - value: "true", - expectedValue: true, + value: "false", + expectedValue: false, }, { param: "zerotext", @@ -203,6 +205,104 @@ func TestParseConnectionStringWithParameters(t *testing.T) { } } +func TestParseConnectionStringCompressionCombinations(t *testing.T) { + tests := []struct { + param string + configParam string + value string + expectedValue any + }{ + { + param: "compression", + configParam: "Compression", + value: "false", + expectedValue: false, + }, + { + param: "compression", + configParam: "Compression", + value: "disabled", + expectedValue: false, + }, + { + param: "compression", + configParam: "Compression", + // value not supported + value: "no", + expectedValue: true, + }, + { + param: "compression", + configParam: "Compression", + value: "0", + expectedValue: false, + }, + { + param: "compression", + configParam: "Compression", + value: "true", + expectedValue: true, + }, + { + param: "compression", + configParam: "Compression", + value: "enabled", + expectedValue: true, + }, + { + param: "compression", + configParam: "Compression", + value: "yes", + expectedValue: true, + }, + { + param: "compression", + configParam: "Compression", + value: "1", + expectedValue: true, + }, + { + param: "compress", + configParam: "CompressMode", + value: "lz4", + expectedValue: sqlitecloud.CompressModeLZ4, + }, + { + param: "compress", + configParam: "Compression", + value: "lz4", + expectedValue: true, + }, + { + param: "compress", + configParam: "CompressMode", + value: "no", + expectedValue: sqlitecloud.CompressModeNo, + }, + { + param: "compress", + configParam: "Compression", + value: "no", + expectedValue: false, + }, + } + + for _, tt := range tests { + t.Run(tt.param, func(t *testing.T) { + config, err := sqlitecloud.ParseConnectionString("sqlitecloud://myhost.sqlite.cloud/mydatabase?" + tt.param + "=" + tt.value) + + assert.NoError(t, err) + + actualValue := reflect.ValueOf(*config).FieldByName(tt.configParam) + if !actualValue.IsValid() { + t.Fatalf("Field %s not found in config", tt.configParam) + } else { + assert.Equal(t, tt.expectedValue, actualValue.Interface()) + } + }) + } +} + func TestParseConnectionStringWithTLSParameter(t *testing.T) { tests := []struct { tlsValue string