1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
;; copyright (c) 2020-2021 Sean Corfield, all rights reserved
(ns next.jdbc.datafy-test
"Tests for the datafy extensions over JDBC types."
(:require [clojure.datafy :as d]
[clojure.set :as set]
[clojure.test :refer [deftest is testing use-fixtures]]
[next.jdbc :as jdbc]
[next.jdbc.datafy]
[next.jdbc.result-set :as rs]
[next.jdbc.specs :as specs]
[next.jdbc.test-fixtures
:refer [with-test-db db ds
derby? jtds? mysql? postgres? sqlite?]]))
(set! *warn-on-reflection* true)
(use-fixtures :once with-test-db)
(specs/instrument)
(def ^:private basic-connection-keys
"Generic JDBC Connection fields."
#{:autoCommit :catalog :clientInfo :holdability :metaData
:networkTimeout :schema :transactionIsolation :typeMap :warnings
;; boolean properties
:closed :readOnly
;; configured to be added as if by clojure.core/bean
:class})
(deftest connection-datafy-tests
(testing "connection datafication"
(with-open [con (jdbc/get-connection (ds))]
(let [reference-keys (cond-> basic-connection-keys
(derby?) (-> (disj :networkTimeout)
(conj :networkTimeout/exception))
(jtds?) (-> (disj :clientInfo :networkTimeout :schema)
(conj :clientInfo/exception
:networkTimeout/exception
:schema/exception)))
data (set (keys (d/datafy con)))]
(when-let [diff (seq (set/difference data reference-keys))]
(println (format "%6s :%-10s %s"
(:dbtype (db)) "connection" (str (sort diff)))))
(is (= reference-keys
(set/intersection reference-keys data)))))))
(def ^:private basic-database-metadata-keys
"Generic JDBC Connection fields."
#{:JDBCMajorVersion :JDBCMinorVersion :SQLKeywords :SQLStateType :URL
:catalogSeparator :catalogTerm :catalogs
:clientInfoProperties :connection
:databaseMajorVersion :databaseMinorVersion
:databaseProductName :databaseProductVersion
:defaultTransactionIsolation
:driverMajorVersion :driverMinorVersion :driverName :driverVersion
:extraNameCharacters :identifierQuoteString
:maxBinaryLiteralLength :maxCatalogNameLength :maxCharLiteralLength
:maxColumnNameLength :maxColumnsInGroupBy :maxColumnsInIndex
:maxColumnsInOrderBy :maxColumnsInSelect :maxColumnsInTable
:maxConnections
:maxCursorNameLength :maxIndexLength
:maxProcedureNameLength :maxRowSize :maxSchemaNameLength
:maxStatementLength :maxStatements :maxTableNameLength
:maxTablesInSelect :maxUserNameLength :numericFunctions
:procedureTerm :resultSetHoldability :rowIdLifetime
:schemaTerm :schemas :searchStringEscape :stringFunctions
:systemFunctions :tableTypes :timeDateFunctions
:typeInfo :userName
;; boolean properties
:catalogAtStart :readOnly
;; configured to be added as if by clojure.core/bean
:class
;; added by next.jdbc.datafy if the datafication succeeds
:all-tables})
(deftest database-metadata-datafy-tests
(testing "database metadata datafication"
(with-open [con (jdbc/get-connection (ds))]
(let [reference-keys (cond-> basic-database-metadata-keys
(jtds?) (-> (disj :clientInfoProperties :rowIdLifetime)
(conj :clientInfoProperties/exception
:rowIdLifetime/exception))
(postgres?) (-> (disj :rowIdLifetime)
(conj :rowIdLifetime/exception))
(sqlite?) (-> (disj :clientInfoProperties :rowIdLifetime)
(conj :clientInfoProperties/exception
:rowIdLifetime/exception)))
data (set (keys (d/datafy (.getMetaData con))))]
(when-let [diff (seq (set/difference data reference-keys))]
(println (format "%6s :%-10s %s"
(:dbtype (db)) "db-meta" (str (sort diff)))))
(is (= reference-keys
(set/intersection reference-keys data))))))
(testing "nav to catalogs yields object"
(with-open [con (jdbc/get-connection (ds))]
(let [data (d/datafy (.getMetaData con))]
(doseq [k (cond-> #{:catalogs :clientInfoProperties :schemas :tableTypes :typeInfo}
(jtds?) (disj :clientInfoProperties)
(sqlite?) (disj :clientInfoProperties))]
(let [rs (d/nav data k nil)]
(is (vector? rs))
(is (every? map? rs))))))))
(deftest result-set-metadata-datafy-tests
(testing "result set metadata datafication"
(let [data (reduce (fn [_ row] (reduced (rs/metadata row)))
nil
(jdbc/plan (ds) [(str "SELECT * FROM "
(if (mysql?) "fruit" "FRUIT"))]))]
(is (vector? data))
(is (= 5 (count data)))
(is (every? map? data))
(is (every? :label data)))))
(comment
(def con (jdbc/get-connection (ds)))
(rs/datafiable-result-set (.getTables (.getMetaData con) nil nil nil nil) con {})
(def ps (jdbc/prepare con ["SELECT * FROM fruit WHERE grade > ?"]))
(require '[next.jdbc.prepare :as prep])
(prep/set-parameters ps [30])
(.execute ps)
(.getResultSet ps)
(.close ps)
(.close con))
|