|
| 1 | +open Xapi_stdext_date.Date |
| 2 | +let check_float = Alcotest.(check @@ float 1e-2 ) |
| 3 | +let check_float_neq = Alcotest.(check @@ neg @@ float 1e-2) |
| 4 | +let check_string = Alcotest.(check string) |
| 5 | +let check_true str = Alcotest.(check bool) str true |
| 6 | +let dash_time_str = "2020-04-07T08:28:32Z" |
| 7 | +let no_dash_utc_time_str = "20200407T08:28:32Z" |
| 8 | + |
| 9 | +let iso8601_tests = |
| 10 | + let test_of_float_invertible () = |
| 11 | + let non_int_time = 1586245987.70200706 in |
| 12 | + let time = non_int_time |> Float.floor in |
| 13 | + check_float "to_float inverts of_float" time (time |> of_float |> to_float); |
| 14 | + check_true "of_float inverts to_float" @@ eq (time |> of_float) (time |> of_float |> to_float |> of_float); |
| 15 | + in |
| 16 | + |
| 17 | + let test_only_utc () = |
| 18 | + let utc = "2020-12-20T18:10:19Z" in |
| 19 | + let _ = of_string utc in (* UTC is valid *) |
| 20 | + let non_utc = "2020-12-20T18:10:19+02:00" in |
| 21 | + let exn = Invalid_argument "date.ml:of_string: 2020-12-20T18:10:19+02:00" in |
| 22 | + Alcotest.check_raises "only UTC is accepted" exn (fun () -> of_string non_utc |> ignore) |
| 23 | + in |
| 24 | + |
| 25 | + let test_ca333908 () = |
| 26 | + check_float "dash time and no dash time have same float repr" |
| 27 | + (dash_time_str |> of_string |> to_float) |
| 28 | + (no_dash_utc_time_str |> of_string |> to_float) |
| 29 | + in |
| 30 | + |
| 31 | + let test_of_string_invertible_when_no_dashes () = |
| 32 | + check_string "to_string inverts of_string" no_dash_utc_time_str (no_dash_utc_time_str |> of_string |> to_string); |
| 33 | + check_true "of_string inverts to_string" (eq (no_dash_utc_time_str |> of_string) (no_dash_utc_time_str |> of_string |> to_string |> of_string)); |
| 34 | + in |
| 35 | + |
| 36 | + (* CA-338243 - breaking backwards compatibility will break XC and XRT *) |
| 37 | + let test_to_string_backwards_compatibility () = |
| 38 | + check_string "to_string is backwards compatible" no_dash_utc_time_str |
| 39 | + (dash_time_str |> of_string |> to_string) |
| 40 | + in |
| 41 | + |
| 42 | + let test_localtime_string () = |
| 43 | + let[@warning "-8"] (Ok (t, _, _)) = |
| 44 | + Ptime.of_rfc3339 "2020-04-07T09:01:28Z" |
| 45 | + in |
| 46 | + let minus_2_hrs = -7200 in |
| 47 | + let plus_3_hrs = 10800 in |
| 48 | + let zero_hrs = 0 in |
| 49 | + check_string "can subtract 2 hours" (_localtime_string (Some minus_2_hrs) t) "20200407T07:01:28"; |
| 50 | + check_string "can add 3 hours" (_localtime_string (Some plus_3_hrs) t) "20200407T12:01:28"; |
| 51 | + check_string "can add None" (_localtime_string None t) "20200407T09:01:28"; |
| 52 | + check_string "can add zero" (_localtime_string (Some zero_hrs) t) "20200407T09:01:28" |
| 53 | + in |
| 54 | + |
| 55 | + (* sanity check (on top of test_localtime_string) that localtime produces valid looking output *) |
| 56 | + let test_ca342171 () = |
| 57 | + (* no exception is thrown + backward compatible formatting *) |
| 58 | + let localtime_string = localtime () |> to_string in |
| 59 | + Alcotest.(check int) "localtime string has correct number of chars" |
| 60 | + (String.length localtime_string) (String.length no_dash_utc_time_str - 1); |
| 61 | + Alcotest.(check bool) "localtime string does not contain a Z" false (String.contains localtime_string 'Z') |
| 62 | + in |
| 63 | + |
| 64 | + let test_xsi894 () = |
| 65 | + let missing_tz_no_dash = "20201210T17:19:20" in |
| 66 | + let missing_tz_dash = "2020-12-10T17:19:20" in |
| 67 | + check_string "can process missing tz no dash" missing_tz_no_dash (missing_tz_no_dash |> of_string |> to_string) ; |
| 68 | + check_string "can process missing tz with dashes, but return without dashes" missing_tz_no_dash (missing_tz_dash |> of_string |> to_string) ; |
| 69 | + |
| 70 | + check_float "to_float assumes UTC" 1607620760. (missing_tz_no_dash |> of_string |> to_float) ; |
| 71 | + |
| 72 | + let localtime' = localtime () in |
| 73 | + check_string "to_string inverts of_string for localtime" (localtime' |> to_string) (localtime' |> to_string |> of_string |> to_string) ; |
| 74 | + in |
| 75 | + |
| 76 | + [ "test_of_float_invertible", `Quick, test_of_float_invertible |
| 77 | + ; "test_only_utc", `Quick, test_only_utc |
| 78 | + ; "test_ca333908", `Quick, test_ca333908 |
| 79 | + ; "test_of_string_invertible_when_no_dashes", `Quick, test_of_string_invertible_when_no_dashes |
| 80 | + ; "test_to_string_backwards_compatibility", `Quick, test_to_string_backwards_compatibility |
| 81 | + ; "test_localtime_string", `Quick, test_localtime_string |
| 82 | + ; "test_ca342171", `Quick, test_ca342171 |
| 83 | + ; "test_xsi894", `Quick, test_xsi894 |
| 84 | + ] |
| 85 | + |
| 86 | +let () = Alcotest.run "Date" [ "ISO 8601", iso8601_tests ] |
0 commit comments