(* Copyright (C) 2014--2024 Petter A. Urkedal * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or (at your * option) any later version, with the LGPL-3.0 Linking Exception. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * and the LGPL-3.0 Linking Exception along with this library. If not, see * and , respectively. *) open Async_kernel open Async_unix open Core module List = Stdlib.List open Testlib open Testlib_async module Test_error_cause = Test_error_cause.Make (Testlib_async) module Test_parallel = Test_parallel.Make (Testlib_async) module Test_param = Test_param.Make (Testlib_async) module Test_sql = Test_sql.Make (Testlib_async) module Test_failure = Test_failure.Make (Testlib_async) module Test_connect = Test_connect.Make (Testlib_async) let mk_test (name, connect, pool) = let pass_connect (name, speed, f) = (name, speed, (fun () -> f connect)) in let pass_conn (name, speed, f) = let f' () = Caqti_async.Pool.use (fun c -> f c >>| fun () -> Ok ()) pool >>| function | Ok () -> () | Error err -> Alcotest.failf "%a" Caqti_error.pp err in (name, speed, f') in let pass_pool (name, speed, f) = (name, speed, (fun () -> f pool)) in let test_cases = List.map pass_conn Test_sql.connection_test_cases @ List.map pass_conn Test_error_cause.test_cases @ List.map pass_pool Test_parallel.test_cases @ List.map pass_conn Test_param.test_cases @ List.map pass_conn Test_failure.test_cases @ List.map pass_pool Test_sql.pool_test_cases @ List.map pass_connect Test_connect.test_cases in (name, test_cases) let post_connect conn = List_result_fiber.iter_s (fun f -> f conn) [ Test_sql.post_connect; ] let env = let (&) f g di var = try f di var with Stdlib.Not_found -> g di var in Test_sql.env & Test_error_cause.env let mk_tests {uris; connect_config} = let pool_config = Caqti_pool_config.create ~max_size:16 () in let create_target uri = let connect () = Caqti_async.connect ~config:connect_config ~env uri in (match Caqti_async.connect_pool uri ~pool_config ~post_connect ~config:connect_config ~env with | Ok pool -> (test_name_of_uri uri, connect, pool) | Error err -> Error.raise (Error.of_exn (Caqti_error.Exn err))) in let targets = List.map create_target uris in List.map mk_test targets let main () = Deferred.upon (Alcotest_cli.run_with_args_dependency "test_sql_async" (Testlib.common_args ()) mk_tests) (fun () -> Shutdown.shutdown 0) let () = never_returns (Scheduler.go_main ~main ())