Documentation
¶
Overview ¶
Package mocks provides mock implementations for TableTheory interfaces and AWS SDK operations
Package mocks provides mock implementations for TableTheory interfaces and AWS SDK operations ¶
Package mocks provides mock implementations for TableTheory interfaces.
This package solves the common issue of having to implement all 26+ methods of the core.Query interface when writing unit tests. Instead of discovering missing methods through trial and error, you can use these pre-built mocks.
Installation ¶
Import the mocks package in your test files:
import "github.com/theory-cloud/tabletheory/pkg/mocks"
Basic Usage ¶
The most common use case is mocking database queries:
func TestUserService(t *testing.T) {
// Create mocks
mockDB := new(mocks.MockDB)
mockQuery := new(mocks.MockQuery)
// Setup expectations
mockDB.On("Model", &User{}).Return(mockQuery)
mockQuery.On("Where", "ID", "=", "123").Return(mockQuery)
mockQuery.On("First", mock.Anything).Return(nil)
// Use in your service
service := NewUserService(mockDB)
user, err := service.GetUser("123")
// Assert expectations were met
mockDB.AssertExpectations(t)
mockQuery.AssertExpectations(t)
}
Chaining Methods ¶
Query methods typically return themselves to allow chaining:
mockQuery.On("Where", "Status", "=", "active").Return(mockQuery)
mockQuery.On("OrderBy", "CreatedAt", "DESC").Return(mockQuery)
mockQuery.On("Limit", 10).Return(mockQuery)
mockQuery.On("All", mock.Anything).Return(nil)
Working with Results ¶
To return data from queries, use mock.Run to populate the destination:
users := []User{{ID: "1", Name: "Alice"}, {ID: "2", Name: "Bob"}}
mockQuery.On("All", mock.Anything).Run(func(args mock.Arguments) {
dest := args.Get(0).(*[]User)
*dest = users
}).Return(nil)
Error Handling ¶
To simulate errors:
mockQuery.On("First", mock.Anything).Return(errors.New("not found"))
Update Operations ¶
For update operations with the builder pattern:
mockUpdateBuilder := new(mocks.MockUpdateBuilder)
mockQuery.On("UpdateBuilder").Return(mockUpdateBuilder)
mockUpdateBuilder.On("Set", "Status", "completed").Return(mockUpdateBuilder)
mockUpdateBuilder.On("Execute").Return(nil)
AWS SDK Level Mocking ¶
For testing infrastructure code that directly uses the AWS SDK:
mockClient := new(mocks.MockDynamoDBClient)
mockWaiter := new(mocks.MockTableExistsWaiter)
// Mock table creation
mockClient.On("CreateTable", mock.Anything, mock.Anything, mock.Anything).
Return(mocks.NewMockCreateTableOutput("test-table"), nil)
// Mock waiting for table to be ready
mockWaiter.On("Wait", mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(nil)
Tips ¶
1. Use mock.Anything when you don't need to assert on specific arguments 2. Use mock.MatchedBy for custom argument matching 3. Always assert expectations were met with AssertExpectations 4. Return the mock itself for chainable methods 5. Use Run to modify output parameters before returning 6. Use the helper functions (NewMockCreateTableOutput, etc.) for realistic responses 7. AWS SDK mocks complement TableTheory interface mocks for complete test coverage
Package mocks provides mock implementations for TableTheory interfaces. These mocks are designed to be used with github.com/stretchr/testify/mock for unit testing applications that use TableTheory.
Index ¶
- func NewMockCreateTableOutput(tableName string) *dynamodb.CreateTableOutput
- func NewMockDeleteTableOutput(tableName string) *dynamodb.DeleteTableOutput
- func NewMockDescribeTableOutput(tableName string, status types.TableStatus) *dynamodb.DescribeTableOutput
- func NewMockUpdateTimeToLiveOutput(tableName string) *dynamodb.UpdateTimeToLiveOutput
- type BatchGetBuilder
- type DB
- type DynamoDBClient
- type ExtendedDB
- type MockBatchGetBuilder
- func (m *MockBatchGetBuilder) ChunkSize(size int) core.BatchGetBuilder
- func (m *MockBatchGetBuilder) ConsistentRead() core.BatchGetBuilder
- func (m *MockBatchGetBuilder) Execute(dest any) error
- func (m *MockBatchGetBuilder) Keys(keys []any) core.BatchGetBuilder
- func (m *MockBatchGetBuilder) OnError(handler core.BatchChunkErrorHandler) core.BatchGetBuilder
- func (m *MockBatchGetBuilder) OnProgress(callback core.BatchProgressCallback) core.BatchGetBuilder
- func (m *MockBatchGetBuilder) Parallel(maxConcurrency int) core.BatchGetBuilder
- func (m *MockBatchGetBuilder) Select(fields ...string) core.BatchGetBuilder
- func (m *MockBatchGetBuilder) WithRetry(policy *core.RetryPolicy) core.BatchGetBuilder
- type MockDB
- type MockDynamoDBClient
- func (m *MockDynamoDBClient) BatchGetItem(ctx context.Context, params *dynamodb.BatchGetItemInput, ...) (*dynamodb.BatchGetItemOutput, error)
- func (m *MockDynamoDBClient) BatchWriteItem(ctx context.Context, params *dynamodb.BatchWriteItemInput, ...) (*dynamodb.BatchWriteItemOutput, error)
- func (m *MockDynamoDBClient) CreateTable(ctx context.Context, params *dynamodb.CreateTableInput, ...) (*dynamodb.CreateTableOutput, error)
- func (m *MockDynamoDBClient) DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, ...) (*dynamodb.DeleteItemOutput, error)
- func (m *MockDynamoDBClient) DeleteTable(ctx context.Context, params *dynamodb.DeleteTableInput, ...) (*dynamodb.DeleteTableOutput, error)
- func (m *MockDynamoDBClient) DescribeTable(ctx context.Context, params *dynamodb.DescribeTableInput, ...) (*dynamodb.DescribeTableOutput, error)
- func (m *MockDynamoDBClient) GetItem(ctx context.Context, params *dynamodb.GetItemInput, ...) (*dynamodb.GetItemOutput, error)
- func (m *MockDynamoDBClient) PutItem(ctx context.Context, params *dynamodb.PutItemInput, ...) (*dynamodb.PutItemOutput, error)
- func (m *MockDynamoDBClient) Query(ctx context.Context, params *dynamodb.QueryInput, ...) (*dynamodb.QueryOutput, error)
- func (m *MockDynamoDBClient) Scan(ctx context.Context, params *dynamodb.ScanInput, ...) (*dynamodb.ScanOutput, error)
- func (m *MockDynamoDBClient) UpdateItem(ctx context.Context, params *dynamodb.UpdateItemInput, ...) (*dynamodb.UpdateItemOutput, error)
- func (m *MockDynamoDBClient) UpdateTimeToLive(ctx context.Context, params *dynamodb.UpdateTimeToLiveInput, ...) (*dynamodb.UpdateTimeToLiveOutput, error)
- type MockExtendedDB
- func (m *MockExtendedDB) AutoMigrateWithOptions(model any, opts ...any) error
- func (m *MockExtendedDB) CreateTable(model any, opts ...any) error
- func (m *MockExtendedDB) DeleteTable(model any) error
- func (m *MockExtendedDB) DescribeTable(model any) (any, error)
- func (m *MockExtendedDB) EnsureTable(model any) error
- func (m *MockExtendedDB) RegisterTypeConverter(typ reflect.Type, converter pkgTypes.CustomConverter) error
- func (m *MockExtendedDB) Transact() core.TransactionBuilder
- func (m *MockExtendedDB) TransactWrite(ctx context.Context, fn func(core.TransactionBuilder) error) error
- func (m *MockExtendedDB) TransactionFunc(fn func(tx any) error) error
- func (m *MockExtendedDB) WithLambdaTimeout(ctx context.Context) core.DB
- func (m *MockExtendedDB) WithLambdaTimeoutBuffer(buffer time.Duration) core.DB
- type MockKMSClient
- type MockQuery
- func (m *MockQuery) All(dest any) error
- func (m *MockQuery) AllPaginated(dest any) (*core.PaginatedResult, error)
- func (m *MockQuery) BatchCreate(items any) error
- func (m *MockQuery) BatchDelete(keys []any) error
- func (m *MockQuery) BatchGet(keys []any, dest any) error
- func (m *MockQuery) BatchGetBuilder() core.BatchGetBuilder
- func (m *MockQuery) BatchGetWithOptions(keys []any, dest any, opts *core.BatchGetOptions) error
- func (m *MockQuery) BatchUpdateWithOptions(items []any, fields []string, options ...any) error
- func (m *MockQuery) BatchWrite(putItems []any, deleteKeys []any) error
- func (m *MockQuery) ConsistentRead() core.Query
- func (m *MockQuery) Count() (int64, error)
- func (m *MockQuery) Create() error
- func (m *MockQuery) CreateOrUpdate() error
- func (m *MockQuery) Cursor(cursor string) core.Query
- func (m *MockQuery) Delete() error
- func (m *MockQuery) Filter(field string, op string, value any) core.Query
- func (m *MockQuery) FilterGroup(fn func(core.Query)) core.Query
- func (m *MockQuery) First(dest any) error
- func (m *MockQuery) IfExists() core.Query
- func (m *MockQuery) IfNotExists() core.Query
- func (m *MockQuery) Index(indexName string) core.Query
- func (m *MockQuery) Limit(limit int) core.Query
- func (m *MockQuery) Offset(offset int) core.Query
- func (m *MockQuery) OrFilter(field string, op string, value any) core.Query
- func (m *MockQuery) OrFilterGroup(fn func(core.Query)) core.Query
- func (m *MockQuery) OrderBy(field string, order string) core.Query
- func (m *MockQuery) ParallelScan(segment int32, totalSegments int32) core.Query
- func (m *MockQuery) Scan(dest any) error
- func (m *MockQuery) ScanAllSegments(dest any, totalSegments int32) error
- func (m *MockQuery) Select(fields ...string) core.Query
- func (m *MockQuery) SetCursor(cursor string) error
- func (m *MockQuery) Update(fields ...string) error
- func (m *MockQuery) UpdateBuilder() core.UpdateBuilder
- func (m *MockQuery) Where(field string, op string, value any) core.Query
- func (m *MockQuery) WithCondition(field, operator string, value any) core.Query
- func (m *MockQuery) WithConditionExpression(expr string, values map[string]any) core.Query
- func (m *MockQuery) WithContext(ctx context.Context) core.Query
- func (m *MockQuery) WithRetry(maxRetries int, initialDelay time.Duration) core.Query
- type MockTableExistsWaiter
- type MockTableNotExistsWaiter
- type MockTransactionBuilder
- func (m *MockTransactionBuilder) ConditionCheck(model any, conditions ...core.TransactCondition) core.TransactionBuilder
- func (m *MockTransactionBuilder) Create(model any, conditions ...core.TransactCondition) core.TransactionBuilder
- func (m *MockTransactionBuilder) Delete(model any, conditions ...core.TransactCondition) core.TransactionBuilder
- func (m *MockTransactionBuilder) Execute() error
- func (m *MockTransactionBuilder) ExecuteWithContext(ctx context.Context) error
- func (m *MockTransactionBuilder) Put(model any, conditions ...core.TransactCondition) core.TransactionBuilder
- func (m *MockTransactionBuilder) Update(model any, fields []string, conditions ...core.TransactCondition) core.TransactionBuilder
- func (m *MockTransactionBuilder) UpdateWithBuilder(model any, updateFn func(core.UpdateBuilder) error, ...) core.TransactionBuilder
- func (m *MockTransactionBuilder) WithContext(ctx context.Context) core.TransactionBuilder
- type MockUpdateBuilder
- func (m *MockUpdateBuilder) Add(field string, value any) core.UpdateBuilder
- func (m *MockUpdateBuilder) AppendToList(field string, values any) core.UpdateBuilder
- func (m *MockUpdateBuilder) Condition(field string, operator string, value any) core.UpdateBuilder
- func (m *MockUpdateBuilder) ConditionExists(field string) core.UpdateBuilder
- func (m *MockUpdateBuilder) ConditionNotExists(field string) core.UpdateBuilder
- func (m *MockUpdateBuilder) ConditionVersion(currentVersion int64) core.UpdateBuilder
- func (m *MockUpdateBuilder) Decrement(field string) core.UpdateBuilder
- func (m *MockUpdateBuilder) Delete(field string, value any) core.UpdateBuilder
- func (m *MockUpdateBuilder) Execute() error
- func (m *MockUpdateBuilder) ExecuteWithResult(result any) error
- func (m *MockUpdateBuilder) Increment(field string) core.UpdateBuilder
- func (m *MockUpdateBuilder) OrCondition(field string, operator string, value any) core.UpdateBuilder
- func (m *MockUpdateBuilder) PrependToList(field string, values any) core.UpdateBuilder
- func (m *MockUpdateBuilder) Remove(field string) core.UpdateBuilder
- func (m *MockUpdateBuilder) RemoveFromListAt(field string, index int) core.UpdateBuilder
- func (m *MockUpdateBuilder) ReturnValues(option string) core.UpdateBuilder
- func (m *MockUpdateBuilder) Set(field string, value any) core.UpdateBuilder
- func (m *MockUpdateBuilder) SetIfNotExists(field string, value any, defaultValue any) core.UpdateBuilder
- func (m *MockUpdateBuilder) SetListElement(field string, index int, value any) core.UpdateBuilder
- type Query
- type TableExistsWaiter
- type TableNotExistsWaiter
- type TransactionBuilder
- type UpdateBuilder
- type User
- type UserService
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewMockCreateTableOutput ¶
func NewMockCreateTableOutput(tableName string) *dynamodb.CreateTableOutput
NewMockCreateTableOutput creates a mock CreateTable response
func NewMockDeleteTableOutput ¶
func NewMockDeleteTableOutput(tableName string) *dynamodb.DeleteTableOutput
NewMockDeleteTableOutput creates a mock DeleteTable response
func NewMockDescribeTableOutput ¶
func NewMockDescribeTableOutput(tableName string, status types.TableStatus) *dynamodb.DescribeTableOutput
NewMockDescribeTableOutput creates a mock DescribeTable response
func NewMockUpdateTimeToLiveOutput ¶
func NewMockUpdateTimeToLiveOutput(tableName string) *dynamodb.UpdateTimeToLiveOutput
NewMockUpdateTimeToLiveOutput creates a mock UpdateTimeToLive response
Types ¶
type BatchGetBuilder ¶
type BatchGetBuilder = MockBatchGetBuilder
BatchGetBuilder is an alias for MockBatchGetBuilder to simplify usage in tests.
type DynamoDBClient ¶
type DynamoDBClient = MockDynamoDBClient
DynamoDBClient is an alias for MockDynamoDBClient
type ExtendedDB ¶
type ExtendedDB = MockExtendedDB
ExtendedDB is an alias for MockExtendedDB to allow shorter declarations
type MockBatchGetBuilder ¶
MockBatchGetBuilder is a mock implementation of core.BatchGetBuilder.
func (*MockBatchGetBuilder) ChunkSize ¶
func (m *MockBatchGetBuilder) ChunkSize(size int) core.BatchGetBuilder
ChunkSize configures the chunk size.
func (*MockBatchGetBuilder) ConsistentRead ¶
func (m *MockBatchGetBuilder) ConsistentRead() core.BatchGetBuilder
ConsistentRead enables strongly consistent reads.
func (*MockBatchGetBuilder) Execute ¶
func (m *MockBatchGetBuilder) Execute(dest any) error
Execute performs the batch get operation.
func (*MockBatchGetBuilder) Keys ¶
func (m *MockBatchGetBuilder) Keys(keys []any) core.BatchGetBuilder
Keys sets the keys to retrieve.
func (*MockBatchGetBuilder) OnError ¶
func (m *MockBatchGetBuilder) OnError(handler core.BatchChunkErrorHandler) core.BatchGetBuilder
OnError registers an error handler.
func (*MockBatchGetBuilder) OnProgress ¶
func (m *MockBatchGetBuilder) OnProgress(callback core.BatchProgressCallback) core.BatchGetBuilder
OnProgress registers a callback for progress updates.
func (*MockBatchGetBuilder) Parallel ¶
func (m *MockBatchGetBuilder) Parallel(maxConcurrency int) core.BatchGetBuilder
Parallel configures concurrency.
func (*MockBatchGetBuilder) Select ¶
func (m *MockBatchGetBuilder) Select(fields ...string) core.BatchGetBuilder
Select limits the projection.
func (*MockBatchGetBuilder) WithRetry ¶
func (m *MockBatchGetBuilder) WithRetry(policy *core.RetryPolicy) core.BatchGetBuilder
WithRetry overrides the retry policy.
type MockDB ¶
MockDB is a mock implementation of the core.DB interface. It can be used for unit testing code that depends on TableTheory.
Example usage:
mockDB := new(mocks.MockDB)
mockQuery := new(mocks.MockQuery)
mockDB.On("Model", &User{}).Return(mockQuery)
mockQuery.On("Where", "ID", "=", "123").Return(mockQuery)
mockQuery.On("First", mock.Anything).Return(nil)
func (*MockDB) AutoMigrate ¶
AutoMigrate creates or updates tables based on the given models
func (*MockDB) Transaction ¶
Transaction executes a function within a database transaction
type MockDynamoDBClient ¶
MockDynamoDBClient provides a mock implementation of the AWS DynamoDB client for testing infrastructure code that directly uses the AWS SDK.
This complements the existing TableTheory interface mocks by providing low-level AWS SDK operation mocking.
Example usage:
mockClient := new(mocks.MockDynamoDBClient)
mockClient.On("CreateTable", mock.Anything, mock.Anything).Return(&dynamodb.CreateTableOutput{}, nil)
// Use in your infrastructure code
store := &DynamoDBConnectionStore{client: mockClient}
func (*MockDynamoDBClient) BatchGetItem ¶
func (m *MockDynamoDBClient) BatchGetItem(ctx context.Context, params *dynamodb.BatchGetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.BatchGetItemOutput, error)
BatchGetItem mocks the DynamoDB BatchGetItem operation
func (*MockDynamoDBClient) BatchWriteItem ¶
func (m *MockDynamoDBClient) BatchWriteItem(ctx context.Context, params *dynamodb.BatchWriteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.BatchWriteItemOutput, error)
BatchWriteItem mocks the DynamoDB BatchWriteItem operation
func (*MockDynamoDBClient) CreateTable ¶
func (m *MockDynamoDBClient) CreateTable(ctx context.Context, params *dynamodb.CreateTableInput, optFns ...func(*dynamodb.Options)) (*dynamodb.CreateTableOutput, error)
CreateTable mocks the DynamoDB CreateTable operation
func (*MockDynamoDBClient) DeleteItem ¶
func (m *MockDynamoDBClient) DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
DeleteItem mocks the DynamoDB DeleteItem operation
func (*MockDynamoDBClient) DeleteTable ¶
func (m *MockDynamoDBClient) DeleteTable(ctx context.Context, params *dynamodb.DeleteTableInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteTableOutput, error)
DeleteTable mocks the DynamoDB DeleteTable operation
func (*MockDynamoDBClient) DescribeTable ¶
func (m *MockDynamoDBClient) DescribeTable(ctx context.Context, params *dynamodb.DescribeTableInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DescribeTableOutput, error)
DescribeTable mocks the DynamoDB DescribeTable operation
func (*MockDynamoDBClient) GetItem ¶
func (m *MockDynamoDBClient) GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
GetItem mocks the DynamoDB GetItem operation
func (*MockDynamoDBClient) PutItem ¶
func (m *MockDynamoDBClient) PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
PutItem mocks the DynamoDB PutItem operation
func (*MockDynamoDBClient) Query ¶
func (m *MockDynamoDBClient) Query(ctx context.Context, params *dynamodb.QueryInput, optFns ...func(*dynamodb.Options)) (*dynamodb.QueryOutput, error)
Query mocks the DynamoDB Query operation
func (*MockDynamoDBClient) Scan ¶
func (m *MockDynamoDBClient) Scan(ctx context.Context, params *dynamodb.ScanInput, optFns ...func(*dynamodb.Options)) (*dynamodb.ScanOutput, error)
Scan mocks the DynamoDB Scan operation
func (*MockDynamoDBClient) UpdateItem ¶
func (m *MockDynamoDBClient) UpdateItem(ctx context.Context, params *dynamodb.UpdateItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.UpdateItemOutput, error)
UpdateItem mocks the DynamoDB UpdateItem operation
func (*MockDynamoDBClient) UpdateTimeToLive ¶
func (m *MockDynamoDBClient) UpdateTimeToLive(ctx context.Context, params *dynamodb.UpdateTimeToLiveInput, optFns ...func(*dynamodb.Options)) (*dynamodb.UpdateTimeToLiveOutput, error)
UpdateTimeToLive mocks the DynamoDB UpdateTimeToLive operation
type MockExtendedDB ¶
type MockExtendedDB struct {
// TransactWriteBuilder is used when TransactWrite auto-executes the provided callback.
// If nil, a new MockTransactionBuilder is created for each call.
TransactWriteBuilder core.TransactionBuilder
// TransactionFuncTx is passed to TransactionFunc when auto-executing callbacks.
TransactionFuncTx any
MockDB // Embed MockDB to inherit base methods
}
MockExtendedDB is a complete mock implementation of core.ExtendedDB. It embeds MockDB to inherit the base DB interface methods and adds the additional methods required by ExtendedDB.
Example usage:
mockDB := mocks.NewMockExtendedDB()
mockQuery := new(mocks.MockQuery)
mockDB.On("Model", &User{}).Return(mockQuery)
mockQuery.On("Create").Return(nil)
func NewMockExtendedDB ¶
func NewMockExtendedDB() *MockExtendedDB
NewMockExtendedDB creates a new MockExtendedDB with sensible defaults for methods that are rarely used in unit tests. This reduces boilerplate in tests that only need to mock core functionality.
func NewMockExtendedDBStrict ¶
func NewMockExtendedDBStrict() *MockExtendedDB
NewMockExtendedDBStrict creates a MockExtendedDB without any default expectations. Use this when you want to explicitly set all expectations.
func (*MockExtendedDB) AutoMigrateWithOptions ¶
func (m *MockExtendedDB) AutoMigrateWithOptions(model any, opts ...any) error
AutoMigrateWithOptions performs enhanced auto-migration with options
func (*MockExtendedDB) CreateTable ¶
func (m *MockExtendedDB) CreateTable(model any, opts ...any) error
CreateTable creates a DynamoDB table for the given model
func (*MockExtendedDB) DeleteTable ¶
func (m *MockExtendedDB) DeleteTable(model any) error
DeleteTable deletes the DynamoDB table for the given model
func (*MockExtendedDB) DescribeTable ¶
func (m *MockExtendedDB) DescribeTable(model any) (any, error)
DescribeTable returns the table description for the given model
func (*MockExtendedDB) EnsureTable ¶
func (m *MockExtendedDB) EnsureTable(model any) error
EnsureTable checks if a table exists and creates it if not
func (*MockExtendedDB) RegisterTypeConverter ¶
func (m *MockExtendedDB) RegisterTypeConverter(typ reflect.Type, converter pkgTypes.CustomConverter) error
RegisterTypeConverter registers a custom converter for a specific type
func (*MockExtendedDB) Transact ¶
func (m *MockExtendedDB) Transact() core.TransactionBuilder
Transact returns a transaction builder mock
func (*MockExtendedDB) TransactWrite ¶
func (m *MockExtendedDB) TransactWrite(ctx context.Context, fn func(core.TransactionBuilder) error) error
TransactWrite executes a function with a transaction builder
func (*MockExtendedDB) TransactionFunc ¶
func (m *MockExtendedDB) TransactionFunc(fn func(tx any) error) error
TransactionFunc executes a function within a full transaction context
func (*MockExtendedDB) WithLambdaTimeout ¶
func (m *MockExtendedDB) WithLambdaTimeout(ctx context.Context) core.DB
WithLambdaTimeout sets a deadline based on Lambda context
func (*MockExtendedDB) WithLambdaTimeoutBuffer ¶
func (m *MockExtendedDB) WithLambdaTimeoutBuffer(buffer time.Duration) core.DB
WithLambdaTimeoutBuffer sets a custom timeout buffer
type MockKMSClient ¶
MockKMSClient provides a mock implementation of the AWS KMS client for testing TableTheory encryption flows without real AWS KMS calls.
Example usage:
kmsMock := new(mocks.MockKMSClient)
kmsMock.On("GenerateDataKey", mock.Anything, mock.Anything, mock.Anything).
Return(&kms.GenerateDataKeyOutput{Plaintext: key, CiphertextBlob: edk}, nil)
db, _ := tabletheory.New(session.Config{
Region: "us-east-1",
KMSKeyARN: "arn:aws:kms:us-east-1:111111111111:key/test",
KMSClient: kmsMock,
})
func (*MockKMSClient) Decrypt ¶
func (m *MockKMSClient) Decrypt(ctx context.Context, params *kms.DecryptInput, optFns ...func(*kms.Options)) (*kms.DecryptOutput, error)
func (*MockKMSClient) GenerateDataKey ¶
func (m *MockKMSClient) GenerateDataKey(ctx context.Context, params *kms.GenerateDataKeyInput, optFns ...func(*kms.Options)) (*kms.GenerateDataKeyOutput, error)
type MockQuery ¶
MockQuery is a mock implementation of the core.Query interface. It can be used for unit testing code that depends on TableTheory queries.
Example usage:
mockQuery := new(mocks.MockQuery)
mockQuery.On("Where", "ID", "=", "123").Return(mockQuery)
mockQuery.On("First", mock.Anything).Return(nil)
func (*MockQuery) AllPaginated ¶
func (m *MockQuery) AllPaginated(dest any) (*core.PaginatedResult, error)
AllPaginated retrieves all matching items with pagination metadata
func (*MockQuery) BatchCreate ¶
BatchCreate creates multiple items
func (*MockQuery) BatchDelete ¶
BatchDelete deletes multiple items by their primary keys
func (*MockQuery) BatchGetBuilder ¶
func (m *MockQuery) BatchGetBuilder() core.BatchGetBuilder
BatchGetBuilder returns a fluent builder for BatchGet
func (*MockQuery) BatchGetWithOptions ¶
BatchGetWithOptions retrieves multiple items with custom options
func (*MockQuery) BatchUpdateWithOptions ¶
BatchUpdateWithOptions performs batch update operations with custom options
func (*MockQuery) BatchWrite ¶
BatchWrite performs mixed batch write operations
func (*MockQuery) ConsistentRead ¶
ConsistentRead enables strongly consistent reads for Query operations
func (*MockQuery) CreateOrUpdate ¶
CreateOrUpdate creates a new item or updates an existing one (upsert)
func (*MockQuery) FilterGroup ¶
FilterGroup adds a group of filters with AND logic
func (*MockQuery) IfNotExists ¶
IfNotExists adds a condition that the item must not exist
func (*MockQuery) OrFilterGroup ¶
OrFilterGroup adds a group of filters with OR logic
func (*MockQuery) ParallelScan ¶
ParallelScan configures parallel scanning
func (*MockQuery) ScanAllSegments ¶
ScanAllSegments performs parallel scan across all segments
func (*MockQuery) UpdateBuilder ¶
func (m *MockQuery) UpdateBuilder() core.UpdateBuilder
UpdateBuilder returns a builder for complex update operations
func (*MockQuery) WithCondition ¶
WithCondition adds a generic condition expression
func (*MockQuery) WithConditionExpression ¶
WithConditionExpression adds a raw condition expression
func (*MockQuery) WithContext ¶
WithContext sets the context for the query
type MockTableExistsWaiter ¶
MockTableExistsWaiter provides a mock implementation of the DynamoDB table exists waiter
Example usage:
mockWaiter := new(mocks.MockTableExistsWaiter)
mockWaiter.On("Wait", mock.Anything, mock.Anything, mock.Anything).Return(nil)
func (*MockTableExistsWaiter) Wait ¶
func (m *MockTableExistsWaiter) Wait(ctx context.Context, params *dynamodb.DescribeTableInput, maxWaitDur time.Duration, optFns ...func(*dynamodb.TableExistsWaiterOptions)) error
Wait mocks waiting for a table to exist
type MockTableNotExistsWaiter ¶
MockTableNotExistsWaiter provides a mock implementation of the DynamoDB table not exists waiter
Example usage:
mockWaiter := new(mocks.MockTableNotExistsWaiter)
mockWaiter.On("Wait", mock.Anything, mock.Anything, mock.Anything).Return(nil)
func (*MockTableNotExistsWaiter) Wait ¶
func (m *MockTableNotExistsWaiter) Wait(ctx context.Context, params *dynamodb.DescribeTableInput, maxWaitDur time.Duration, optFns ...func(*dynamodb.TableNotExistsWaiterOptions)) error
Wait mocks waiting for a table to not exist (be deleted)
type MockTransactionBuilder ¶ added in v1.2.0
type MockTransactionBuilder struct {
mock.Mock
// UpdateBuilder is used when executing UpdateWithBuilder callbacks during Execute/ExecuteWithContext.
// If nil, a no-op implementation is used.
UpdateBuilder core.UpdateBuilder
// contains filtered or unexported fields
}
MockTransactionBuilder is a mock implementation of the core.TransactionBuilder interface.
It supports the fluent transaction DSL used by ExtendedDB.TransactWrite and can optionally execute UpdateWithBuilder callbacks when Execute/ExecuteWithContext is invoked.
func (*MockTransactionBuilder) ConditionCheck ¶ added in v1.2.0
func (m *MockTransactionBuilder) ConditionCheck(model any, conditions ...core.TransactCondition) core.TransactionBuilder
ConditionCheck adds a pure condition check without mutating data.
func (*MockTransactionBuilder) Create ¶ added in v1.2.0
func (m *MockTransactionBuilder) Create(model any, conditions ...core.TransactCondition) core.TransactionBuilder
Create adds a put operation guarded by attribute_not_exists on the primary key.
func (*MockTransactionBuilder) Delete ¶ added in v1.2.0
func (m *MockTransactionBuilder) Delete(model any, conditions ...core.TransactCondition) core.TransactionBuilder
Delete removes the provided model by primary key.
func (*MockTransactionBuilder) Execute ¶ added in v1.2.0
func (m *MockTransactionBuilder) Execute() error
Execute commits the transaction using the currently configured context.
func (*MockTransactionBuilder) ExecuteWithContext ¶ added in v1.2.0
func (m *MockTransactionBuilder) ExecuteWithContext(ctx context.Context) error
ExecuteWithContext commits the transaction with an explicit context override.
func (*MockTransactionBuilder) Put ¶ added in v1.2.0
func (m *MockTransactionBuilder) Put(model any, conditions ...core.TransactCondition) core.TransactionBuilder
Put adds a put (upsert) operation.
func (*MockTransactionBuilder) Update ¶ added in v1.2.0
func (m *MockTransactionBuilder) Update(model any, fields []string, conditions ...core.TransactCondition) core.TransactionBuilder
Update updates selected fields on the provided model.
func (*MockTransactionBuilder) UpdateWithBuilder ¶ added in v1.2.0
func (m *MockTransactionBuilder) UpdateWithBuilder(model any, updateFn func(core.UpdateBuilder) error, conditions ...core.TransactCondition) core.TransactionBuilder
UpdateWithBuilder allows complex expression-based updates.
func (*MockTransactionBuilder) WithContext ¶ added in v1.2.0
func (m *MockTransactionBuilder) WithContext(ctx context.Context) core.TransactionBuilder
WithContext sets the context used for DynamoDB calls.
type MockUpdateBuilder ¶
MockUpdateBuilder is a mock implementation of the core.UpdateBuilder interface. It can be used for unit testing code that uses TableTheory's update builder pattern.
Example usage:
mockUpdateBuilder := new(mocks.MockUpdateBuilder)
mockUpdateBuilder.On("Set", "status", "active").Return(mockUpdateBuilder)
mockUpdateBuilder.On("Execute").Return(nil)
func (*MockUpdateBuilder) Add ¶
func (m *MockUpdateBuilder) Add(field string, value any) core.UpdateBuilder
Add performs atomic addition (for numbers) or adds to a set
func (*MockUpdateBuilder) AppendToList ¶
func (m *MockUpdateBuilder) AppendToList(field string, values any) core.UpdateBuilder
AppendToList appends values to the end of a list
func (*MockUpdateBuilder) Condition ¶
func (m *MockUpdateBuilder) Condition(field string, operator string, value any) core.UpdateBuilder
Condition adds a condition that must be met for the update to succeed
func (*MockUpdateBuilder) ConditionExists ¶
func (m *MockUpdateBuilder) ConditionExists(field string) core.UpdateBuilder
ConditionExists adds a condition that the field must exist
func (*MockUpdateBuilder) ConditionNotExists ¶
func (m *MockUpdateBuilder) ConditionNotExists(field string) core.UpdateBuilder
ConditionNotExists adds a condition that the field must not exist
func (*MockUpdateBuilder) ConditionVersion ¶
func (m *MockUpdateBuilder) ConditionVersion(currentVersion int64) core.UpdateBuilder
ConditionVersion adds optimistic locking based on version
func (*MockUpdateBuilder) Decrement ¶
func (m *MockUpdateBuilder) Decrement(field string) core.UpdateBuilder
Decrement decrements a numeric field by 1
func (*MockUpdateBuilder) Delete ¶
func (m *MockUpdateBuilder) Delete(field string, value any) core.UpdateBuilder
Delete removes values from a set
func (*MockUpdateBuilder) Execute ¶
func (m *MockUpdateBuilder) Execute() error
Execute performs the update operation
func (*MockUpdateBuilder) ExecuteWithResult ¶
func (m *MockUpdateBuilder) ExecuteWithResult(result any) error
ExecuteWithResult performs the update and returns the result
func (*MockUpdateBuilder) Increment ¶
func (m *MockUpdateBuilder) Increment(field string) core.UpdateBuilder
Increment increments a numeric field by 1
func (*MockUpdateBuilder) OrCondition ¶
func (m *MockUpdateBuilder) OrCondition(field string, operator string, value any) core.UpdateBuilder
OrCondition adds a condition with OR logic
func (*MockUpdateBuilder) PrependToList ¶
func (m *MockUpdateBuilder) PrependToList(field string, values any) core.UpdateBuilder
PrependToList prepends values to the beginning of a list
func (*MockUpdateBuilder) Remove ¶
func (m *MockUpdateBuilder) Remove(field string) core.UpdateBuilder
Remove removes an attribute from the item
func (*MockUpdateBuilder) RemoveFromListAt ¶
func (m *MockUpdateBuilder) RemoveFromListAt(field string, index int) core.UpdateBuilder
RemoveFromListAt removes an element at a specific index from a list
func (*MockUpdateBuilder) ReturnValues ¶
func (m *MockUpdateBuilder) ReturnValues(option string) core.UpdateBuilder
ReturnValues specifies what values to return after the update
func (*MockUpdateBuilder) Set ¶
func (m *MockUpdateBuilder) Set(field string, value any) core.UpdateBuilder
Set updates a field to a new value
func (*MockUpdateBuilder) SetIfNotExists ¶
func (m *MockUpdateBuilder) SetIfNotExists(field string, value any, defaultValue any) core.UpdateBuilder
SetIfNotExists sets a field value only if it doesn't already exist
func (*MockUpdateBuilder) SetListElement ¶
func (m *MockUpdateBuilder) SetListElement(field string, index int, value any) core.UpdateBuilder
SetListElement sets a specific element in a list
type TableExistsWaiter ¶
type TableExistsWaiter = MockTableExistsWaiter
TableExistsWaiter is an alias for MockTableExistsWaiter
type TableNotExistsWaiter ¶
type TableNotExistsWaiter = MockTableNotExistsWaiter
TableNotExistsWaiter is an alias for MockTableNotExistsWaiter
type TransactionBuilder ¶ added in v1.2.0
type TransactionBuilder = MockTransactionBuilder
TransactionBuilder is an alias for MockTransactionBuilder to simplify usage in tests.
type UpdateBuilder ¶
type UpdateBuilder = MockUpdateBuilder
UpdateBuilder is an alias for MockUpdateBuilder to allow shorter declarations
type UserService ¶
type UserService struct {
// contains filtered or unexported fields
}
🚀 STEP 2: Create a service that uses TableTheory This is the real business logic you want to test
func NewUserService ¶
func NewUserService(db core.DB) *UserService
func (*UserService) CreateUser ¶
func (s *UserService) CreateUser(user *User) error
CreateUser creates a new user
func (*UserService) GetActiveUsers ¶
func (s *UserService) GetActiveUsers() ([]User, error)
GetActiveUsers gets all users over 18
func (*UserService) GetUser ¶
func (s *UserService) GetUser(id string) (*User, error)
GetUser fetches a user by ID
func (*UserService) UpdateUserEmail ¶
func (s *UserService) UpdateUserEmail(id, newEmail string) error
UpdateUserEmail updates a user's email