Created
May 20, 2009 22:04
-
-
Save ashafa/115119 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns clojure-couchdb | |
(:use com.ashafa.couchdb | |
(clojure.contrib [test-is :as test-is]))) | |
(set-couchdb-config! {:language "clojure"}) | |
(def test-doc-1 {:name "John Smith" | |
:email "[email protected]" | |
:score 65}) | |
(def test-doc-2 {:name "Jane Thompson" | |
:email "[email protected]" | |
:score 98}) | |
(def test-doc-3 {:name "Robert Jones" | |
:email "[email protected]" | |
:score 80}) | |
(def test-doc-4 {:name "Sarah Parker" | |
:email "[email protected]" | |
:score 59}) | |
(def names-with-score-over-70-view (with-clj-map-reduce | |
#(if (> (:score %) 70) [nil (:name %)]))) | |
(def sum-of-all-scores-view (with-clj-map-reduce | |
(fn [doc] [nil (:score doc)]) ;map | |
(fn [keys values _] (apply + values)))) ;reduce | |
(defmacro defdbtest [name & body] | |
`(deftest ~name | |
(try | |
(create-database "test_db") | |
(with-db "test_db" ~@body) | |
(finally | |
(delete-database "test_db"))))) | |
(defdbtest create-a-document | |
(is (contains? (create-doc test-doc-1) :id))) | |
(defdbtest create-a-document-with-id | |
(is (= "my_id" (:id (create-doc-with-id "my_id" test-doc-2))))) | |
(defdbtest get-a-document | |
(let [doc-meta (create-doc test-doc-3)] | |
(is (= (test-doc-3 :name) (:name (get-doc (doc-meta :id))))))) | |
(defdbtest update-a-document | |
(let [id (:id (create-doc test-doc-4))] | |
(update-doc (get-doc id) {:email "[email protected]"}) | |
(is (= "[email protected]" (:email (get-doc id)))))) | |
(defdbtest update-a-document-with-a-function | |
(let [id (:id (create-doc test-doc-4))] | |
(update-doc (get-doc id) (partial + 4) [:score]) | |
(is (= 63 (:score (get-doc id)))))) | |
(defdbtest get-all-documents-meta | |
(create-doc test-doc-1) | |
(create-doc test-doc-2) | |
(create-doc test-doc-3) | |
(is (= 3 (:total_rows (get-all-docs))))) | |
(defdbtest get-all-documents-using-query-params | |
(create-doc-with-id 1 test-doc-1) | |
(create-doc-with-id 2 test-doc-2) | |
(create-doc-with-id 3 test-doc-3) | |
(is (= "Robert Jones" (-> (get-all-docs {:include_docs true :descending true}) | |
:rows first :doc :name)))) | |
(defdbtest create-a-design-view | |
(let [design-doc-meta (create-view "users" :names-with-score-over-70 names-with-score-over-70-view)] | |
(is (map? (-> (get-doc (design-doc-meta :id)) :views :names-with-score-over-70))))) | |
(defdbtest use-a-design-view-with-only-map | |
(create-doc test-doc-1) | |
(create-doc test-doc-2) | |
(create-doc test-doc-3) | |
(create-doc test-doc-4) | |
(create-view "users" :names-with-score-over-70 names-with-score-over-70-view) | |
(is (= #{"Robert Jones" "Jane Thompson"} | |
(set (map :value (:rows (get-view "users" :names-with-score-over-70))))))) | |
(defdbtest use-a-design-view-with-both-map-and-reduce | |
(create-doc test-doc-1) | |
(create-doc test-doc-2) | |
(create-doc test-doc-3) | |
(create-doc test-doc-4) | |
(create-view "scores" :sum-of-all-scores sum-of-all-scores-view) | |
(is (= 302 (-> (get-view "scores" :sum-of-all-scores) :rows first :value)))) | |
(defdbtest use-a-tempoary-view | |
(create-doc test-doc-1) | |
(create-doc test-doc-2) | |
(create-doc test-doc-3) | |
(create-doc test-doc-4) | |
(is (= #{"[email protected]" "[email protected]"} | |
(set (map :value (:rows (get-tempoary-view | |
(with-clj-map-reduce | |
(fn [doc] (if (re-find #"example\.com$" (:email doc)) | |
[nil (:email doc)])))))))))) | |
(defdbtest use-a-tempoary-view-using-javascript-view-server | |
(create-doc test-doc-1) | |
(create-doc test-doc-2) | |
(create-doc test-doc-3) | |
(create-doc test-doc-4) | |
(is (= #{"[email protected]" "[email protected]"} | |
(set (map :value (:rows (get-tempoary-view | |
{:language "javascript" | |
:map "function(doc){if(doc.email.indexOf('test.com')>0)emit(null,doc.email);}"}))))))) | |
(run-tests) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment