monger.cache tests now pass
This commit is contained in:
parent
d76127de49
commit
d99b6c52aa
2 changed files with 50 additions and 125 deletions
|
|
@ -29,65 +29,33 @@
|
|||
;; API
|
||||
;;
|
||||
|
||||
(defrecord BasicMongerCache [collection])
|
||||
(defrecord BasicMongerCache [db collection])
|
||||
|
||||
(extend-protocol cache/CacheProtocol
|
||||
BasicMongerCache
|
||||
(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)))
|
||||
(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]
|
||||
this)
|
||||
(miss [c k v]
|
||||
(mc/insert (get c :collection) {:_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)
|
||||
(mc/insert (:db c) (:collection c) {:_id k :value v})
|
||||
c)
|
||||
(evict [c k]
|
||||
(mc/remove-by-id (:db c) (:collection c) k)
|
||||
c)
|
||||
(seed [c m]
|
||||
(mc/insert-batch (:db c) (:collection c) (map (fn [[k v]]
|
||||
{:_id k :value v}) m) WriteConcern/SAFE)
|
||||
{:_id k :value v}) m))
|
||||
c))
|
||||
|
||||
|
||||
(defn db-aware-monger-cache-factory
|
||||
([db]
|
||||
(DatabaseAwareMongerCache. db default-cache-collection))
|
||||
([db collection]
|
||||
(DatabaseAwareMongerCache. db collection))
|
||||
([db collection base]
|
||||
(cache/seed (DatabaseAwareMongerCache. db collection) base)))
|
||||
(defn basic-monger-cache-factory
|
||||
([^DB db]
|
||||
(BasicMongerCache. db default-cache-collection))
|
||||
([^DB db collection]
|
||||
(BasicMongerCache. db collection))
|
||||
([^DB db collection base]
|
||||
(cache/seed (BasicMongerCache. db collection) base)))
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
(ns monger.test.cache-test
|
||||
(:require [monger.test.helper :as helper]
|
||||
[monger.core :as mg]
|
||||
(:require [monger.core :as mg]
|
||||
[monger.collection :as mc]
|
||||
[clojure.core.cache :refer :all]
|
||||
[clojure.test :refer :all]
|
||||
|
|
@ -83,87 +82,45 @@
|
|||
;; Tests
|
||||
;;
|
||||
|
||||
(helper/connect!)
|
||||
|
||||
(use-fixtures :each (fn [f]
|
||||
(mc/remove "basic_monger_cache_entries")
|
||||
(let [db (mg/get-db "altcache")]
|
||||
(mc/remove db "db_aware_monger_cache_entries" {}))
|
||||
(f)
|
||||
(mc/remove "basic_monger_cache_entries")
|
||||
(let [db (mg/get-db "altcache")]
|
||||
(mc/remove db "db_aware_monger_cache_entries" {}))))
|
||||
(let [conn (mg/connect)
|
||||
db (mg/get-db conn "monger-test")]
|
||||
(use-fixtures :each (fn [f]
|
||||
(mc/remove db "basic_monger_cache_entries")
|
||||
(f)
|
||||
(mc/remove db "basic_monger_cache_entries")))
|
||||
|
||||
|
||||
(deftest ^{:cache true}
|
||||
test-has?-with-basic-monger-cache
|
||||
(testing "that has? returns false for misses"
|
||||
(let [coll "basic_monger_cache_entries"
|
||||
c (basic-monger-cache-factory coll)]
|
||||
(is (not (has? c (str (UUID/randomUUID)))))
|
||||
(is (not (has? c (str (UUID/randomUUID)))))))
|
||||
(testing "that has? returns true for hits"
|
||||
(let [coll "basic_monger_cache_entries"
|
||||
c (basic-monger-cache-factory 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-has?-with-basic-monger-cache
|
||||
(testing "that has? returns false for misses"
|
||||
(let [coll "basic_monger_cache_entries"
|
||||
c (basic-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 [coll "basic_monger_cache_entries"
|
||||
c (basic-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-basic-moger-cache
|
||||
(testing "that lookup returns nil for misses"
|
||||
(let [coll "basic_monger_cache_entries"
|
||||
c (basic-monger-cache-factory 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)
|
||||
coll "basic_monger_cache_entries"
|
||||
c (basic-monger-cache-factory coll {:skey "Value" :lkey l "kkey" :keyword})]
|
||||
(are [v k] (is (= v (lookup c k)))
|
||||
"Value" :skey
|
||||
l :lkey
|
||||
"keyword" "kkey"))))
|
||||
(deftest ^{:cache true}
|
||||
test-lookup-with-basic-monger-cache
|
||||
(testing "that lookup returns nil for misses"
|
||||
(let [coll "basic_monger_cache_entries"
|
||||
c (basic-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)
|
||||
coll "basic_monger_cache_entries"
|
||||
c (basic-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")))))
|
||||
|
||||
|
||||
(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"))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue