From 3b0b059f628010e19a4ec09f71aa4a384b51191b Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Thu, 18 Apr 2019 21:35:38 -0700 Subject: [PATCH] Add connection tests Improve handling of relative files with H2 database connections. --- .gitignore | 2 +- src/next/jdbc/connection.clj | 9 +++- test/next/jdbc/connection_test.clj | 87 +++++++++++++++++++++++++++++- test/next/jdbc/sql_test.clj | 3 ++ 4 files changed, 97 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index c1ddb82..0747c89 100644 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,5 @@ pom.xml.asc /.nrepl-port .hgignore .hg/ -/clojure_test +/clojure_test* /derby.log diff --git a/src/next/jdbc/connection.clj b/src/next/jdbc/connection.clj index 795feec..c188fd9 100644 --- a/src/next/jdbc/connection.clj +++ b/src/next/jdbc/connection.clj @@ -90,7 +90,14 @@ db-sep (dbname-separators dbtype "/") url (cond (= "h2:mem" dbtype) (str "jdbc:" subprotocol ":" dbname ";DB_CLOSE_DELAY=-1") - (#{"derby" "h2" "hsqldb" "sqlite"} subprotocol) + (#{"h2"} subprotocol) + (str "jdbc:" subprotocol ":" + (if (re-find #"^([A-Za-z]:)?[\./\\]" dbname) + ;; DB name starts with relative or absolute path + dbname + ;; otherwise make it local + (str "./" dbname))) + (#{"derby" "hsqldb" "sqlite"} subprotocol) (str "jdbc:" subprotocol ":" dbname) :else (str "jdbc:" subprotocol ":" diff --git a/test/next/jdbc/connection_test.clj b/test/next/jdbc/connection_test.clj index 8f4d8d2..bbd4ff1 100644 --- a/test/next/jdbc/connection_test.clj +++ b/test/next/jdbc/connection_test.clj @@ -1,5 +1,88 @@ ;; copyright (c) 2019 Sean Corfield, all rights reserved (ns next.jdbc.connection-test - (:require [clojure.test :refer [deftest is testing]] - [next.jdbc.connection :refer :all])) + (:require [clojure.string :as str] + [clojure.test :refer [deftest is testing]] + [next.jdbc.connection :as c] + [next.jdbc.protocols :as p])) + +(def ^:private db-name "clojure_test") + +(deftest test-aliases-and-defaults + (testing "aliases" + (is (= (#'c/spec->url+etc {:dbtype "hsql" :dbname db-name}) + (#'c/spec->url+etc {:dbtype "hsqldb" :dbname db-name}))) + (is (= (#'c/spec->url+etc {:dbtype "jtds" :dbname db-name}) + (#'c/spec->url+etc {:dbtype "jtds:sqlserver" :dbname db-name}))) + (is (= (#'c/spec->url+etc {:dbtype "mssql" :dbname db-name}) + (#'c/spec->url+etc {:dbtype "sqlserver" :dbname db-name}))) + (is (= (#'c/spec->url+etc {:dbtype "oracle" :dbname db-name}) + (#'c/spec->url+etc {:dbtype "oracle:thin" :dbname db-name}))) + (is (= (#'c/spec->url+etc {:dbtype "oracle:sid" :dbname db-name}) + (-> (#'c/spec->url+etc {:dbtype "oracle:thin" :dbname db-name}) + ;; oracle:sid uses : before DB name, not / + (update 0 str/replace (re-pattern (str "/" db-name)) (str ":" db-name))))) + (is (= (#'c/spec->url+etc {:dbtype "oracle:oci" :dbname db-name}) + (-> (#'c/spec->url+etc {:dbtype "oracle:thin" :dbname db-name}) + ;; oracle:oci and oracle:thin only differ in the protocol + (update 0 str/replace #":thin" ":oci")))) + (is (= (#'c/spec->url+etc {:dbtype "postgres" :dbname db-name}) + (#'c/spec->url+etc {:dbtype "postgresql" :dbname db-name})))) + (testing "default ports" + (is (= (-> (#'c/spec->url+etc {:dbtype "jtds:sqlserver" :dbname db-name}) + ;; defaults don't show up in etc (just in the url) + (update 1 assoc :port 1433)) + (#'c/spec->url+etc {:dbtype "jtds:sqlserver" :dbname db-name :port 1433}))) + (is (= (-> (#'c/spec->url+etc {:dbtype "mysql" :dbname db-name}) + ;; defaults don't show up in etc (just in the url) + (update 1 assoc :port 3306)) + (#'c/spec->url+etc {:dbtype "mysql" :dbname db-name :port 3306}))) + (is (= (-> (#'c/spec->url+etc {:dbtype "oracle:oci" :dbname db-name}) + ;; defaults don't show up in etc (just in the url) + (update 1 assoc :port 1521)) + (#'c/spec->url+etc {:dbtype "oracle:oci" :dbname db-name :port 1521}))) + (is (= (-> (#'c/spec->url+etc {:dbtype "oracle:sid" :dbname db-name}) + ;; defaults don't show up in etc (just in the url) + (update 1 assoc :port 1521)) + (#'c/spec->url+etc {:dbtype "oracle:sid" :dbname db-name :port 1521}))) + (is (= (-> (#'c/spec->url+etc {:dbtype "oracle:thin" :dbname db-name}) + ;; defaults don't show up in etc (just in the url) + (update 1 assoc :port 1521)) + (#'c/spec->url+etc {:dbtype "oracle:thin" :dbname db-name :port 1521}))) + (is (= (-> (#'c/spec->url+etc {:dbtype "postgresql" :dbname db-name}) + ;; defaults don't show up in etc (just in the url) + (update 1 assoc :port 5432)) + (#'c/spec->url+etc {:dbtype "postgresql" :dbname db-name :port 5432}))) + (is (= (-> (#'c/spec->url+etc {:dbtype "sqlserver" :dbname db-name}) + ;; defaults don't show up in etc (just in the url) + (update 1 assoc :port 1433)) + (#'c/spec->url+etc {:dbtype "sqlserver" :dbname db-name :port 1433}))))) + +;; these are the 'local' databases that we can always test against +(def test-db-type ["derby" "h2" "h2:mem" "hsqldb" "sqlite"]) + +(def test-dbs + (for [db test-db-type] + (cond-> {:dbtype db :dbname (str db-name "_" (str/replace db #":" "_"))} + (= "derby" db) + (assoc :create true)))) + +(deftest test-get-connection + (doseq [db test-dbs] + (println 'test-get-connection (:dbtype db)) + (testing "datasource via Associative" + (let [ds (p/get-datasource db)] + (is (instance? javax.sql.DataSource ds)) + ;; checks get-datasource on a DataSource is identity + (is (identical? ds (p/get-datasource ds))) + (with-open [con (p/get-connection ds {})] + (is (instance? java.sql.Connection con))))) + (testing "datasource via String" + (let [[url _] (#'c/spec->url+etc db) + ds (p/get-datasource url)] + (is (instance? javax.sql.DataSource ds)) + (with-open [con (p/get-connection ds {})] + (is (instance? java.sql.Connection con))))) + (testing "connection via map (Object)" + (with-open [con (p/get-connection db {})] + (is (instance? java.sql.Connection con)))))) diff --git a/test/next/jdbc/sql_test.clj b/test/next/jdbc/sql_test.clj index 06c3d0f..7a7937f 100644 --- a/test/next/jdbc/sql_test.clj +++ b/test/next/jdbc/sql_test.clj @@ -1,6 +1,9 @@ ;; copyright (c) 2019 Sean Corfield, all rights reserved (ns next.jdbc.sql-test + "Tests for the (private) SQL string building functions in next.jdbc.sql. + + At some future date, tests for the syntactic sugar functions will be added." (:require [clojure.test :refer [deftest is testing]] [next.jdbc.quoted :refer [mysql sql-server]] [next.jdbc.sql :as sql]))