Skip to content

Commit

Permalink
Fix query functions not working when in page view
Browse files Browse the repository at this point in the history
  • Loading branch information
logseq-cldwalker authored and tiensonqin committed Jun 6, 2023
1 parent e2214b9 commit c3aa16d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/main/frontend/components/block/macros.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@

(defn function-macro
"Provides functionality for {{function}}"
[query-result arguments]
(let [fn-string (-> (gstring/format "(fn [result] %s)" (first arguments))
[query-result* arguments]
(let [query-result (if (map? query-result*)
;; Ungroup results grouped by page in page view
(mapcat val query-result*)
query-result*)
fn-string (-> (gstring/format "(fn [result] %s)" (first arguments))
(common-handler/safe-read-string "failed to parse function")
(normalize-query-function query-result)
(str))
Expand Down
15 changes: 13 additions & 2 deletions src/test/frontend/components/block/macros_test.cljs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
(ns frontend.components.block.macros-test
(:require [frontend.components.block.macros :as block-macros]
[frontend.db.utils :as db-utils]
[clojure.test :refer [deftest are testing is]]))

(deftest macro-function
(testing "Default table functions with property argument"
(testing "Default functions as an argument"
(are [user-input result]
(= result
(block-macros/function-macro
Expand All @@ -15,12 +16,22 @@
"(min :total)" 10
"(count :total)" 3))

(testing "Table function with clojure function argument"
(testing "Custom clojure function as an argument"
(is (= 130
(block-macros/function-macro
(mapv #(hash-map :block/properties %)
[{:total 10 :qty 3} {:total 20 :qty 5}])
["(sum (map (fn [x] (* (:total x) (:qty x))) result))"]))))

(testing "When query results are in page view"
(is (= 60
(block-macros/function-macro
(db-utils/group-by-page
[{:block/properties {:total 10} :block/page 1}
{:block/properties {:total 20} :block/page 2}
{:block/properties {:total 30} :block/page 1}])
["(sum :total)"]))
"Default function works like in query table view"))

(testing "Edge cases"
(is (= 40
Expand Down

0 comments on commit c3aa16d

Please sign in to comment.