Skip to content

test(mysql): add pagination and error handling tests#4703

Open
lingcoder wants to merge 2 commits intogogf:masterfrom
lingcoder:test/mysql-pagination-error-handling
Open

test(mysql): add pagination and error handling tests#4703
lingcoder wants to merge 2 commits intogogf:masterfrom
lingcoder:test/mysql-pagination-error-handling

Conversation

@lingcoder
Copy link
Contributor

Summary

  • Add comprehensive pagination tests (Limit, Offset, Page, ForPage)
  • Add error handling tests for invalid operations
  • Add tests for edge cases and boundary conditions

Test coverage added:

  • Pagination: ~28 test functions
  • Error handling: ~20 test functions

Ref #4689

Test plan

cd contrib/drivers/mysql
go test -v -run "TestModel_Pagination|TestModel_Error|TestModel_InvalidOperation"

Add 55 new test functions (1,011 lines) to improve MySQL driver test coverage.

## New Test Files

### Pagination Tests (22 functions, 531 lines)
- AllAndCount: 7 tests covering basic usage, WHERE conditions, pagination, field selection, empty results, cache support, DISTINCT queries
- ScanAndCount: 7 tests covering basic usage, WHERE conditions, pagination, single record, empty results, field selection, cache support
- Chunk: 5 tests covering basic iteration, early stop, WHERE conditions, error handling, empty results
- Page/Limit: 3 tests covering boundary values (0, negative, beyond data), combination usage

### Error Handling Tests (33 functions, 480 lines)
- Nil/empty data handling: Insert/Update with nil, empty map, empty slice
- Missing WHERE clause: Update/Delete without WHERE
- Scan errors: nil pointer, invalid pointer type, empty result
- Invalid SQL: invalid operators, non-existent fields/tables
- SQL injection prevention: WHERE clause, Insert, Update (parameterized queries)
- Aggregate functions: Max/Min/Avg/Sum with empty results
- Context: cancelled context handling
- Transaction: rollback after error
- Duplicate key errors
- Invalid connections

## Framework Bugs Discovered

During test development, discovered 3 framework edge case bugs:

- gogf#4697: Fields("") should handle empty string gracefully
  - Regression test: Test_Model_Fields_Empty
  - gogf#4697

- gogf#4698: AllAndCount(true) with multiple fields should work correctly
  - Regression test: Test_Model_AllAndCount_WithFields
  - gogf#4698

- gogf#4699: Negative Limit/Page values should be sanitized
  - Regression tests: Test_Model_Page_Boundary, Test_Model_Limit_Boundary
  - gogf#4699

These tests define correct expected behavior and will pass once bugs are fixed.

## Test Quality
- All tests use gtest.C() wrapper pattern
- Independent test execution (no dependencies)
- Comprehensive boundary/edge case coverage
- Proper error validation
- Code formatted with gofmt and gci

ref gogf#4689
Replace t.Assert(len(result) <= 1) with t.AssertLE(len(result), 1).
t.Assert requires two arguments (actual, expected) but was given a boolean expression.
lingcoder added a commit to lingcoder/gf that referenced this pull request Feb 13, 2026
## Summary
Fix bug where AllAndCount(true) with multiple fields generates invalid
SQL COUNT(field1, field2, ...) causing syntax error.

## Root Cause
When useFieldForCount=true, the COUNT query inherits the fields
configuration from the model, generating COUNT(multiple fields) which
is invalid SQL syntax.

## Fix
Always use COUNT(1) regardless of useFieldForCount parameter since
COUNT() accepts only one argument.

Applied to both AllAndCount() and ScanAndCount() methods in
database/gdb/gdb_model_select.go.

## Tests
Added Test_Issue4698 in contrib/drivers/mysql/mysql_z_unit_issue_test.go
with 5 test cases covering all scenarios.

Updated SQLiteCGo tests to expect correct behavior after framework fix.

Fixes gogf#4698
Ref gogf#4703
lingcoder added a commit to lingcoder/gf that referenced this pull request Feb 13, 2026
## Summary
Fix bug where AllAndCount(true) with multiple fields generates invalid
SQL COUNT(field1, field2, ...) causing syntax error.

## Root Cause
When useFieldForCount=true, the COUNT query inherits the fields
configuration from the model, generating COUNT(multiple fields) which
is invalid SQL syntax.

## Fix
Always use COUNT(1) regardless of useFieldForCount parameter since
COUNT() accepts only one argument.

Applied to both AllAndCount() and ScanAndCount() methods in
database/gdb/gdb_model_select.go.

## Tests
Added Test_Issue4698 in contrib/drivers/mysql/mysql_z_unit_issue_test.go
with 5 test cases covering all scenarios.

Updated SQLiteCGo tests to expect correct behavior after framework fix.

Fixes gogf#4698
Ref gogf#4703
@lingcoder lingcoder force-pushed the test/mysql-pagination-error-handling branch from 7f29c65 to 2433f49 Compare February 13, 2026 13:48
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.

1 participant

Comments