Skip to content

Commit

Permalink
Add tests for timestamp blocks
Browse files Browse the repository at this point in the history
Also make block attributes more explicit in pre-block and reduce needless coupling
  • Loading branch information
logseq-cldwalker committed Feb 27, 2023
1 parent 6506c36 commit 6108aa1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
33 changes: 18 additions & 15 deletions deps/graph-parser/src/logseq/graph_parser/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -526,21 +526,24 @@
(when (coll? refs)
refs))))
(map :block/original-name))
block {:uuid id
:content content
:level 1
:properties properties
:properties-order (vec properties-order)
:properties-text-values properties-text-values
:invalid-properties invalid-properties
:refs property-refs
:pre-block? true
:unordered true
:macros (extract-macros-from-ast body)
:body body}
block (with-page-block-refs block false supported-formats db date-formatter)
block' (update block :refs concat (:block-refs pre-block-properties))]
(block-keywordize block'))
block {:block/uuid id
:block/content content
:block/level 1
:block/properties properties
:block/properties-order (vec properties-order)
:block/properties-text-values properties-text-values
:block/invalid-properties invalid-properties
:block/pre-block? true
:block/unordered true
:block/macros (extract-macros-from-ast body)
:block/body body}
{:keys [tags refs]}
(with-page-block-refs {:body body :refs property-refs} false supported-formats db date-formatter)]
(cond-> block
tags
(assoc :block/tags tags)
true
(assoc :block/refs (concat refs (:block-refs pre-block-properties)))))
(select-keys first-block [:block/format :block/page]))
blocks)
blocks)]
Expand Down
41 changes: 29 additions & 12 deletions deps/graph-parser/test/logseq/graph_parser/block_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -110,37 +110,54 @@
{:property-pages/enabled? true})))
"Only editable linkable built-in properties have page-refs in property values")))

(defn find-block-for-content
[db content]
(->> (d/q '[:find (pull ?b [* {:block/refs [:block/uuid]}])
:in $ ?content
:where [?b :block/content ?content]]
db
content)
(map first)
first))

(deftest refs-from-block-refs
(let [conn (ldb/start-conn)
id "63f528da-284a-45d1-ac9c-5d6a7435f6b4"
block (str "A block\nid:: " 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))
body (str "- " block "\n- " block-ref-via-content "\n- " block-ref-via-block-properties)
find-block-for-content (fn [content]
(->> (d/q '[:find (pull ?b [:block/content {:block/refs [:block/uuid]}])
:in $ ?content
:where [?b :block/content ?content]]
@conn
content)
(map first)
first))]
body (str "- " block "\n- " block-ref-via-content "\n- " block-ref-via-block-properties)]
(graph-parser/parse-file conn "foo.md" body {})

(testing "Block refs in blocks"
(is (= [{:block/uuid (uuid id)}]
(:block/refs (find-block-for-content block-ref-via-content)))
(:block/refs (find-block-for-content @conn block-ref-via-content)))
"Block that links to a block via paragraph content has correct block ref")

(is (contains?
(set (:block/refs (find-block-for-content block-ref-via-block-properties)))
(set (:block/refs (find-block-for-content @conn block-ref-via-block-properties)))
{:block/uuid (uuid id)})
"Block that links to a block via block properties has correct block ref"))

(testing "Block refs in pre-block"
(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 {})
(is (contains?
(set (:block/refs (find-block-for-content block-ref-via-page-properties)))
(set (:block/refs (find-block-for-content @conn block-ref-via-page-properties)))
{:block/uuid (uuid id)})
"Block that links to a block via page properties has correct block ref")))))

(deftest timestamp-blocks
(let [conn (ldb/start-conn)
deadline-block "do something\nDEADLINE: <2023-02-21 Tue>"
scheduled-block "do something else\nSCHEDULED: <2023-02-20 Mon>"
body (str "- " deadline-block "\n- " scheduled-block)]
(graph-parser/parse-file conn "foo.md" body {})

(is (= 20230220
(:block/scheduled (find-block-for-content @conn scheduled-block)))
"Scheduled block has correct block attribute and value")

(is (= 20230221
(:block/deadline (find-block-for-content @conn deadline-block)))
"Deadline block has correct block attribute and value")))

0 comments on commit 6108aa1

Please sign in to comment.