finally tests

This commit is contained in:
Benedetta 2024-04-24 09:33:01 +02:00
parent 3a554a82fe
commit 17d314550d
3 changed files with 42 additions and 34 deletions

View file

@ -1,12 +1,15 @@
type when_ = | Before | After | SameDay
type alert_requestp = {
date_offset: int;
exact_time: int * int
exact_time: int * int;
when_: when_;
}
type alert_request = {
date_offset: int;
exact_time: int * int;
due_date: Ptime.date;
when_: when_;
}
let of_alert_request ar =
let h, m = ar.exact_time in
@ -17,7 +20,8 @@ let to_exact_date offset ar =
let src = ar.due_date |> Ptime.of_date in
let secs_in_day = 24 * 60 * 60 in
let span = ar.date_offset * secs_in_day |> Ptime.Span.of_int_s in
let date = Option.bind src (fun s -> Ptime.add_span s span) |> Option.map (Ptime.to_date ~tz_offset_s:offset) in
let add_span = match ar.when_ with | Before -> Ptime.sub_span | _ -> Ptime.add_span in
let date = Option.bind src (fun s -> add_span s span) |> Option.map (Ptime.to_date ~tz_offset_s:offset) in
let h, m = ar.exact_time in
let ptime_time = (h, m, 0), offset in
Option.bind date (fun date -> Ptime.of_date_time (date, ptime_time))
@ -30,10 +34,7 @@ let parse_body str =
| line::rest ->
if line |> String.starts_with ~prefix:"@alert: " then
let until = String.length line - idx in
let _ = print_endline [%string "doing_sub: %{line}|%{until#Int}|%{idx#Int}"] in
let alert_rest = String.sub line idx until |> String.split_on_char ' ' in
let p = String.concat "; " alert_rest in
let _ = print_endline [%string "done_sub: %{p}|%{until#Int}|%{idx#Int}"] in
find_alert (alert_rest@acc) rest
else
find_alert acc rest
@ -58,14 +59,14 @@ let to_alert_request str =
else Ok (n, str)
| _ -> Ok (n, str)
in
let od =
if String.starts_with ~prefix:"-" str then count 0 str "-"
else if String.starts_with ~prefix:"+" str then count 0 str "+"
else Ok (0, str)
let od, when_ =
if String.starts_with ~prefix:"-" str then (count 0 str "-", Before)
else if String.starts_with ~prefix:"+" str then (count 0 str "+", After)
else (Ok (0, str), SameDay)
in
Result.bind od (fun (offset, date_str) ->
match String.split_on_char ':' date_str with
| h::m::[] when is_number h && is_number m -> Ok {date_offset=offset; exact_time=(int_of_string h, int_of_string m)}
| h::m::[] when is_number h && is_number m -> Ok {date_offset=offset; exact_time=(int_of_string h, int_of_string m); when_=when_}
| _ -> Error [%string "Malformed time: %{str}"])
let to_datetime: Datatypes.reminder -> (Ptime.t list option, string) result = function
@ -90,7 +91,7 @@ let to_datetime: Datatypes.reminder -> (Ptime.t list option, string) result = f
| Error e, _ -> Error e
| _, Error e -> Error e
| Ok (date, offset), Ok alerts ->
let convert = (fun (a: alert_requestp) -> {date_offset=a.date_offset; exact_time=a.exact_time; due_date=date}) in
let convert = (fun (a: alert_requestp) -> {date_offset=a.date_offset; exact_time=a.exact_time; due_date=date; when_=a.when_}) in
List.map (fun ap -> ap |> convert |> to_exact_date offset) alerts
|> all' []
|> Result.ok

View file

@ -1,3 +1,6 @@
(test
(name test_pam)
(libraries pam))
(libraries pam batteries alcotest)
(preprocess
(pps lwt_ppx ppx_string ))
)

View file

@ -1,33 +1,37 @@
open Pam.Datatypes
let test1 () =
let body = {|@alert: -12:38 10:02 +09:24
## Description
open Alcotest
Specify a time format by writing the exact time of the alert, then adding zero or more '+' or '-'.
Examples, due date is 1/1/1970:
1. 12:38: you get the alert 1/1/1970 at 12:38
2. -12:38: you get the alert 31/12/1969 at 12:38
3. --12:38: you get the alert 30/12/1969 at 12:38
4. +12:38: you get the alert 2/1/1970 at 12:38 |}
let pprint_ptime ppf x = Fmt.pf ppf "%s" (Ptime.to_rfc3339 x)
let ptime_testable = Alcotest.testable pprint_ptime Ptime.equal
let test_parse () =
let body = {|@alert: -12:38 10:02 +09:24
## Description
Specify a time format by writing the exact time of the alert, then adding zero or more '+' or '-'.
Examples, due date is 1/1/1970:
1. 12:38: you get the alert 1/1/1970 at 12:38
2. -12:38: you get the alert 31/12/1969 at 12:38
3. --12:38: you get the alert 30/12/1969 at 12:38
4. +12:38: you get the alert 2/1/1970 at 12:38 |}
in
let _a = ("https://salsa.lezzo.org/api/v1/repos/bparodi/Documenti/issues/25", "Casa", ("2024-04-20T23:59:59+02:00"), body) in
let m = MatrixRoom.make "test" in
let reminder = {url="example.org"; title= "example"; due_date= Some "2024-04-20T23:59:59+02:00"; body=body; matrix_target=m} in
let expected = [
"2024-04-19T12:38:00+02:00" ;
"2024-04-20T10:02:00+02:00" ;
"2024-04-21T09:24:00+02:00" ;
] |> List.map (fun e -> e |> Ptime.of_rfc3339 |> Result.get_ok |> function | t, _, _ -> t) in
let res = Pam.Issue_parser.to_datetime reminder in
match res with
| Ok None -> failwith "none"
| Error e -> failwith e
| Ok (Some ar) ->
let _ = ar
|> List.map (Ptime.to_rfc3339)|> List.iter print_endline in
let _ = Ptime_clock.now () |> Ptime.to_rfc3339 |> print_endline in
failwith "MAnca +2 offset";
failwith "MAnca giorni negativi o positive '+'/'-'"
| Ok None -> Alcotest.fail "Got Ok None"
| Error e -> Alcotest.fail [%string "Got Error %{e}"]
| Ok (Some got) -> Alcotest.check (list ptime_testable) "Alert times" expected got
let () =
let _ = test1 ()
in ()
Alcotest.run "test1" [
"alert_times", [test_case "Test Alert Times" `Quick test_parse]
]