testadapter

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

upper/db Test Adapter

This is an upper/db database adapter used for unit tests.

Usage

testadapter can be imported with import "gitlab.com/zyrthofar/testadapter".

testadapter.Session follows the db.Session interface. When a package uses a db.Session dependency, testadapter can be used during unit tests instead of connecting to a real database, which would actually make them integration tests. It is then configured with specific expectations so that the outcome of tests are predictable and reproductable.

How expectations are defined is similar to the actual database functions. For example:

sess := testadapter.NewSession(t)

sess.Collection("users").
    Find(db.Cond{"id": 1234}).
    Where(db.Cond{"disabled": false}).(*testadapter.Result).
    ExpectOneArgs(&User{}, &User{ID: 1234, Name: "John"}, nil)

Alternatively, the previous expectation's expected and returned values can be grouped together in a ResultOneExpectation, which is useful in test suites. A more complete example follows, in which the last test (a database failure) cannot usually be tested during integration or end-to-end tests:

func TestFindByID(t *testing.T) {
    tests := map[string]struct {
        userID               int
        dbResultOneExpt      *testadapter.ResultOneExpectation
        expectedErrorMessage string
    }{
        "whenUserIsValid": {
            userID: 1234,
            dbResultOneExpt: &testadapter.ResultOneExpectation{
                ExpectedItem:  &User{},
                ReturnedItem:  &User{ID: 1234, Name: "John"},
                ReturnedError: nil,
            },
            expectedUser:         &User{ID: 1234, Name: "John"},
            expectedErrorMessage: nil,
        },
        "whenUserIsNotFound": {
            userID: -1,
            dbResultOneExpt: &testadapter.ResultOneExpectation{
                ExpectedItem:  &User{},
                ReturnedItem:  nil,
                ReturnedError: ErrUserNotFound,
            },
            expectedUser:         nil,
            expectedErrorMessage: "error retrieving user: user not found",
        },
        "whenDatabaseFailedToRetrieveUser": {
            userID: 1234,
            dbResultOneExpt: &testadapter.ResultOneExpectation{
                ExpectedItem:  &User{},
                ReturnedItem:  nil,
                ReturnedError: ErrDatabaseUnreachable,
            },
            expectedUser:         nil,
            expectedErrorMessage: "error retrieving user: database unreachable",
        },
    }

    for name, tt := range tests {
        tt := tt

        t.Run(name, func(t *testing.T) {
            sess := testadapter.NewSession(t)
            sess.Collection("users").
                Find(db.Cond{"id": tt.userID}).
                Where(db.Cond{"disabled": false}).(*testadapter.Result).
                ExpectOne(tt.dbResultOneExpt)

            user, err := Users(sess).FindByID(tt.userID)
            if err != nil {
                if tt.expectedErrorMessage != err.Error() {
                    t.Fatalf(
                        "expected error message to be `%s`, but was `%s`",
                        tt.expectedErrorMessage,
                        err.Error(),
                    )
                }
                return
            }

            if tt.expectedUser != user {
                t.Fatalf(
                    "expected user to be `%v`, but was `%v`",
                    tt.expectedUser,
                    user,
                )
            }
        })
    }
}

Limitations

Not all features derived from db.Session are currently implemented. The following sections list the methods that are available. Information about their usage can be found in their respective files.

Session
  • session.Get
  • session.Save
  • session.Delete
Collection
  • session.Collection.Count
  • session.Collection.Exists
  • session.Collection.Insert
  • session.Collection.InsertReturning
  • session.Collection.UpdateReturning
  • session.Collection.Truncate
Result
  • session.Collection.Find.*.Count
  • session.Collection.Find.*.Exists
  • session.Collection.Find.*.All
  • session.Collection.Find.*.One
  • session.Collection.Find.*.Update
  • session.Collection.Find.*.Delete
Selector
  • session.SQL.Select|SelectFrom.*.All
  • session.SQL.Select|SelectFrom.*.One

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoMatchingExpectations = errors.New("no matching expectations")

ErrNoMatchingExpectations defines the error returned when no expectations could be matched to the current action.

View Source
var ErrUnimplemented = errors.New("unimplemented")

ErrUnimplemented defines the error returned when a method is not yet implemented.

Functions

This section is empty.

Types

type Collection

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

Collection defines an upper.io collection used in tests to simulate a database.

func (*Collection) Count

func (c *Collection) Count() (uint64, error)

Count returns the first expectation from the list.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

func (*Collection) Exists

func (c *Collection) Exists() (bool, error)

Exists returns the first expectation from the list.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

func (*Collection) ExpectCount

func (c *Collection) ExpectCount(e *CollectionCountExpectation)

ExpectCount adds an expectation for the Count method.

func (*Collection) ExpectCountArgs

func (c *Collection) ExpectCountArgs(retResult uint64, retError error)

ExpectCountArgs adds an expectation for the Count method, by separate arguments.

func (*Collection) ExpectExists

func (c *Collection) ExpectExists(e *CollectionExistsExpectation)

ExpectExists adds an expectation for the Exists method.

func (*Collection) ExpectExistsArgs

func (c *Collection) ExpectExistsArgs(retResult bool, retError error)

ExpectExistsArgs adds an expectation for the Exists method, by separate arguments.

func (*Collection) ExpectInsert

func (c *Collection) ExpectInsert(e *CollectionInsertExpectation)

ExpectInsert adds an expectation for the Insert method.

func (*Collection) ExpectInsertArgs

func (c *Collection) ExpectInsertArgs(item interface{}, retID interface{}, retError error)

ExpectInsertArgs adds an expectation for the Insert method, by separate arguments.

func (*Collection) ExpectInsertReturning

func (c *Collection) ExpectInsertReturning(e *CollectionInsertReturningExpectation)

ExpectInsertReturning adds an expectation for the InsertReturning method.

func (*Collection) ExpectInsertReturningArgs

func (c *Collection) ExpectInsertReturningArgs(item interface{}, retItem interface{}, retError error)

ExpectInsertReturningArgs adds an expectation for the InsertReturning method, by separate arguments.

func (*Collection) ExpectTruncate

func (c *Collection) ExpectTruncate(e *CollectionTruncateExpectation)

ExpectTruncate adds an expectation for the Truncate method.

func (*Collection) ExpectTruncateArgs

func (c *Collection) ExpectTruncateArgs(retError error)

ExpectTruncateArgs adds an expectation for the Truncate method, by separate arguments.

func (*Collection) ExpectUpdateReturning

func (c *Collection) ExpectUpdateReturning(e *CollectionUpdateReturningExpectation)

ExpectUpdateReturning adds an expectation for the UpdateReturning method.

func (*Collection) ExpectUpdateReturningArgs

func (c *Collection) ExpectUpdateReturningArgs(item interface{}, retItem interface{}, retError error)

ExpectUpdateReturningArgs adds an expectation for the UpdateReturning method, by separate arguments.

func (*Collection) Find

func (c *Collection) Find(args ...interface{}) db.Result

Find creates a new test result.

func (*Collection) Insert

func (c *Collection) Insert(item interface{}) (db.InsertResult, error)

Insert returns the first matching expectation from the list.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

func (*Collection) InsertReturning

func (c *Collection) InsertReturning(item interface{}) error

InsertReturning returns the first matching expectation from the list.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

func (*Collection) Name

func (c *Collection) Name() string

Name returns test collection name.

func (*Collection) Session

func (c *Collection) Session() db.Session

Session returns the current test session.

func (*Collection) Truncate

func (c *Collection) Truncate() error

Truncate returns the first expectation from the list.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

func (*Collection) UpdateReturning

func (c *Collection) UpdateReturning(item interface{}) error

UpdateReturning returns the first matching expectation from the list.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

type CollectionCountExpectation

type CollectionCountExpectation struct {
	ReturnedResult uint64
	ReturnedError  error
}

CollectionCountExpectation defines the returned values for the Count method.

type CollectionExistsExpectation

type CollectionExistsExpectation struct {
	ReturnedResult bool
	ReturnedError  error
}

CollectionExistsExpectation defines the returned values for the Exists method.

type CollectionInsertExpectation

type CollectionInsertExpectation struct {
	ExpectedItem  interface{}
	ReturnedID    interface{}
	ReturnedError error
}

CollectionInsertExpectation defines the expected and returned values for the Insert method.

type CollectionInsertReturningExpectation

type CollectionInsertReturningExpectation struct {
	ExpectedItem  interface{}
	ReturnedItem  interface{}
	ReturnedError error
}

CollectionInsertReturningExpectation defines the expected and returned values for the InsertReturning method.

type CollectionTruncateExpectation

type CollectionTruncateExpectation struct {
	ReturnedError error
}

CollectionTruncateExpectation defines the returned values for the Truncate method.

type CollectionUpdateReturningExpectation

type CollectionUpdateReturningExpectation struct {
	ExpectedItem  interface{}
	ReturnedItem  interface{}
	ReturnedError error
}

CollectionUpdateReturningExpectation defines the expected and returned values for the UpdateReturning method.

type Result

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

Result defines an upper.io result used in tests to simulate a database.

func (*Result) All

func (r *Result) All(slice interface{}) error

All returns the first matching expectation from the list, replacing the specified slice with the returned items.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

func (*Result) And

func (r *Result) And(args ...interface{}) db.Result

And adds an argument expectation to the result.

func (*Result) Close

func (r *Result) Close() error

Close does nothing.

func (*Result) Count

func (r *Result) Count() (uint64, error)

Count returns the first expectation from the list.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

func (*Result) Cursor

func (r *Result) Cursor(arg string) db.Result

Cursor adds an argument expectation to the result.

func (*Result) Delete

func (r *Result) Delete() error

Delete returns the first matching expectation from the list.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

func (*Result) Err

func (r *Result) Err() error

Err is not implemented yet.

func (*Result) Exists

func (r *Result) Exists() (bool, error)

Exists returns the first expectation from the list.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

func (*Result) ExpectAll

func (r *Result) ExpectAll(e *ResultAllExpectation)

ExpectAll adds an expectation for the All method.

func (*Result) ExpectAllArgs

func (r *Result) ExpectAllArgs(slice interface{}, retSlice interface{}, retError error)

ExpectAllArgs adds an expectation for the All method, by separate arguments.

func (*Result) ExpectCount

func (r *Result) ExpectCount(e *ResultCountExpectation)

ExpectCount adds an expectation for the Count method.

func (*Result) ExpectCountArgs

func (r *Result) ExpectCountArgs(expectedResult uint64, expectedError error)

ExpectCountArgs adds an expectation for the Count method, by separate arguments.

func (*Result) ExpectDelete

func (r *Result) ExpectDelete(e *ResultDeleteExpectation)

ExpectDelete adds an expectation for the Delete method.

func (*Result) ExpectDeleteArgs

func (r *Result) ExpectDeleteArgs(retError error)

ExpectDeleteArgs adds an expectation for the Delete method, by separate arguments.

func (*Result) ExpectExists

func (r *Result) ExpectExists(e *ResultExistsExpectation)

ExpectExists adds an expectation for the Exists method.

func (*Result) ExpectExistsArgs

func (r *Result) ExpectExistsArgs(expectedResult bool, expectedError error)

ExpectExistsArgs adds an expectation for the Exists method, by separate arguments.

func (*Result) ExpectOne

func (r *Result) ExpectOne(e *ResultOneExpectation)

ExpectOne adds an expectation for the One method.

func (*Result) ExpectOneArgs

func (r *Result) ExpectOneArgs(item interface{}, retItem interface{}, retError error)

ExpectOneArgs adds an expectation for the One method, by separate arguments.

func (*Result) ExpectUpdate

func (r *Result) ExpectUpdate(e *ResultUpdateExpectation)

ExpectUpdate adds an expectation for the Update method.

func (*Result) ExpectUpdateArgs

func (r *Result) ExpectUpdateArgs(item interface{}, retError error)

ExpectUpdateArgs adds an expectation for the Update method, by separate arguments.

func (*Result) GroupBy

func (r *Result) GroupBy(args ...interface{}) db.Result

GroupBy adds an argument expectation to the result.

func (*Result) Limit

func (r *Result) Limit(arg int) db.Result

Limit adds an argument expectation to the result.

func (*Result) Next

func (r *Result) Next(ptrToStruct interface{}) bool

Next is not implemented yet.

func (*Result) NextPage

func (r *Result) NextPage(arg interface{}) db.Result

NextPage adds an argument expectation to the result.

func (*Result) Offset

func (r *Result) Offset(arg int) db.Result

Offset adds an argument expectation to the result.

func (*Result) One

func (r *Result) One(item interface{}) error

One returns the first matching expectation from the list, replacing the specified item with the returned one.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

func (*Result) OrderBy

func (r *Result) OrderBy(args ...interface{}) db.Result

OrderBy adds an argument expectation to the result.

func (*Result) Page

func (r *Result) Page(arg uint) db.Result

Page adds an argument expectation to the result.

func (*Result) Paginate

func (r *Result) Paginate(arg uint) db.Result

Paginate adds an argument expectation to the result.

func (*Result) PrevPage

func (r *Result) PrevPage(arg interface{}) db.Result

PrevPage adds an argument expectation to the result.

func (*Result) Select

func (r *Result) Select(args ...interface{}) db.Result

Select adds an argument expectation to the result.

func (*Result) String

func (r *Result) String() string

String is not implemented yet.

func (*Result) TotalEntries

func (r *Result) TotalEntries() (uint64, error)

TotalEntries is not implemented yet.

func (*Result) TotalPages

func (r *Result) TotalPages() (uint, error)

TotalPages is not implemented yet.

func (*Result) Update

func (r *Result) Update(item interface{}) error

Update returns the first matching expectation from the list.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

type ResultAllExpectation

type ResultAllExpectation struct {
	ExpectedSlice interface{}
	ReturnedSlice interface{}
	ReturnedError error
	// contains filtered or unexported fields
}

ResultAllExpectation defines the expected and returned values for the All method.

type ResultCountExpectation

type ResultCountExpectation struct {
	ReturnedResult uint64
	ReturnedError  error
	// contains filtered or unexported fields
}

ResultCountExpectation defines the returned values for the Count method.

type ResultDeleteExpectation

type ResultDeleteExpectation struct {
	ReturnedError error
	// contains filtered or unexported fields
}

ResultDeleteExpectation defines the returned values for the Delete method.

type ResultExistsExpectation

type ResultExistsExpectation struct {
	ReturnedResult bool
	ReturnedError  error
	// contains filtered or unexported fields
}

ResultExistsExpectation defines the returned values for the Exists method.

type ResultOneExpectation

type ResultOneExpectation struct {
	ExpectedItem  interface{}
	ReturnedItem  interface{}
	ReturnedError error
	// contains filtered or unexported fields
}

ResultOneExpectation defines the expected and returned values for the One method.

type ResultUpdateExpectation

type ResultUpdateExpectation struct {
	ExpectedItem  interface{}
	ReturnedError error
	// contains filtered or unexported fields
}

ResultUpdateExpectation defines the expected and returned values for the Update method.

type SQL

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

SQL defines an upper.io sql object used in tests to simulate a database.

func (*SQL) DeleteFrom

func (s *SQL) DeleteFrom(table string) db.Deleter

DeleteFrom is not implemented yet.

func (*SQL) Exec

func (s *SQL) Exec(query interface{}, args ...interface{}) (sql.Result, error)

Exec is not implemented yet.

func (*SQL) ExecContext

func (s *SQL) ExecContext(ctx context.Context, query interface{}, args ...interface{}) (sql.Result, error)

ExecContext is not implemented yet.

func (*SQL) InsertInto

func (s *SQL) InsertInto(table string) db.Inserter

InsertInto is not implemented yet.

func (*SQL) Iterator

func (s *SQL) Iterator(query interface{}, args ...interface{}) db.Iterator

Iterator is not implemented yet.

func (*SQL) IteratorContext

func (s *SQL) IteratorContext(ctx context.Context, query interface{}, args ...interface{}) db.Iterator

IteratorContext is not implemented yet.

func (*SQL) NewIterator

func (s *SQL) NewIterator(rows *sql.Rows) db.Iterator

NewIterator is not implemented yet.

func (*SQL) NewIteratorContext

func (s *SQL) NewIteratorContext(ctx context.Context, rows *sql.Rows) db.Iterator

NewIteratorContext is not implemented yet.

func (*SQL) Prepare

func (s *SQL) Prepare(query interface{}) (*sql.Stmt, error)

Prepare is not implemented yet.

func (*SQL) PrepareContext

func (s *SQL) PrepareContext(ctx context.Context, query interface{}) (*sql.Stmt, error)

PrepareContext is not implemented yet.

func (*SQL) Query

func (s *SQL) Query(query interface{}, args ...interface{}) (*sql.Rows, error)

Query is not implemented yet.

func (*SQL) QueryContext

func (s *SQL) QueryContext(ctx context.Context, query interface{}, args ...interface{}) (*sql.Rows, error)

QueryContext is not implemented yet.

func (*SQL) QueryRow

func (s *SQL) QueryRow(query interface{}, args ...interface{}) (*sql.Row, error)

QueryRow is not implemented yet.

func (*SQL) QueryRowContext

func (s *SQL) QueryRowContext(ctx context.Context, query interface{}, args ...interface{}) (*sql.Row, error)

QueryRowContext is not implemented yet.

func (*SQL) Select

func (s *SQL) Select(args ...interface{}) db.Selector

Select creates a new test selector.

func (*SQL) SelectFrom

func (s *SQL) SelectFrom(args ...interface{}) db.Selector

SelectFrom creates a new test selector.

func (*SQL) Update

func (s *SQL) Update(table string) db.Updater

Update is not implemented yet.

type Selector

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

Selector represents a SELECT statement.

func (*Selector) All

func (s *Selector) All(slice interface{}) error

All returns the first matching expectation from the list, replacing the specified slice with the returned items.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

func (*Selector) Amend

func (s *Selector) Amend(arg func(string) string) db.Selector

Amend adds an argument expectation to the selector.

func (*Selector) And

func (s *Selector) And(args ...interface{}) db.Selector

And adds an argument expectation to the selector.

func (*Selector) Arguments

func (s *Selector) Arguments() []interface{}

Arguments is not implemented yet.

func (*Selector) As

func (s *Selector) As(arg string) db.Selector

As adds an argument expectation to the selector.

func (*Selector) Columns

func (s *Selector) Columns(args ...interface{}) db.Selector

Columns adds an argument expectation to the selector.

func (*Selector) CrossJoin

func (s *Selector) CrossJoin(args ...interface{}) db.Selector

CrossJoin adds an argument expectation to the selector.

func (*Selector) Distinct

func (s *Selector) Distinct(args ...interface{}) db.Selector

Distinct adds an argument expectation to the selector.

func (*Selector) ExpectAll

func (s *Selector) ExpectAll(e *SelectorAllExpectation)

ExpectAll adds an expectation for the All method.

func (*Selector) ExpectAllArgs

func (s *Selector) ExpectAllArgs(slice interface{}, retSlice interface{}, retError error)

ExpectAllArgs adds an expectation for the All method, by separate arguments.

func (*Selector) ExpectOne

func (s *Selector) ExpectOne(e *SelectorOneExpectation)

ExpectOne adds an expectation for the One method.

func (*Selector) ExpectOneArgs

func (s *Selector) ExpectOneArgs(item interface{}, retItem interface{}, retError error)

ExpectOneArgs adds an expectation for the One method, by separate arguments.

func (*Selector) From

func (s *Selector) From(args ...interface{}) db.Selector

From adds an argument expectation to the selector.

func (*Selector) FullJoin

func (s *Selector) FullJoin(args ...interface{}) db.Selector

FullJoin adds an argument expectation to the selector.

func (*Selector) GroupBy

func (s *Selector) GroupBy(args ...interface{}) db.Selector

GroupBy adds an argument expectation to the selector.

func (*Selector) Iterator

func (s *Selector) Iterator() db.Iterator

Iterator is not implemented yet.

func (*Selector) IteratorContext

func (s *Selector) IteratorContext(ctx context.Context) db.Iterator

IteratorContext is not implemented yet.

func (*Selector) Join

func (s *Selector) Join(args ...interface{}) db.Selector

Join adds an argument expectation to the selector.

func (*Selector) LeftJoin

func (s *Selector) LeftJoin(args ...interface{}) db.Selector

LeftJoin adds an argument expectation to the selector.

func (*Selector) Limit

func (s *Selector) Limit(arg int) db.Selector

Limit adds an argument expectation to the selector.

func (*Selector) Offset

func (s *Selector) Offset(arg int) db.Selector

Offset adds an argument expectation to the selector.

func (*Selector) On

func (s *Selector) On(args ...interface{}) db.Selector

On adds an argument expectation to the selector.

func (*Selector) One

func (s *Selector) One(item interface{}) error

One returns the first matching expectation from the list, replacing the specified item with the returned one.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

func (*Selector) OrderBy

func (s *Selector) OrderBy(args ...interface{}) db.Selector

OrderBy adds an argument expectation to the selector.

func (*Selector) Paginate

func (s *Selector) Paginate(uint) db.Paginator

Paginate is not implemented yet.

func (*Selector) Prepare

func (s *Selector) Prepare() (*sql.Stmt, error)

Prepare is not implemented yet.

func (*Selector) PrepareContext

func (s *Selector) PrepareContext(context.Context) (*sql.Stmt, error)

PrepareContext is not implemented yet.

func (*Selector) Query

func (s *Selector) Query() (*sql.Rows, error)

Query is not implemented yet.

func (*Selector) QueryContext

func (s *Selector) QueryContext(context.Context) (*sql.Rows, error)

QueryContext is not implemented yet.

func (*Selector) QueryRow

func (s *Selector) QueryRow() (*sql.Row, error)

QueryRow is not implemented yet.

func (*Selector) QueryRowContext

func (s *Selector) QueryRowContext(ctx context.Context) (*sql.Row, error)

QueryRowContext is not implemented yet.

func (*Selector) RightJoin

func (s *Selector) RightJoin(args ...interface{}) db.Selector

RightJoin adds an argument expectation to the selector.

func (*Selector) String

func (s *Selector) String() string

String is not implemented yet.

func (*Selector) Using

func (s *Selector) Using(args ...interface{}) db.Selector

Using adds an argument expectation to the selector.

func (*Selector) Where

func (s *Selector) Where(args ...interface{}) db.Selector

Where adds an argument expectation to the selector.

type SelectorAllExpectation

type SelectorAllExpectation struct {
	ExpectedSlice interface{}
	ReturnedSlice interface{}
	ReturnedError error
	// contains filtered or unexported fields
}

SelectorAllExpectation defines the expected and returned values for the All method.

type SelectorOneExpectation

type SelectorOneExpectation struct {
	ExpectedItem  interface{}
	ReturnedItem  interface{}
	ReturnedError error
	// contains filtered or unexported fields
}

SelectorOneExpectation defines the expected and returned values for the One method.

type Session

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

Session defines an upper.io session used in tests to simulate a database.

func NewSession

func NewSession(t *testing.T) *Session

NewSession creates a new test session.

func (*Session) Close

func (s *Session) Close() error

Close does nothing.

func (*Session) Collection

func (s *Session) Collection(name string) db.Collection

Collection returns the test collection by the specified name, creating it if needed.

func (*Session) Collections

func (s *Session) Collections() ([]db.Collection, error)

Collections is not implemented yet.

func (*Session) ConnMaxLifetime

func (s *Session) ConnMaxLifetime() time.Duration

ConnMaxLifetime does nothing and always returns one second.

func (*Session) ConnectionURL

func (s *Session) ConnectionURL() db.ConnectionURL

ConnectionURL does nothing.

func (*Session) Context

func (s *Session) Context() context.Context

Context returns a new background context.

func (*Session) Delete

func (s *Session) Delete(record db.Record) error

Delete returns the first matching expectation from the list.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

func (*Session) Driver

func (s *Session) Driver() interface{}

Driver does nothing and always returns nil.

func (*Session) ExpectDelete

func (s *Session) ExpectDelete(e *SessionDeleteExpectation)

ExpectDelete adds an expectation for the Delete method.

func (*Session) ExpectDeleteArgs

func (s *Session) ExpectDeleteArgs(record db.Record, retError error)

ExpectDeleteArgs adds an expectation for the Delete method, by separate arguments.

func (*Session) ExpectGet

func (s *Session) ExpectGet(e *SessionGetExpectation)

ExpectGet adds an expectation for the Get method.

func (*Session) ExpectGetArgs

func (s *Session) ExpectGetArgs(record db.Record, cond interface{}, retRecord db.Record, retError error)

ExpectGetArgs adds an expectation for the Get method, by separate arguments.

func (*Session) ExpectSave

func (s *Session) ExpectSave(e *SessionSaveExpectation)

ExpectSave adds an expectation for the Save method.

func (*Session) ExpectSaveArgs

func (s *Session) ExpectSaveArgs(record db.Record, retError error)

ExpectSaveArgs adds an expectation for the Save method, by separate arguments.

func (*Session) Get

func (s *Session) Get(record db.Record, cond interface{}) error

Get tries to find an expectation, matching against both record and cond arguments.

If a match is found, record will be replaced with the match's ReturnedRecord, and ReturnedError will be returned.

getExpt := &testadapter.SessionGetExpectation{
    ExpectedRecord: &record{},
    ExpectedCond:   db.Cond{"id": 1},
    ReturnedRecord: &record{ID: 1},
    ReturnedError:  nil,
}

sess := testadapter.NewSession(t)
sess.ExpectGet(getExpt)

With the previous test setup, the following code ends up with rec == &record{ID: 1} and err nil:

rec := new(record)
err := sess.Get(rec, db.Cond{"id": 1})

If no matches are found, this function panics with an ErrNoMatchingExpectations error.

func (*Session) MaxIdleConns

func (s *Session) MaxIdleConns() int

MaxIdleConns does nothing and always returns 0.

func (*Session) MaxOpenConns

func (s *Session) MaxOpenConns() int

MaxOpenConns does nothing and always returns 0.

func (*Session) MaxTransactionRetries

func (s *Session) MaxTransactionRetries() int

MaxTransactionRetries does nothing and always returns 0.

func (*Session) Name

func (s *Session) Name() string

Name returns the name of the test adapter.

func (*Session) Ping

func (s *Session) Ping() error

Ping is not implemented yet.

func (*Session) PreparedStatementCacheEnabled

func (s *Session) PreparedStatementCacheEnabled() bool

PreparedStatementCacheEnabled does nothing and always returns false.

func (*Session) Reset

func (s *Session) Reset()

Reset does nothing.

func (*Session) SQL

func (s *Session) SQL() db.SQL

SQL returns a special interface for SQL databases.

func (*Session) Save

func (s *Session) Save(record db.Record) error

Save returns the first matching expectation from the list.

This function panics with an ErrNoMatchingExpectations error if there are no such expectations.

func (*Session) SetConnMaxLifetime

func (s *Session) SetConnMaxLifetime(time.Duration)

SetConnMaxLifetime does nothing.

func (*Session) SetMaxIdleConns

func (s *Session) SetMaxIdleConns(int)

SetMaxIdleConns does nothing.

func (*Session) SetMaxOpenConns

func (s *Session) SetMaxOpenConns(int)

SetMaxOpenConns does nothing.

func (*Session) SetMaxTransactionRetries

func (s *Session) SetMaxTransactionRetries(int)

SetMaxTransactionRetries does nothing.

func (*Session) SetPreparedStatementCache

func (s *Session) SetPreparedStatementCache(bool)

SetPreparedStatementCache does nothing.

func (*Session) Tx

func (s *Session) Tx(fn func(db.Session) error) error

Tx simply executes the provided function and returns its result.

func (*Session) TxContext

func (s *Session) TxContext(_ context.Context, fn func(db.Session) error, _ *sql.TxOptions) error

TxContext simply executes the provided function and returns its result.

func (*Session) WithContext

func (s *Session) WithContext(ctx context.Context) db.Session

WithContext does nothing and simply returns the current session.

type SessionDeleteExpectation

type SessionDeleteExpectation struct {
	ExpectedRecord db.Record
	ReturnedError  error
}

SessionDeleteExpectation defines the expected and returned values for the Delete method.

type SessionGetExpectation

type SessionGetExpectation struct {
	ExpectedRecord db.Record
	ExpectedCond   interface{}
	ReturnedRecord db.Record
	ReturnedError  error
}

SessionGetExpectation defines the expected and returned values for the Get method.

type SessionSaveExpectation

type SessionSaveExpectation struct {
	ExpectedRecord db.Record
	ReturnedError  error
}

SessionSaveExpectation defines the expected and returned values for the Save method.

Jump to

Keyboard shortcuts

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