diff --git a/publisher/src/net/deertopia/publisher/roam.clj b/publisher/src/net/deertopia/publisher/roam.clj index c281d3c..f688958 100644 --- a/publisher/src/net/deertopia/publisher/roam.clj +++ b/publisher/src/net/deertopia/publisher/roam.clj @@ -12,7 +12,7 @@ (defonce ^:dynamic *use-db-cache?* true) -(defn- ds [] +(defn ds [] (sql/get-datasource {:dbtype "sqlite" :dbname (-> cfg/*cfg* ::publisher/org-roam-db-path str)})) @@ -34,7 +34,7 @@ (defrecord Node [id cache]) -(defn- uuid-exists? [uuid] +(defn uuid-exists? [uuid] (sql/execute-one! (ds) ["select 1 from nodes where id = ? limit 1" (-> uuid str elisp/print)])) diff --git a/publisher/test/net/deertopia/publisher/config_test.clj b/publisher/test/net/deertopia/publisher/config_test.clj new file mode 100644 index 0000000..634693e --- /dev/null +++ b/publisher/test/net/deertopia/publisher/config_test.clj @@ -0,0 +1,19 @@ +(ns net.deertopia.publisher.config-test + (:require [clojure.test :as t] + [net.deertopia.publisher.config :as publisher-cfg] + [net.deertopia.doerg.config :as doerg-cfg] + [net.deertopia.publisher :as-alias publisher] + [net.deertopia.doerg :as-alias doerg])) + +(defn test-config-fixture + "`clojure.test` fixture to run tests with the :test configuration." + [f] + (binding [doerg-cfg/*cfg* + (doerg-cfg/read-config + ::doerg/config doerg-cfg/sources + :profile :test) + publisher-cfg/*cfg* + (doerg-cfg/read-config + ::publisher/config publisher-cfg/sources + :profile :test)] + (f))) diff --git a/publisher/test/net/deertopia/publisher/roam-test/404.org b/publisher/test/net/deertopia/publisher/roam-test/404.org new file mode 100644 index 0000000..2fd2627 --- /dev/null +++ b/publisher/test/net/deertopia/publisher/roam-test/404.org @@ -0,0 +1,23 @@ +:PROPERTIES: +:ID: 3c60c74e-f533-43ad-89b3-563975bf80f2 +:DeertopiaVisibility: public +:END: +#+title: page not found + +the page you're looking for doesn't exist, or you lack the permissions necessary to view it........ + +#+attr_doerg: :center? true :alt "A doe sitting and sobbing" :scale 1.4 +#+begin_example + \{ / \ }/ + \/ . . \/ + .. \Y` `Y/ .. + \ `\|~==~|/' / + '". T T ."` + {`^' } + | | + /> [(System/getenv "EMACS") "test-emacs" "emacs"] + (some #(some-> % fs/which)))] + (io/copy (-> "net/deertopia/publisher/org-roam-db-sync.el" + io/resource io/reader) + (fs/file script-file)) + (p/shell {:out :string :err :string} + emacs "-Q" "-x" script-file org-roam-directory db-file) + (fs/delete script-file))) + +(defn test-db-fixture [f] + (let [db-file (-> publisher-cfg/*cfg* ::publisher/org-roam-db-path)] + (assert (->> db-file fs/canonicalize str + (re-matches #"^/(build|tmp)/")) + (format "i'm scared to delete a non-test database... %s" + (str db-file))) + (fs/delete-if-exists db-file) + (org-roam-db-sync db-file) + (f) + (fs/delete db-file))) + +(t/use-fixtures + :once (t/join-fixtures [test-config-fixture test-db-fixture])) + + + +(t/deftest all-nodes-exist + (let [known-node-files (->> (fs/list-dir org-roam-directory) + (map (comp str fs/canonicalize)) + (into #{})) + database-nodes + (->> (sql/execute! + (sut/ds) ["select file, id from nodes"]) + (map (fn [x] + {:file (-> x :nodes/file elisp/read-string) + :id (-> x :nodes/id elisp/read-string parse-uuid)})) + (into #{}))] + (t/testing "database has a node for each file?" + (t/is (= known-node-files (sp/transform + [sp/ALL] + #(:file %) + database-nodes)))) + (t/testing "each uuid exists?" + (t/is (every? (comp sut/uuid-exists? :id) + database-nodes))))) diff --git a/publisher/test/net/deertopia/publisher/server_test.clj b/publisher/test/net/deertopia/publisher/server_test.clj index 6df26b8..0f61d5f 100644 --- a/publisher/test/net/deertopia/publisher/server_test.clj +++ b/publisher/test/net/deertopia/publisher/server_test.clj @@ -8,44 +8,12 @@ [net.deertopia.doerg :as-alias doerg] [net.deertopia.publisher.roam :as roam] [babashka.fs :as fs] - [babashka.process :as p] - [clojure.java.io :as io])) - -(def org-roam-directory - (fs/file "test/net/deertopia/publisher/roam-test")) - -(defn config-fixture [f] - (binding [doerg-cfg/*cfg* - (doerg-cfg/read-config - ::doerg/config doerg-cfg/sources - :profile :test) - publisher-cfg/*cfg* - (doerg-cfg/read-config - ::publisher/config publisher-cfg/sources - :profile :test)] - (f))) - -(defn org-roam-db-sync [db-file] - (let [script-file (fs/create-temp-file {:prefix "org-roam-db-sync-" - :suffix ".el"}) - emacs (->> [(System/getenv "EMACS") "test-emacs" "emacs"] - (some #(some-> % fs/which)))] - (io/copy (-> "net/deertopia/publisher/org-roam-db-sync.el" - io/resource io/reader) - (fs/file script-file)) - (p/shell {:out :string :err :string} - emacs "-Q" "-x" script-file org-roam-directory db-file) - (fs/delete script-file))) - -(defn test-db-fixture [f] - (let [db-file (-> publisher-cfg/*cfg* ::publisher/org-roam-db-path)] - (fs/delete-if-exists db-file) - (org-roam-db-sync db-file) - (f) - (fs/delete db-file))) + [clojure.java.io :as io] + [net.deertopia.publisher.roam-test :refer [test-db-fixture]] + [net.deertopia.publisher.config-test :refer [test-config-fixture]])) (t/use-fixtures - :once (t/join-fixtures [config-fixture test-db-fixture])) + :once (t/join-fixtures [test-config-fixture test-db-fixture])) (defn with-server [f] (let [was-already-running? (= :running (sut/status))] @@ -67,13 +35,11 @@ (with-server (fn [] (t/is (= :running (sut/status))) - ;; 테스트 데이터베이스를 아직 안 준비한다. (t/is (->> (format "http://localhost:%d" - (::publisher/port publisher-cfg/*cfg*)) + (::publisher/port publisher-cfg/*cfg*)) slurp string?))))) -;; 테스트 데이터베이스를 아직 안 준비한다. (t/deftest get-nonexistent-node (let [slug "3Lxvxnb0QrivoU3DX-l_5w"] (assert (nil? (roam/make-node slug))) @@ -81,7 +47,6 @@ (-> (str "/n/" slug) get-sut :status))))) -;; 테스트 데이터베이스를 아직 안 준비한다. (t/deftest get-homepage (let [resp (-> (str "/n/" sut/homepage-slug) get-sut)]