feat(rules): add Go testing standards
This commit is contained in:
parent
91e5d7529f
commit
8a42bea740
1 changed files with 41 additions and 0 deletions
|
@ -57,6 +57,20 @@ When working with Go:
|
||||||
- `gorm:"index"` for indexed fields
|
- `gorm:"index"` for indexed fields
|
||||||
- `gorm:"type:text"` for specific SQL types
|
- `gorm:"type:text"` for specific SQL types
|
||||||
|
|
||||||
|
7. Testing Standards:
|
||||||
|
- Place tests in _test.go files next to the code they test
|
||||||
|
- Use table-driven tests for multiple test cases
|
||||||
|
- Use meaningful test names that describe the scenario
|
||||||
|
- Create test helpers for common setup/teardown
|
||||||
|
- Use subtests for related test cases
|
||||||
|
- Test both success and error cases
|
||||||
|
- For database tests:
|
||||||
|
- Use in-memory SQLite for speed
|
||||||
|
- Clean up after each test
|
||||||
|
- Use transactions where appropriate
|
||||||
|
- Write focused tests that test one thing
|
||||||
|
- Include examples in test files when helpful
|
||||||
|
|
||||||
metadata:
|
metadata:
|
||||||
priority: high
|
priority: high
|
||||||
version: 1.0
|
version: 1.0
|
||||||
|
@ -98,4 +112,31 @@ examples:
|
||||||
# Good: GORM query building and error handling
|
# Good: GORM query building and error handling
|
||||||
if err := db.Model(&Note{}).Where("id = ?", id).Update("content", content).Error; err != nil {
|
if err := db.Model(&Note{}).Where("id = ?", id).Update("content", content).Error; err != nil {
|
||||||
return fmt.Errorf("failed to update note: %w", err)
|
return fmt.Errorf("failed to update note: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
- input: |
|
||||||
|
# Bad: Single monolithic test
|
||||||
|
func TestFeature(t *testing.T) {
|
||||||
|
// Test everything here
|
||||||
|
}
|
||||||
|
output: |
|
||||||
|
# Good: Table-driven tests with subtests
|
||||||
|
func TestFeature(t *testing.T) {
|
||||||
|
tests := []struct{
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{"simple case", "input", "want"},
|
||||||
|
{"edge case", "edge", "result"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got := Feature(tt.input)
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("got %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue