gocqlxmock

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

gocqlxmock Go Report Card CircleCI codecov

Simple mock for gocqlx. As gocqlx doesn't provide interfaces for its methods and functions in order to make the mock possible, we're using igocqlx, which is a wrapper around gocqlx that provides interfaces.

You can find more about igocqlx in this repo.

Installation

go get github.com/Guilospanck/gocqlxmock

How to use

In an application that is using igocqlx as a wrapper for gocqlx, you can do something like:

// query_builder.go
type queryBuilder struct {
  model   igocqlxtable.ITable
  session igocqlx.ISessionx
  logger  interfaces.ILogger
}

func NewQueryBuider(model igocqlxtable.ITable, session igocqlx.ISessionx, logger interfaces.ILogger) *queryBuilder {
  return &queryBuilder{
    model,
    session,
    logger,
  }
}

func (queryBuilder *queryBuilder) Insert(ctx context.Context, insertData *entities.TrackingData) error {
  insertStatement, insertNames := queryBuilder.model.Insert()
  insertQuery := queryBuilder.session.Query(insertStatement, insertNames).WithContext(ctx)

  err := insertQuery.BindStruct(insertData).ExecRelease()
  if err != nil {
    queryBuilder.logger.Error(fmt.Sprintf("Insert() error %s", err.Error()))
    return err
  }

  return nil
}
// query_builder_test.go
func Test_Insert(t *testing.T) {
  t.Run("Should insert data and have no error", func(t *testing.T) {
    // arrange
    stmt := `INSERT INTO tracking_data (first_name,last_name,timestamp,heat,location,speed,telepathy_powers) VALUES (?,?,?,?,?,?,?) `
    names := []string{"first_name", "last_name", "timestamp", "heat", "location", "speed", "telepathy_powers"}
    ctx := context.Background()


    trackingModel := models.NewTrackingDataTable().Table
    loggerSpy := logger.LoggerSpy{}
    sessionMock := &gocqlxmock.SessionxMock{}
    queryMock := &gocqlxmock.QueryxMock{
      Ctx:   ctx,
      Stmt:  stmt,
      Names: names,
    }

    queryBuilder := NewQueryBuider(trackingModel, sessionMock, loggerSpy)

    sessionMock.On("Query", stmt, names).Return(queryMock)
    queryMock.On("WithContext", context.Background()).Return(queryMock)
    queryMock.On("BindStruct", &mocks.CompleteDataEntity).Return(queryMock)
    queryMock.On("ExecRelease").Return(nil)

    // act
    err := queryBuilder.Insert(ctx, &mocks.CompleteDataEntity)

    // assert
    assert.NoError(t, err)
    sessionMock.AssertExpectations(t)
    sessionMock.AssertNumberOfCalls(t, "Query", 1)
    queryMock.AssertNumberOfCalls(t, "WithContext", 1)
    queryMock.AssertNumberOfCalls(t, "BindStruct", 1)
    queryMock.AssertNumberOfCalls(t, "ExecRelease", 1)
    sessionMock.AssertCalled(t, "Query", stmt, names)
    queryMock.AssertCalled(t, "WithContext", context.Background())
    queryMock.AssertCalled(t, "BindStruct", &mocks.CompleteDataEntity)
    queryMock.AssertCalled(t, "ExecRelease")
  })
}

Documentation

Overview

Simple package to mock github.com/scylladb/gocqlx

In order to use it, you should use github.com/Guilospanck/igocqlx

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IterxMock

type IterxMock struct {
	mock.Mock
}

func (*IterxMock) Close

func (mock *IterxMock) Close() error

func (*IterxMock) Get

func (mock *IterxMock) Get(dest interface{}) error

func (*IterxMock) MapScan

func (mock *IterxMock) MapScan(m map[string]interface{}) bool

func (*IterxMock) Scan

func (mock *IterxMock) Scan(dest ...interface{}) bool

func (*IterxMock) Select

func (mock *IterxMock) Select(dest interface{}) error

func (*IterxMock) StructOnly

func (mock *IterxMock) StructOnly() igocqlx.IIterx

func (*IterxMock) StructScan

func (mock *IterxMock) StructScan(dest interface{}) bool

func (*IterxMock) Unsafe

func (mock *IterxMock) Unsafe() igocqlx.IIterx

type QueryxMock

type QueryxMock struct {
	mock.Mock
	Ctx   context.Context
	Stmt  string
	Names []string
}

func (*QueryxMock) Bind

func (mock *QueryxMock) Bind(v ...interface{}) igocqlx.IQueryx

func (*QueryxMock) BindMap

func (mock *QueryxMock) BindMap(arg map[string]interface{}) igocqlx.IQueryx

func (*QueryxMock) BindStruct

func (mock *QueryxMock) BindStruct(arg interface{}) igocqlx.IQueryx

func (*QueryxMock) BindStructMap

func (mock *QueryxMock) BindStructMap(arg0 interface{}, arg1 map[string]interface{}) igocqlx.IQueryx

func (*QueryxMock) Consistency

func (mock *QueryxMock) Consistency(c gocql.Consistency) igocqlx.IQueryx

func (*QueryxMock) CustomPayload

func (mock *QueryxMock) CustomPayload(customPayload map[string][]byte) igocqlx.IQueryx

func (*QueryxMock) DefaultTimestamp

func (mock *QueryxMock) DefaultTimestamp(enable bool) igocqlx.IQueryx

func (*QueryxMock) Err

func (mock *QueryxMock) Err() error

func (*QueryxMock) Exec

func (mock *QueryxMock) Exec() error

func (*QueryxMock) ExecCAS

func (mock *QueryxMock) ExecCAS() (applied bool, err error)

func (*QueryxMock) ExecCASRelease

func (mock *QueryxMock) ExecCASRelease() (bool, error)

func (*QueryxMock) ExecRelease

func (mock *QueryxMock) ExecRelease() error

func (*QueryxMock) Get

func (mock *QueryxMock) Get(dest interface{}) error

func (*QueryxMock) GetCAS

func (mock *QueryxMock) GetCAS(dest interface{}) (applied bool, err error)

func (*QueryxMock) GetCASRelease

func (mock *QueryxMock) GetCASRelease(dest interface{}) (bool, error)

func (*QueryxMock) GetRelease

func (mock *QueryxMock) GetRelease(dest interface{}) error

func (*QueryxMock) Idempotent

func (mock *QueryxMock) Idempotent(value bool) igocqlx.IQueryx

func (*QueryxMock) Iter

func (mock *QueryxMock) Iter() igocqlx.IIterx

func (*QueryxMock) NoSkipMetadata

func (mock *QueryxMock) NoSkipMetadata() igocqlx.IQueryx

func (*QueryxMock) Observer

func (mock *QueryxMock) Observer(observer gocql.QueryObserver) igocqlx.IQueryx

func (*QueryxMock) PageSize

func (mock *QueryxMock) PageSize(n int) igocqlx.IQueryx

func (*QueryxMock) PageState

func (mock *QueryxMock) PageState(state []byte) igocqlx.IQueryx

func (*QueryxMock) Prefetch

func (mock *QueryxMock) Prefetch(p float64) igocqlx.IQueryx

func (*QueryxMock) Release

func (mock *QueryxMock) Release()

func (*QueryxMock) RetryPolicy

func (mock *QueryxMock) RetryPolicy(r gocql.RetryPolicy) igocqlx.IQueryx

func (*QueryxMock) RoutingKey

func (mock *QueryxMock) RoutingKey(routingKey []byte) igocqlx.IQueryx

func (*QueryxMock) Scan

func (mock *QueryxMock) Scan(dest ...interface{}) error

func (*QueryxMock) Select

func (mock *QueryxMock) Select(dest interface{}) error

func (*QueryxMock) SelectRelease

func (mock *QueryxMock) SelectRelease(dest interface{}) error

func (*QueryxMock) SerialConsistency

func (mock *QueryxMock) SerialConsistency(cons gocql.SerialConsistency) igocqlx.IQueryx

func (*QueryxMock) SetSpeculativeExecutionPolicy

func (mock *QueryxMock) SetSpeculativeExecutionPolicy(sp gocql.SpeculativeExecutionPolicy) igocqlx.IQueryx

func (*QueryxMock) Trace

func (mock *QueryxMock) Trace(trace gocql.Tracer) igocqlx.IQueryx

func (*QueryxMock) WithBindTransformer

func (mock *QueryxMock) WithBindTransformer(tr gocqlx.Transformer) igocqlx.IQueryx

func (*QueryxMock) WithContext

func (mock *QueryxMock) WithContext(ctx context.Context) igocqlx.IQueryx

func (*QueryxMock) WithTimestamp

func (mock *QueryxMock) WithTimestamp(timestamp int64) igocqlx.IQueryx

type SessionxMock

type SessionxMock struct {
	mock.Mock
}

func (*SessionxMock) AwaitSchemaAgreement

func (mock *SessionxMock) AwaitSchemaAgreement(ctx context.Context) error

func (*SessionxMock) Close

func (mock *SessionxMock) Close()

func (*SessionxMock) ContextQuery

func (mock *SessionxMock) ContextQuery(ctx context.Context, stmt string, names []string) igocqlx.IQueryx

func (*SessionxMock) ExecStmt

func (mock *SessionxMock) ExecStmt(stmt string) error

func (*SessionxMock) Query

func (mock *SessionxMock) Query(stmt string, names []string) igocqlx.IQueryx

Jump to

Keyboard shortcuts

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