|
| 1 | +(ns orchard.clojuredocs-test |
| 2 | + (:require |
| 3 | + [clojure.java.io :as io] |
| 4 | + [clojure.test :as test :refer [deftest is testing use-fixtures]] |
| 5 | + [orchard.clojuredocs :as docs]) |
| 6 | + (:import |
| 7 | + (java.time Instant))) |
| 8 | + |
| 9 | +(def ^:private test-edn-file |
| 10 | + (io/resource "clojuredocs/export.edn")) |
| 11 | + |
| 12 | +(defn- create-dummy-cache-file [& [timestamp]] |
| 13 | + (doto (io/file docs/cache-file-name) |
| 14 | + (spit (slurp test-edn-file)) |
| 15 | + (cond-> timestamp (.setLastModified timestamp)))) |
| 16 | + |
| 17 | +(defn- clojuredocs-test-fixture [f] |
| 18 | + (with-redefs [docs/cache-file-name "target/clojuredocs/export.edn"] |
| 19 | + (docs/clean-cache!) |
| 20 | + (f) |
| 21 | + (docs/clean-cache!))) |
| 22 | + |
| 23 | +(use-fixtures :each clojuredocs-test-fixture) |
| 24 | + |
| 25 | +(deftest load-cache!-test |
| 26 | + (let [cache-file (io/file docs/cache-file-name) |
| 27 | + now (-> (Instant/now) .getEpochSecond (* 1000)) |
| 28 | + new-timestamp (- now (/ docs/cache-updating-threshold 2)) |
| 29 | + old-timestamp (- now docs/cache-updating-threshold)] |
| 30 | + (testing "No cache-file" |
| 31 | + (is (not (.exists cache-file))) |
| 32 | + (is (empty? @docs/cache)) |
| 33 | + (docs/load-cache! test-edn-file) |
| 34 | + (is (.exists cache-file)) |
| 35 | + (is (not (empty? @docs/cache)))) |
| 36 | + |
| 37 | + (testing "Old cache-file" |
| 38 | + (create-dummy-cache-file old-timestamp) |
| 39 | + (reset! docs/cache {:dummy "not-empty-dummy-data"}) |
| 40 | + |
| 41 | + (is (= old-timestamp (.lastModified cache-file))) |
| 42 | + (is (contains? @docs/cache :dummy)) |
| 43 | + (docs/load-cache! test-edn-file) |
| 44 | + (is (< old-timestamp (.lastModified cache-file))) |
| 45 | + (is (not (contains? @docs/cache :dummy)))) |
| 46 | + |
| 47 | + (testing "Sufficiently new cache-file and no cached documents" |
| 48 | + (create-dummy-cache-file new-timestamp) |
| 49 | + (reset! docs/cache {}) |
| 50 | + |
| 51 | + (is (= new-timestamp (.lastModified cache-file))) |
| 52 | + (is (empty? @docs/cache)) |
| 53 | + (docs/load-cache! test-edn-file) |
| 54 | + (is (= new-timestamp (.lastModified cache-file))) |
| 55 | + (is (not (empty? @docs/cache)))) |
| 56 | + |
| 57 | + (testing "Sufficiently new cache file and already cached documents" |
| 58 | + (create-dummy-cache-file new-timestamp) |
| 59 | + (reset! docs/cache {:dummy "not-empty-dummy-data"}) |
| 60 | + |
| 61 | + (is (= new-timestamp (.lastModified cache-file))) |
| 62 | + (is (contains? @docs/cache :dummy)) |
| 63 | + (docs/load-cache! test-edn-file) ; Does nothing |
| 64 | + (is (= new-timestamp (.lastModified cache-file))) |
| 65 | + (is (contains? @docs/cache :dummy))))) |
| 66 | + |
| 67 | +(deftest clean-cache!-test |
| 68 | + (create-dummy-cache-file) |
| 69 | + (reset! docs/cache {:dummy "not-empty-dummy-data"}) |
| 70 | + (let [cache-file (io/file docs/cache-file-name)] |
| 71 | + (is (.exists cache-file)) |
| 72 | + (is (not (empty? @docs/cache))) |
| 73 | + (docs/clean-cache!) |
| 74 | + (is (not (.exists cache-file))) |
| 75 | + (is (empty? @docs/cache)))) |
| 76 | + |
| 77 | +(deftest find-doc-test |
| 78 | + (testing "find existing document" |
| 79 | + (is (empty? @docs/cache)) |
| 80 | + (is (not (.exists (io/file docs/cache-file-name)))) |
| 81 | + (let [result (docs/find-doc "clojure.core" "first" test-edn-file)] |
| 82 | + (is (map? result)) |
| 83 | + (is (every? #(contains? result %) |
| 84 | + [:arglists :doc :examples :name :notes :ns :see-alsos])) |
| 85 | + (is (.exists (io/file docs/cache-file-name))) |
| 86 | + (is (not (empty? @docs/cache))))) |
| 87 | + |
| 88 | + (testing "find non-existing document" |
| 89 | + (is (nil? (docs/find-doc test-edn-file "non-existing-ns" "non-existing-var"))))) |
0 commit comments