gockle

package module
v0.0.0-...-3c81b67 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2021 License: MIT Imports: 3 Imported by: 0

README

gockle

Documentation Build Report Test Coverage

Note: Test coverage is low because there is no Cassandra database for the tests to use. Providing one yields 97.37% test coverage. Some code is uncovered because gocql cannot be mocked. This is one difficulty your code avoids by using gockle.

Documentation

Overview

Package gockle simplifies and mocks github.com/gocql/gocql. It provides simple interfaces to insert, query, and mutate Cassandra data, as well as get basic keyspace and table metadata.

The entry points are NewSession and NewSimpleSession. Call them to get a Session. Session interacts with the database. It executes queries and batched queries and iterates result rows. Closing the Session closes the underlying gocql.Session, including the one passed to NewSimpleSession.

Mocks are provided for testing use of Batch, Iterator, and Session.

Tx is short for transaction.

The name gockle comes from a pronunciation of gocql.

Index

Constants

View Source
const ColumnApplied = "[applied]"

ColumnApplied is the name of a special column that has a bool that indicates whether a conditional statement was applied.

Variables

This section is empty.

Functions

This section is empty.

Types

type Batch

type Batch interface {
	// Add adds the query for statement and arguments.
	Add(statement string, arguments ...interface{})

	// Exec executes the queries in the order they were added.
	Exec() error

	// ExecTx executes the queries in the order they were added. It returns a slice
	// of maps from columns to values, the maps corresponding to all the conditional
	// queries, and ordered in the same relative order. The special column
	// ColumnApplied has a bool that indicates whether the conditional statement was
	// applied. If a conditional statement was not applied, the current values for
	// the columns are put into the map.
	ExecTx() ([]map[string]interface{}, error)
}

Batch is an ordered collection of CQL queries.

type BatchKind

type BatchKind byte

BatchKind is the kind of Batch. The choice of kind mostly affects performance.

const (
	// BatchLogged queries are atomic. Queries are only isolated within a single
	// partition.
	BatchLogged BatchKind = 0

	// BatchUnlogged queries are not atomic. Atomic queries spanning multiple partitions cost performance.
	BatchUnlogged BatchKind = 1

	// BatchCounter queries update counters and are not idempotent.
	BatchCounter BatchKind = 2
)

Kinds of batches.

type BatchMock

type BatchMock struct {
	mock.Mock
}

BatchMock is a mock Batch. See github.com/maraino/go-mock.

func (BatchMock) Add

func (m BatchMock) Add(statement string, arguments ...interface{})

Add implements Batch.

func (BatchMock) Exec

func (m BatchMock) Exec() error

Exec implements Batch.

func (BatchMock) ExecTx

func (m BatchMock) ExecTx() ([]map[string]interface{}, error)

ExecTx implements Batch.

type Iterator

type Iterator interface {
	// Close closes the Iterator.
	Close() error

	// Scan puts the current result row in results and returns whether there are
	// more result rows.
	Scan(results ...interface{}) bool

	// ScanMap puts the current result row in results and returns whether there are
	// more result rows.
	ScanMap(results map[string]interface{}) bool
}

Iterator iterates CQL query result rows.

type IteratorMock

type IteratorMock struct {
	mock.Mock
}

IteratorMock is a mock Iterator. See github.com/maraino/go-mock.

func (IteratorMock) Close

func (m IteratorMock) Close() error

Close implements Iterator.

func (IteratorMock) Scan

func (m IteratorMock) Scan(results ...interface{}) bool

Scan implements Iterator.

func (IteratorMock) ScanMap

func (m IteratorMock) ScanMap(results map[string]interface{}) bool

ScanMap implements Iterator.

type Query

type Query interface {
	Bind(...interface{}) Query
	Exec() error
	Iter() IterAPI
	Scan(...interface{}) error
	ScanCAS(...interface{}) (bool, error)
}

QueryAPI allows gomock mock of gocql.Query

type QueryMock

type QueryMock struct {
	mock.Mock
}

func (QueryMock) Bind

func (m QueryMock) Bind(v ...interface{}) QueryMock

func (QueryMock) Exec

func (m QueryMock) Exec() error

func (QueryMock) Iter

func (m QueryMock) Iter() IteratorMock

func (QueryMock) Scan

func (m QueryMock) Scan(values ...interface{}) error

func (QueryMock) ScanCAS

func (m QueryMock) ScanCAS(values ...interface{}) (bool, error)

type Session

type Session interface {
	// Batch returns a new Batch for the Session.
	Batch(kind BatchKind) Batch

	// Close closes the Session.
	Close()

	// Query generates a new query object for interacting witht the database.
	Query(string, ...interface{}) Query

	// Columns returns a map from column names to types for keyspace and table.
	// Schema changes during a session are not reflected; you must open a new
	// Session to observe them.
	Columns(keyspace, table string) (map[string]gocql.TypeInfo, error)

	// Exec executes the query for statement and arguments.
	Exec(statement string, arguments ...interface{}) error

	// Scan executes the query for statement and arguments and puts the first
	// result row in results.
	Scan(statement string, results []interface{}, arguments ...interface{}) error

	// ScanIterator executes the query for statement and arguments and returns an
	// Iterator for the results.
	ScanIterator(statement string, arguments ...interface{}) Iterator

	// ScanMap executes the query for statement and arguments and puts the first
	// result row in results.
	ScanMap(statement string, results map[string]interface{}, arguments ...interface{}) error

	// ScanMapSlice executes the query for statement and arguments and returns all
	// the result rows.
	ScanMapSlice(statement string, arguments ...interface{}) ([]map[string]interface{}, error)

	// ScanMapTx executes the query for statement and arguments as a lightweight
	// transaction. If the query is not applied, it puts the current values for the
	// conditional columns in results. It returns whether the query is applied.
	ScanMapTx(statement string, results map[string]interface{}, arguments ...interface{}) (bool, error)

	// Tables returns the table names for keyspace. Schema changes during a session
	// are not reflected; you must open a new Session to observe them.
	Tables(keyspace string) ([]string, error)
}

Session is a Cassandra connection. The Query methods run CQL queries. The Columns and Tables methods provide simple metadata.

func NewSession

func NewSession(s *gocql.Session) Session

NewSession returns a new Session for s.

func NewSimpleSession

func NewSimpleSession(hosts ...string) (Session, error)

NewSimpleSession returns a new Session for hosts. It uses native protocol version 4.

type SessionMock

type SessionMock struct {
	mock.Mock
}

SessionMock is a mock Session. See github.com/maraino/go-mock.

func (SessionMock) Batch

func (m SessionMock) Batch(kind BatchKind) Batch

Batch implements Session.

func (SessionMock) Close

func (m SessionMock) Close()

Close implements Session.

func (SessionMock) Columns

func (m SessionMock) Columns(keyspace, table string) (map[string]gocql.TypeInfo, error)

Columns implements Session.

func (SessionMock) Exec

func (m SessionMock) Exec(statement string, arguments ...interface{}) error

Exec implements Session.

func (SessionMock) Scan

func (m SessionMock) Scan(statement string, results []interface{}, arguments ...interface{}) error

Scan implements Session.

func (SessionMock) ScanIterator

func (m SessionMock) ScanIterator(statement string, arguments ...interface{}) Iterator

ScanIterator implements Session.

func (SessionMock) ScanMap

func (m SessionMock) ScanMap(statement string, results map[string]interface{}, arguments ...interface{}) error

ScanMap implements Session.

func (SessionMock) ScanMapSlice

func (m SessionMock) ScanMapSlice(statement string, arguments ...interface{}) ([]map[string]interface{}, error)

ScanMapSlice implements Session.

func (SessionMock) ScanMapTx

func (m SessionMock) ScanMapTx(statement string, results map[string]interface{}, arguments ...interface{}) (bool, error)

ScanMapTx implements Session.

func (SessionMock) Tables

func (m SessionMock) Tables(keyspace string) ([]string, error)

Tables implements Session.

Jump to

Keyboard shortcuts

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