monger.cache tests now pass

This commit is contained in:
Michael Klishin 2014-05-10 16:45:36 -04:00
parent d76127de49
commit d99b6c52aa
2 changed files with 50 additions and 125 deletions

View file

@ -29,65 +29,33 @@
;; API ;; API
;; ;;
(defrecord BasicMongerCache [collection]) (defrecord BasicMongerCache [db collection])
(extend-protocol cache/CacheProtocol (extend-protocol cache/CacheProtocol
BasicMongerCache BasicMongerCache
(lookup [c k] (lookup [c k]
(let [m (mc/find-map-by-id (:collection c) k)] (let [m (mc/find-map-by-id (:db c) (:collection c) k)]
(:value m))) (:value m)))
(has? [c k] (has? [c k]
(not (nil? (mc/find-by-id (get c :collection) k)))) (not (nil? (mc/find-by-id (:db c) (:collection c) k))))
(hit [this k] (hit [this k]
this) this)
(miss [c k v] (miss [c k v]
(mc/insert (get c :collection) {:_id k :value v}) (mc/insert (:db c) (:collection c) {:_id k :value v})
c)
(evict [c k]
(mc/remove-by-id (get c :collection) k)
c)
(seed [c m]
(mc/insert-batch (get c :collection) (map (fn [[k v]]
{:_id k :value v}) m))
c))
(defn basic-monger-cache-factory
([]
(BasicMongerCache. default-cache-collection))
([collection]
(BasicMongerCache. collection))
([collection base]
(cache/seed (BasicMongerCache. collection) base)))
(defrecord DatabaseAwareMongerCache [db collection])
(extend-protocol cache/CacheProtocol
DatabaseAwareMongerCache
(lookup [c k]
(let [m (find-map-by-id (:db c) (:collection c) k)]
(:value m)))
(has? [c k]
(not (nil? (find-by-id (:db c) (:collection c) k))))
(hit [this k]
this)
(miss [c k v]
(mc/insert (:db c) (:collection c) {:_id k :value v} WriteConcern/SAFE)
c) c)
(evict [c k] (evict [c k]
(mc/remove-by-id (:db c) (:collection c) k) (mc/remove-by-id (:db c) (:collection c) k)
c) c)
(seed [c m] (seed [c m]
(mc/insert-batch (:db c) (:collection c) (map (fn [[k v]] (mc/insert-batch (:db c) (:collection c) (map (fn [[k v]]
{:_id k :value v}) m) WriteConcern/SAFE) {:_id k :value v}) m))
c)) c))
(defn db-aware-monger-cache-factory (defn basic-monger-cache-factory
([db] ([^DB db]
(DatabaseAwareMongerCache. db default-cache-collection)) (BasicMongerCache. db default-cache-collection))
([db collection] ([^DB db collection]
(DatabaseAwareMongerCache. db collection)) (BasicMongerCache. db collection))
([db collection base] ([^DB db collection base]
(cache/seed (DatabaseAwareMongerCache. db collection) base))) (cache/seed (BasicMongerCache. db collection) base)))

View file

@ -1,6 +1,5 @@
(ns monger.test.cache-test (ns monger.test.cache-test
(:require [monger.test.helper :as helper] (:require [monger.core :as mg]
[monger.core :as mg]
[monger.collection :as mc] [monger.collection :as mc]
[clojure.core.cache :refer :all] [clojure.core.cache :refer :all]
[clojure.test :refer :all] [clojure.test :refer :all]
@ -83,87 +82,45 @@
;; Tests ;; Tests
;; ;;
(helper/connect!) (let [conn (mg/connect)
db (mg/get-db conn "monger-test")]
(use-fixtures :each (fn [f] (use-fixtures :each (fn [f]
(mc/remove "basic_monger_cache_entries") (mc/remove db "basic_monger_cache_entries")
(let [db (mg/get-db "altcache")] (f)
(mc/remove db "db_aware_monger_cache_entries" {})) (mc/remove db "basic_monger_cache_entries")))
(f)
(mc/remove "basic_monger_cache_entries")
(let [db (mg/get-db "altcache")]
(mc/remove db "db_aware_monger_cache_entries" {}))))
(deftest ^{:cache true} (deftest ^{:cache true}
test-has?-with-basic-monger-cache test-has?-with-basic-monger-cache
(testing "that has? returns false for misses" (testing "that has? returns false for misses"
(let [coll "basic_monger_cache_entries" (let [coll "basic_monger_cache_entries"
c (basic-monger-cache-factory coll)] c (basic-monger-cache-factory db coll)]
(is (not (has? c (str (UUID/randomUUID))))) (is (not (has? c (str (UUID/randomUUID)))))
(is (not (has? c (str (UUID/randomUUID))))))) (is (not (has? c (str (UUID/randomUUID)))))))
(testing "that has? returns true for hits" (testing "that has? returns true for hits"
(let [coll "basic_monger_cache_entries" (let [coll "basic_monger_cache_entries"
c (basic-monger-cache-factory coll {"a" 1 "b" "cache" "c" 3/4})] c (basic-monger-cache-factory db coll {"a" 1 "b" "cache" "c" 3/4})]
(is (has? c "a")) (is (has? c "a"))
(is (has? c "b")) (is (has? c "b"))
(is (has? c "c")) (is (has? c "c"))
(is (not (has? c "d")))))) (is (not (has? c "d"))))))
(deftest ^{:cache true} (deftest ^{:cache true}
test-lookup-with-basic-moger-cache test-lookup-with-basic-monger-cache
(testing "that lookup returns nil for misses" (testing "that lookup returns nil for misses"
(let [coll "basic_monger_cache_entries" (let [coll "basic_monger_cache_entries"
c (basic-monger-cache-factory coll)] c (basic-monger-cache-factory db coll)]
(are [v] (is (nil? (lookup c v))) (are [v] (is (nil? (lookup c v)))
:missing-key :missing-key
"missing-key" "missing-key"
(gensym "missing-key")))) (gensym "missing-key"))))
(testing "that lookup returns cached values for hits" (testing "that lookup returns cached values for hits"
(let [l (Long/valueOf 10000) (let [l (Long/valueOf 10000)
coll "basic_monger_cache_entries" coll "basic_monger_cache_entries"
c (basic-monger-cache-factory coll {:skey "Value" :lkey l "kkey" :keyword})] c (basic-monger-cache-factory db coll {:skey "Value" :lkey l "kkey" :keyword})]
(are [v k] (is (= v (lookup c k))) (are [v k] (is (= v (lookup c k)))
"Value" :skey "Value" :skey
l :lkey l :lkey
"keyword" "kkey")))) "keyword" "kkey")))))
(deftest ^{:cache true}
test-has?-with-db-aware-monger-cache
(testing "that has? returns false for misses"
(let [db (mg/get-db "altcache")
coll "db_aware_monger_cache_entries"
c (db-aware-monger-cache-factory db coll)]
(is (not (has? c (str (UUID/randomUUID)))))
(is (not (has? c (str (UUID/randomUUID)))))))
(testing "that has? returns true for hits"
(let [db (mg/get-db "altcache")
coll "db_aware_monger_cache_entries"
c (db-aware-monger-cache-factory db coll {"a" 1 "b" "cache" "c" 3/4})]
(is (has? c "a"))
(is (has? c "b"))
(is (has? c "c"))
(is (not (has? c "d"))))))
(deftest ^{:cache true}
test-lookup-with-db-aware-moger-cache
(testing "that lookup returns nil for misses"
(let [db (mg/get-db "altcache")
coll "db_aware_monger_cache_entries"
c (db-aware-monger-cache-factory db coll)]
(are [v] (is (nil? (lookup c v)))
:missing-key
"missing-key"
(gensym "missing-key"))))
(testing "that lookup returns cached values for hits"
(let [l (Long/valueOf 10000)
db (mg/get-db "altcache")
coll "db_aware_monger_cache_entries"
c (db-aware-monger-cache-factory db coll {:skey "Value" :lkey l "kkey" :keyword})]
(are [v k] (is (= v (lookup c k)))
"Value" :skey
l :lkey
"keyword" "kkey"))))