Skip to content
Snippets Groups Projects
Commit 9ad4e74f authored by Tom Robinson's avatar Tom Robinson
Browse files

Merge branch 'csv-params-fix' of github.com:metabase/metabase into json-download

parents 3096e946 e6a332ee
No related branches found
No related tags found
No related merge requests found
......@@ -274,16 +274,16 @@
(run-query-for-card card-id parameters))
(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 (:parameters (json/parse-string query keyword)))))
"Run the query associated with a Card, and return its results as CSV. Note that this expects the parameters as serialized JSON in the 'parameters' parameter"
[card-id parameters]
{parameters (s/maybe su/JSONString)}
(dataset-api/as-csv (run-query-for-card card-id (json/parse-string parameters keyword))))
(defendpoint POST "/:card-id/query/json"
"Run the query associated with a Card, and return its results as JSON. Note that this expects the query as serialized JSON in the 'query' parameters."
[card-id query]
{query (s/maybe su/JSONString)}
(dataset-api/as-json (run-query-for-card card-id (:parameters (json/parse-string query keyword)))))
"Run the query associated with a Card, and return its results as JSON. Note that this expects the parameters as serialized JSON in the 'parameters' parameter"
[card-id parameters]
{parameters (s/maybe su/JSONString)}
(dataset-api/as-json (run-query-for-card card-id (json/parse-string parameters keyword))))
(define-routes)
(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)))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment