 (ns metabase.api.common.internal-test
   (:require [expectations :refer :all]
+            [medley.core :as medley]
             (metabase.api.common [internal :refer :all])))
 
 ;;; TESTS FOR ROUTE-FN-NAME

(expect "/"
  (no-regex (typify-route "/")))

(expect ["/:id" :id "#[0-9]+"]
  (no-regex (typify-route "/:id")))

(expect ["/:id/card" :id "#[0-9]+"]
  (no-regex (typify-route "/:id/card")))

(expect ["/:card-id" :card-id "#[0-9]+"]
  (no-regex (typify-route "/:card-id")))

(expect "/:fish"
  (no-regex (typify-route "/:fish")))

(expect ["/:id/tables/:card-id" :id "#[0-9]+" :card-id "#[0-9]+"]
  (no-regex (typify-route "/:id/tables/:card-id")))

;; don't try to typify route that's already typified
(expect ["/:id/:crazy-id" :crazy-id "#[0-9]+"]
  (no-regex (typify-route ["/:id/:crazy-id" :crazy-id "#[0-9]+"])))


;; TESTS FOR LET-FORM-FOR-ARG

;;; TESTS FOR ROUTE-PARAM-REGEX

;; expectations (internally, `clojure.data/diff`) doesn't think two regexes with the same exact pattern are equal.
;; so in order to make sure we're getting back the right output we'll just change them to strings, e.g. `#"^[0-9]+$" -> "#^[0-9]+$"`
(defmacro no-regex [& body]
  `(binding [*auto-parse-types* (medley/map-vals #(medley/update % :route-param-regex (partial str "#"))
                                                 *auto-parse-types*) ]
     ~@body))

(expect nil
  (no-regex (route-param-regex :fish)))

(expect [:id "#^[0-9]+$"]
  (no-regex (route-param-regex :id)))

(expect [:card-id "#^[0-9]+$"]
  (no-regex (route-param-regex :card-id)))


