feat(rules): add Go testing standards

This commit is contained in:
Nicola Zangrandi 2025-02-21 09:02:27 +01:00
parent 91e5d7529f
commit 8a42bea740
Signed by: wasp
GPG key ID: 43C1470D890F23ED

View file

@ -57,6 +57,20 @@ When working with Go:
- `gorm:"index"` for indexed fields
- `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:
priority: high
version: 1.0
@ -98,4 +112,31 @@ examples:
# Good: GORM query building and error handling
if err := db.Model(&Note{}).Where("id = ?", id).Update("content", content).Error; err != nil {
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)
}
})
}
}