Files
beads/internal/storage/sqlite/validators_test.go
Steve Yegge b5db80c412 Refactor sqlite.go: Extract hash IDs, batch ops, validators (bd-90a5, bd-c796, bd-d9e0)
- Extract hash ID generation to hash_ids.go (bd-90a5)
  - generateHashID, getNextChildNumber, GetNextChildID
  - Reduced sqlite.go from 1880 to 1799 lines

- Extract batch operations to batch_ops.go (bd-c796)
  - validateBatchIssues, generateBatchIDs, bulkInsertIssues
  - bulkRecordEvents, bulkMarkDirty, CreateIssues
  - Reduced sqlite.go from 1799 to 1511 lines

- Extract validation functions to validators.go (bd-d9e0)
  - validatePriority, validateStatus, validateIssueType
  - validateTitle, validateEstimatedMinutes, validateFieldUpdate
  - Reduced sqlite.go from 1511 to 1447 lines

- Add comprehensive validator tests (bd-3b7f)
  - validators_test.go with full coverage

Total reduction: 2298 → 1447 lines (851 lines extracted, 37% reduction)

Part of epic bd-fc2d to modularize sqlite.go
All tests pass

Amp-Thread-ID: https://ampcode.com/threads/T-09c4383b-bc5c-455e-be24-02b4f9df7d78
Co-authored-by: Amp <amp@ampcode.com>
2025-11-01 19:55:48 -07:00

152 lines
3.7 KiB
Go

package sqlite
import (
"testing"
"github.com/steveyegge/beads/internal/types"
)
func TestValidatePriority(t *testing.T) {
tests := []struct {
name string
value interface{}
wantErr bool
}{
{"valid priority 0", 0, false},
{"valid priority 1", 1, false},
{"valid priority 2", 2, false},
{"valid priority 3", 3, false},
{"valid priority 4", 4, false},
{"invalid negative", -1, true},
{"invalid too high", 5, true},
{"non-int ignored", "not an int", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validatePriority(tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("validatePriority() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateStatus(t *testing.T) {
tests := []struct {
name string
value interface{}
wantErr bool
}{
{"valid open", string(types.StatusOpen), false},
{"valid in_progress", string(types.StatusInProgress), false},
{"valid blocked", string(types.StatusBlocked), false},
{"valid closed", string(types.StatusClosed), false},
{"invalid status", "invalid", true},
{"non-string ignored", 123, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateStatus(tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("validateStatus() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateIssueType(t *testing.T) {
tests := []struct {
name string
value interface{}
wantErr bool
}{
{"valid bug", string(types.TypeBug), false},
{"valid feature", string(types.TypeFeature), false},
{"valid task", string(types.TypeTask), false},
{"valid epic", string(types.TypeEpic), false},
{"valid chore", string(types.TypeChore), false},
{"invalid type", "invalid", true},
{"non-string ignored", 123, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateIssueType(tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("validateIssueType() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateTitle(t *testing.T) {
tests := []struct {
name string
value interface{}
wantErr bool
}{
{"valid title", "Valid Title", false},
{"empty title", "", true},
{"max length title", string(make([]byte, 500)), false},
{"too long title", string(make([]byte, 501)), true},
{"non-string ignored", 123, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateTitle(tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("validateTitle() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateEstimatedMinutes(t *testing.T) {
tests := []struct {
name string
value interface{}
wantErr bool
}{
{"valid zero", 0, false},
{"valid positive", 60, false},
{"invalid negative", -1, true},
{"non-int ignored", "not an int", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateEstimatedMinutes(tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("validateEstimatedMinutes() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateFieldUpdate(t *testing.T) {
tests := []struct {
name string
key string
value interface{}
wantErr bool
}{
{"valid priority", "priority", 1, false},
{"invalid priority", "priority", 5, true},
{"valid status", "status", string(types.StatusOpen), false},
{"invalid status", "status", "invalid", true},
{"unknown field", "unknown_field", "any value", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateFieldUpdate(tt.key, tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("validateFieldUpdate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}