db_test.cljs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. (ns logseq.db.sqlite.db-test
  2. (:require [cljs.test :refer [deftest async use-fixtures is testing]]
  3. ["fs" :as fs]
  4. ["path" :as node-path]
  5. [datascript.core :as d]
  6. [logseq.db.sqlite.db :as sqlite-db]
  7. [logseq.db.sqlite.restore :as sqlite-restore]))
  8. (use-fixtures
  9. :each
  10. ;; Cleaning tmp/ before leaves last tmp/ after a test run for dev and debugging
  11. {:before
  12. #(async done
  13. (if (fs/existsSync "tmp")
  14. (fs/rm "tmp" #js {:recursive true} (fn [err]
  15. (when err (js/console.log err))
  16. (done)))
  17. (done)))})
  18. (defn- create-graph-dir
  19. [dir db-name]
  20. (fs/mkdirSync (node-path/join dir db-name) #js {:recursive true}))
  21. (deftest get-initial-data
  22. (testing "Fetches a defined block"
  23. (create-graph-dir "tmp/graphs" "test-db")
  24. (sqlite-db/open-db! "tmp/graphs" "test-db")
  25. (let [blocks [{:block/uuid (random-uuid)
  26. :file/path "logseq/config.edn"
  27. :file/content "{:foo :bar}"}]
  28. _ (sqlite-db/transact! "test-db" blocks {})]
  29. (is (= blocks
  30. (->> (sqlite-db/get-initial-data "test-db")
  31. sqlite-restore/restore-initial-data
  32. deref
  33. (d/q '[:find (pull ?b [:block/uuid :file/path :file/content]) :where [?b :file/content]])
  34. (map first)))
  35. "Correct file with content is found"))))