testing

package
v1.0.35 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package testing provides utilities for testing applications that use DynamORM. It includes mock factories, test helpers, and common testing scenarios.

Index

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

CreateDB creates a mock database with applied configuration

func (*ConfigurableMockDBFactory) WithConfig

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

type Middleware func(next Operation) Operation

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

type MockUpdateBuilder struct {
	mock.Mock
}

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

type TestDB struct {
	MockDB    *mocks.MockDB
	MockQuery *mocks.MockQuery
}

TestDB provides a fluent interface for setting up mock expectations

func NewTestDB

func NewTestDB() *TestDB

NewTestDB creates a new test database with common setup

func (*TestDB) AssertExpectations

func (t *TestDB) AssertExpectations(testing mock.TestingT)

AssertExpectations asserts that all expectations were met

func (*TestDB) ExpectAll

func (t *TestDB) ExpectAll(results interface{}) *TestDB

ExpectAll sets up expectations for retrieving all records

func (*TestDB) ExpectBatchCreate

func (t *TestDB) ExpectBatchCreate(items interface{}) *TestDB

ExpectBatchCreate sets up expectations for batch create operations

func (*TestDB) ExpectBatchDelete

func (t *TestDB) ExpectBatchDelete(keys []interface{}) *TestDB

ExpectBatchDelete sets up expectations for batch delete operations

func (*TestDB) ExpectBatchGet

func (t *TestDB) ExpectBatchGet(keys []interface{}, results interface{}) *TestDB

ExpectBatchGet sets up expectations for batch get operations

func (*TestDB) ExpectCount

func (t *TestDB) ExpectCount(count int64) *TestDB

ExpectCount sets up expectations for count operations

func (*TestDB) ExpectCreate

func (t *TestDB) ExpectCreate() *TestDB

ExpectCreate sets up expectations for a create operation

func (*TestDB) ExpectCreateError

func (t *TestDB) ExpectCreateError(err error) *TestDB

ExpectCreateError sets up expectations for a failed create

func (*TestDB) ExpectDelete

func (t *TestDB) ExpectDelete() *TestDB

ExpectDelete sets up expectations for delete operations

func (*TestDB) ExpectDeleteError

func (t *TestDB) ExpectDeleteError(err error) *TestDB

ExpectDeleteError sets up expectations for failed delete

func (*TestDB) ExpectFind

func (t *TestDB) ExpectFind(result interface{}) *TestDB

ExpectFind sets up expectations for finding a record

func (*TestDB) ExpectIndex

func (t *TestDB) ExpectIndex(indexName string) *TestDB

ExpectIndex sets up expectations for index usage

func (*TestDB) ExpectLimit

func (t *TestDB) ExpectLimit(limit int) *TestDB

ExpectLimit sets up expectations for limit operations

func (*TestDB) ExpectModel

func (t *TestDB) ExpectModel(model interface{}) *TestDB

ExpectModel sets up expectations for Model calls

func (*TestDB) ExpectNotFound

func (t *TestDB) ExpectNotFound() *TestDB

ExpectNotFound sets up expectations for a not found error

func (*TestDB) ExpectOffset

func (t *TestDB) ExpectOffset(offset int) *TestDB

ExpectOffset sets up expectations for offset operations

func (*TestDB) ExpectOrderBy

func (t *TestDB) ExpectOrderBy(field string, order string) *TestDB

ExpectOrderBy sets up expectations for ordering

func (*TestDB) ExpectTransaction

func (t *TestDB) ExpectTransaction(setupFunc func(tx *core.Tx)) *TestDB

ExpectTransaction sets up transaction expectations

func (*TestDB) ExpectTransactionError

func (t *TestDB) ExpectTransactionError(err error) *TestDB

ExpectTransactionError sets up expectations for a failed transaction

func (*TestDB) ExpectUpdate

func (t *TestDB) ExpectUpdate(fields ...string) *TestDB

ExpectUpdate sets up expectations for update operations

func (*TestDB) ExpectUpdateError

func (t *TestDB) ExpectUpdateError(err error, fields ...string) *TestDB

ExpectUpdateError sets up expectations for failed update

func (*TestDB) ExpectWhere

func (t *TestDB) ExpectWhere(field string, op string, value interface{}) *TestDB

ExpectWhere sets up expectations for where conditions

func (*TestDB) NewQueryChain

func (t *TestDB) NewQueryChain() *QueryChain

NewQueryChain creates a new query chain builder

func (*TestDB) Reset

func (t *TestDB) Reset()

Reset clears all expectations

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

func (*TestDBFactory) Reset

func (f *TestDBFactory) Reset()

Reset clears all tracked instances

Jump to

Keyboard shortcuts

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