Skip to content

Commit 7894823

Browse files
snippet.clj:
- updated to use parameterized queries - added tests
1 parent 039b5f0 commit 7894823

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

examples/snippet.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,20 @@
7474

7575
; START: sql-query
7676
(defn sql-query [q]
77-
(with-query-results res [q] (doall res)))
77+
(with-query-results res q (doall res)))
7878
; END: sql-query
7979

8080
(defn select-snippet [id]
8181
(with-connection db
82-
(first (sql-query (str "select * from snippets where id = " (coerce Integer id))))))
82+
(first (sql-query ["select * from snippets where id = ?" (coerce Integer id)]))))
8383

8484
; START: last-created-id
8585
(defn last-created-id
8686
"Extract the last created id. Must be called in a transaction
8787
that performed an insert. Expects HSQLDB return structure of
8888
the form [{:@p0 id}]."
8989
[]
90-
(first (vals (first (sql-query "CALL IDENTITY()")))))
90+
(first (vals (first (sql-query ["CALL IDENTITY()"])))))
9191
; END: last-created-id
9292

9393
; START: insert-snippet

examples/test.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
:sequences :index-of-any :life-without-multi :multimethods.default
1010
:macros :macros.chain-1 :macros.chain-2 :macros.chain-3 :macros.chain-4
1111
:macros.chain-5 :lazy-index-of-any :macros.bench-1
12-
:concurrency :functional :snake]))
12+
:concurrency :functional :snake :snippet]))
1313

1414
(def lancet-tests
1515
(map #(symbol (str "lancet.test." (name %)))

examples/test/snippet.clj

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(ns examples.test.snippet
2+
(:use clojure.contrib.test-is clojure.contrib.sql examples.snippet))
3+
4+
(deftest insert-and-select-some-snippets
5+
(let [stub-time (now)]
6+
(binding [now (fn [] stub-time)]
7+
(sample-snippets)
8+
(is (= [{:id 0, :body "(println :boo)", :created_at stub-time}
9+
{:id 1, :body "(defn foo [] 1)", :created_at stub-time}]
10+
(select-snippets)))
11+
(insert-snippet "boo!")
12+
(is (= {:id 2, :body "boo!", :created_at stub-time}
13+
(select-snippet 2))))))
14+
15+
(deftest drop-and-create-snippets-table
16+
(with-connection db (drop-snippets))
17+
(is (= "Table not found in statement [select * from snippets]"
18+
(.getMessage (is (thrown? java.sql.SQLException (select-snippets))))))
19+
(ensure-snippets-table-exists)
20+
(is (= nil (select-snippets))))
21+
22+
23+
24+
25+

runtests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
java -cp lib/clojure.jar:lib/clojure-contrib.jar:lib/ant.jar:lib/ant-launcher.jar:. clojure.lang.Script examples/test.clj
1+
java -cp lib/clojure.jar:lib/clojure-contrib.jar:lib/hsqldb.jar:lib/ant.jar:lib/ant-launcher.jar:. clojure.lang.Script examples/test.clj

0 commit comments

Comments
 (0)