From 8e012f4804ecf1665819e761283120a3c0e73643 Mon Sep 17 00:00:00 2001
From: Sébastien Dailly <sebastien@dailly.me>
Date: Fri, 17 Jan 2025 20:48:43 +0100
Subject: Switched from OUnit to alcotest

---
 tests/sql_db.ml | 74 ++++++++++++++++++++++++---------------------------------
 1 file changed, 31 insertions(+), 43 deletions(-)

(limited to 'tests/sql_db.ml')

diff --git a/tests/sql_db.ml b/tests/sql_db.ml
index c966f4e..28666b2 100644
--- a/tests/sql_db.ml
+++ b/tests/sql_db.ml
@@ -1,21 +1,22 @@
 (** Test the behavior of the sqlite with a in-memory database *)
 
-open OUnit2
 open StdLabels
+open Test_migration
+
+let result = Alcotest.(result Test_migration.csv_result reject)
+let check = Alcotest.check result
 
 let ( let* ) res cont =
   match res with
   | Ok value -> cont value
-  | Error e -> raise e
+  | Error e -> Error e
 
-(** Test a process with a simple configuration in-memory *)
+(** Test a process with a simple configuration in-memory. Only one table is
+    handlded *)
 let run_test ~configuration ~input ~expected name =
-  name >:: fun _ ->
+  name >:: fun () ->
   (* We expect a valid configuration *)
-  let conf =
-    ImportConf.t_of_toml (Otoml.Parser.from_string configuration)
-    |> Result.get_ok
-  in
+  let conf = ConfLoader.load configuration in
 
   let exec db =
     let table = List.hd @@ ImportAnalyser.Dependency.get_process_order conf in
@@ -35,31 +36,22 @@ let run_test ~configuration ~input ~expected name =
           (result, i + 1))
     in
     let* () = result in
-
     let* () = ImportSQL.Db.finalize stmt in
 
-    let expected = ref expected in
-
     (* Collect the data *)
+    let data = ref [] in
     let* () =
       ImportSQL.Db.query db conf ~f:(fun rows ->
-          match !expected with
-          | [] -> ()
-          | hd :: tl ->
-              expected := tl;
-              let () =
-                Array.iter2 rows hd ~f:(fun (_, value) expected ->
-                    assert_equal ~printer:ImportCSV.DataType.to_string value
-                      expected)
-              in
-              ())
+          let values = Array.map ~f:snd rows in
+          data := values :: !data)
     in
 
-    Ok ()
+    Ok (List.rev !data)
   in
 
   (* Use a magic keyword for in-memory database *)
-  ignore @@ ImportSQL.Db.with_db ":memory:" exec
+  let result = ImportSQL.Db.with_db ":memory:" exec in
+  check name expected result
 
 (** Simple test used to check the process *)
 let simple_extraction =
@@ -85,13 +77,14 @@ columns = [
         ];
       ]
     ~expected:
-      [
-        [|
-          ImportCSV.DataType.Content "123_";
-          ImportCSV.DataType.Integer 2;
-          ImportCSV.DataType.Integer 5;
-        |];
-      ]
+      (Ok
+         [
+           [|
+             ImportCSV.DataType.Content "123_";
+             ImportCSV.DataType.Integer 2;
+             ImportCSV.DataType.Integer 5;
+           |];
+         ])
 
 (** Ensure the behavior of the sum function when a filter is given. It is
     expected to accumulate the values over each line *)
@@ -128,11 +121,12 @@ columns = [
         ];
       ]
     ~expected:
-      [
-        [| ImportCSV.DataType.Integer 1; ImportCSV.DataType.Integer 100 |];
-        [| ImportCSV.DataType.Integer 2; ImportCSV.DataType.Integer 200 |];
-        [| ImportCSV.DataType.Integer 3; ImportCSV.DataType.Integer 300 |];
-      ]
+      (Ok
+         [
+           [| ImportCSV.DataType.Integer 1; ImportCSV.DataType.Integer 100 |];
+           [| ImportCSV.DataType.Integer 2; ImportCSV.DataType.Integer 200 |];
+           [| ImportCSV.DataType.Integer 3; ImportCSV.DataType.Integer 300 |];
+         ])
 
 let sum_total =
   run_test "sum_total"
@@ -158,10 +152,7 @@ columns = [
         ];
       ]
     ~expected:
-      [
-        [| ImportCSV.DataType.Integer 1; ImportCSV.DataType.Integer 200 |];
-        [| ImportCSV.DataType.Integer 2; ImportCSV.DataType.Integer 200 |];
-      ]
+      (Ok [ [| ImportCSV.DataType.Integer 1; ImportCSV.DataType.Integer 200 |] ])
 
 (** Ensure the behavior of the sum function when no filter is given. It is
     expected to get the total sum for each line *)
@@ -189,10 +180,7 @@ columns = [
         ];
       ]
     ~expected:
-      [
-        [| ImportCSV.DataType.Integer 1; ImportCSV.DataType.Integer 200 |];
-        [| ImportCSV.DataType.Integer 2; ImportCSV.DataType.Integer 200 |];
-      ]
+      (Ok [ [| ImportCSV.DataType.Integer 1; ImportCSV.DataType.Integer 200 |] ])
 
 let test_suit = [ simple_extraction; sum_sort; sum_total; sum_unfiltered ]
 let tests = "sql_db" >::: test_suit
-- 
cgit v1.2.3