From d4f79a68fdddae0c0db0836d4ecd33365fe4ff94 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Thu, 18 Apr 2019 22:43:19 -0700 Subject: [PATCH] Add datafication tests; fix regex bug! --- src/next/jdbc/result_set.clj | 2 +- test/next/jdbc/result_set_test.clj | 49 ++++++++++++++++++++++++++++-- test/next/jdbc/test_fixtures.clj | 43 ++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 test/next/jdbc/test_fixtures.clj diff --git a/src/next/jdbc/result_set.clj b/src/next/jdbc/result_set.clj index 9c44aa6..9809c24 100644 --- a/src/next/jdbc/result_set.clj +++ b/src/next/jdbc/result_set.clj @@ -368,7 +368,7 @@ If a column name ends with _id or id, it is assumed to be a foreign key into the table identified by the first part of the column name." [col] - (let [[_ table] (re-find #"(?i)^(.+)_?id$" (name col))] + (let [[_ table] (re-find #"(?i)^(.+?)_?id$" (name col))] (when table [(keyword table) :id]))) diff --git a/test/next/jdbc/result_set_test.clj b/test/next/jdbc/result_set_test.clj index 6fea93f..685d641 100644 --- a/test/next/jdbc/result_set_test.clj +++ b/test/next/jdbc/result_set_test.clj @@ -10,5 +10,50 @@ * datafy/nav support * ResultSet-as-map for reducible! / -execute protocol * -execute-one and -execute-all implementations" - (:require [clojure.test :refer [deftest is testing]] - [next.jdbc.result-set :refer :all])) + (:require [clojure.datafy :as d] + [clojure.test :refer [deftest is testing use-fixtures]] + [next.jdbc.test-fixtures :refer [with-test-db ds]] + [next.jdbc.result-set :as rs])) + +(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 {}) + data (d/datafy test-row) + v (get data :table/fruit_id)] + ;; check datafication is sane + (is (= 2 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)))))) + (testing "custom schema :one" + (let [connectable (ds) + test-row (rs/datafiable-row {:foo/bar 2} connectable + {:schema {:foo/bar [:fruit :id]}}) + data (d/datafy test-row) + v (get data :foo/bar)] + ;; check datafication is sane + (is (= 2 v)) + (let [object (d/nav data :foo/bar 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)))))) + (testing "custom schema :many" + (let [connectable (ds) + test-row (rs/datafiable-row {:foo/bar 2} connectable + {:schema {:foo/bar [:fruit :id :many]}}) + data (d/datafy test-row) + v (get data :foo/bar)] + ;; check datafication is sane + (is (= 2 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)))))))) diff --git a/test/next/jdbc/test_fixtures.clj b/test/next/jdbc/test_fixtures.clj new file mode 100644 index 0000000..3531a10 --- /dev/null +++ b/test/next/jdbc/test_fixtures.clj @@ -0,0 +1,43 @@ +;; copyright (c) 2019 Sean Corfield, all rights reserved + +(ns next.jdbc.test-fixtures + (:require [next.jdbc :as jdbc] + [next.jdbc.sql :as sql])) + +(def ^:private test-db-spec {:dbtype "h2:mem" :dbname "clojure_test_fixture"}) + +(def ^:private test-datasource (atom nil)) + +(defn ds + "Tests should call this to get the DataSource to use inside a fixture." + [] + @test-datasource) + +(defn with-test-db + "Given a test function (or suite), run it in the context of an in-memory + H2 database set up with a simple fruit table containing four rows of data. + + Tests can reach into here and call ds (above) to get a DataSource for use + in test functions (that operate inside this fixture)." + [t] + (reset! test-datasource (jdbc/get-datasource test-db-spec)) + (with-open [con (jdbc/get-connection (ds))] + (try + (jdbc/execute-one! con ["DROP TABLE fruit"]) + (catch Exception _)) + (jdbc/execute-one! con [" +CREATE TABLE fruit ( + id int default 0, + name varchar(32) primary key, + appearance varchar(32), + cost int, + grade real +)"]) + (sql/insert-multi! con :fruit + [:id :name :appearance :cost :grade] + [[1 "Apple" "red" 59 87] + [2,"Banana","yellow",29,92.2] + [3,"Peach","fuzzy",139,90.0] + [4,"Orange","juicy",89,88.6]] + {:return-keys false}) + (t)))