mockhouse

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

README

ClickHouse-go-mock

A mock library implementing support for clickhouse-go/v2/lib/driver

Install

go get github.com/srikanthccv/ClickHouse-go-mock

Quick Start


package main

import (
	"context"
	"log"

	"github.com/ClickHouse/clickhouse-go/v2"
	cmock "github.com/srikanthccv/ClickHouse-go-mock"
)

type Video struct {
	Name    string `db:"name"`
	Title   string `db:"title"`
	Content string `db:"content"`
}

func fetchVideos(conn clickhouse.Conn) (*Video, error) {
	var video Video
	if _, err := conn.Query(context.TODO(), "SELECT name, title, content FROM videos WHERE name LIKE ?", "%Cocomelon%"); err != nil {
		return nil, err
	}
	return &video, nil
}

func main() {
	mock, err := cmock.NewClickHouseNative(nil)
	if err != nil {
		log.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
	}

	mock.ExpectQuery("SELECT name, title, content FROM videos WHERE name LIKE ?").WithArgs("%Cocomelon%")
	_, err = fetchVideos(mock)
	if err != nil {
		log.Fatalf("an error '%s' was not expected when querying a statement", err)
	}

	if err := mock.ExpectationsWereMet(); err != nil {
		log.Fatalf("there were unfulfilled expectations: %s", err)
	}
}

Documentation

Please see the package documentation at godoc.org.

And tests are the best documentation, see clickconnmock_test.go.

License

The Apache License, Version 2.0 - see LICENSE for more details.

Credits

This library is built on top of clickhouse-go and sqlmock

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClickHouseNative

func NewClickHouseNative(options *clickhouse.Options) (*clickhousemock, error)

NewClickHouseNative creates clickhousemock database mock to manage expectations.

func NewClickHouseWithQueryMatcher added in v0.6.0

func NewClickHouseWithQueryMatcher(
	options *clickhouse.Options,
	queryMatcher sqlmock.QueryMatcher,
) (*clickhousemock, error)

func NewColumnType

func NewColumnType(name string, chType string, nullable bool, scanType reflect.Type) driver.ColumnType

Types

type ClickConnMockCommon

type ClickConnMockCommon interface {
	// ExpectClose queues an expectation for this database
	// action to be triggered. the *ExpectedClose allows
	// to mock database response
	ExpectClose() *ExpectedClose

	// ExpectStats queues an expectation for this database
	// action to be triggered. the *ExpectedStats allows
	// to mock database response
	ExpectStats() *ExpectedStats

	// ExpectPing expects *driver.Conn.Ping to be called.
	// the *ExpectedPing allows to mock database response
	ExpectPing() *ExpectedPing

	// ExpectAsyncInsert expects *driver.Conn.AsyncInsert to be called.
	// the *ExpectedAsyncInsert allows to mock database response
	ExpectAsyncInsert(expectedSQL string, expectedWait bool) *ExpectedAsyncInsert

	// ExpectExec expects Exec() to be called with expectedSQL query.
	// the *ExpectedExec allows to mock database response
	ExpectExec(expectedSQL string) *ExpectedExec

	// ExpectPrepareBatch expects PrepareBatch() to be called with expectedSQL query.
	// the *ExpectedPrepareBatch allows to mock database response.
	// Note that you may expect Append() or AppendStruct() on the *ExpectedPrepareBatch
	// statement to prevent repeating expectedSQL
	ExpectPrepareBatch(expectedSQL string) *ExpectedPrepareBatch

	// ExpectQueryRow expects QueryRow() to be called with expectedSQL query.
	// the *ExpectedQuery allows to mock database response.
	ExpectQueryRow(expectedSQL string) *ExpectedQueryRow

	// ExpectQuery expects Query() to be called with expectedSQL query.
	// the *ExpectedQuery allows to mock database response.
	ExpectQuery(expectedSQL string) *ExpectedQuery

	// ExpectSelect expects Select() to be called with expectedSQL query.
	// the *ExpectedSelect allows to mock database response.
	ExpectSelect(expectedSQL string) *ExpectedSelect

	// ExpectServerVersion queues an expectation for this database
	// action to be triggered. the *ExpectedServerVersion allows
	// to mock database response
	ExpectServerVersion() *ExpectedServerVersion

	// ExpectContributors queues an expectation for this database
	// action to be triggered. the *ExpectedContributors allows
	// to mock database response
	ExpectContributors() *ExpectedContributors

	// ExpectationsWereMet checks whether all queued expectations
	// were met in order. If any of them was not met - an error is returned.
	ExpectationsWereMet() error

	// MatchExpectationsInOrder gives an option whether to match all
	// expectations in the order they were set or not.
	//
	// By default it is set to true. But if you use goroutines
	// to parallelize your query executation, that option may
	// be handy.
	//
	// This option may be turned on anytime during tests. As soon
	// as it is switched to false, expectations will be matched
	// in any order. Or otherwise if switched to true, any unmatched
	// expectations will be expected in order
	MatchExpectationsInOrder(bool)
}

ClickConnMockCommon interface serves to create expectations for any kind of database action in order to mock and test real database behavior.

type ColumnType added in v0.2.0

type ColumnType struct {
	Name string
	Type column.Type
}

type ExpectedAbort

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

func (*ExpectedAbort) String

func (e *ExpectedAbort) String() string

func (*ExpectedAbort) WillReturnError

func (e *ExpectedAbort) WillReturnError(err error) *ExpectedAbort

WillReturnError allows to set an error for the expected *batch.Abort action.

type ExpectedAppend

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

func (*ExpectedAppend) String

func (e *ExpectedAppend) String() string

func (*ExpectedAppend) WillReturnError

func (e *ExpectedAppend) WillReturnError(err error) *ExpectedAppend

WillReturnError allows to set an error for the expected *batch.Append action.

type ExpectedAppendStruct

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

func (*ExpectedAppendStruct) String

func (e *ExpectedAppendStruct) String() string

func (*ExpectedAppendStruct) WillReturnError

func (e *ExpectedAppendStruct) WillReturnError(err error) *ExpectedAppendStruct

WillReturnError allows to set an error for the expected *batch.AppendStruct action.

type ExpectedAsyncInsert

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

func (*ExpectedAsyncInsert) String

func (e *ExpectedAsyncInsert) String() string

func (*ExpectedAsyncInsert) WillDelay

WillDelay allows to set a delay for the expected *Conn.AsyncInsert action.

func (*ExpectedAsyncInsert) WillReturnError

func (e *ExpectedAsyncInsert) WillReturnError(err error) *ExpectedAsyncInsert

WillReturnError allows to set an error for the expected *Conn.AsyncInsert action.

func (*ExpectedAsyncInsert) WillWait

WillWait allows to set a wait for the expected *Conn.AsyncInsert action.

type ExpectedClose

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

ExpectedClose is used to manage *driver.Conn.Close expectation returned by *clickhousemock.ExpectClose.

func (*ExpectedClose) String

func (e *ExpectedClose) String() string

String returns string representation

func (*ExpectedClose) WillReturnError

func (e *ExpectedClose) WillReturnError(err error) *ExpectedClose

WillReturnError allows to set an error for *driver.Conn.Close action

type ExpectedColumn

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

func (*ExpectedColumn) String

func (e *ExpectedColumn) String() string

func (*ExpectedColumn) WillReturnBatchColumn

func (e *ExpectedColumn) WillReturnBatchColumn(batchcolumn clikhouseDriver.BatchColumn) *ExpectedColumn

WillReturnBatchColumn allows to set an error for the expected *batch.Column action.

type ExpectedContributors

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

func (*ExpectedContributors) String

func (e *ExpectedContributors) String() string

func (*ExpectedContributors) WillReturnContributors

func (e *ExpectedContributors) WillReturnContributors(contributors ...string) *ExpectedContributors

WillReturnContributors allows to set an error for the expected *Conn.Contributors action.

type ExpectedExec

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

ExpectedExec is used to manage *driver.Conn.Exec expectations. Returned by *clickhousemock.ExpectExec.

func (*ExpectedExec) String

func (e *ExpectedExec) String() string

String returns string representation

func (*ExpectedExec) WillDelayFor

func (e *ExpectedExec) WillDelayFor(duration time.Duration) *ExpectedExec

WillDelayFor allows to specify duration for which it will delay result. May be used together with Context

func (*ExpectedExec) WillReturnError

func (e *ExpectedExec) WillReturnError(err error) *ExpectedExec

WillReturnError allows to set an error for expected database exec action

func (*ExpectedExec) WithArgs

func (e *ExpectedExec) WithArgs(args ...any) *ExpectedExec

WithArgs will match given expected args to actual database exec operation arguments. if at least one argument does not match, it will return an error. For specific arguments an clickhousemock.Argument interface can be used to match an argument.

type ExpectedFlush

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

func (*ExpectedFlush) String

func (e *ExpectedFlush) String() string

func (*ExpectedFlush) WillReturnError

func (e *ExpectedFlush) WillReturnError(err error) *ExpectedFlush

WillReturnError allows to set an error for the expected *batch.Flush action.

type ExpectedIsSent

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

func (*ExpectedIsSent) String

func (e *ExpectedIsSent) String() string

func (*ExpectedIsSent) WillReturnBool

func (e *ExpectedIsSent) WillReturnBool(b bool) *ExpectedIsSent

WillReturnBool allows to set an bool for the expected *batch.IsSent action.

type ExpectedPing

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

ExpectedPing is used to manage *driver.Conn.Ping expectations. Returned by *clickhousemock.ExpectPing.

func (*ExpectedPing) String

func (e *ExpectedPing) String() string

String returns string representation

func (*ExpectedPing) WillDelayFor

func (e *ExpectedPing) WillDelayFor(duration time.Duration) *ExpectedPing

WillDelayFor allows to specify duration for which it will delay result. May be used together with Context.

func (*ExpectedPing) WillReturnError

func (e *ExpectedPing) WillReturnError(err error) *ExpectedPing

WillReturnError allows to set an error for expected database ping

type ExpectedPrepareBatch

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

ExpectedPrepareBatch is used to manage *driver.Conn.PrepareBatch expectations. Returned by *clickhousemock.ExpectedPrepareBatch.

func (*ExpectedPrepareBatch) ExpectAbort

func (e *ExpectedPrepareBatch) ExpectAbort() *ExpectedAbort

ExpectAbort allows to expect Abort() on this prepared batch statement.

func (*ExpectedPrepareBatch) ExpectAppend

func (e *ExpectedPrepareBatch) ExpectAppend() *ExpectedAppend

ExpectAppend allows to expect Append() on this prepared batch statement. This method is convenient in order to prevent duplicating sql query string matching.

func (*ExpectedPrepareBatch) ExpectAppendStruct

func (e *ExpectedPrepareBatch) ExpectAppendStruct() *ExpectedAppendStruct

ExpectAppendStruct allows to expect AppendStruct() on this prepared batch statement.

func (*ExpectedPrepareBatch) ExpectColumn

func (e *ExpectedPrepareBatch) ExpectColumn() *ExpectedColumn

ExpectColumn allows to expect Column() on this prepared batch statement.

func (*ExpectedPrepareBatch) ExpectFlush

func (e *ExpectedPrepareBatch) ExpectFlush() *ExpectedFlush

ExpectFlush allows to expect Flush() on this prepared batch statement.

func (*ExpectedPrepareBatch) ExpectIsSent

func (e *ExpectedPrepareBatch) ExpectIsSent() *ExpectedIsSent

ExpectIsSent allows to expect IsSent() on this prepared batch statement.

func (*ExpectedPrepareBatch) ExpectSend

func (e *ExpectedPrepareBatch) ExpectSend() *ExpectedSend

ExpectSend allows to expect Send() on this prepared batch statement.

func (*ExpectedPrepareBatch) String

func (e *ExpectedPrepareBatch) String() string

String returns string representation

func (*ExpectedPrepareBatch) WillBeSent

func (e *ExpectedPrepareBatch) WillBeSent() *ExpectedPrepareBatch

WillBeSent expects this prepared batch statement to be set with sent.

func (*ExpectedPrepareBatch) WillDelayFor

func (e *ExpectedPrepareBatch) WillDelayFor(duration time.Duration) *ExpectedPrepareBatch

WillDelayFor allows to specify duration for which it will delay result. May be used together with Context

func (*ExpectedPrepareBatch) WillReturnError

func (e *ExpectedPrepareBatch) WillReturnError(err error) *ExpectedPrepareBatch

WillReturnError allows to set an error for the expected *driver.Conn.PrepareBatch action.

func (*ExpectedPrepareBatch) WillReturnRows

func (e *ExpectedPrepareBatch) WillReturnRows(rows int) *ExpectedPrepareBatch

WillReturnRows expects this prepared batch statement to be set with rows.

type ExpectedQuery

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

ExpectedQuery is used to manage *driver.Conn.Query expectations. Returned by *clickhousemock.ExpectQuery.

func (*ExpectedQuery) RowsWillBeClosed

func (e *ExpectedQuery) RowsWillBeClosed() *ExpectedQuery

RowsWillBeClosed expects this query rows to be closed.

func (*ExpectedQuery) String

func (e *ExpectedQuery) String() string

String returns string representation

func (*ExpectedQuery) WillDelayFor

func (e *ExpectedQuery) WillDelayFor(duration time.Duration) *ExpectedQuery

WillDelayFor allows to specify duration for which it will delay result. May be used together with Context

func (*ExpectedQuery) WillReturnError

func (e *ExpectedQuery) WillReturnError(err error) *ExpectedQuery

WillReturnError allows to set an error for expected database query

func (*ExpectedQuery) WillReturnRows

func (e *ExpectedQuery) WillReturnRows(rows *Rows) *ExpectedQuery

WillReturnRows allows to set rows for expected database query

func (*ExpectedQuery) WithArgs

func (e *ExpectedQuery) WithArgs(args ...any) *ExpectedQuery

WithArgs will match given expected args to actual database query arguments. if at least one argument does not match, it will return an error. For specific arguments an clickhousemock.Argument interface can be used to match an argument.

type ExpectedQueryRow

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

func (*ExpectedQueryRow) String

func (e *ExpectedQueryRow) String() string

func (*ExpectedQueryRow) WillDelay

WillDelay allows to set a delay for the expected *Conn.QueryRow action.

func (*ExpectedQueryRow) WillReturnRow

func (e *ExpectedQueryRow) WillReturnRow(row *Row) *ExpectedQueryRow

WillReturnRow allows to set a row for the expected *Conn.QueryRow action.

type ExpectedSelect

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

func (*ExpectedSelect) String

func (e *ExpectedSelect) String() string

func (*ExpectedSelect) WillDelay

func (e *ExpectedSelect) WillDelay(d time.Duration) *ExpectedSelect

WillDelay allows to set a delay for the expected *Conn.Select action.

func (*ExpectedSelect) WillReturnError

func (e *ExpectedSelect) WillReturnError(err error) *ExpectedSelect

WillReturnError allows to set an error for the expected *Conn.Select action.

type ExpectedSend

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

func (*ExpectedSend) String

func (e *ExpectedSend) String() string

func (*ExpectedSend) WillReturnError

func (e *ExpectedSend) WillReturnError(err error) *ExpectedSend

WillReturnError allows to set an error for the expected *batch.Send action.

type ExpectedServerVersion

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

func (*ExpectedServerVersion) String

func (e *ExpectedServerVersion) String() string

func (*ExpectedServerVersion) WillReturnVersion

func (e *ExpectedServerVersion) WillReturnVersion(version proto.ServerHandshake) *ExpectedServerVersion

WillReturnVersion allows to set an error for the expected *Conn.ServerVersion action.

type ExpectedStats

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

func (*ExpectedStats) String

func (e *ExpectedStats) String() string

func (*ExpectedStats) WillReturnStats

func (e *ExpectedStats) WillReturnStats(stats clikhouseDriver.Stats) *ExpectedStats

WillReturnStats allows to set an error for the expected *Conn.Stats action.

type OpError

type OpError struct {
	Op         string
	ColumnName string
	Err        error
}

func (*OpError) Error

func (e *OpError) Error() string

type Row

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

func (*Row) Err

func (r *Row) Err() error

func (*Row) Scan

func (r *Row) Scan(dest ...any) error

func (*Row) ScanStruct

func (r *Row) ScanStruct(dest any) error

type Rows

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

func NewRows

func NewRows(columns []ColumnType, values [][]any) *Rows

func (*Rows) Close

func (r *Rows) Close() error

func (*Rows) ColumnTypes

func (r *Rows) ColumnTypes() []driver.ColumnType

func (*Rows) Columns

func (r *Rows) Columns() []string

func (*Rows) Err

func (r *Rows) Err() error

func (*Rows) Next

func (r *Rows) Next() bool

func (*Rows) Scan

func (r *Rows) Scan(dest ...any) error

func (*Rows) ScanStruct

func (r *Rows) ScanStruct(dest any) error

func (*Rows) Totals

func (r *Rows) Totals(dest ...any) error

Jump to

Keyboard shortcuts

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