; Generate default file containing DB URIs to test against, if not present.

(rule
  (targets uris.conf)
  (mode fallback)
  (action (write-file %{targets} "sqlite3:test.db?busy_timeout=60000\n")))

(executable
  (name deps_of_uris)
  (modules deps_of_uris)
  (libraries testlib testlib_tls uri))

(rule
  (target dune.uri-deps-blocking)
  (deps
    (:U %{env:CAQTI_TEST_URIS_FILE=uris.conf})
    (env_var CAQTI_TEST_X509_AUTHENTICATOR))
  (action
    ; FIXME: Do we need to work around missing TLS?
    (with-stdout-to %{target}
      (run %{dep:deps_of_uris.exe} --profile %{profile} -U %{U}))))
(rule
  (target dune.uri-deps-eio)
  (deps
    (:U %{env:CAQTI_TEST_URIS_FILE=uris.conf})
    (env_var CAQTI_TEST_X509_AUTHENTICATOR))
  (action
    (with-stdout-to %{target}
      (run %{dep:deps_of_uris.exe} --profile %{profile} -U %{U}
           --tls-library caqti-tls-eio))))
(rule
  (target dune.uri-deps-lwt)
  (deps
    (:U %{env:CAQTI_TEST_URIS_FILE=uris.conf})
    (env_var CAQTI_TEST_X509_AUTHENTICATOR))
  (action
    (with-stdout-to %{target}
      (run %{dep:deps_of_uris.exe} --profile %{profile} -U %{U}
           --tls-library caqti-tls-lwt.unix))))
(rule
  (target dune.uri-deps-async)
  (deps
    (:U %{env:CAQTI_TEST_URIS_FILE=uris.conf})
    (env_var CAQTI_TEST_X509_AUTHENTICATOR))
  (action
    (with-stdout-to %{target}
      (run %{dep:deps_of_uris.exe} --profile %{profile} -U %{U}
           --tls-library caqti-tls-async))))

(rule
 (target dune.uri-deps-miou-unix)
 (deps
  (:U %{env:CAQTI_TEST_URIS_FILE=uris.conf})
  (env_var CAQTI_TEST_X509_AUTHENTICATOR))
 (action
  (with-stdout-to
   %{target}
   (run
    %{dep:deps_of_uris.exe}
    --profile
    %{profile}
    -U
    %{U}
    --tls-library
    caqti-tls-miou))))

; The generic testsuite as a library.

(library
  (name testsuite)
  (wrapped false)
  (flags (:standard -alert -caqti_unstable))
  (modules
    test_connect
    test_error_cause
    test_failure
    test_parallel
    test_param
    test_sql)
  (libraries caqti caqti.platform ptime.clock.os testlib testlib_tls))

; Instantiations of the testsuite.

(executable
  (name main_blocking)
  (modules main_blocking)
  (libraries
    caqti caqti.blocking caqti.plugin
    alcotest testlib testlib_blocking testsuite))

(rule
  (alias runtest)
  ; This breaks the scheme of placing tests under the platform packages in order
  ; to avoid a cyclic with-test dependency.
  (package caqti-driver-sqlite3)
  (deps
    (:test main_blocking.exe)
    (:U %{env:CAQTI_TEST_URIS_FILE=uris.conf})
    (include dune.uri-deps-blocking)
    (env_var CAQTI_TEST_X509_AUTHENTICATOR))
  (locks /db/testsuite)
  (action (run %{test} -U %{U})))

(executable
  (name main_eio_unix)
  (enabled_if (>= %{ocaml_version} "5.0"))
  (modules main_eio_unix)
  (libraries
    caqti caqti-eio.unix caqti.plugin
    alcotest eio eio_main mirage-crypto-rng.unix
    testlib testlib_eio_unix testsuite))

(rule
  (alias runtest)
  (package caqti-eio)
  (enabled_if (>= %{ocaml_version} "5.0"))
  (deps
    (:test main_eio_unix.exe)
    (:U %{env:CAQTI_TEST_URIS_FILE=uris.conf})
    (include dune.uri-deps-eio)
    (env_var CAQTI_TEST_X509_AUTHENTICATOR))
  (locks /db/testsuite)
  (action (run %{test} -U %{U})))

(executable
 (name main_miou_unix)
 (enabled_if
  (>= %{ocaml_version} "5.0"))
 (modules main_miou_unix)
 (libraries
  caqti
  caqti-miou.unix
  caqti.plugin
  alcotest
  threads
  mirage-crypto-rng-miou-unix
  testlib
  testlib_miou_unix
  testsuite))

(rule
 (alias runtest)
 (package caqti-miou)
 (enabled_if
  (>= %{ocaml_version} "5.0"))
 (deps
  (:test main_miou_unix.exe)
  (:U %{env:CAQTI_TEST_URIS_FILE=uris.conf})
  (include dune.uri-deps-miou-unix)
  (env_var CAQTI_TEST_X509_AUTHENTICATOR))
 (locks /db/testsuite)
 (action
  (run %{test} -U %{U})))

(executable
  (name main_async)
  (modules main_async)
  (libraries
    caqti caqti.platform caqti.plugin caqti-async
    alcotest-async testlib testlib_async testsuite))

(rule
  (alias runtest)
  (package caqti-async)
  (deps
    (:test main_async.exe)
    (:U %{env:CAQTI_TEST_URIS_FILE=uris.conf})
    (include dune.uri-deps-async)
    (env_var CAQTI_TEST_X509_AUTHENTICATOR))
  (locks /db/testsuite)
  (action (run %{test} -U %{U})))

(executable
  (name main_lwt_unix)
  (modules main_lwt_unix)
  (libraries
    caqti caqti.platform caqti.plugin
    caqti-lwt caqti-lwt.unix
    alcotest-lwt testlib testlib_lwt_unix testsuite))

(rule
  (alias runtest)
  (package caqti-lwt)
  (deps (:test main_lwt_unix.exe) (include dune.uri-deps-lwt) uris.conf)
  (locks /db/testsuite)
  (action (run %{test})))
