chore: reduce logging verbosity of parser tests

feat/asset-sync
Gabriel Horner 2024-07-08 12:15:07 -04:00
parent 3d732cb018
commit 47cbe96126
4 changed files with 79 additions and 74 deletions

View File

@ -604,7 +604,7 @@
(defn fix-duplicate-id (defn fix-duplicate-id
[block] [block]
(println "Logseq will assign a new id for this block: " block) (println "Logseq will assign a new id for block with content:" (pr-str (:block/content block)))
(-> block (-> block
(assoc :block/uuid (d/squuid)) (assoc :block/uuid (d/squuid))
(update :block/properties dissoc :id) (update :block/properties dissoc :id)

View File

@ -120,6 +120,10 @@
(map first) (map first)
first)) first))
(defn- parse-file
[conn file-path file-content & [options]]
(graph-parser/parse-file conn file-path file-content (merge-with merge options {:extract-options {:verbose false}})))
(deftest refs-from-block-refs (deftest refs-from-block-refs
(let [conn (gp-db/start-conn) (let [conn (gp-db/start-conn)
id "63f528da-284a-45d1-ac9c-5d6a7435f6b4" id "63f528da-284a-45d1-ac9c-5d6a7435f6b4"
@ -127,7 +131,7 @@
block-ref-via-content (str "Link to " (block-ref/->block-ref id)) block-ref-via-content (str "Link to " (block-ref/->block-ref id))
block-ref-via-block-properties (str "B block\nref:: " (block-ref/->block-ref id)) block-ref-via-block-properties (str "B block\nref:: " (block-ref/->block-ref id))
body (str "- " block "\n- " block-ref-via-content "\n- " block-ref-via-block-properties)] body (str "- " block "\n- " block-ref-via-content "\n- " block-ref-via-block-properties)]
(graph-parser/parse-file conn "foo.md" body {}) (parse-file conn "foo.md" body {})
(testing "Block refs in blocks" (testing "Block refs in blocks"
(is (= [{:block/uuid (uuid id)}] (is (= [{:block/uuid (uuid id)}]
@ -141,7 +145,7 @@
(testing "Block refs in pre-block" (testing "Block refs in pre-block"
(let [block-ref-via-page-properties (str "page-ref:: " (block-ref/->block-ref id))] (let [block-ref-via-page-properties (str "page-ref:: " (block-ref/->block-ref id))]
(graph-parser/parse-file conn "foo2.md" block-ref-via-page-properties {}) (parse-file conn "foo2.md" block-ref-via-page-properties {})
(is (contains? (is (contains?
(set (:block/refs (find-block-for-content @conn block-ref-via-page-properties))) (set (:block/refs (find-block-for-content @conn block-ref-via-page-properties)))
{:block/uuid (uuid id)}) {:block/uuid (uuid id)})
@ -152,7 +156,7 @@
deadline-block "do something\nDEADLINE: <2023-02-21 Tue>" deadline-block "do something\nDEADLINE: <2023-02-21 Tue>"
scheduled-block "do something else\nSCHEDULED: <2023-02-20 Mon>" scheduled-block "do something else\nSCHEDULED: <2023-02-20 Mon>"
body (str "- " deadline-block "\n- " scheduled-block)] body (str "- " deadline-block "\n- " scheduled-block)]
(graph-parser/parse-file conn "foo.md" body {}) (parse-file conn "foo.md" body {})
(is (= 20230220 (is (= 20230220
(:block/scheduled (find-block-for-content @conn scheduled-block))) (:block/scheduled (find-block-for-content @conn scheduled-block)))

View File

@ -45,7 +45,8 @@
(defn- extract [file content & [options]] (defn- extract [file content & [options]]
(extract/extract file (extract/extract file
content content
(merge {:block-pattern "-" :db (d/empty-db db-schema/schema)} (merge {:block-pattern "-" :db (d/empty-db db-schema/schema)
:verbose false}
options))) options)))
(defn- extract-block-content (defn- extract-block-content

View File

@ -54,10 +54,14 @@
:block/original-name "Bar" :block/original-name "Bar"
:block/uuid #uuid "71515b7d-b5fc-496b-b6bf-c58004a34ee3"})}) :block/uuid #uuid "71515b7d-b5fc-496b-b6bf-c58004a34ee3"})})
(deftest parse-file (defn- parse-file
[conn file-path file-content & [options]]
(graph-parser/parse-file conn file-path file-content (merge-with merge options {:extract-options {:verbose false}})))
(deftest parse-file-test
(testing "id properties" (testing "id properties"
(let [conn (gp-db/start-conn)] (let [conn (gp-db/start-conn)]
(graph-parser/parse-file conn "foo.md" "- id:: 628953c1-8d75-49fe-a648-f4c612109098" {}) (parse-file conn "foo.md" "- id:: 628953c1-8d75-49fe-a648-f4c612109098")
(is (= [{:id "628953c1-8d75-49fe-a648-f4c612109098"}] (is (= [{:id "628953c1-8d75-49fe-a648-f4c612109098"}]
(->> (d/q '[:find (pull ?b [*]) (->> (d/q '[:find (pull ?b [*])
:in $ :in $
@ -73,16 +77,16 @@
(with-redefs [gp-block/with-pre-block-if-exists (fn stub-failure [& _args] (with-redefs [gp-block/with-pre-block-if-exists (fn stub-failure [& _args]
(throw (js/Error "Testing unexpected failure")))] (throw (js/Error "Testing unexpected failure")))]
(try (try
(graph-parser/parse-file conn "foo.md" "- id:: 628953c1-8d75-49fe-a648-f4c612109098" (parse-file conn "foo.md" "- id:: 628953c1-8d75-49fe-a648-f4c612109098"
{:delete-blocks-fn (fn [_db page _file _uuids] {:delete-blocks-fn (fn [_db page _file _uuids]
(reset! deleted-page page))}) (reset! deleted-page page))})
(catch :default _))) (catch :default _)))
(is (= nil @deleted-page) (is (= nil @deleted-page)
"Page should not be deleted when there is unexpected failure"))) "Page should not be deleted when there is unexpected failure")))
(testing "parsing whiteboard page" (testing "parsing whiteboard page"
(let [conn (gp-db/start-conn)] (let [conn (gp-db/start-conn)]
(graph-parser/parse-file conn "/whiteboards/foo.edn" (pr-str foo-edn) {}) (parse-file conn "/whiteboards/foo.edn" (pr-str foo-edn))
(let [blocks (d/q '[:find (pull ?b [* {:block/page (let [blocks (d/q '[:find (pull ?b [* {:block/page
[:block/name [:block/name
:block/original-name :block/original-name
@ -102,16 +106,18 @@
(testing "Loading whiteboard pages that same block/uuid should throw an error." (testing "Loading whiteboard pages that same block/uuid should throw an error."
(let [conn (gp-db/start-conn)] (let [conn (gp-db/start-conn)]
(graph-parser/parse-file conn "/whiteboards/foo.edn" (pr-str foo-edn) {}) (parse-file conn "/whiteboards/foo.edn" (pr-str foo-edn))
(is (thrown-with-msg? ;; Reduce output with with-out-str
js/Error (with-out-str
#"Conflicting upserts" (is (thrown-with-msg?
(graph-parser/parse-file conn "/whiteboards/foo-conflict.edn" (pr-str foo-conflict-edn) {}))))) js/Error
#"Conflicting upserts"
(parse-file conn "/whiteboards/foo-conflict.edn" (pr-str foo-conflict-edn)))))))
(testing "Loading whiteboard pages should ignore the :block/name property inside :block/parent." (testing "Loading whiteboard pages should ignore the :block/name property inside :block/parent."
(let [conn (gp-db/start-conn)] (let [conn (gp-db/start-conn)]
(graph-parser/parse-file conn "/whiteboards/foo.edn" (pr-str foo-edn) {}) (parse-file conn "/whiteboards/foo.edn" (pr-str foo-edn))
(graph-parser/parse-file conn "/whiteboards/bar.edn" (pr-str bar-edn) {}) (parse-file conn "/whiteboards/bar.edn" (pr-str bar-edn))
(let [pages (d/q '[:find ?name (let [pages (d/q '[:find ?name
:in $ :in $
:where :where
@ -128,7 +134,7 @@
(string/join "\n")) (string/join "\n"))
;; Test page properties and block properties ;; Test page properties and block properties
body (str text "\n- " text) body (str text "\n- " text)
_ (graph-parser/parse-file conn "foo.md" body {}) _ (parse-file conn "foo.md" body)
properties-orders (->> (d/q '[:find (pull ?b [*]) properties-orders (->> (d/q '[:find (pull ?b [*])
:in $ :in $
:where [?b :block/content] [(missing? $ ?b :block/name)]] :where [?b :block/content] [(missing? $ ?b :block/name)]]
@ -148,10 +154,10 @@
(deftest quoted-property-values (deftest quoted-property-values
(let [conn (gp-db/start-conn) (let [conn (gp-db/start-conn)
_ (graph-parser/parse-file conn _ (parse-file conn
"foo.md" "foo.md"
"- desc:: \"#foo is not a ref\"" "- desc:: \"#foo is not a ref\""
{:extract-options {:user-config {}}}) {:extract-options {:user-config {}}})
block (->> (d/q '[:find (pull ?b [* {:block/refs [*]}]) block (->> (d/q '[:find (pull ?b [* {:block/refs [*]}])
:in $ :in $
:where [?b :block/properties]] :where [?b :block/properties]]
@ -167,10 +173,9 @@
(deftest non-string-property-values (deftest non-string-property-values
(let [conn (gp-db/start-conn)] (let [conn (gp-db/start-conn)]
(graph-parser/parse-file conn (parse-file conn
"lythe-of-heaven.md" "lythe-of-heaven.md"
"rating:: 8\nrecommend:: true\narchive:: false" "rating:: 8\nrecommend:: true\narchive:: false")
{})
(is (= {:rating 8 :recommend true :archive false} (is (= {:rating 8 :recommend true :archive false}
(->> (d/q '[:find (pull ?b [*]) (->> (d/q '[:find (pull ?b [*])
:in $ :in $
@ -181,12 +186,11 @@
(deftest linkable-built-in-properties (deftest linkable-built-in-properties
(let [conn (gp-db/start-conn) (let [conn (gp-db/start-conn)
_ (graph-parser/parse-file conn _ (parse-file conn
"lol.md" "lol.md"
(str "alias:: 233\ntags:: fun, facts" (str "alias:: 233\ntags:: fun, facts"
"\n- " "\n- "
"alias:: 666\ntags:: block, facts") "alias:: 666\ntags:: block, facts"))
{})
page-block (->> (d/q '[:find (pull ?b [:block/properties {:block/alias [:block/name]} {:block/tags [:block/name]}]) page-block (->> (d/q '[:find (pull ?b [:block/properties {:block/alias [:block/name]} {:block/tags [:block/name]}])
:in $ :in $
:where [?b :block/name "lol"]] :where [?b :block/name "lol"]]
@ -225,10 +229,10 @@
block-property-transform (fn [m] (update-keys m #(keyword (str "block-" (name %))))) block-property-transform (fn [m] (update-keys m #(keyword (str "block-" (name %)))))
block-file-properties (block-property-transform file-properties) block-file-properties (block-property-transform file-properties)
block-content (gp-property/->block-content block-file-properties) block-content (gp-property/->block-content block-file-properties)
_ (graph-parser/parse-file conn _ (parse-file conn
"property-relationships.md" "property-relationships.md"
(str page-content "\n- " block-content) (str page-content "\n- " block-content)
{:extract-options {:user-config user-config}}) {:extract-options {:user-config user-config}})
pages (->> (d/q '[:find (pull ?b [* :block/properties]) pages (->> (d/q '[:find (pull ?b [* :block/properties])
:in $ :in $
:where [?b :block/name] [?b :block/properties]] :where [?b :block/name] [?b :block/properties]]
@ -299,7 +303,7 @@
"#blarg" "invalid"} "#blarg" "invalid"}
body (str (gp-property/->block-content properties) body (str (gp-property/->block-content properties)
"\n- " (gp-property/->block-content properties))] "\n- " (gp-property/->block-content properties))]
(graph-parser/parse-file conn "foo.md" body {}) (parse-file conn "foo.md" body)
(is (= [{:block/properties {:foo "valid"} (is (= [{:block/properties {:foo "valid"}
:block/invalid-properties #{"[[foo]]" "some,prop" "#blarg"}}] :block/invalid-properties #{"[[foo]]" "some,prop" "#blarg"}}]
@ -328,10 +332,7 @@
(testing "from title" (testing "from title"
(let [conn (gp-db/start-conn) (let [conn (gp-db/start-conn)
built-in-pages (set (map string/lower-case gp-db/built-in-pages-names))] built-in-pages (set (map string/lower-case gp-db/built-in-pages-names))]
(graph-parser/parse-file conn (parse-file conn "foo.md" "title:: core.async")
"foo.md"
"title:: core.async"
{})
(is (= #{"core.async"} (is (= #{"core.async"}
(->> (d/q '[:find (pull ?b [*]) (->> (d/q '[:find (pull ?b [*])
:in $ :in $
@ -344,13 +345,12 @@
(testing "from cased org title" (testing "from cased org title"
(let [conn (gp-db/start-conn) (let [conn (gp-db/start-conn)
built-in-pages (set gp-db/built-in-pages-names)] built-in-pages (set gp-db/built-in-pages-names)]
(graph-parser/parse-file conn (parse-file conn
"foo.org" "foo.org"
":PROPERTIES: ":PROPERTIES:
:ID: 72289d9a-eb2f-427b-ad97-b605a4b8c59b :ID: 72289d9a-eb2f-427b-ad97-b605a4b8c59b
:END: :END:
#+tItLe: Well parsed!" #+tItLe: Well parsed!")
{})
(is (= #{"Well parsed!"} (is (= #{"Well parsed!"}
(->> (d/q '[:find (pull ?b [*]) (->> (d/q '[:find (pull ?b [*])
:in $ :in $
@ -364,16 +364,16 @@
(testing "for file, mailto, web and other uris in markdown" (testing "for file, mailto, web and other uris in markdown"
(let [conn (gp-db/start-conn) (let [conn (gp-db/start-conn)
built-in-pages (set (map string/lower-case gp-db/built-in-pages-names))] built-in-pages (set (map string/lower-case gp-db/built-in-pages-names))]
(graph-parser/parse-file conn (parse-file conn
"foo.md" "foo.md"
(str "- [title]([[bar]])\n" (str "- [title]([[bar]])\n"
;; all of the uris below do not create pages ;; all of the uris below do not create pages
"- ![image.png](../assets/image_1630480711363_0.png)\n" "- ![image.png](../assets/image_1630480711363_0.png)\n"
"- [Filename.txt](file:///E:/test/Filename.txt)\n" "- [Filename.txt](file:///E:/test/Filename.txt)\n"
"- [mail](mailto:test@test.com?subject=TestSubject)\n" "- [mail](mailto:test@test.com?subject=TestSubject)\n"
"- [onenote link](onenote:https://d.docs.live.net/b2127346582e6386a/blablabla/blablabla/blablabla%20blablabla.one#Etat%202019&section-id={133DDF16-9A1F-4815-9A05-44303784442E6F94}&page-id={3AAB677F0B-328F-41D0-AFF5-66408819C085}&end)\n" "- [onenote link](onenote:https://d.docs.live.net/b2127346582e6386a/blablabla/blablabla/blablabla%20blablabla.one#Etat%202019&section-id={133DDF16-9A1F-4815-9A05-44303784442E6F94}&page-id={3AAB677F0B-328F-41D0-AFF5-66408819C085}&end)\n"
"- [lock file](deps/graph-parser/yarn.lock)" "- [lock file](deps/graph-parser/yarn.lock)"
"- [example](https://example.com)")) "- [example](https://example.com)"))
(is (= #{"foo" "bar"} (is (= #{"foo" "bar"}
(->> (d/q '[:find (pull ?b [*]) (->> (d/q '[:find (pull ?b [*])
:in $ :in $
@ -383,15 +383,15 @@
(remove built-in-pages) (remove built-in-pages)
set))))) set)))))
(testing "for web and page uris in org" (testing "for web and page uris in org"
(let [conn (gp-db/start-conn) (let [conn (gp-db/start-conn)
built-in-pages (set (map string/lower-case gp-db/built-in-pages-names))] built-in-pages (set (map string/lower-case gp-db/built-in-pages-names))]
(graph-parser/parse-file conn (parse-file conn
"foo.org" "foo.org"
(str "* [[bar][title]]\n" (str "* [[bar][title]]\n"
;; all of the uris below do not create pages ;; all of the uris below do not create pages
"* [[https://example.com][example]]\n" "* [[https://example.com][example]]\n"
"* [[../assets/conga_parrot.gif][conga]]")) "* [[../assets/conga_parrot.gif][conga]]"))
(is (= #{"foo" "bar"} (is (= #{"foo" "bar"}
(->> (d/q '[:find (pull ?b [*]) (->> (d/q '[:find (pull ?b [*])
:in $ :in $
@ -407,14 +407,14 @@
extract-block-ids (atom #{}) extract-block-ids (atom #{})
parse-opts {:extract-options {:extract-block-ids extract-block-ids}} parse-opts {:extract-options {:extract-block-ids extract-block-ids}}
block-id #uuid "63f199bc-c737-459f-983d-84acfcda14fe"] block-id #uuid "63f199bc-c737-459f-983d-84acfcda14fe"]
(graph-parser/parse-file conn (parse-file conn
"foo.md" "foo.md"
"- foo "- foo
id:: 63f199bc-c737-459f-983d-84acfcda14fe id:: 63f199bc-c737-459f-983d-84acfcda14fe
- bar - bar
id:: 63f199bc-c737-459f-983d-84acfcda14fe id:: 63f199bc-c737-459f-983d-84acfcda14fe
" "
parse-opts) parse-opts)
(let [blocks (:block/_parent (ldb/get-page @conn "foo"))] (let [blocks (:block/_parent (ldb/get-page @conn "foo"))]
(is (= 2 (count blocks))) (is (= 2 (count blocks)))
(is (= 1 (count (filter #(= (:block/uuid %) block-id) blocks))))))) (is (= 1 (count (filter #(= (:block/uuid %) block-id) blocks)))))))
@ -424,21 +424,21 @@ id:: 63f199bc-c737-459f-983d-84acfcda14fe
extract-block-ids (atom #{}) extract-block-ids (atom #{})
parse-opts {:extract-options {:extract-block-ids extract-block-ids}} parse-opts {:extract-options {:extract-block-ids extract-block-ids}}
block-id #uuid "63f199bc-c737-459f-983d-84acfcda14fe"] block-id #uuid "63f199bc-c737-459f-983d-84acfcda14fe"]
(graph-parser/parse-file conn (parse-file conn
"foo.md" "foo.md"
"- foo "- foo
id:: 63f199bc-c737-459f-983d-84acfcda14fe id:: 63f199bc-c737-459f-983d-84acfcda14fe
bar bar
- test" - test"
parse-opts) parse-opts)
(graph-parser/parse-file conn (parse-file conn
"bar.md" "bar.md"
"- bar "- bar
id:: 63f199bc-c737-459f-983d-84acfcda14fe id:: 63f199bc-c737-459f-983d-84acfcda14fe
bar bar
- test - test
" "
parse-opts) parse-opts)
(is (= "foo" (is (= "foo"
(-> (d/entity @conn [:block/uuid block-id]) (-> (d/entity @conn [:block/uuid block-id])
:block/page :block/page