11(ns pod.babashka.duckdb-test
22 (:require [clojure.test :refer [deftest is testing]]
3- [next.jdbc :as jdbc]
4- [clojure.string :as str])
5- (:import [org.duckdb DuckDBDriver]))
6-
7- ; ; Registra o driver do DuckDB
8- (DuckDBDriver. )
9-
10- (defn duckdb-array->vec [arr]
11- (when arr
12- (into [] (str/split (str/replace (str arr) #"[\[\] ]" " " ) #", " ))))
13-
14- (deftest duckdb-test
15- (let [db-spec {:dbtype " duckdb"
16- :dbname " test.duckdb" }]
17- ; ; Clean up any existing database
18- (try
19- (jdbc/execute! db-spec [" drop table if exists foo;" ])
20- (catch Exception _))
21-
22- (testing " basic operations"
23- (is (jdbc/execute! db-spec [" create table foo (foo integer);" ]))
24- (is (thrown-with-msg? Exception #"already exists"
25- (jdbc/execute! db-spec [" create table foo (foo integer);" ])))
26- (is (jdbc/execute! db-spec [" insert into foo values (1), (2), (3);" ]))
27- (let [query-result (jdbc/execute! db-spec [" select * from foo;" ])]
28- (is (= [{:foo 1 } {:foo 2 } {:foo 3 }] query-result))))
29-
30- (testing " connection"
31- (with-open [conn (jdbc/get-connection db-spec)]
32- (is (= [{:foo 1 } {:foo 2 } {:foo 3 }]
33- (jdbc/execute! conn [" select * from foo;" ])))))
34-
35- (testing " transaction"
36- (jdbc/with-transaction [tx db-spec]
37- (jdbc/execute! tx [" insert into foo values (4);" ])
38- (is (= [{:foo 1 } {:foo 2 } {:foo 3 } {:foo 4 }]
39- (jdbc/execute! tx [" select * from foo;" ]))))
40-
41- (testing " rollback"
42- (try
43- (jdbc/with-transaction [tx db-spec]
44- (jdbc/execute! tx [" insert into foo values (5);" ])
45- (throw (Exception. " rollback" )))
46- (catch Exception _))
47- (is (= [{:foo 1 } {:foo 2 } {:foo 3 } {:foo 4 }]
48- (jdbc/execute! db-spec [" select * from foo;" ]))))
49-
50- (testing " with-transaction"
51- (jdbc/with-transaction [tx db-spec]
52- (jdbc/execute! tx [" insert into foo values (5);" ])
53- (jdbc/execute! tx [" insert into foo values (6), (7);" ]))
54-
55- (is (= [{:foo 1 } {:foo 2 } {:foo 3 } {:foo 4 }
56- {:foo 5 } {:foo 6 } {:foo 7 }]
57- (jdbc/execute! db-spec [" select * from foo;" ])))))
58-
59- (jdbc/execute! db-spec [" drop table foo;" ]))
60-
61- (testing " list support"
62- (let [db-spec {:dbtype " duckdb"
63- :dbname " test-list.duckdb" }]
64- ; ; Clean up any existing database
65- (try
66- (jdbc/execute! db-spec [" drop table if exists foo;" ])
67- (catch Exception _))
68-
69- (is (jdbc/execute! db-spec [" create table foo (foo integer[]);" ]))
70- (is (jdbc/execute! db-spec [" insert into foo select [1, 2, 3];" ]))
71- (let [result (jdbc/execute! db-spec [" select foo from foo" ])
72- result-vec (update-in result [0 :foo ] duckdb-array->vec)]
73- (is (= [{:foo [" 1" " 2" " 3" ]}] result-vec)))
74- (let [result (jdbc/execute-one! db-spec [" select foo from foo" ])
75- result-vec (update result :foo duckdb-array->vec)]
76- (is (= {:foo [" 1" " 2" " 3" ]} result-vec))))))
3+ [clojure.string :as str]))
0 commit comments