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 = { type alert_requestp = {
date_offset: int; date_offset: int;
exact_time: int * int exact_time: int * int;
when_: when_;
} }
type alert_request = { type alert_request = {
date_offset: int; date_offset: int;
exact_time: int * int; exact_time: int * int;
due_date: Ptime.date; due_date: Ptime.date;
when_: when_;
} }
let of_alert_request ar = let of_alert_request ar =
let h, m = ar.exact_time in 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 src = ar.due_date |> Ptime.of_date in
let secs_in_day = 24 * 60 * 60 in let secs_in_day = 24 * 60 * 60 in
let span = ar.date_offset * secs_in_day |> Ptime.Span.of_int_s 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 h, m = ar.exact_time in
let ptime_time = (h, m, 0), offset in let ptime_time = (h, m, 0), offset in
Option.bind date (fun date -> Ptime.of_date_time (date, ptime_time)) Option.bind date (fun date -> Ptime.of_date_time (date, ptime_time))
@ -30,10 +34,7 @@ let parse_body str =
| line::rest -> | line::rest ->
if line |> String.starts_with ~prefix:"@alert: " then if line |> String.starts_with ~prefix:"@alert: " then
let until = String.length line - idx in 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 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 find_alert (alert_rest@acc) rest
else else
find_alert acc rest find_alert acc rest
@ -58,14 +59,14 @@ let to_alert_request str =
else Ok (n, str) else Ok (n, str)
| _ -> Ok (n, str) | _ -> Ok (n, str)
in in
let od = let od, when_ =
if String.starts_with ~prefix:"-" str then count 0 str "-" if String.starts_with ~prefix:"-" str then (count 0 str "-", Before)
else if String.starts_with ~prefix:"+" str then count 0 str "+" else if String.starts_with ~prefix:"+" str then (count 0 str "+", After)
else Ok (0, str) else (Ok (0, str), SameDay)
in in
Result.bind od (fun (offset, date_str) -> Result.bind od (fun (offset, date_str) ->
match String.split_on_char ':' date_str with 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}"]) | _ -> Error [%string "Malformed time: %{str}"])
let to_datetime: Datatypes.reminder -> (Ptime.t list option, string) result = function 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
| _, Error e -> Error e | _, Error e -> Error e
| Ok (date, offset), Ok alerts -> | 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 List.map (fun ap -> ap |> convert |> to_exact_date offset) alerts
|> all' [] |> all' []
|> Result.ok |> Result.ok

View file

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

View file

@ -1,5 +1,11 @@
open Pam.Datatypes open Pam.Datatypes
let test1 () =
open Alcotest
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 let body = {|@alert: -12:38 10:02 +09:24
## Description ## Description
@ -11,23 +17,21 @@ let test1 () =
3. --12:38: you get the alert 30/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 |} 4. +12:38: you get the alert 2/1/1970 at 12:38 |}
in 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 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 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 let res = Pam.Issue_parser.to_datetime reminder in
match res with match res with
| Ok None -> failwith "none" | Ok None -> Alcotest.fail "Got Ok None"
| Error e -> failwith e | Error e -> Alcotest.fail [%string "Got Error %{e}"]
| Ok (Some ar) -> | Ok (Some got) -> Alcotest.check (list ptime_testable) "Alert times" expected got
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 '+'/'-'"
let () = let () =
let _ = test1 () Alcotest.run "test1" [
in () "alert_times", [test_case "Test Alert Times" `Quick test_parse]
]