Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (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 "john.smith@test.com"
- :score 65})
- (def test-doc-2 {:name "Jane Thompson"
- :email "jane.thompson@test.com"
- :score 98})
- (def test-doc-3 {:name "Robert Jones"
- :email "robert.jones@example.com"
- :score 80})
- (def test-doc-4 {:name "Sarah Parker"
- :email "sarah.parker@example.com"
- :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 "test@example.com"})
- (is (= "test@example.com" (: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 (= #{"robert.jones@example.com" "sarah.parker@example.com"}
- (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 (= #{"john.smith@test.com" "jane.thompson@test.com"}
- (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)
Add Comment
Please, Sign In to add comment