Browse Source

Fix query functions not working when in page view

Gabriel Horner 2 years ago
parent
commit
c3aa16de3e

+ 6 - 2
src/main/frontend/components/block/macros.cljs

@@ -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))

+ 13 - 2
src/test/frontend/components/block/macros_test.cljs

@@ -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
@@ -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