Skip to content

Commit fbc0ffc

Browse files
committed
added h2 support, along with tests
1 parent 70ba428 commit fbc0ffc

File tree

6 files changed

+126
-6
lines changed

6 files changed

+126
-6
lines changed

project.clj

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
[fulcrologic/fulcro-spec "1.0.0-beta8" :scope "test" :exclusions [org.clojure/tools.reader]]
2929
[clj-time "0.14.0" :scope "test"]
3030
[org.mariadb.jdbc/mariadb-java-client "2.1.0" :scope "test"]
31-
[org.postgresql/postgresql "42.1.4.jre7" :scope "test"]])
31+
[org.postgresql/postgresql "42.1.4.jre7" :scope "test"]
32+
[com.h2database/h2 "1.4.196" :scope "test"]])

src/dev/user.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
{:config {:port 8888}
88
:test-paths ["src/test"]
99
:source-paths ["src/main"]}
10-
{:available #{:focused :integration :mysql}
10+
{:available #{:focused :integration :mysql :h2}
1111
:default #{::sel/none :focused}})

src/main/fulcro_sql/core.clj

+4
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@
154154
(jdbc/execute! db ["DROP SCHEMA PUBLIC CASCADE"])
155155
(jdbc/execute! db ["CREATE SCHEMA PUBLIC"]))
156156

157+
(defmethod create-drop* :h2 [db dbkey dbconfig]
158+
(timbre/info "Create-drop was set. Cleaning everything out of the database " dbkey " (PostgreSQL).")
159+
(jdbc/execute! db ["DROP ALL OBJECTS"]))
160+
157161
(defmethod create-drop* :mysql [db dbkey dbconfig]
158162
(let [name (get dbconfig :database-name (name dbkey))]
159163
(timbre/info "Create-drop was set. Cleaning everything out of the database " name " (MySQL).")

src/test/fulcro_sql/core_spec.clj

+55-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
:database-name "test"
2020
:migrations ["classpath:migrations/mysqltest"]})
2121

22+
(def h2-database {:hikaricp-config "h2test.properties"
23+
:driver :h2
24+
:database-name "test"
25+
:migrations ["classpath:migrations/h2test"]})
26+
2227
(def test-schema {::core/graph->sql {:person/name :member/name
2328
:person/account :member/account_id
2429
:settings/auto-open? :settings/auto_open
@@ -42,6 +47,9 @@
4247
:driver :mysql
4348
:database-name "test")) ; needed for create/drop in mysql...there is no drop schema
4449

50+
(def h2-schema
51+
(assoc test-schema :driver :h2))
52+
4553
(specification "Database Component" :integration
4654
(behavior "Can create a functional database pool from HikariCP properties and Flyway migrations."
4755
(with-database [db test-database]
@@ -225,7 +233,7 @@
225233
(core/seed-row :todo_list_item {:id :item-2-1 :label "B.1" :parent_item_id :item-2})
226234
(core/seed-row :todo_list_item {:id :item-2-2 :label "B.2" :parent_item_id :item-2})])
227235

228-
(specification "Integration Tests for Graph Queries (PostgreSQL)" :integration :focused
236+
(specification "Integration Tests for Graph Queries (PostgreSQL)" :integration
229237
(with-database [db test-database]
230238
(let [{:keys [id/joe id/mary id/invoice-1 id/invoice-2 id/gadget id/widget id/spanner id/sam id/sally id/judy id/joe-settings
231239
list-1 item-1 item-1-1 item-1-1-1 item-2 item-2-1 item-2-2]} (core/seed! db test-schema test-rows)
@@ -303,9 +311,9 @@
303311
:account/members [{:db/id sam :person/name "Sam"}
304312
{:db/id sally :person/name "Sally"}]
305313
:account/settings {:db/id joe-settings :settings/auto-open? true}}
306-
{:db/id mary
307-
:account/name "Mary"
308-
:account/members [{:db/id judy :person/name "Judy"}]}]
314+
{:db/id mary
315+
:account/name "Mary"
316+
:account/members [{:db/id judy :person/name "Judy"}]}]
309317
query-3 [:db/id :item/name {:item/invoices [:db/id {:invoice/account [:db/id :account/name]}]}]
310318
expected-result-3 [{:db/id gadget :item/name "gadget"
311319
:item/invoices [{:db/id invoice-1 :invoice/account {:db/id joe :account/name "Joe"}}
@@ -326,6 +334,49 @@
326334
"many-to-many (reverse)"
327335
(core/run-query db mysql-schema :account/id query-3 (sorted-set gadget)) => expected-result-3))))
328336

337+
(specification "H2 Integration Tests" :h2
338+
(with-database [db h2-database]
339+
(let [{:keys [id/joe id/mary id/invoice-1 id/invoice-2 id/gadget id/widget id/spanner id/sam id/sally id/judy id/joe-settings]} (core/seed! db h2-schema test-rows)
340+
query [:db/id :account/name {:account/invoices [:db/id
341+
; TODO: data on join table
342+
;{:invoice/invoice_items [:invoice_items/quantity]}
343+
{:invoice/items [:db/id :item/name]}]}]
344+
expected-result {:db/id joe
345+
:account/name "Joe"
346+
:account/invoices [{:db/id invoice-1 :invoice/items [{:db/id gadget :item/name "gadget"}]}
347+
{:db/id invoice-2 :invoice/items [{:db/id widget :item/name "widget"}
348+
{:db/id spanner :item/name "spanner"}
349+
{:db/id gadget :item/name "gadget"}]}]}
350+
query-2 [:db/id :account/name {:account/members [:db/id :person/name]} {:account/settings [:db/id :settings/auto-open?]}]
351+
expected-result-2 [{:db/id joe
352+
:account/name "Joe"
353+
:account/members [{:db/id sam :person/name "Sam"}
354+
{:db/id sally :person/name "Sally"}]
355+
:account/settings {:db/id joe-settings :settings/auto-open? true}}
356+
{:db/id mary
357+
:account/name "Mary"
358+
:account/members [{:db/id judy :person/name "Judy"}]}]
359+
query-3 [:db/id :item/name {:item/invoices [:db/id {:invoice/account [:db/id :account/name]}]}]
360+
expected-result-3 [{:db/id gadget :item/name "gadget"
361+
:item/invoices [{:db/id invoice-1 :invoice/account {:db/id joe :account/name "Joe"}}
362+
{:db/id invoice-2 :invoice/account {:db/id joe :account/name "Joe"}}]}]
363+
root-set #{joe}
364+
source-table :account
365+
fix-nums (fn [result]
366+
(clojure.walk/postwalk
367+
(fn [ele]
368+
(if (= java.math.BigInteger (type ele))
369+
(long ele)
370+
ele)) result))]
371+
(assertions
372+
"many-to-many (forward)"
373+
(core/run-query db h2-schema :account/id query #{joe}) => [expected-result]
374+
"one-to-many query (forward)"
375+
(core/run-query db h2-schema :account/id query-2 (sorted-set joe mary)) => expected-result-2
376+
"many-to-many (reverse)"
377+
(core/run-query db h2-schema :account/id query-3 (sorted-set gadget)) => expected-result-3))))
378+
379+
329380
(comment
330381
(do
331382
(require 'taoensso.timbre)

test-resources/h2test.properties

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dataSourceClassName=org.h2.jdbcx.JdbcDataSource
2+
dataSource.URL=jdbc:h2:/tmp/test
3+
dataSource.user=sa
4+
dataSource.password=sa
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
CREATE SEQUENCE settings_id_seq;
2+
CREATE TABLE settings (
3+
id INTEGER DEFAULT nextval('settings_id_seq') PRIMARY KEY ,
4+
auto_open BOOLEAN NOT NULL DEFAULT FALSE,
5+
keyboard_shortcuts BOOLEAN NOT NULL DEFAULT TRUE
6+
);
7+
8+
CREATE SEQUENCE account_id_seq;
9+
CREATE TABLE account (
10+
id INTEGER DEFAULT nextval('account_id_seq'),
11+
name VARCHAR(200),
12+
last_edited_by INTEGER,
13+
settings_id INTEGER REFERENCES settings (id),
14+
created_on TIMESTAMP NOT NULL DEFAULT now(),
15+
spouse_id INTEGER REFERENCES account (id)
16+
);
17+
18+
CREATE SEQUENCE member_id_seq;
19+
CREATE TABLE member (
20+
id INTEGER DEFAULT nextval('member_id_seq'),
21+
name VARCHAR(200),
22+
account_id INTEGER NOT NULL REFERENCES account (id)
23+
);
24+
ALTER TABLE account
25+
ADD CONSTRAINT account_last_edit_by_fkey FOREIGN KEY (last_edited_by) REFERENCES member (id);
26+
27+
CREATE SEQUENCE invoice_id_seq;
28+
CREATE TABLE invoice (
29+
id INTEGER DEFAULT nextval('invoice_id_seq'),
30+
invoice_date TIMESTAMP NOT NULL DEFAULT now(),
31+
account_id INTEGER NOT NULL REFERENCES account (id)
32+
);
33+
34+
CREATE SEQUENCE item_id_seq;
35+
CREATE TABLE item (
36+
id INTEGER DEFAULT nextval('item_id_seq'),
37+
name VARCHAR(200) NOT NULL
38+
);
39+
40+
CREATE SEQUENCE invoice_items_id_seq;
41+
CREATE TABLE invoice_items (
42+
id INTEGER DEFAULT nextval('invoice_items_id_seq'),
43+
quantity SMALLINT NOT NULL,
44+
invoice_id INTEGER NOT NULL REFERENCES invoice (id),
45+
item_id INTEGER NOT NULL REFERENCES item (id)
46+
);
47+
48+
CREATE SEQUENCE todo_list_id_seq;
49+
CREATE TABLE todo_list (
50+
id INTEGER DEFAULT nextval('todo_list_id_seq'),
51+
name VARCHAR(200)
52+
);
53+
54+
CREATE SEQUENCE todo_list_item_id_seq;
55+
CREATE TABLE todo_list_item (
56+
id INTEGER DEFAULT nextval('todo_list_item_id_seq'),
57+
label VARCHAR(200),
58+
todo_list_id INTEGER REFERENCES todo_list (id),
59+
parent_item_id INTEGER REFERENCES todo_list_item (id)
60+
);

0 commit comments

Comments
 (0)