From bfda745a703411782ce9f5489899d535482ff0d6 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Thu, 18 Apr 2019 23:03:09 -0700 Subject: [PATCH] Add row builder tests; fix row builder bug in execute-one --- src/next/jdbc/result_set.clj | 6 ++-- test/next/jdbc/result_set_test.clj | 58 ++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/next/jdbc/result_set.clj b/src/next/jdbc/result_set.clj index 9809c24..a287bf3 100644 --- a/src/next/jdbc/result_set.clj +++ b/src/next/jdbc/result_set.clj @@ -307,8 +307,10 @@ (rest sql-params) opts)] (if-let [rs (stmt->result-set stmt opts)] - (when (.next rs) - (datafiable-row (row-builder (as-maps rs opts)) this opts)) + (let [gen-fn (get opts :gen-fn as-maps) + gen (gen-fn rs opts)] + (when (.next rs) + (datafiable-row (row-builder gen) this opts))) {:next.jdbc/update-count (.getUpdateCount stmt)})))) (-execute-all [this sql-params opts] (with-open [con (p/get-connection this opts)] diff --git a/test/next/jdbc/result_set_test.clj b/test/next/jdbc/result_set_test.clj index 685d641..3396930 100644 --- a/test/next/jdbc/result_set_test.clj +++ b/test/next/jdbc/result_set_test.clj @@ -7,29 +7,30 @@ * column name generation functions * ReadableColumn protocol extension point * RowBuilder and ResultSetBuilder machinery - * datafy/nav support * ResultSet-as-map for reducible! / -execute protocol * -execute-one and -execute-all implementations" (:require [clojure.datafy :as d] + [clojure.string :as str] [clojure.test :refer [deftest is testing use-fixtures]] - [next.jdbc.test-fixtures :refer [with-test-db ds]] - [next.jdbc.result-set :as rs])) + [next.jdbc.protocols :as p] + [next.jdbc.result-set :as rs] + [next.jdbc.test-fixtures :refer [with-test-db ds]])) (use-fixtures :once with-test-db) (deftest test-datafy-nav (testing "default schema" (let [connectable (ds) - test-row (rs/datafiable-row {:table/fruit_id 2} connectable {}) + test-row (rs/datafiable-row {:table/fruit_id 1} connectable {}) data (d/datafy test-row) v (get data :table/fruit_id)] ;; check datafication is sane - (is (= 2 v)) + (is (= 1 v)) (let [object (d/nav data :table/fruit_id v)] ;; check nav produces a single map with the expected key/value data ;; and remember H2 is all UPPERCASE! - (is (= 2 (:FRUIT/ID object))) - (is (= "Banana" (:FRUIT/NAME object)))))) + (is (= 1 (:FRUIT/ID object))) + (is (= "Apple" (:FRUIT/NAME object)))))) (testing "custom schema :one" (let [connectable (ds) test-row (rs/datafiable-row {:foo/bar 2} connectable @@ -45,15 +46,50 @@ (is (= "Banana" (:FRUIT/NAME object)))))) (testing "custom schema :many" (let [connectable (ds) - test-row (rs/datafiable-row {:foo/bar 2} connectable + test-row (rs/datafiable-row {:foo/bar 3} connectable {:schema {:foo/bar [:fruit :id :many]}}) data (d/datafy test-row) v (get data :foo/bar)] ;; check datafication is sane - (is (= 2 v)) + (is (= 3 v)) (let [object (d/nav data :foo/bar v)] ;; check nav produces a result set with the expected key/value data ;; and remember H2 is all UPPERCASE! (is (vector? object)) - (is (= 2 (:FRUIT/ID (first object)))) - (is (= "Banana" (:FRUIT/NAME (first object)))))))) + (is (= 3 (:FRUIT/ID (first object)))) + (is (= "Peach" (:FRUIT/NAME (first object)))))))) + + +(defn get-lower-column-names [^java.sql.ResultSetMetaData rsmeta opts] + (let [idxs (range 1 (inc (.getColumnCount rsmeta)))] + (mapv (fn [^Integer i] + (keyword (str/lower-case (.getColumnLabel rsmeta i)))) + idxs))) + +(defn as-lower-maps [^java.sql.ResultSet rs opts] + (let [rsmeta (.getMetaData rs) + cols (get-lower-column-names rsmeta opts)] + (rs/->MapResultSetBuilder rs rsmeta cols))) + +(deftest test-map-row-builder + (testing "default row builder" + (let [row (p/-execute-one (ds) + ["select * from fruit where id = ?" 1] + {})] + (is (map? row)) + (is (= 1 (:FRUIT/ID row))) + (is (= "Apple" (:FRUIT/NAME row))))) + (testing "unqualified row builder" + (let [row (p/-execute-one (ds) + ["select * from fruit where id = ?" 2] + {:gen-fn rs/as-unqualified-maps})] + (is (map? row)) + (is (= 2 (:ID row))) + (is (= "Banana" (:NAME row))))) + (testing "lower-case row builder" + (let [row (p/-execute-one (ds) + ["select * from fruit where id = ?" 3] + {:gen-fn as-lower-maps})] + (is (map? row)) + (is (= 3 (:id row))) + (is (= "Peach" (:name row))))))