From c4df28bfe83b618b39b7eb3d29a706bc71d2d531 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Thu, 23 Dec 2021 16:42:02 -0800 Subject: [PATCH] fix #193 by expanding some specs --- CHANGELOG.md | 1 + src/next/jdbc/specs.clj | 11 +++++++---- test/next/jdbc_test.clj | 10 ++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5b552d..4704bb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ Only accretive/fixative changes will be made from now on. * 1.2.next in progress + * Address #193 by expanding the argument specs for `get-datasource` and `get-connection`. * Update log4j2 test dependency. * Update `build-clj` to v0.6.5. diff --git a/src/next/jdbc/specs.clj b/src/next/jdbc/specs.clj index 8e2a65d..de9fd19 100644 --- a/src/next/jdbc/specs.clj +++ b/src/next/jdbc/specs.clj @@ -9,8 +9,7 @@ Just `:args` are spec'd. These specs are intended to aid development with `next.jdbc` by catching simple errors in calling the library. The `connectable` argument is currently just `any?` but both - `get-datasource` and `get-connection` have stricter specs. If you - extend `Sourceable` or `Connectable`, those specs will likely be too strict. + `get-datasource` and `get-connection` have stricter specs. In addition, there is an `instrument` function that provides a simple way to instrument all of the `next.jdbc` functions, and `unstrument` @@ -20,6 +19,7 @@ [next.jdbc :as jdbc] [next.jdbc.connection :as connection] [next.jdbc.prepare :as prepare] + [next.jdbc.protocols :as p] [next.jdbc.sql :as sql]) (:import (java.sql Connection PreparedStatement Statement) (javax.sql DataSource))) @@ -65,6 +65,9 @@ :ds ::datasource)) (s/def ::db-spec-or-jdbc (s/or :db-spec ::db-spec-map :jdbc-url ::jdbc-url-map)) +(s/def ::proto-connectable (s/or :db-spec ::db-spec + :connectable #(satisfies? p/Connectable %) + :sourceable #(satisfies? p/Sourceable %))) (s/def ::connectable any?) (s/def ::key-map (s/map-of keyword? any?)) @@ -99,10 +102,10 @@ (s/def ::batch-opts (s/keys :opt-un [::batch-size ::large])) (s/fdef jdbc/get-datasource - :args (s/cat :spec ::db-spec)) + :args (s/cat :spec ::proto-connectable)) (s/fdef jdbc/get-connection - :args (s/cat :spec ::db-spec + :args (s/cat :spec ::proto-connectable :opts (s/? ::opts-map))) (s/fdef jdbc/prepare diff --git a/test/next/jdbc_test.clj b/test/next/jdbc_test.clj index 5bfe37e..a4d9f8e 100644 --- a/test/next/jdbc_test.clj +++ b/test/next/jdbc_test.clj @@ -26,6 +26,16 @@ (specs/instrument) +(deftest spec-tests + (let [db-spec {:dbtype "h2:mem" :dbname "clojure_test"}] + ;; some sanity checks on instrumented function calls: + (jdbc/get-datasource db-spec) + (jdbc/get-connection db-spec) + ;; and again with options: + (let [db-spec' (jdbc/with-options db-spec {})] + (jdbc/get-datasource db-spec') + (jdbc/get-connection db-spec')))) + (deftest basic-tests ;; use ds-opts instead of (ds) anywhere you want default options applied: (let [ds-opts (jdbc/with-options (ds) (default-options))]