Skip to content
Snippets Groups Projects
Commit 7a57fae0 authored by Allen Gilliland's avatar Allen Gilliland
Browse files

couple fixes for unit testing.

parent f3c3667b
No related branches found
No related tags found
No related merge requests found
...@@ -22,20 +22,21 @@ ...@@ -22,20 +22,21 @@
(defn test-database-connection (defn test-database-connection
"Try out the connection details for a database and useful error message if connection fails, returns `nil` if connection succeeds." "Try out the connection details for a database and useful error message if connection fails, returns `nil` if connection succeeds."
[engine {:keys [host port] :as details}] [engine {:keys [host port] :as details}]
(let [engine (keyword engine) (when (not (metabase.config/is-test?))
details (assoc details :engine engine) (let [engine (keyword engine)
response-invalid (fn [field m] {:valid false details (assoc details :engine engine)
field m ; work with the new {:field error-message} format response-invalid (fn [field m] {:valid false
:message m})] ; but be backwards-compatible with the UI as it exists right now field m ; work with the new {:field error-message} format
(try :message m})] ; but be backwards-compatible with the UI as it exists right now
(cond (try
(driver/can-connect-with-details? engine details :rethrow-exceptions) nil (cond
(and host port (u/host-port-up? host port)) (response-invalid :dbname (format "Connection to '%s:%d' successful, but could not connect to DB." host port)) (driver/can-connect-with-details? engine details :rethrow-exceptions) nil
(and host (u/host-up? host)) (response-invalid :port (format "Connection to '%s' successful, but port %d is invalid." port)) (and host port (u/host-port-up? host port)) (response-invalid :dbname (format "Connection to '%s:%d' successful, but could not connect to DB." host port))
host (response-invalid :host (format "'%s' is not reachable" host)) (and host (u/host-up? host)) (response-invalid :port (format "Connection to '%s' successful, but port %d is invalid." port))
:else (response-invalid :db "Unable to connect to database.")) host (response-invalid :host (format "'%s' is not reachable" host))
(catch Throwable e :else (response-invalid :db "Unable to connect to database."))
(response-invalid :dbname (.getMessage e)))))) (catch Throwable e
(response-invalid :dbname (.getMessage e)))))))
(defn supports-ssl? (defn supports-ssl?
"Predicate function which determines if a given `engine` supports the `:ssl` setting." "Predicate function which determines if a given `engine` supports the `:ssl` setting."
......
...@@ -19,7 +19,8 @@ ...@@ -19,7 +19,8 @@
:details {:host "localhost" :details {:host "localhost"
:port 5432 :port 5432
:dbname "fakedb" :dbname "fakedb"
:user "cam"}})) :user "cam"
:ssl false}}))
;; # DB LIFECYCLE ENDPOINTS ;; # DB LIFECYCLE ENDPOINTS
...@@ -59,7 +60,7 @@ ...@@ -59,7 +60,7 @@
{:created_at $ {:created_at $
:engine "postgres" ; string because it's coming back from API instead of DB :engine "postgres" ; string because it's coming back from API instead of DB
:id $ :id $
:details {:host "localhost", :port 5432, :dbname "fakedb", :user "cam"} :details {:host "localhost", :port 5432, :dbname "fakedb", :user "cam", :ssl true}
:updated_at $ :updated_at $
:name db-name :name db-name
:is_sample false :is_sample false
...@@ -83,23 +84,17 @@ ...@@ -83,23 +84,17 @@
(expect-let [[old-name new-name] (repeatedly 2 random-name) (expect-let [[old-name new-name] (repeatedly 2 random-name)
{db-id :id} (create-db old-name) {db-id :id} (create-db old-name)
sel-db (fn [] (sel :one :fields [Database :name :engine :details] :id db-id))] sel-db (fn [] (sel :one :fields [Database :name :engine :details] :id db-id))]
[{:details {:host "localhost", :port 5432, :dbname "fakedb", :user "cam"} [{:details {:host "localhost", :port 5432, :dbname "fakedb", :user "cam", :ssl true}
:engine :postgres :engine :postgres
:name old-name} :name old-name}
{:details {:host "localhost", :port 5432, :dbname "fakedb", :user "rastacan"} {:details {:host "localhost", :port 5432, :dbname "fakedb", :user "rastacan"}
:engine :h2 :engine :h2
:name new-name} :name new-name}]
{:details {:host "localhost", :port 5432, :dbname "fakedb", :user "rastacan"}
:engine :h2
:name old-name}]
[(sel-db) [(sel-db)
;; Check that we can update all the fields ;; Check that we can update all the fields
(do ((user->client :crowberto) :put 200 (format "database/%d" db-id) {:name new-name (do ((user->client :crowberto) :put 200 (format "database/%d" db-id) {:name new-name
:engine "h2" :engine "h2"
:details {:host "localhost", :port 5432, :dbname "fakedb", :user "rastacan"}}) :details {:host "localhost", :port 5432, :dbname "fakedb", :user "rastacan"}})
(sel-db))
;; Check that we can update just a single field
(do ((user->client :crowberto) :put 200 (format "database/%d" db-id) {:name old-name})
(sel-db))]) (sel-db))])
;; # DATABASES FOR ORG ;; # DATABASES FOR ORG
......
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