View raw

1 (** Tests for the automatic development account fixture. *) 2 3 open Hito_app 4 module Seed = Development_seed.Make (Test_repo) (Test_hash) 5 module Service = Service.Make (Test_repo) (Test_hash) 6 module Workout = Evidence.Workout 7 8 let run = Lwt_main.run 9 let day = 86_400 10 let now = Recovery.timestamp_of_unix_seconds (100 * day) 11 12 let development_seed_tests = 13 [ 14 ( "creates test with three months of native Ideal Routine history", 15 `Quick, 16 fun () -> 17 let repo = Test_repo.create () in 18 run (Seed.seed repo ~now); 19 let service = Service.make ~repo in 20 let trainee = 21 match 22 run (Service.authenticate service ~username:"test" ~password:"test") 23 with 24 | Some trainee -> trainee 25 | None -> Alcotest.fail "expected the development account" 26 in 27 let history = run (Test_repo.history repo trainee.Trainee.id) in 28 Alcotest.(check int) "thirteen weekly cycles" 39 (List.length history); 29 Alcotest.(check bool) 30 "all workouts are complete" true 31 (List.for_all 32 (fun record -> Workout.is_finished record.Repository.workout) 33 history); 34 let oldest = List.hd (List.rev history) in 35 let newest = List.hd history in 36 Alcotest.(check int) 37 "oldest workout starts ninety days ago" 38 ((100 * day) - (90 * day)) 39 (Recovery.timestamp_to_unix_seconds 40 (Workout.started_at oldest.Repository.workout)); 41 Alcotest.(check int) 42 "newest workout starts two days ago" 43 ((100 * day) - (2 * day)) 44 (Recovery.timestamp_to_unix_seconds 45 (Workout.started_at newest.Repository.workout)); 46 match run (Service.active_routine service trainee.Trainee.id) with 47 | Some (_, routine) -> 48 Alcotest.(check string) 49 "Ideal Routine is active" "Ideal Routine" 50 (Prescription.Routine.name routine) 51 | None -> Alcotest.fail "expected an active routine" ); 52 ( "does not duplicate existing development history", 53 `Quick, 54 fun () -> 55 let repo = Test_repo.create () in 56 run (Seed.seed repo ~now); 57 run (Seed.seed repo ~now); 58 let trainee = 59 match 60 run 61 (Test_repo.find_trainee_by_username repo 62 (Result.get_ok (Trainee.username "test"))) 63 with 64 | Some trainee -> trainee 65 | None -> Alcotest.fail "expected the development account" 66 in 67 Alcotest.(check int) 68 "history remains one fixture" 39 69 (List.length (run (Test_repo.history repo trainee.Trainee.id))) ); 70 ] 71 72 let suite = [ ("development seed", development_seed_tests) ] 73