Skip to content

Commit 11209af

Browse files
authored
Test that joins come back (#20)
* Test that joins come back * Additional test
1 parent f4b5b94 commit 11209af

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

test/toucan2/select_test.clj

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
[toucan2.test :as test]
1313
[toucan2.tools.compile :as tools.compile])
1414
(:import
15-
(java.time OffsetDateTime)))
15+
(java.time LocalDateTime OffsetDateTime)))
1616

1717
(set! *warn-on-reflection* true)
1818

@@ -388,3 +388,38 @@
388388
(select/select-one ::test/venues nil)
389389
(select/select-one-fn :id ::test/venues nil)
390390
(select/select-one-fn int ::test/venues nil)))))
391+
392+
(deftest select-join-test
393+
(testing "Extra columns from joined tables should come back"
394+
(is (= (instance/instance ::test/venues
395+
{:id 1
396+
:name "bar"
397+
:category "bar"
398+
:created-at (LocalDateTime/parse "2017-01-01T00:00")
399+
:updated-at (LocalDateTime/parse "2017-01-01T00:00")
400+
:slug "bar_01"
401+
:parent-category nil})
402+
(select/select-one ::test/venues
403+
{:left-join [[:category :c] [:= :venues.category :c.name]]
404+
:order-by [[:id :asc]]})))))
405+
406+
(derive ::venues.with-category ::test/venues)
407+
408+
(m/defmethod query/build :after [#_query-type :toucan.query-type/select.*
409+
#_model ::venues.with-category
410+
#_query clojure.lang.IPersistentMap]
411+
[_query-type _model built-query]
412+
(assoc built-query :left-join [[:category :c] [:= :venues.category :c.name]]))
413+
414+
(deftest joined-model-test
415+
(is (= (instance/instance ::venues.with-category
416+
{:id 1
417+
:name "bar"
418+
:category "bar"
419+
:created-at (LocalDateTime/parse "2017-01-01T00:00")
420+
:updated-at (LocalDateTime/parse "2017-01-01T00:00")
421+
:slug "bar_01"
422+
:parent-category nil})
423+
(select/select-one ::venues.with-category
424+
{:left-join [[:category :c] [:= :venues.category :c.name]]
425+
:order-by [[:id :asc]]}))))

test/toucan2/test.clj

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@
118118
(fn [db-type model-or-table-name]
119119
[(keyword db-type) (keyword model-or-table-name)]))
120120

121-
(m/defmethod create-table-sql-file [:postgres :people] [_db-type _table] "toucan2/test/people.postgres.sql")
122-
(m/defmethod create-table-sql-file [:h2 :people] [_db-type _table] "toucan2/test/people.h2.sql")
123-
(m/defmethod create-table-sql-file [:postgres :venues] [_db-type _table] "toucan2/test/venues.postgres.sql")
124-
(m/defmethod create-table-sql-file [:h2 :venues] [_db-type _table] "toucan2/test/venues.h2.sql")
125-
(m/defmethod create-table-sql-file [:default :birds] [_db-type _table] "toucan2/test/birds.sql")
121+
(m/defmethod create-table-sql-file [:postgres :people] [_db-type _table] "toucan2/test/people.postgres.sql")
122+
(m/defmethod create-table-sql-file [:h2 :people] [_db-type _table] "toucan2/test/people.h2.sql")
123+
(m/defmethod create-table-sql-file [:postgres :venues] [_db-type _table] "toucan2/test/venues.postgres.sql")
124+
(m/defmethod create-table-sql-file [:h2 :venues] [_db-type _table] "toucan2/test/venues.h2.sql")
125+
(m/defmethod create-table-sql-file [:default :birds] [_db-type _table] "toucan2/test/birds.sql")
126+
(m/defmethod create-table-sql-file [:default :categories] [_db-type _table] "toucan2/test/categories.sql")
126127

127128
(derive ::people ::models)
128129

@@ -142,6 +143,12 @@
142143
[_model]
143144
"birds")
144145

146+
(derive ::categories ::models)
147+
148+
(m/defmethod model/table-name ::categories
149+
[_model]
150+
"category")
151+
145152
(defn- create-table-statements [db-type table-name]
146153
(let [filename (create-table-sql-file db-type table-name)
147154
file (some-> (io/resource filename) io/file)]
@@ -184,7 +191,8 @@
184191
(with-open [conn (java.sql.DriverManager/getConnection (test-db-url db-type))]
185192
(doseq [table-name [:people
186193
:venues
187-
:birds]]
194+
:birds
195+
:categories]]
188196
(create-table! db-type conn table-name)))
189197
(swap! initialized-test-dbs conj db-type)))
190198

test/toucan2/test/categories.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
DROP TABLE IF EXISTS category;
2+
3+
CREATE TABLE category (
4+
name text PRIMARY KEY NOT NULL,
5+
slug text,
6+
parent_category text REFERENCES category (name) ON DELETE CASCADE
7+
);
8+
9+
INSERT INTO category (name, slug, parent_category)
10+
VALUES
11+
('bar', 'bar_01', NULL),
12+
('store', 'store_02', NULL),
13+
('dive-bar', 'dive_bar_03', 'bar');

0 commit comments

Comments
 (0)