Documentation
¶
Overview ¶
Package testing provides utilities for testing applications that use DynamORM. It includes mock factories, test helpers, and common testing scenarios.
Index ¶
- type CommonScenarios
- func (s *CommonScenarios) SetupBatchOperations()
- func (s *CommonScenarios) SetupCRUD(model interface{})
- func (s *CommonScenarios) SetupComplexQuery()
- func (s *CommonScenarios) SetupErrorScenarios(errorMap map[string]error)
- func (s *CommonScenarios) SetupIndexQuery(indexName string)
- func (s *CommonScenarios) SetupMultiTenant(tenantID string)
- func (s *CommonScenarios) SetupPagination(pageSize int)
- func (s *CommonScenarios) SetupScanScenario()
- func (s *CommonScenarios) SetupTransactionScenario(success bool)
- func (s *CommonScenarios) SetupUpdateBuilder()
- type ConfigurableDBFactory
- type ConfigurableMockDBFactory
- type DBFactory
- type DefaultDBFactory
- type FactoryConfig
- type Middleware
- type MockDBFactory
- 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 Operation
- type QueryChain
- func (q *QueryChain) ExpectAll(results interface{}) *TestDB
- func (q *QueryChain) ExpectFirst(result interface{}) *TestDB
- func (q *QueryChain) Limit(limit int) *QueryChain
- func (q *QueryChain) OrderBy(field string, order string) *QueryChain
- func (q *QueryChain) Where(field string, op string, value interface{}) *QueryChain
- type TestDB
- func (t *TestDB) AssertExpectations(testing mock.TestingT)
- func (t *TestDB) ExpectAll(results interface{}) *TestDB
- func (t *TestDB) ExpectBatchCreate(items interface{}) *TestDB
- func (t *TestDB) ExpectBatchDelete(keys []interface{}) *TestDB
- func (t *TestDB) ExpectBatchGet(keys []interface{}, results interface{}) *TestDB
- func (t *TestDB) ExpectCount(count int64) *TestDB
- func (t *TestDB) ExpectCreate() *TestDB
- func (t *TestDB) ExpectCreateError(err error) *TestDB
- func (t *TestDB) ExpectDelete() *TestDB
- func (t *TestDB) ExpectDeleteError(err error) *TestDB
- func (t *TestDB) ExpectFind(result interface{}) *TestDB
- func (t *TestDB) ExpectIndex(indexName string) *TestDB
- func (t *TestDB) ExpectLimit(limit int) *TestDB
- func (t *TestDB) ExpectModel(model interface{}) *TestDB
- func (t *TestDB) ExpectNotFound() *TestDB
- func (t *TestDB) ExpectOffset(offset int) *TestDB
- func (t *TestDB) ExpectOrderBy(field string, order string) *TestDB
- func (t *TestDB) ExpectTransaction(setupFunc func(tx *core.Tx)) *TestDB
- func (t *TestDB) ExpectTransactionError(err error) *TestDB
- func (t *TestDB) ExpectUpdate(fields ...string) *TestDB
- func (t *TestDB) ExpectUpdateError(err error, fields ...string) *TestDB
- func (t *TestDB) ExpectWhere(field string, op string, value interface{}) *TestDB
- func (t *TestDB) NewQueryChain() *QueryChain
- func (t *TestDB) Reset()
- type TestDBFactory
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CommonScenarios ¶
type CommonScenarios struct {
// contains filtered or unexported fields
}
CommonScenarios provides pre-built test scenarios
func NewCommonScenarios ¶
func NewCommonScenarios(db *TestDB) *CommonScenarios
NewCommonScenarios creates common test scenarios
func (*CommonScenarios) SetupBatchOperations ¶
func (s *CommonScenarios) SetupBatchOperations()
SetupBatchOperations sets up expectations for batch operations
func (*CommonScenarios) SetupCRUD ¶
func (s *CommonScenarios) SetupCRUD(model interface{})
SetupCRUD sets up expectations for basic CRUD operations
func (*CommonScenarios) SetupComplexQuery ¶
func (s *CommonScenarios) SetupComplexQuery()
SetupComplexQuery sets up expectations for complex queries with multiple conditions
func (*CommonScenarios) SetupErrorScenarios ¶
func (s *CommonScenarios) SetupErrorScenarios(errorMap map[string]error)
SetupErrorScenarios sets up common error scenarios
func (*CommonScenarios) SetupIndexQuery ¶
func (s *CommonScenarios) SetupIndexQuery(indexName string)
SetupIndexQuery sets up expectations for index-based queries
func (*CommonScenarios) SetupMultiTenant ¶
func (s *CommonScenarios) SetupMultiTenant(tenantID string)
SetupMultiTenant sets up expectations for multi-tenant queries
func (*CommonScenarios) SetupPagination ¶
func (s *CommonScenarios) SetupPagination(pageSize int)
SetupPagination sets up expectations for paginated queries
func (*CommonScenarios) SetupScanScenario ¶
func (s *CommonScenarios) SetupScanScenario()
SetupScanScenario sets up expectations for scan operations
func (*CommonScenarios) SetupTransactionScenario ¶
func (s *CommonScenarios) SetupTransactionScenario(success bool)
SetupTransactionScenario sets up expectations for transactional operations
func (*CommonScenarios) SetupUpdateBuilder ¶
func (s *CommonScenarios) SetupUpdateBuilder()
SetupUpdateBuilder sets up expectations for complex update operations
type ConfigurableDBFactory ¶
type ConfigurableDBFactory interface {
DBFactory
// WithConfig applies configuration to the factory
WithConfig(config FactoryConfig) ConfigurableDBFactory
}
ConfigurableDBFactory is a factory that supports additional configuration
type ConfigurableMockDBFactory ¶
type ConfigurableMockDBFactory struct {
*MockDBFactory
// contains filtered or unexported fields
}
ConfigurableMockDBFactory extends MockDBFactory with configuration support
func NewConfigurableMockDBFactory ¶
func NewConfigurableMockDBFactory() *ConfigurableMockDBFactory
NewConfigurableMockDBFactory creates a new configurable mock factory
func (*ConfigurableMockDBFactory) CreateDB ¶
func (f *ConfigurableMockDBFactory) CreateDB(config session.Config) (core.ExtendedDB, error)
CreateDB creates a mock database with applied configuration
func (*ConfigurableMockDBFactory) WithConfig ¶
func (f *ConfigurableMockDBFactory) WithConfig(config FactoryConfig) ConfigurableDBFactory
WithConfig applies configuration to the factory
type DBFactory ¶
type DBFactory interface {
// CreateDB creates a new database instance with the given configuration
CreateDB(config session.Config) (core.ExtendedDB, error)
}
DBFactory is an interface for creating DynamORM database instances. It allows for dependency injection in middleware and makes testing easier. This version returns ExtendedDB for full compatibility.
func SimpleMockFactory ¶
func SimpleMockFactory(setupFunc func(db *mocks.MockExtendedDB)) DBFactory
SimpleMockFactory provides a simple way to create mock factories for testing
type DefaultDBFactory ¶
type DefaultDBFactory struct{}
DefaultDBFactory creates real DynamORM instances for production use
func (*DefaultDBFactory) CreateDB ¶
func (f *DefaultDBFactory) CreateDB(config session.Config) (core.ExtendedDB, error)
CreateDB creates a real DynamORM database connection
type FactoryConfig ¶
type FactoryConfig struct {
// EnableLogging enables debug logging for database operations
EnableLogging bool
// EnableMetrics enables metrics collection
EnableMetrics bool
// Middleware allows injecting custom middleware into the database
Middleware []Middleware
}
FactoryConfig provides configuration options for database factories
type Middleware ¶
Middleware represents a database operation middleware
type MockDBFactory ¶
type MockDBFactory struct {
// MockDB is the mock database instance that will be returned
MockDB core.ExtendedDB
// Error can be set to simulate connection failures
Error error
// OnCreateDB is called when CreateDB is invoked, useful for assertions
OnCreateDB func(config session.Config)
}
MockDBFactory creates mock DynamORM instances for testing
func NewMockDBFactory ¶
func NewMockDBFactory() *MockDBFactory
NewMockDBFactory creates a new MockDBFactory with a default MockExtendedDB
func (*MockDBFactory) CreateDB ¶
func (f *MockDBFactory) CreateDB(config session.Config) (core.ExtendedDB, error)
CreateDB returns the configured mock database or error
func (*MockDBFactory) WithError ¶
func (f *MockDBFactory) WithError(err error) *MockDBFactory
WithError configures the factory to return an error
func (*MockDBFactory) WithMockDB ¶
func (f *MockDBFactory) WithMockDB(mockDB core.ExtendedDB) *MockDBFactory
WithMockDB sets a specific mock database instance
type MockUpdateBuilder ¶
MockUpdateBuilder is a mock implementation of UpdateBuilder
func (*MockUpdateBuilder) Add ¶
func (m *MockUpdateBuilder) Add(field string, value any) core.UpdateBuilder
func (*MockUpdateBuilder) AppendToList ¶
func (m *MockUpdateBuilder) AppendToList(field string, values any) core.UpdateBuilder
func (*MockUpdateBuilder) Condition ¶
func (m *MockUpdateBuilder) Condition(field string, operator string, value any) core.UpdateBuilder
func (*MockUpdateBuilder) ConditionExists ¶
func (m *MockUpdateBuilder) ConditionExists(field string) core.UpdateBuilder
func (*MockUpdateBuilder) ConditionNotExists ¶
func (m *MockUpdateBuilder) ConditionNotExists(field string) core.UpdateBuilder
func (*MockUpdateBuilder) ConditionVersion ¶
func (m *MockUpdateBuilder) ConditionVersion(currentVersion int64) core.UpdateBuilder
func (*MockUpdateBuilder) Decrement ¶
func (m *MockUpdateBuilder) Decrement(field string) core.UpdateBuilder
func (*MockUpdateBuilder) Delete ¶
func (m *MockUpdateBuilder) Delete(field string, value any) core.UpdateBuilder
func (*MockUpdateBuilder) Execute ¶
func (m *MockUpdateBuilder) Execute() error
func (*MockUpdateBuilder) ExecuteWithResult ¶
func (m *MockUpdateBuilder) ExecuteWithResult(result any) error
func (*MockUpdateBuilder) Increment ¶
func (m *MockUpdateBuilder) Increment(field string) core.UpdateBuilder
func (*MockUpdateBuilder) OrCondition ¶
func (m *MockUpdateBuilder) OrCondition(field string, operator string, value any) core.UpdateBuilder
func (*MockUpdateBuilder) PrependToList ¶
func (m *MockUpdateBuilder) PrependToList(field string, values any) core.UpdateBuilder
func (*MockUpdateBuilder) Remove ¶
func (m *MockUpdateBuilder) Remove(field string) core.UpdateBuilder
func (*MockUpdateBuilder) RemoveFromListAt ¶
func (m *MockUpdateBuilder) RemoveFromListAt(field string, index int) core.UpdateBuilder
func (*MockUpdateBuilder) ReturnValues ¶
func (m *MockUpdateBuilder) ReturnValues(option string) core.UpdateBuilder
func (*MockUpdateBuilder) Set ¶
func (m *MockUpdateBuilder) Set(field string, value any) core.UpdateBuilder
func (*MockUpdateBuilder) SetIfNotExists ¶
func (m *MockUpdateBuilder) SetIfNotExists(field string, value any, defaultValue any) core.UpdateBuilder
func (*MockUpdateBuilder) SetListElement ¶
func (m *MockUpdateBuilder) SetListElement(field string, index int, value any) core.UpdateBuilder
type Operation ¶
type Operation func() error
Operation represents a database operation that can be intercepted
type QueryChain ¶
type QueryChain struct {
// contains filtered or unexported fields
}
QueryChain helps build complex query expectations
func (*QueryChain) ExpectAll ¶
func (q *QueryChain) ExpectAll(results interface{}) *TestDB
ExpectAll finalizes the chain with an All expectation
func (*QueryChain) ExpectFirst ¶
func (q *QueryChain) ExpectFirst(result interface{}) *TestDB
ExpectFirst finalizes the chain with a First expectation
func (*QueryChain) Limit ¶
func (q *QueryChain) Limit(limit int) *QueryChain
Limit adds a limit to the chain
func (*QueryChain) OrderBy ¶
func (q *QueryChain) OrderBy(field string, order string) *QueryChain
OrderBy adds ordering to the chain
func (*QueryChain) Where ¶
func (q *QueryChain) Where(field string, op string, value interface{}) *QueryChain
Where adds a where condition to the chain
type TestDB ¶
TestDB provides a fluent interface for setting up mock expectations
func (*TestDB) AssertExpectations ¶
AssertExpectations asserts that all expectations were met
func (*TestDB) ExpectBatchCreate ¶
ExpectBatchCreate sets up expectations for batch create operations
func (*TestDB) ExpectBatchDelete ¶
ExpectBatchDelete sets up expectations for batch delete operations
func (*TestDB) ExpectBatchGet ¶
ExpectBatchGet sets up expectations for batch get operations
func (*TestDB) ExpectCount ¶
ExpectCount sets up expectations for count operations
func (*TestDB) ExpectCreate ¶
ExpectCreate sets up expectations for a create operation
func (*TestDB) ExpectCreateError ¶
ExpectCreateError sets up expectations for a failed create
func (*TestDB) ExpectDelete ¶
ExpectDelete sets up expectations for delete operations
func (*TestDB) ExpectDeleteError ¶
ExpectDeleteError sets up expectations for failed delete
func (*TestDB) ExpectFind ¶
ExpectFind sets up expectations for finding a record
func (*TestDB) ExpectIndex ¶
ExpectIndex sets up expectations for index usage
func (*TestDB) ExpectLimit ¶
ExpectLimit sets up expectations for limit operations
func (*TestDB) ExpectModel ¶
ExpectModel sets up expectations for Model calls
func (*TestDB) ExpectNotFound ¶
ExpectNotFound sets up expectations for a not found error
func (*TestDB) ExpectOffset ¶
ExpectOffset sets up expectations for offset operations
func (*TestDB) ExpectOrderBy ¶
ExpectOrderBy sets up expectations for ordering
func (*TestDB) ExpectTransaction ¶
ExpectTransaction sets up transaction expectations
func (*TestDB) ExpectTransactionError ¶
ExpectTransactionError sets up expectations for a failed transaction
func (*TestDB) ExpectUpdate ¶
ExpectUpdate sets up expectations for update operations
func (*TestDB) ExpectUpdateError ¶
ExpectUpdateError sets up expectations for failed update
func (*TestDB) ExpectWhere ¶
ExpectWhere sets up expectations for where conditions
func (*TestDB) NewQueryChain ¶
func (t *TestDB) NewQueryChain() *QueryChain
NewQueryChain creates a new query chain builder
type TestDBFactory ¶
type TestDBFactory struct {
// Instances tracks all database instances created by this factory
Instances []core.ExtendedDB
// CreateFunc allows customizing the creation behavior
CreateFunc func(config session.Config) (core.ExtendedDB, error)
}
TestDBFactory is a specialized factory for testing that tracks all created instances
func (*TestDBFactory) CreateDB ¶
func (f *TestDBFactory) CreateDB(config session.Config) (core.ExtendedDB, error)
CreateDB creates a database instance and tracks it
func (*TestDBFactory) GetLastInstance ¶
func (f *TestDBFactory) GetLastInstance() core.ExtendedDB
GetLastInstance returns the most recently created instance