Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 31, 2025

This PR adds a new testing module that provides mock implementations of all dbx interfaces, making it significantly easier to unit test dbx-based applications without requiring actual database connections or complex sqlmock setups.

What's New

The testing module includes:

  • MockExecutor - Complete mock implementation of dbx.Executor interface
  • MockDatabase - Mock implementation of dbx.DatabaseWithContext interface
  • MockTransactor - Mock implementation of dbx.Transactor interface for transaction testing
  • MockContext - Mock implementation of dbx.Context interface with proper context delegation
  • MockResult - Mock implementation of sql.Result interface with configurable return values

Usage Example

Before this change, testing dbx-based code required complex sqlmock setup:

func TestUserService_CreateUser(t *testing.T) {
    // Complex sqlmock setup
    mockDB, mock, err := sqlmock.New()
    require.NoError(t, err)
    defer mockDB.Close()
    
    mock.ExpectExec("INSERT INTO users").
        WithArgs("John").
        WillReturnResult(sqlmock.NewResult(123, 1))
    
    db := dbx.New(mockDB)
    service := NewUserService(db)
    // ... rest of test
}

Now you can use the simpler mock interface:

func TestUserService_CreateUser(t *testing.T) {
    // Simple mock setup
    mockDB := dbxtesting.NewMockDatabase()
    expectedResult := dbxtesting.NewResult(123, 1)
    mockDB.On("Exec", "INSERT INTO users (name) VALUES (?)", "John").
        Return(expectedResult, nil)
    
    service := NewUserService(mockDB)
    userID, err := service.CreateUser(context.Background(), "John")
    
    require.NoError(t, err)
    assert.Equal(t, int64(123), userID)
    mockDB.AssertExpectations(t)
}

Key Features

  • testify/mock Integration: Uses the popular testify/mock framework for familiar expectation setup
  • Full Interface Compatibility: Works with all dbx interfaces (Executor, Database, Context, etc.)
  • Error Testing Support: Easy testing of both operation errors and result-specific errors
  • Helper Functions: NewResult(), NewResultWithError() for quick result mocking
  • Comprehensive Documentation: Complete README with usage patterns and best practices

Testing

The module includes comprehensive tests covering all mock functionality and real-world usage examples. All existing tests continue to pass, ensuring backward compatibility.

The testing module makes unit testing dbx-based code much more straightforward while maintaining the same interface-driven design principles that make dbx flexible and testable.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits August 31, 2025 19:41
Co-authored-by: ziflex <1607148+ziflex@users.noreply.github.com>
Co-authored-by: ziflex <1607148+ziflex@users.noreply.github.com>
Copilot AI changed the title [WIP] Create a new testing module that provides mocks for testing. Add comprehensive testing module with mocks for dbx interfaces Aug 31, 2025
Copilot AI requested a review from ziflex August 31, 2025 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants