From c70e44944d669f8d28b951ff9dabfc9e2f675d0d Mon Sep 17 00:00:00 2001 From: luk3skyw4lker Date: Thu, 20 Jun 2024 18:47:16 -0300 Subject: [PATCH] refactor: minor code rabbit changes --- clickhouse/clickhouse_test.go | 90 ++++++++++++++++++++++++++++------- clickhouse/config.go | 16 ++++--- 2 files changed, 81 insertions(+), 25 deletions(-) diff --git a/clickhouse/clickhouse_test.go b/clickhouse/clickhouse_test.go index a98acad0..f56ddd5e 100644 --- a/clickhouse/clickhouse_test.go +++ b/clickhouse/clickhouse_test.go @@ -12,28 +12,34 @@ type TestOrBench interface { Helper() } -func getTestConnection(t TestOrBench) (*Storage, error) { +func getTestConnection(t TestOrBench, cfg Config) (*Storage, error) { t.Helper() - client, err := New(Config{ - Host: "127.0.0.1", - Port: 9001, - Engine: "Memory", - Table: "test_table", - Clean: true, - }) + client, err := New(cfg) return client, err } func Test_Connection(t *testing.T) { - _, err := getTestConnection(t) + _, err := getTestConnection(t, Config{ + Host: "127.0.0.1", + Port: 9001, + Engine: Memory, + Table: "test_table", + Clean: true, + }) require.NoError(t, err) } func Test_Set(t *testing.T) { - client, err := getTestConnection(t) + client, err := getTestConnection(t, Config{ + Host: "127.0.0.1", + Port: 9001, + Engine: Memory, + Table: "test_table", + Clean: true, + }) require.NoError(t, err) defer client.Close() @@ -42,7 +48,13 @@ func Test_Set(t *testing.T) { } func Test_SetWithExp(t *testing.T) { - client, err := getTestConnection(t) + client, err := getTestConnection(t, Config{ + Host: "127.0.0.1", + Port: 9001, + Engine: Memory, + Table: "test_table", + Clean: true, + }) require.NoError(t, err) defer client.Close() @@ -51,7 +63,13 @@ func Test_SetWithExp(t *testing.T) { } func Test_Get(t *testing.T) { - client, err := getTestConnection(t) + client, err := getTestConnection(t, Config{ + Host: "127.0.0.1", + Port: 9001, + Engine: Memory, + Table: "test_table", + Clean: true, + }) require.NoError(t, err) defer client.Close() @@ -66,7 +84,13 @@ func Test_Get(t *testing.T) { } func Test_GetWithExp(t *testing.T) { - client, err := getTestConnection(t) + client, err := getTestConnection(t, Config{ + Host: "127.0.0.1", + Port: 9001, + Engine: Memory, + Table: "test_table", + Clean: true, + }) require.NoError(t, err) defer client.Close() @@ -88,7 +112,13 @@ func Test_GetWithExp(t *testing.T) { } func Test_Delete(t *testing.T) { - client, err := getTestConnection(t) + client, err := getTestConnection(t, Config{ + Host: "127.0.0.1", + Port: 9001, + Engine: Memory, + Table: "test_table", + Clean: true, + }) require.NoError(t, err) defer client.Close() @@ -106,7 +136,13 @@ func Test_Delete(t *testing.T) { } func Test_Reset(t *testing.T) { - client, err := getTestConnection(t) + client, err := getTestConnection(t, Config{ + Host: "127.0.0.1", + Port: 9001, + Engine: Memory, + Table: "test_table", + Clean: true, + }) require.NoError(t, err) defer client.Close() @@ -127,7 +163,13 @@ func Benchmark_Clickhouse_Set(b *testing.B) { b.ReportAllocs() b.ResetTimer() - client, err := getTestConnection(b) + client, err := getTestConnection(b, Config{ + Host: "127.0.0.1", + Port: 9001, + Engine: Memory, + Table: "test_table", + Clean: true, + }) require.NoError(b, err) defer client.Close() @@ -143,7 +185,13 @@ func Benchmark_Clickhouse_Get(b *testing.B) { b.ReportAllocs() b.ResetTimer() - client, err := getTestConnection(b) + client, err := getTestConnection(b, Config{ + Host: "127.0.0.1", + Port: 9001, + Engine: Memory, + Table: "test_table", + Clean: true, + }) require.NoError(b, err) defer client.Close() @@ -161,7 +209,13 @@ func Benchmark_Clickhouse_Set_And_Delete(b *testing.B) { b.ReportAllocs() b.ResetTimer() - client, err := getTestConnection(b) + client, err := getTestConnection(b, Config{ + Host: "127.0.0.1", + Port: 9001, + Engine: Memory, + Table: "test_table", + Clean: true, + }) require.NoError(b, err) defer client.Close() diff --git a/clickhouse/config.go b/clickhouse/config.go index 6ee9ed64..9b3aa28a 100644 --- a/clickhouse/config.go +++ b/clickhouse/config.go @@ -9,17 +9,19 @@ import ( driver "github.com/ClickHouse/clickhouse-go/v2" ) +type ClickhouseEngine string + type schema struct { Value string `ch:"value"` Expiration time.Time `ch:"expiration"` } const ( - Memory = "Memory" - MergeTree = "MergeTree" - StripeLog = "StripeLog" - TinyLog = "TinyLog" - Log = "Log" + Memory ClickhouseEngine = "Memory" + MergeTree ClickhouseEngine = "MergeTree" + StripeLog ClickhouseEngine = "StripeLog" + TinyLog ClickhouseEngine = "TinyLog" + Log ClickhouseEngine = "Log" ) // Config defines configuration options for Clickhouse connection. @@ -37,7 +39,7 @@ type Config struct { // The name of the table that will store the data Table string // The engine that should be used in the table - Engine string + Engine ClickhouseEngine // Should start a clean table, default false Clean bool // TLS configuration, default nil @@ -48,7 +50,7 @@ type Config struct { Debugf func(format string, v ...any) } -func defaultConfig(configuration Config) (driver.Options, string, error) { +func defaultConfig(configuration Config) (driver.Options, ClickhouseEngine, error) { if configuration.Table == "" { return driver.Options{}, "", errors.New("table name not provided") }