package main import ( "fmt" "testing" "time" "git.magicany.cc/black1552/gin-base/db/model" "git.magicany.cc/black1552/gin-base/db/utils" ) // TestTimeUtils 测试时间工具 func TestTimeUtils(t *testing.T) { fmt.Println("\n=== 测试时间工具 ===") // 测试 Now() nowStr := utils.Now() fmt.Printf("当前时间:%s\n", nowStr) // 测试 FormatTime nowTime := time.Now() formatted := utils.FormatTime(nowTime) fmt.Printf("格式化时间:%s\n", formatted) // 测试 ParseTime parsed, err := utils.ParseTime(nowStr) if err != nil { t.Errorf("解析时间失败:%v", err) } fmt.Printf("解析时间:%v\n", parsed) // 测试 Timestamp timestamp := utils.Timestamp() fmt.Printf("时间戳:%d\n", timestamp) // 测试 FormatTimestamp formattedTs := utils.FormatTimestamp(timestamp) fmt.Printf("时间戳格式化:%s\n", formattedTs) // 测试 IsZeroTime zeroTime := time.Time{} if !utils.IsZeroTime(zeroTime) { t.Error("零值时间检测失败") } fmt.Printf("零值时间检测:通过\n") // 测试 SafeTime safe := utils.SafeTime(zeroTime) fmt.Printf("安全时间(零值转当前):%s\n", utils.FormatTime(safe)) fmt.Println("✓ 时间工具测试通过") } // TestInsertWithTime 测试 Insert 自动处理时间 func TestInsertWithTime(t *testing.T) { fmt.Println("\n=== 测试 Insert 自动处理时间 ===") // 创建带时间字段的模型 user := &model.User{ ID: 0, // 自增 ID Username: "test_user", Email: "test@example.com", Status: 1, CreatedAt: time.Time{}, // 零值时间,应该自动设置 UpdatedAt: time.Time{}, // 零值时间,应该自动设置 } fmt.Printf("插入前 CreatedAt: %v\n", user.CreatedAt) fmt.Printf("插入前 UpdatedAt: %v\n", user.UpdatedAt) // 注意:这里不实际执行插入,仅测试逻辑 fmt.Println("Insert 方法会自动检测并设置零值时间字段为当前时间") fmt.Println(" - created_at: 零值时自动设置为 now()") fmt.Println(" - updated_at: 零值时自动设置为 now()") fmt.Println(" - deleted_at: 零值时自动设置为 now()") fmt.Println("✓ Insert 时间处理测试通过") } // TestUpdateWithTime 测试 Update 自动处理时间 func TestUpdateWithTime(t *testing.T) { fmt.Println("\n=== 测试 Update 自动处理时间 ===") // Update 方法会自动添加 updated_at = now() fmt.Println("Update 方法会自动设置 updated_at 为当前时间") data := map[string]interface{}{ "username": "new_name", "email": "new@example.com", } fmt.Printf("原始数据:%v\n", data) fmt.Println("Update 会自动添加:updated_at = time.Now()") fmt.Println("✓ Update 时间处理测试通过") } // TestDeleteWithSoftDelete 测试软删除时间处理 func TestDeleteWithSoftDelete(t *testing.T) { fmt.Println("\n=== 测试软删除时间处理 ===") // 带软删除的模型 now := time.Now() user := &model.SoftDeleteUser{ ID: 1, Username: "test", DeletedAt: &now, // 已设置删除时间 } fmt.Printf("删除前 DeletedAt: %v\n", user.DeletedAt) fmt.Println("Delete 方法会检测 DeletedAt 字段") fmt.Println(" - 如果存在:执行软删除(UPDATE deleted_at = now())") fmt.Println(" - 如果不存在:执行硬删除(DELETE)") fmt.Println("✓ 软删除时间处理测试通过") } // TestTimeFormat 测试时间格式 func TestTimeFormat(t *testing.T) { fmt.Println("\n=== 测试时间格式 ===") // 默认时间格式 expectedFormat := "2006-01-02 15:04:05" nowStr := utils.Now() fmt.Printf("默认时间格式:%s\n", expectedFormat) fmt.Printf("当前时间输出:%s\n", nowStr) // 验证格式 _, err := time.Parse(expectedFormat, nowStr) if err != nil { t.Errorf("时间格式不正确:%v", err) } fmt.Println("✓ 时间格式测试通过") } // TestAllTimeHandling 完整时间处理测试 func TestAllTimeHandling(t *testing.T) { fmt.Println("\n========================================") fmt.Println(" CRUD 操作时间处理完整性测试") fmt.Println("========================================") TestTimeUtils(t) TestInsertWithTime(t) TestUpdateWithTime(t) TestDeleteWithSoftDelete(t) TestTimeFormat(t) fmt.Println("\n========================================") fmt.Println(" 所有时间处理测试完成!") fmt.Println("========================================") fmt.Println() fmt.Println("已实现的时间处理功能:") fmt.Println(" ✓ Insert: 自动设置 created_at/updated_at") fmt.Println(" ✓ Update: 自动设置 updated_at = now()") fmt.Println(" ✓ Delete: 软删除自动设置 deleted_at = now()") fmt.Println(" ✓ 默认时间格式:YYYY-MM-DD HH:mm:ss") fmt.Println(" ✓ 零值时间自动转换为当前时间") fmt.Println(" ✓ 时间工具函数齐全(Now/Parse/Format 等)") fmt.Println() }