Documentation
¶
Overview ¶
Package clickhouse provides a ClickHouse client implementation
Package clickhouse provides test mocks for the ClickHouse client. This file should only be imported in test files.
Index ¶
- Variables
- func EnsureDatabase(ctx context.Context, client ClientInterface, database, cluster string) error
- func TableExists(ctx context.Context, client ClientInterface, database, table string) (bool, error)
- type ClientInterface
- type Config
- type MockCall
- type MockClient
- func (m *MockClient) BulkInsert(ctx context.Context, table string, data interface{}) error
- func (m *MockClient) Execute(ctx context.Context, query string) error
- func (m *MockClient) GetCallCount(method string) int
- func (m *MockClient) QueryMany(ctx context.Context, query string, dest interface{}) error
- func (m *MockClient) QueryOne(ctx context.Context, query string, dest interface{}) error
- func (m *MockClient) Reset()
- func (m *MockClient) SetError(err error)
- func (m *MockClient) SetQueryManyResponse(data interface{})
- func (m *MockClient) SetQueryOneResponse(data interface{})
- func (m *MockClient) Start() error
- func (m *MockClient) Stop() error
- func (m *MockClient) WasCalled(method string) bool
Constants ¶
This section is empty.
Variables ¶
var ( ErrDestMustBePointerToSlice = errors.New("dest must be a pointer to a slice") ErrDataMustBeSlice = errors.New("data must be a slice") ErrClickHouseResponse = errors.New("clickhouse error") )
Define static errors
var (
ErrBothMustBeSlices = errors.New("both dest and data must be slices")
)
Define static errors
var (
ErrURLRequired = errors.New("URL is required")
)
Static errors for configuration validation
Functions ¶
func EnsureDatabase ¶
func EnsureDatabase(ctx context.Context, client ClientInterface, database, cluster string) error
EnsureDatabase creates a database if it doesn't exist
func TableExists ¶
TableExists checks if a table exists in the given database
Types ¶
type ClientInterface ¶
type ClientInterface interface {
// QueryOne executes a query and returns a single result
QueryOne(ctx context.Context, query string, dest interface{}) error
// QueryMany executes a query and returns multiple results
QueryMany(ctx context.Context, query string, dest interface{}) error
// Execute runs a query without expecting results
Execute(ctx context.Context, query string) error
// BulkInsert performs a bulk insert operation
BulkInsert(ctx context.Context, table string, data interface{}) error
// IsStorageEmpty checks if a table has any records matching the given conditions
IsStorageEmpty(ctx context.Context, table string, conditions map[string]interface{}) (bool, error)
// Start initializes the client
Start() error
// Stop closes the client
Stop() error
}
ClientInterface defines the methods for interacting with ClickHouse
type Config ¶
type Config struct {
URL string `yaml:"url" validate:"required,url"`
Cluster string `yaml:"cluster"`
LocalSuffix string `yaml:"local_suffix"`
QueryTimeout time.Duration `yaml:"query_timeout"`
InsertTimeout time.Duration `yaml:"insert_timeout"`
Debug bool `yaml:"debug"`
KeepAlive time.Duration `yaml:"keep_alive"`
AdminDatabase string `yaml:"admin_database"`
AdminTable string `yaml:"admin_table"`
}
Config contains ClickHouse connection and cluster settings
func (*Config) SetDefaults ¶
func (c *Config) SetDefaults()
SetDefaults sets default values for the configuration
type MockCall ¶
type MockCall struct {
Method string
Args []interface{}
}
MockCall represents a method call made to the mock
type MockClient ¶
type MockClient struct {
// Function fields that can be set by tests
QueryOneFunc func(ctx context.Context, query string, dest interface{}) error
QueryManyFunc func(ctx context.Context, query string, dest interface{}) error
ExecuteFunc func(ctx context.Context, query string) error
BulkInsertFunc func(ctx context.Context, table string, data interface{}) error
StartFunc func() error
StopFunc func() 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 ¶
func NewMockClient() *MockClient
NewMockClient creates a new mock client with default implementations
func (*MockClient) BulkInsert ¶
func (m *MockClient) BulkInsert(ctx context.Context, table string, data interface{}) error
BulkInsert implements ClientInterface
func (*MockClient) Execute ¶
func (m *MockClient) Execute(ctx context.Context, query string) error
Execute implements ClientInterface
func (*MockClient) GetCallCount ¶
func (m *MockClient) GetCallCount(method string) int
GetCallCount returns the number of times a method was called
func (*MockClient) QueryMany ¶
func (m *MockClient) QueryMany(ctx context.Context, query string, dest interface{}) error
QueryMany implements ClientInterface
func (*MockClient) QueryOne ¶
func (m *MockClient) QueryOne(ctx context.Context, query string, dest interface{}) error
QueryOne implements ClientInterface
func (*MockClient) SetError ¶
func (m *MockClient) SetError(err error)
SetError sets all functions to return the specified error
func (*MockClient) SetQueryManyResponse ¶
func (m *MockClient) SetQueryManyResponse(data interface{})
SetQueryManyResponse sets up the mock to return specific data for QueryMany
func (*MockClient) SetQueryOneResponse ¶
func (m *MockClient) SetQueryOneResponse(data interface{})
SetQueryOneResponse sets up the mock to return specific data for QueryOne
func (*MockClient) WasCalled ¶
func (m *MockClient) WasCalled(method string) bool
WasCalled returns true if the specified method was called