oxedyne/fe2o3/fe2o3_datime/tests/validation.rs
5.5 KiB, 8 runs
created by r1870400018:6423, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 2 | //! Anthropic Claude |
| 3 | |
| 4 | use oxedyne_fe2o3_datime::prelude::*; |
| 5 | use oxedyne_fe2o3_core::prelude::*; |
| 6 | |
| 7 | #[test] |
| 8 | fn test_basic_validation() -> Outcome<()> { |
| 9 | let zone = CalClockZone::utc(); |
| 10 | |
| 11 | // Test valid CalClock |
| 12 | let valid_calclock = res!(CalClock::new(2024, 6, 15, 14, 30, 0, 0, zone.clone())); |
| 13 | let validator = CalClockValidator::new(); |
| 14 | |
| 15 | // Should pass basic validation |
| 16 | assert!(validator.is_valid_calclock(&valid_calclock)); |
| 17 | assert!(validator.validate_calclock(&valid_calclock).is_ok()); |
| 18 | |
| 19 | Ok(()) |
| 20 | } |
| 21 | |
| 22 | #[test] |
| 23 | fn test_business_hours_validation() -> Outcome<()> { |
| 24 | let zone = CalClockZone::utc(); |
| 25 | |
| 26 | // Test during business hours (weekday, 10 AM) |
| 27 | let business_time = res!(CalClock::new(2024, 6, 17, 10, 0, 0, 0, zone.clone())); // Monday |
| 28 | |
| 29 | // Test outside business hours (weekend) |
| 30 | let weekend_time = res!(CalClock::new(2024, 6, 16, 10, 0, 0, 0, zone.clone())); // Sunday |
| 31 | |
| 32 | let business_rule = ValidationRules::business_hours(); |
| 33 | |
| 34 | // Business hours should pass |
| 35 | assert!(business_rule.validate_calclock(&business_time).is_ok()); |
| 36 | |
| 37 | // Weekend should fail |
| 38 | assert!(business_rule.validate_calclock(&weekend_time).is_err()); |
| 39 | |
| 40 | Ok(()) |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn test_custom_validation_rule() -> Outcome<()> { |
| 45 | let zone = CalClockZone::utc(); |
| 46 | |
| 47 | // Create a custom rule that only allows even hours |
| 48 | let even_hours_rule = ValidationRule::new("even_hours_only") |
| 49 | .description("Only allows even hour values") |
| 50 | .with_time_validator(|time| { |
| 51 | if time.hour().of() % 2 == 0 { |
| 52 | Ok(()) |
| 53 | } else { |
| 54 | Err(vec![ValidationError::new( |
| 55 | "even_hours_only", |
| 56 | "Hour must be even" |
| 57 | ).field("hour") |
| 58 | .value(time.hour().of().to_string()) |
| 59 | .expected("even number")]) |
| 60 | } |
| 61 | }); |
| 62 | |
| 63 | let even_hour_time = res!(CalClock::new(2024, 6, 15, 14, 30, 0, 0, zone.clone())); // 14 is even |
| 64 | let odd_hour_time = res!(CalClock::new(2024, 6, 15, 15, 30, 0, 0, zone.clone())); // 15 is odd |
| 65 | |
| 66 | // Test the time component directly since our rule is for time |
| 67 | assert!(even_hours_rule.validate_time(even_hour_time.time()).is_ok()); |
| 68 | |
| 69 | // Odd hour should fail |
| 70 | let result = even_hours_rule.validate_time(odd_hour_time.time()); |
| 71 | assert!(result.is_err()); |
| 72 | |
| 73 | if let Err(errors) = result { |
| 74 | assert_eq!(errors.len(), 1); |
| 75 | assert_eq!(errors[0].rule, "even_hours_only"); |
| 76 | } |
| 77 | |
| 78 | Ok(()) |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn test_validator_with_multiple_rules() -> Outcome<()> { |
| 83 | let zone = CalClockZone::utc(); |
| 84 | |
| 85 | let mut validator = CalClockValidator::new(); |
| 86 | validator.add_rule(ValidationRules::whole_minutes_only()); |
| 87 | validator.add_rule(ValidationRules::hour_range(9, 17)); |
| 88 | |
| 89 | // Valid: 10:30:00 (whole minute, within hour range) |
| 90 | let valid_time = res!(CalClock::new(2024, 6, 17, 10, 30, 0, 0, zone.clone())); |
| 91 | assert!(validator.validate_calclock(&valid_time).is_ok()); |
| 92 | |
| 93 | // Invalid: 10:30:30 (has seconds) |
| 94 | let invalid_seconds = res!(CalClock::new(2024, 6, 17, 10, 30, 30, 0, zone.clone())); |
| 95 | assert!(validator.validate_calclock(&invalid_seconds).is_err()); |
| 96 | |
| 97 | // Invalid: 8:30:00 (outside hour range) |
| 98 | let invalid_hour = res!(CalClock::new(2024, 6, 17, 8, 30, 0, 0, zone.clone())); |
| 99 | assert!(validator.validate_calclock(&invalid_hour).is_err()); |
| 100 | |
| 101 | Ok(()) |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn test_date_range_validation() -> Outcome<()> { |
| 106 | let zone = CalClockZone::utc(); |
| 107 | |
| 108 | let min_date = res!(CalendarDate::new(2024, 1, 1, zone.clone())); |
| 109 | let max_date = res!(CalendarDate::new(2024, 12, 31, zone.clone())); |
| 110 | |
| 111 | let validator = CalClockValidator::new(); |
| 112 | |
| 113 | // Valid date within range |
| 114 | let valid_date = res!(CalendarDate::new(2024, 6, 15, zone.clone())); |
| 115 | assert!(validator.validate_date_range(&valid_date, &min_date, &max_date).is_ok()); |
| 116 | |
| 117 | // Invalid date before range |
| 118 | let too_early = res!(CalendarDate::new(2023, 12, 31, zone.clone())); |
| 119 | assert!(validator.validate_date_range(&too_early, &min_date, &max_date).is_err()); |
| 120 | |
| 121 | // Invalid date after range |
| 122 | let too_late = res!(CalendarDate::new(2025, 1, 1, zone.clone())); |
| 123 | assert!(validator.validate_date_range(&too_late, &min_date, &max_date).is_err()); |
| 124 | |
| 125 | Ok(()) |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn test_batch_validation() -> Outcome<()> { |
| 130 | let zone = CalClockZone::utc(); |
| 131 | |
| 132 | let calclocks = vec![ |
| 133 | res!(CalClock::new(2024, 6, 15, 10, 0, 0, 0, zone.clone())), // Valid |
| 134 | res!(CalClock::new(2024, 6, 15, 12, 30, 0, 0, zone.clone())), // Valid but let's make invalid with validator |
| 135 | ]; |
| 136 | |
| 137 | // Create a valid date for testing (constructors validate inputs) |
| 138 | let test_date = res!(CalendarDate::new(2024, 6, 15, zone.clone())); |
| 139 | |
| 140 | // We'll test validation rules rather than invalid construction |
| 141 | |
| 142 | let validator = CalClockValidator::new(); |
| 143 | let result = validator.validate_many_calclocks(&calclocks); |
| 144 | |
| 145 | // Basic CalClocks should be valid |
| 146 | assert!(result.is_ok()); |
| 147 | |
| 148 | // Test date validation - valid date should pass |
| 149 | let date_result = validator.validate_date(&test_date); |
| 150 | assert!(date_result.is_ok()); |
| 151 | |
| 152 | // All CalClocks should be valid with basic validator |
| 153 | assert_eq!(validator.count_valid_calclocks(&calclocks), 2); |
| 154 | |
| 155 | // Filter should return all valid ones |
| 156 | let valid_only = validator.filter_valid_calclocks(calclocks); |
| 157 | assert_eq!(valid_only.len(), 2); |
| 158 | |
| 159 | Ok(()) |
| 160 | } |