From e6a332ee745bc2cce2621039ae7c44acebe15967 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cam=20Sa=C3=BCl?= <cammsaul@gmail.com>
Date: Fri, 18 Nov 2016 13:55:25 -0800
Subject: [PATCH] Change param for card CSV/JSON download w/ params & add tests

---
 src/metabase/api/card.clj       | 12 +++++-----
 test/metabase/api/card_test.clj | 42 ++++++++++++++++++++++++++++++---
 2 files changed, 45 insertions(+), 9 deletions(-)

diff --git a/src/metabase/api/card.clj b/src/metabase/api/card.clj
index fdfbf6f1890..3e451cfe5da 100644
--- a/src/metabase/api/card.clj
+++ b/src/metabase/api/card.clj
@@ -275,15 +275,15 @@
 
 (defendpoint POST "/:card-id/query/csv"
   "Run the query associated with a Card, and return its results as CSV. Note that this expects the query as serialized JSON in the 'query' parameters."
-  [card-id query]
-  {query (s/maybe su/JSONString)}
-  (dataset-api/as-csv (run-query-for-card card-id (when query
-                                                    (:parameters (json/parse-string query keyword))))))
+  [card-id parameters]
+  {parameters (s/maybe su/JSONString)}
+  (dataset-api/as-csv (run-query-for-card card-id (json/parse-string parameters keyword))))
 
 (defendpoint GET "/:card-id/json"
   "Fetch the results of a Card as JSON."
-  [card-id]
-  (let [{{:keys [columns rows]} :data} (run-query-for-card card-id nil)]
+  [card-id parameters]
+  {parameters (s/maybe su/JSONString)}
+  (let [{{:keys [columns rows]} :data} (run-query-for-card card-id (json/parse-string parameters keyword))]
     (for [row rows]
       (zipmap columns row))))
 
diff --git a/test/metabase/api/card_test.clj b/test/metabase/api/card_test.clj
index 596e5b17293..351c2027e70 100644
--- a/test/metabase/api/card_test.clj
+++ b/test/metabase/api/card_test.clj
@@ -1,6 +1,7 @@
 (ns metabase.api.card-test
   "Tests for /api/card endpoints."
-  (:require [expectations :refer :all]
+  (:require [cheshire.core :as json]
+            [expectations :refer :all]
             [metabase.db :as db]
             [metabase.http-client :refer :all, :as http]
             [metabase.middleware :as middleware]
@@ -317,8 +318,7 @@
                Table     [{table-id :id}    {:db_id database-id, :name "CATEGORIES"}]
                Card      [card              {:dataset_query {:database database-id
                                                              :type     :native
-                                                             :native   {:query "SELECT COUNT(*) FROM CATEGORIES;"}
-                                                             :query    {:source-table table-id, :aggregation {:aggregation-type :count}}}}]]
+                                                             :native   {:query "SELECT COUNT(*) FROM CATEGORIES;"}}}]]
     ;; delete all permissions for this DB
     (perms/delete-related-permissions! (perms-group/all-users) (perms/object-path database-id))
     (f database-id card)))
@@ -350,3 +350,39 @@
     (fn [database-id card]
       (perms/grant-native-read-permissions! (perms-group/all-users) database-id)
       ((user->client :rasta) :get 200 (format "card/%d/json" (u/get-id card))))))
+
+
+;;; Test GET /api/card/:id/query/csv & GET /api/card/:id/json **WITH PARAMETERS**
+
+(defn- do-with-temp-native-card-with-params {:style/indent 0} [f]
+  (with-temp* [Database  [{database-id :id} {:details (:details (Database (id))), :engine :h2}]
+               Table     [{table-id :id}    {:db_id database-id, :name "VENUES"}]
+               Card      [card              {:dataset_query {:database database-id
+                                                             :type     :native
+                                                             :native   {:query         "SELECT COUNT(*) FROM VENUES WHERE CATEGORY_ID = {{category}};"
+                                                                        :template_tags {:category {:id           "a9001580-3bcc-b827-ce26-1dbc82429163"
+                                                                                                   :name         "category"
+                                                                                                   :display_name "Category"
+                                                                                                   :type         "number"
+                                                                                                   :required     true}}}}}]]
+    (f database-id card)))
+
+(def ^:private ^:const ^String encoded-params
+  (json/generate-string [{:type   :category
+                          :target [:variable [:template-tag :category]]
+                          :value  2}]))
+
+;; CSV
+(expect
+  (str "COUNT(*)\n"
+       "8\n")
+  (do-with-temp-native-card-with-params
+    (fn [database-id card]
+      ((user->client :rasta) :post 200 (format "card/%d/query/csv?parameters=%s" (u/get-id card) encoded-params)))))
+
+;; JSON
+(expect
+  [{(keyword "COUNT(*)") 8}]
+  (do-with-temp-native-card-with-params
+    (fn [database-id card]
+      ((user->client :rasta) :get 200 (format "card/%d/json?parameters=%s" (u/get-id card) encoded-params)))))
-- 
GitLab