Ben Yorke 2 лет назад
Родитель
Сommit
a2efbdb333

+ 11 - 13
deps/graph-parser/src/logseq/graph_parser/util/db.cljs

@@ -72,9 +72,6 @@ it will return 1622433600000, which is equivalent to Mon May 31 2021 00 :00:00."
        (min 59  (parse-long (str s1 s2)))
        (min 999 (parse-long (str ms1 ms2 ms3)))])))
 
-(let [[h1 h2 m1 m2 s1 s2 ms1 ms2 ms3] (str "235959" "0000000000")]
-  [(parse-long (str h1 h2)) (parse-long (str m1 m2)) (parse-long (str s1 s2)) (parse-long (str ms1 ms2 ms3))])
-
 (defn keyword-input-dispatch [input]
   (cond 
     (#{:current-page :current-block :parent-block :today :yesterday :tomorrow :right-now-ms} input) input
@@ -91,16 +88,16 @@ it will return 1622433600000, which is equivalent to Mon May 31 2021 00 :00:00."
 (defmulti resolve-keyword-input (fn [_db input _opts] (keyword-input-dispatch input))) 
 
 (defmethod resolve-keyword-input :current-page [_ _ {:keys [current-page-fn]}]
-  ;; TODO: handle current-page-fn not being provided
-  (some-> (current-page-fn) string/lower-case))
+  (when current-page-fn
+    (some-> (current-page-fn) string/lower-case)))
 
 (defmethod resolve-keyword-input :current-block [db _ {:keys [current-block-uuid]}]
-  ;; TODO: handle current-block-uuid not being provided
-  (:db/id (d/entity db [:block/uuid current-block-uuid])))
+  (when current-block-uuid
+    (:db/id (d/entity db [:block/uuid current-block-uuid]))))
 
 (defmethod resolve-keyword-input :parent-block [db _ {:keys [current-block-uuid]}]
-  ;; TODO: handle current-block-uuid not being provided
-  (:db/id (:block/parent (d/entity db [:block/uuid current-block-uuid]))))
+  (when current-block-uuid
+    (:db/id (:block/parent (d/entity db [:block/uuid current-block-uuid])))))
 
 (defmethod resolve-keyword-input :today [_ _ _]
   (date->int (t/today)))
@@ -142,18 +139,19 @@ it will return 1622433600000, which is equivalent to Mon May 31 2021 00 :00:00."
   ;; :Xd, :Xd-before, :Xd-before-ms, :Xd-after, :Xd-after-ms
   (resolve-keyword-input db (old->new-relative-date-format input) opts))
 
+(defmethod resolve-keyword-input :default [_ _ _]
+  nil)
+
 (defn resolve-input
   "Main fn for resolving advanced query :inputs"
   [db input {:keys [current-block-uuid current-page-fn]
              :or {current-page-fn (constantly nil)}}]
   (cond
     (keyword? input) 
-    (try 
+    (or
       (resolve-keyword-input db input {:current-block-uuid current-block-uuid
                                        :current-page-fn current-page-fn})
-      (catch js/Error e
-        (println "Error resolving input" input e)
-        input))
+      input)
     
     (and (string? input) (page-ref/page-ref? input))
     (-> (page-ref/get-page-name input)

+ 42 - 1
src/test/frontend/db/query_react_test.cljs

@@ -39,6 +39,16 @@ adds rules that users often use"
                                         [?bp :block/name ?current-page]]}))))
       ":current-page input resolves to current page name")
 
+  (is (= []
+         (map :block/content
+              (custom-query {:inputs [:current-page]
+                             :query '[:find (pull ?b [*])
+                                      :in $ ?current-page
+                                      :where [?b :block/page ?bp]
+                                      [?bp :block/name ?current-page]]}
+                            {:current-page-fn nil})))
+      ":current-page input fails gracefully when not present")
+
   (is (= ["child 1" "child 2"]
          (let [block-uuid (-> (db-utils/q '[:find (pull ?b [:block/uuid])
                                             :where [?b :block/content "parent"]])
@@ -51,6 +61,24 @@ adds rules that users often use"
                                         :where [?b :block/parent ?current-block]]}
                               {:current-block-uuid block-uuid}))))
       ":current-block input resolves to current block's :db/id")
+
+  (is (= []
+         (map :block/content
+              (custom-query {:inputs [:current-block]
+                             :query '[:find (pull ?b [*])
+                                      :in $ ?current-block
+                                      :where [?b :block/parent ?current-block]]})))
+      ":current-block input fails gracefuly when current-block-uuid is not provided")
+
+  (is (= []
+         (map :block/content
+              (custom-query {:inputs [:current-block]
+                             :query '[:find (pull ?b [*])
+                                      :in $ ?current-block
+                                      :where [?b :block/parent ?current-block]]}
+                            {:current-block-uuid :magic})))
+      ":current-block input fails gracefuly when current-block-uuid is invalid")
+
   (is (= ["parent"]
          (let [block-uuid (-> (db-utils/q '[:find (pull ?b [:block/uuid])
                                             :where [?b :block/content "child 1"]])
@@ -343,7 +371,20 @@ created-at:: %s"
                                         [?b :block/created-at ?timestamp]
                                         [(>= ?timestamp ?start)]
                                         [(<= ?timestamp ?end)]]}))))
-      ":-XT-HHMM and :+XT-HHMM resolve to correct datetime range"))
+      ":-XT-HHMM and :+XT-HHMM resolve to correct datetime range")
+
+  (is (= []
+         (map #(-> % :block/content string/split-lines first)
+              (custom-query {:inputs [:-0d-abcd :+1d-23.45]
+                             :query '[:find (pull ?b [*])
+                                      :in $ ?start ?end
+                                      :where
+                                      [?b :block/content]
+                                      [?b :block/created-at ?timestamp]
+                                      [(>= ?timestamp ?start)]
+                                      [(<= ?timestamp ?end)]]})))
+      ":-XT-HHMM and :+XT-HHMM will not reoslve with invalid time formats but will fail gracefully")) 
+        
 
 (deftest resolve-input-for-relative-date-queries 
   (load-test-files [{:file/content "- -1y" :file/path "journals/2022_01_01.md"}