Refactor error handling in category retrieval test and enhance time field processing

- Updated the test for category retrieval to use t.Fatal instead of t.Error for critical failures, ensuring immediate test termination on error.
- Refactored the mapToStruct function to improve handling of boolean and time fields, converting database time formats to RFC3339 and ensuring proper type conversions for specific keys.
This commit is contained in:
Max 2025-08-31 16:25:12 +08:00
parent e2d8ef551f
commit 5612fb58e2
2 changed files with 17 additions and 4 deletions

View file

@ -684,8 +684,9 @@ func mapToStruct(m maps.MapStr, v interface{}) error {
// Clean up the map data to handle database type conversions
cleanMap := make(map[string]interface{})
for key, value := range m {
// Convert numeric values to proper types for boolean fields
if key == "enabled" || key == "system" || key == "readonly" {
switch key {
case "enabled", "system", "readonly":
// Convert numeric values to proper types for boolean fields
switch val := value.(type) {
case int:
cleanMap[key] = val != 0
@ -698,7 +699,19 @@ func mapToStruct(m maps.MapStr, v interface{}) error {
default:
cleanMap[key] = value
}
} else {
case "created_at", "updated_at", "next_run_at", "last_run_at", "scheduled_at", "started_at", "finished_at", "timestamp":
// Handle time fields - convert database time format to RFC3339
if str, ok := value.(string); ok && str != "" {
// Try to parse database time format "2006-01-02 15:04:05"
if t, err := time.Parse("2006-01-02 15:04:05", str); err == nil {
cleanMap[key] = t.Format(time.RFC3339)
} else {
cleanMap[key] = value
}
} else {
cleanMap[key] = value
}
default:
cleanMap[key] = value
}
}

View file

@ -168,7 +168,7 @@ func TestCategoryCRUD(t *testing.T) {
t.Fatalf("Failed to get categories: %v", err)
}
if len(categories) == 0 {
t.Error("Expected to find the test category")
t.Fatal("Expected to find the test category")
}
if categories[0].Name != testCategory.Name {
t.Errorf("Expected category name '%s', got '%s'", testCategory.Name, categories[0].Name)