Add datafication tests; fix regex bug!
This commit is contained in:
parent
42cfe88859
commit
d4f79a68fd
3 changed files with 91 additions and 3 deletions
|
|
@ -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])))
|
||||
|
||||
|
|
|
|||
|
|
@ -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))))))))
|
||||
|
|
|
|||
43
test/next/jdbc/test_fixtures.clj
Normal file
43
test/next/jdbc/test_fixtures.clj
Normal file
|
|
@ -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)))
|
||||
Loading…
Reference in a new issue