refactor: minor code rabbit changes

This commit is contained in:
luk3skyw4lker
2024-06-20 18:47:16 -03:00
parent ad436b5d82
commit c70e44944d
2 changed files with 81 additions and 25 deletions

View File

@@ -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()

View File

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