a database layer insipred by caqti and ecto
at main 3.0 kB view raw
1open Repodb 2 3let test_default_config () = 4 let config = Stream.default_config in 5 Alcotest.(check int) "default batch size" 1000 config.batch_size; 6 Alcotest.(check (option int)) "no max rows" None config.max_rows 7 8let test_sync_fold () = 9 let rows = [ 1; 2; 3 ] in 10 let count = Stream.Sync.fold ~rows ~init:0 ~f:(fun acc _ -> acc + 1) in 11 Alcotest.(check int) "counted 3 rows" 3 count 12 13let test_sync_iter () = 14 let rows = [ 1; 2 ] in 15 let count = ref 0 in 16 Stream.Sync.iter ~rows ~f:(fun _ -> incr count); 17 Alcotest.(check int) "iterated 2 rows" 2 !count 18 19let test_sync_to_seq () = 20 let rows = [ "1"; "2"; "3" ] in 21 let seq = Stream.Sync.to_seq rows in 22 let list = List.of_seq seq in 23 Alcotest.(check (list string)) "seq to list" [ "1"; "2"; "3" ] list 24 25let test_sync_filter_map () = 26 let rows = [ 1; 2; 3; 4 ] in 27 let result = 28 Stream.Sync.filter_map ~rows ~f:(fun n -> 29 if n mod 2 = 0 then Some n else None) 30 in 31 Alcotest.(check (list int)) "even numbers" [ 2; 4 ] result 32 33let test_sync_take () = 34 let rows = [ 1; 2; 3; 4; 5 ] in 35 let taken = Stream.Sync.take 3 rows in 36 Alcotest.(check (list int)) "took 3" [ 1; 2; 3 ] taken 37 38let test_sync_drop () = 39 let rows = [ 1; 2; 3; 4; 5 ] in 40 let dropped = Stream.Sync.drop 2 rows in 41 Alcotest.(check (list int)) "dropped 2" [ 3; 4; 5 ] dropped 42 43let test_sync_chunks () = 44 let rows = [ 1; 2; 3; 4; 5; 6; 7 ] in 45 let chunks = Stream.Sync.chunks 3 rows in 46 Alcotest.(check int) "3 chunks" 3 (List.length chunks); 47 Alcotest.(check (list int)) "first chunk" [ 1; 2; 3 ] (List.hd chunks) 48 49let test_seq_fold () = 50 let seq = List.to_seq [ 1; 2; 3 ] in 51 let sum = Stream.Seq.fold ~seq ~init:0 ~f:( + ) in 52 Alcotest.(check int) "sum is 6" 6 sum 53 54let test_seq_take () = 55 let seq = List.to_seq [ 1; 2; 3; 4; 5 ] in 56 let taken = Stream.Seq.take 3 seq in 57 let list = List.of_seq taken in 58 Alcotest.(check (list int)) "took 3" [ 1; 2; 3 ] list 59 60let test_seq_drop () = 61 let seq = List.to_seq [ 1; 2; 3; 4; 5 ] in 62 let dropped = Stream.Seq.drop 2 seq in 63 let list = List.of_seq dropped in 64 Alcotest.(check (list int)) "dropped 2" [ 3; 4; 5 ] list 65 66let test_seq_chunks () = 67 let seq = List.to_seq [ 1; 2; 3; 4; 5; 6; 7 ] in 68 let chunked = Stream.Seq.chunks 3 seq in 69 let chunks = List.of_seq chunked in 70 Alcotest.(check int) "3 chunks" 3 (List.length chunks); 71 Alcotest.(check (list int)) "first chunk" [ 1; 2; 3 ] (List.hd chunks) 72 73let tests = 74 [ 75 ("default_config", `Quick, test_default_config); 76 ("sync fold", `Quick, test_sync_fold); 77 ("sync iter", `Quick, test_sync_iter); 78 ("sync to_seq", `Quick, test_sync_to_seq); 79 ("sync filter_map", `Quick, test_sync_filter_map); 80 ("sync take", `Quick, test_sync_take); 81 ("sync drop", `Quick, test_sync_drop); 82 ("sync chunks", `Quick, test_sync_chunks); 83 ("seq fold", `Quick, test_seq_fold); 84 ("seq take", `Quick, test_seq_take); 85 ("seq drop", `Quick, test_seq_drop); 86 ("seq chunks", `Quick, test_seq_chunks); 87 ]