From 5612fb58e2043dba58f941aadc355a40fc4a3511 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 31 Aug 2025 16:25:12 +0800 Subject: [PATCH] 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. --- job/data.go | 19 ++++++++++++++++--- job/data_test.go | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/job/data.go b/job/data.go index a83aa65f..a8441c77 100644 --- a/job/data.go +++ b/job/data.go @@ -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 } } diff --git a/job/data_test.go b/job/data_test.go index 1682e3f7..93894840 100644 --- a/job/data_test.go +++ b/job/data_test.go @@ -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)