clickhouse

package
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2026 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package clickhouse provides test mocks for the ClickHouse client. This file should only be imported in test files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client implements the ClientInterface using ch-go native protocol.

func (*Client) Do added in v0.1.5

func (c *Client) Do(ctx context.Context, query ch.Query) error

Do executes a query using the pool.

func (*Client) Execute added in v0.1.5

func (c *Client) Execute(ctx context.Context, query string) error

Execute runs a query without expecting results.

func (*Client) IsStorageEmpty added in v0.1.5

func (c *Client) IsStorageEmpty(ctx context.Context, table string, conditions map[string]any) (bool, error)

IsStorageEmpty checks if a table has any records matching the given conditions.

func (*Client) QueryMinMaxUInt64 added in v0.1.5

func (c *Client) QueryMinMaxUInt64(ctx context.Context, query string) (minVal, maxVal *uint64, err error)

QueryMinMaxUInt64 executes a query that returns min and max UInt64 values. The query must return columns named "min" and "max". Returns nil for both values if no rows are found.

func (*Client) QueryUInt64 added in v0.1.5

func (c *Client) QueryUInt64(ctx context.Context, query string, columnName string) (*uint64, error)

QueryUInt64 executes a query and returns a single UInt64 value from the specified column. Returns nil if no rows are found.

func (*Client) QueryUInt64Slice added in v0.1.5

func (c *Client) QueryUInt64Slice(ctx context.Context, query string, columnName string) ([]uint64, error)

QueryUInt64Slice executes a query and returns all UInt64 values from the specified column. Returns an empty slice if no rows are found.

func (*Client) SetNetwork added in v0.1.5

func (c *Client) SetNetwork(network string)

SetNetwork updates the network name for metrics labeling.

func (*Client) Start

func (c *Client) Start() error

Start initializes the client by dialing ClickHouse with retry logic.

func (*Client) Stop

func (c *Client) Stop() error

Stop closes the connection pool.

type ClientInterface added in v0.1.1

type ClientInterface interface {
	// Start initializes the client
	Start() error
	// Stop closes the client
	Stop() error
	// SetNetwork updates the network name for metrics labeling
	SetNetwork(network string)
	// Do executes a ch-go query directly for streaming operations
	Do(ctx context.Context, query ch.Query) error
	// Execute runs a query without expecting results
	Execute(ctx context.Context, query string) error
	// IsStorageEmpty checks if a table has any records matching the given conditions
	IsStorageEmpty(ctx context.Context, table string, conditions map[string]any) (bool, error)

	// QueryUInt64 executes a query and returns a single UInt64 value from the specified column.
	// Returns nil if no rows are found.
	QueryUInt64(ctx context.Context, query string, columnName string) (*uint64, error)
	// QueryMinMaxUInt64 executes a query that returns min and max UInt64 values.
	// The query must return columns named "min" and "max".
	// Returns nil for both values if no rows are found.
	QueryMinMaxUInt64(ctx context.Context, query string) (minVal, maxVal *uint64, err error)
	// QueryUInt64Slice executes a query and returns all UInt64 values from the specified column.
	// Returns an empty slice if no rows are found.
	QueryUInt64Slice(ctx context.Context, query string, columnName string) ([]uint64, error)
}

ClientInterface defines the methods for interacting with ClickHouse.

func New added in v0.1.1

func New(cfg *Config) (ClientInterface, error)

New creates a new ch-go native ClickHouse client. The client is not connected until Start() is called.

type Config

type Config struct {
	// Connection
	Addr     string `yaml:"addr"`     // Native protocol address, e.g., "localhost:9000"
	Database string `yaml:"database"` // Database name, default: "default"
	Username string `yaml:"username"`
	Password string `yaml:"password"` //nolint:gosec // G117: config field, not a hardcoded secret

	// Pool settings
	MaxConns          int32         `yaml:"max_conns"`           // Maximum connections in pool, default: 10
	MinConns          int32         `yaml:"min_conns"`           // Minimum connections in pool, default: 2
	ConnMaxLifetime   time.Duration `yaml:"conn_max_lifetime"`   // Maximum connection lifetime, default: 1h
	ConnMaxIdleTime   time.Duration `yaml:"conn_max_idle_time"`  // Maximum idle time, default: 30m
	HealthCheckPeriod time.Duration `yaml:"health_check_period"` // Health check period, default: 1m
	DialTimeout       time.Duration `yaml:"dial_timeout"`        // Dial timeout, default: 10s

	// Performance
	Compression string `yaml:"compression"` // Compression: lz4, zstd, none (default: lz4)

	// Retry settings
	MaxRetries     int           `yaml:"max_retries"`      // Maximum retry attempts, default: 3
	RetryBaseDelay time.Duration `yaml:"retry_base_delay"` // Base delay for exponential backoff, default: 100ms
	RetryMaxDelay  time.Duration `yaml:"retry_max_delay"`  // Max delay between retries, default: 10s

	// Timeout settings
	QueryTimeout time.Duration `yaml:"query_timeout"` // Query timeout per attempt, default: 60s

	// Metrics labels
	Network   string `yaml:"network"`
	Processor string `yaml:"processor"`
	Debug     bool   `yaml:"debug"`
}

Config holds configuration for ClickHouse ch-go native client.

func (*Config) SetDefaults added in v0.1.1

func (c *Config) SetDefaults()

SetDefaults sets default values for unset fields.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks if the config is valid.

type MockCall added in v0.1.1

type MockCall struct {
	Method string
	Args   []any
}

MockCall represents a method call made to the mock.

type MockClient added in v0.1.1

type MockClient struct {
	// Function fields that can be set by tests
	ExecuteFunc           func(ctx context.Context, query string) error
	IsStorageEmptyFunc    func(ctx context.Context, table string, conditions map[string]any) (bool, error)
	StartFunc             func() error
	StopFunc              func() error
	DoFunc                func(ctx context.Context, query ch.Query) error
	QueryUInt64Func       func(ctx context.Context, query string, columnName string) (*uint64, error)
	QueryMinMaxUInt64Func func(ctx context.Context, query string) (minVal, maxVal *uint64, err error)
	QueryUInt64SliceFunc  func(ctx context.Context, query string, columnName string) ([]uint64, error)

	// Track calls for assertions
	Calls []MockCall
}

MockClient is a mock implementation of ClientInterface for testing. It should only be used in test files, not in production code.

func NewMockClient added in v0.1.1

func NewMockClient() *MockClient

NewMockClient creates a new mock client with default implementations.

func (*MockClient) Do added in v0.1.5

func (m *MockClient) Do(ctx context.Context, query ch.Query) error

Do implements ClientInterface.

func (*MockClient) Execute added in v0.1.1

func (m *MockClient) Execute(ctx context.Context, query string) error

Execute implements ClientInterface.

func (*MockClient) GetCallCount added in v0.1.1

func (m *MockClient) GetCallCount(method string) int

GetCallCount returns the number of times a method was called.

func (*MockClient) IsStorageEmpty added in v0.1.5

func (m *MockClient) IsStorageEmpty(ctx context.Context, table string, conditions map[string]any) (bool, error)

IsStorageEmpty implements ClientInterface.

func (*MockClient) QueryMinMaxUInt64 added in v0.1.5

func (m *MockClient) QueryMinMaxUInt64(ctx context.Context, query string) (minVal, maxVal *uint64, err error)

QueryMinMaxUInt64 implements ClientInterface.

func (*MockClient) QueryUInt64 added in v0.1.5

func (m *MockClient) QueryUInt64(ctx context.Context, query string, columnName string) (*uint64, error)

QueryUInt64 implements ClientInterface.

func (*MockClient) QueryUInt64Slice added in v0.1.5

func (m *MockClient) QueryUInt64Slice(ctx context.Context, query string, columnName string) ([]uint64, error)

QueryUInt64Slice implements ClientInterface.

func (*MockClient) Reset added in v0.1.1

func (m *MockClient) Reset()

Reset clears all recorded calls.

func (*MockClient) SetError added in v0.1.1

func (m *MockClient) SetError(err error)

SetError sets all functions to return the specified error.

func (*MockClient) SetNetwork added in v0.1.5

func (m *MockClient) SetNetwork(network string)

SetNetwork implements ClientInterface.

func (*MockClient) SetQueryMinMaxUInt64Response added in v0.1.5

func (m *MockClient) SetQueryMinMaxUInt64Response(minVal, maxVal *uint64)

SetQueryMinMaxUInt64Response sets up the mock to return specific values for QueryMinMaxUInt64.

func (*MockClient) SetQueryUInt64Response added in v0.1.5

func (m *MockClient) SetQueryUInt64Response(value *uint64)

SetQueryUInt64Response sets up the mock to return a specific value for QueryUInt64.

func (*MockClient) Start added in v0.1.1

func (m *MockClient) Start() error

Start implements ClientInterface.

func (*MockClient) Stop added in v0.1.1

func (m *MockClient) Stop() error

Stop implements ClientInterface.

func (*MockClient) WasCalled added in v0.1.1

func (m *MockClient) WasCalled(method string) bool

WasCalled returns true if the specified method was called.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL