Skip to content
Snippets Groups Projects
Commit 57733085 authored by Cam Saul's avatar Cam Saul
Browse files

tests for the revision API endpoints and dashboards

parent 93b9298e
Branches
Tags
No related merge requests found
......@@ -71,27 +71,26 @@
serialized-dashboard)
(defn- describe-diff [_ username dashboard dashboard]
(let [[_ changes] (diff dashboard dashboard)]
(println "CHANGES -> " changes)
(str (->> [(when (:name changes)
(format "renamed it from \"%s\" to \"%s\"" (:name dashboard) (:name dashboard)))
(when (:description changes)
(format "changed the description from \"%s\" to \"%s\"" (:description dashboard) (:description dashboard)))
(when (:public_perms changes)
(if (zero? (:public_perms dashboard))
"made it private"
"made it public")) ; TODO - are both 1 and 2 "public" now ?
(when (:cards changes)
(let [num-cards (count (:cards dashboard))
num-cards (count (:cards dashboard))]
(cond
(> num-cards num-cards) "removed a card"
(< num-cards num-cards) "added a card"
:else "repositioned the cards")))]
(filter identity)
build-sentence
(apply str username " ")
(#(s/replace-first % "it " "this dashboard "))) \.)))
(let [[removals changes] (diff dashboard dashboard)]
(->> [(when (:name changes)
(format "renamed it from \"%s\" to \"%s\"" (:name dashboard) (:name dashboard)))
(when (:description changes)
(format "changed the description from \"%s\" to \"%s\"" (:description dashboard) (:description dashboard)))
(when (:public_perms changes)
(if (zero? (:public_perms dashboard))
"made it private"
"made it public")) ; TODO - are both 1 and 2 "public" now ?
(when (or (:cards changes) (:cards removals))
(let [num-cards (count (:cards dashboard))
num-cards (count (:cards dashboard))]
(cond
(< num-cards num-cards) "added a card"
(> num-cards num-cards) "removed a card"
:else "rearranged the cards")))]
(filter identity)
build-sentence
(apply str username " ")
(#(s/replace-first % "it " "this dashboard ")))))
(extend DashboardEntity
IRevisioned
......
(ns metabase.api.revision-test
(:require [expectations :refer :all]
[korma.core :as k]
[medley.core :as m]
[metabase.api.revision :refer :all]
[metabase.db :as db]
(metabase.models [dashboard :refer [Dashboard]]
[dashboard-card :refer [DashboardCard]]
[revision-test :refer [with-fake-card]])
[metabase.test.data.users :refer :all]))
(defn x []
(with-fake-card [{card-id :id}]
(with-fake-card [{card-id :id}]
((user->client :rasta) :get 200 "revision", :entity :card, :id card-id))))
(defn y []
(with-fake-card [{card-id :id}]
((user->client :rasta) :get 200 "revision", :entity :card, :id card-id)))
(defn- fake-dashboard [& {:as kwargs}]
(m/mapply db/ins Dashboard (merge {:name (str (java.util.UUID/randomUUID))
:public_perms 0
:creator_id (user->id :rasta)}
kwargs)))
(defmacro with-fake-dashboard [[binding & {:as kwargs}] & body]
`(let [dash# (fake-dashboard ~@kwargs)
~binding dash#]
(try ~@body
(finally
(db/cascade-delete Dashboard :id (:id dash#))))))
(def ^:private rasta-revision-info
(delay {:id (user->id :rasta) :common_name "Rasta Toucan", :first_name "Rasta", :last_name "Toucan"}))
;;; # TESTS FOR GET /api/revision
(expect [{:description "First revision.", :user {}}]
(with-fake-card [{card-id :id}]
((user->client :rasta) :get 200 "revision", :entity :card, :id card-id)))
(expect [{:is_reversion false, :user @rasta-revision-info, :description "First revision."}]
(with-fake-dashboard [{dash-id :id}]
(with-fake-card [{card-id :id}]
((user->client :rasta) :post 200 (format "dash/%d/cards" dash-id), {:cardId card-id})
(->> ((user->client :rasta) :get 200 "revision", :entity :dashboard, :id dash-id)
(mapv #(dissoc % :timestamp :id))))))
(expect [{:is_reversion false, :user @rasta-revision-info, :description "Rasta Toucan added a card."}
{:is_reversion false, :user @rasta-revision-info, :description "First revision."}]
(with-fake-dashboard [{dash-id :id}]
(with-fake-card [{card-id :id}]
(with-fake-card [{card-id :id}]
((user->client :rasta) :post 200 (format "dash/%d/cards" dash-id), {:cardId card-id})
((user->client :rasta) :post 200 (format "dash/%d/cards" dash-id), {:cardId card-id})
(->> ((user->client :rasta) :get 200 "revision", :entity :dashboard, :id dash-id)
(mapv #(dissoc % :timestamp :id)))))))
(expect [{:is_reversion false, :user @rasta-revision-info, :description "Rasta Toucan removed a card."}
{:is_reversion false, :user @rasta-revision-info, :description "Rasta Toucan added a card."}
{:is_reversion false, :user @rasta-revision-info, :description "First revision."}]
(with-fake-dashboard [{dash-id :id}]
(with-fake-card [{card-id :id}]
(with-fake-card [{card-id :id}]
((user->client :rasta) :post 200 (format "dash/%d/cards" dash-id), {:cardId card-id})
((user->client :rasta) :post 200 (format "dash/%d/cards" dash-id), {:cardId card-id})
((user->client :rasta) :delete 204 (format "dash/%d/cards" dash-id), :dashcardId (db/sel :one :id DashboardCard (k/order :id :desc)))
(->> ((user->client :rasta) :get 200 "revision", :entity :dashboard, :id dash-id)
(mapv #(dissoc % :timestamp :id)))))))
;;; # TESTS FOR POST /api/revision/revert
(expect [2
[{:is_reversion true, :user @rasta-revision-info, :description "Rasta Toucan reverted to an earlier revision and added a card."}
{:is_reversion false, :user @rasta-revision-info, :description "Rasta Toucan removed a card."}
{:is_reversion false, :user @rasta-revision-info, :description "Rasta Toucan added a card."}
{:is_reversion false, :user @rasta-revision-info, :description "First revision."}]]
(with-fake-dashboard [{dash-id :id}]
(with-fake-card [{card-id :id}]
(with-fake-card [{card-id :id}]
((user->client :rasta) :post 200 (format "dash/%d/cards" dash-id), {:cardId card-id})
((user->client :rasta) :post 200 (format "dash/%d/cards" dash-id), {:cardId card-id})
((user->client :rasta) :delete 204 (format "dash/%d/cards" dash-id), :dashcardId (db/sel :one :id DashboardCard (k/order :id :desc)))
(let [[_ {previous-revision-id :id}] (metabase.models.revision/revisions Dashboard dash-id)]
;; Revert to the previous revision
((user->client :rasta) :post 200 "revision/revert", {:entity :dashboard, :id dash-id, :revision_id previous-revision-id})
[ ;; [1] There should be 2 cards again
(count @(:ordered_cards (Dashboard dash-id)))
;; [2] A new revision recording the reversion should have been pushed
(->> ((user->client :rasta) :get 200 "revision", :entity :dashboard, :id dash-id)
(mapv #(dissoc % :timestamp :id)))])))))
......@@ -9,7 +9,7 @@
[metabase.test.data.users :refer :all]
[metabase.util :as u]))
(defn- fake-card [& {:as kwargs}]
(defn fake-card [& {:as kwargs}]
(m/mapply db/ins Card (merge {:name (str (java.util.UUID/randomUUID))
:display :table
:public_perms 0
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment