2020-08-18 19:26:37 +02:00
|
|
|
(ns app.tests.helpers
|
2019-11-18 11:52:57 +01:00
|
|
|
(:require
|
2020-05-14 13:49:11 +02:00
|
|
|
[clojure.java.io :as io]
|
2019-11-18 11:52:57 +01:00
|
|
|
[clojure.spec.alpha :as s]
|
|
|
|
[promesa.core :as p]
|
2020-05-14 13:49:11 +02:00
|
|
|
[datoteka.core :as fs]
|
2019-11-18 11:52:57 +01:00
|
|
|
[cuerdas.core :as str]
|
|
|
|
[mount.core :as mount]
|
2020-02-03 09:53:46 +01:00
|
|
|
[environ.core :refer [env]]
|
2020-08-18 19:26:37 +02:00
|
|
|
[app.common.pages :as cp]
|
|
|
|
[app.services.init]
|
|
|
|
[app.services.mutations.profile :as profile]
|
|
|
|
[app.services.mutations.projects :as projects]
|
|
|
|
[app.services.mutations.teams :as teams]
|
|
|
|
[app.services.mutations.files :as files]
|
|
|
|
[app.services.mutations.pages :as pages]
|
|
|
|
[app.services.mutations.colors :as colors]
|
|
|
|
[app.migrations]
|
|
|
|
[app.media]
|
|
|
|
[app.media-storage]
|
|
|
|
[app.db :as db]
|
|
|
|
[app.util.blob :as blob]
|
|
|
|
[app.common.uuid :as uuid]
|
|
|
|
[app.util.storage :as ust]
|
|
|
|
[app.config :as cfg])
|
2020-05-14 13:49:11 +02:00
|
|
|
(:import org.postgresql.ds.PGSimpleDataSource))
|
2020-02-06 09:46:24 +01:00
|
|
|
|
2020-05-14 13:49:11 +02:00
|
|
|
(defn testing-datasource
|
|
|
|
[]
|
|
|
|
(doto (PGSimpleDataSource.)
|
|
|
|
(.setServerName "postgres")
|
2020-08-24 12:51:05 +02:00
|
|
|
(.setDatabaseName "uxbox_test")
|
|
|
|
(.setUser "uxbox")
|
|
|
|
(.setPassword "uxbox")))
|
2016-11-20 20:04:52 +01:00
|
|
|
|
|
|
|
(defn state-init
|
|
|
|
[next]
|
2020-02-03 09:53:46 +01:00
|
|
|
(let [config (cfg/read-test-config env)]
|
2016-11-20 20:04:52 +01:00
|
|
|
(try
|
2020-05-14 13:49:11 +02:00
|
|
|
(let [pool (testing-datasource)]
|
2020-08-18 19:26:37 +02:00
|
|
|
(-> (mount/only #{#'app.config/config
|
|
|
|
#'app.db/pool
|
|
|
|
#'app.redis/client
|
|
|
|
#'app.redis/conn
|
|
|
|
#'app.media/semaphore
|
|
|
|
#'app.services.init/query-services
|
|
|
|
#'app.services.init/mutation-services
|
|
|
|
#'app.migrations/migrations
|
|
|
|
#'app.media-storage/assets-storage
|
|
|
|
#'app.media-storage/media-storage})
|
|
|
|
(mount/swap {#'app.config/config config
|
|
|
|
#'app.db/pool pool})
|
2020-05-14 13:49:11 +02:00
|
|
|
(mount/start)))
|
|
|
|
(next)
|
2016-11-20 20:04:52 +01:00
|
|
|
(finally
|
|
|
|
(mount/stop)))))
|
|
|
|
|
|
|
|
(defn database-reset
|
|
|
|
[next]
|
2019-11-18 11:52:57 +01:00
|
|
|
(let [sql (str "SELECT table_name "
|
|
|
|
" FROM information_schema.tables "
|
|
|
|
" WHERE table_schema = 'public' "
|
|
|
|
" AND table_name != 'migrations';")]
|
2020-05-14 13:49:11 +02:00
|
|
|
(db/with-atomic [conn db/pool]
|
|
|
|
(let [result (->> (db/exec! conn [sql])
|
|
|
|
(map :table-name))]
|
|
|
|
(db/exec! conn [(str "TRUNCATE "
|
|
|
|
(apply str (interpose ", " result))
|
|
|
|
" CASCADE;")]))))
|
2019-06-15 18:20:36 +02:00
|
|
|
(try
|
|
|
|
(next)
|
|
|
|
(finally
|
2020-08-18 19:26:37 +02:00
|
|
|
(ust/clear! app.media-storage/media-storage)
|
|
|
|
(ust/clear! app.media-storage/assets-storage))))
|
2016-11-20 20:04:52 +01:00
|
|
|
|
2019-11-18 11:52:57 +01:00
|
|
|
(defn mk-uuid
|
|
|
|
[prefix & args]
|
2020-04-11 11:37:52 +02:00
|
|
|
(uuid/namespaced uuid/zero (apply str prefix args)))
|
2019-11-18 11:52:57 +01:00
|
|
|
|
2020-01-13 23:52:31 +01:00
|
|
|
;; --- Profile creation
|
2019-11-18 11:52:57 +01:00
|
|
|
|
2020-02-17 09:49:04 +01:00
|
|
|
(defn create-profile
|
2016-11-20 20:04:52 +01:00
|
|
|
[conn i]
|
2020-05-26 12:28:35 +02:00
|
|
|
(let [params {:id (mk-uuid "profile" i)
|
|
|
|
:fullname (str "Profile " i)
|
|
|
|
:email (str "profile" i ".test@nodomain.com")
|
|
|
|
:password "123123"}]
|
|
|
|
(->> (#'profile/create-profile conn params)
|
|
|
|
(#'profile/create-profile-relations conn))))
|
2020-02-17 09:49:04 +01:00
|
|
|
|
|
|
|
(defn create-team
|
|
|
|
[conn profile-id i]
|
|
|
|
(#'teams/create-team conn {:id (mk-uuid "team" i)
|
|
|
|
:profile-id profile-id
|
|
|
|
:name (str "team" i)}))
|
2019-11-18 11:52:57 +01:00
|
|
|
|
|
|
|
(defn create-project
|
2020-02-17 09:49:04 +01:00
|
|
|
[conn profile-id team-id i]
|
|
|
|
(#'projects/create-project conn {:id (mk-uuid "project" i)
|
|
|
|
:profile-id profile-id
|
|
|
|
:team-id team-id
|
|
|
|
:name (str "project" i)}))
|
|
|
|
|
|
|
|
(defn create-file
|
2020-08-05 14:31:08 +02:00
|
|
|
[conn profile-id project-id is-shared i]
|
2020-02-17 09:49:04 +01:00
|
|
|
(#'files/create-file conn {:id (mk-uuid "file" i)
|
|
|
|
:profile-id profile-id
|
|
|
|
:project-id project-id
|
2020-08-05 14:31:08 +02:00
|
|
|
:is-shared is-shared
|
2020-02-17 09:49:04 +01:00
|
|
|
:name (str "file" i)}))
|
|
|
|
|
|
|
|
(defn create-page
|
|
|
|
[conn profile-id file-id i]
|
|
|
|
(#'pages/create-page conn {:id (mk-uuid "page" i)
|
|
|
|
:profile-id profile-id
|
|
|
|
:file-id file-id
|
|
|
|
:name (str "page" i)
|
|
|
|
:ordering i
|
2020-04-06 23:32:54 +02:00
|
|
|
:data cp/default-page-data}))
|
2020-02-17 09:49:04 +01:00
|
|
|
|
2019-11-18 11:52:57 +01:00
|
|
|
(defn handle-error
|
2020-02-17 09:49:04 +01:00
|
|
|
[^Throwable err]
|
2019-12-09 16:27:01 +01:00
|
|
|
(if (instance? java.util.concurrent.ExecutionException err)
|
2019-11-18 11:52:57 +01:00
|
|
|
(handle-error (.getCause err))
|
2019-12-09 16:27:01 +01:00
|
|
|
err))
|
2016-11-20 20:04:52 +01:00
|
|
|
|
|
|
|
(defmacro try-on
|
2019-11-18 11:52:57 +01:00
|
|
|
[expr]
|
2016-11-20 20:04:52 +01:00
|
|
|
`(try
|
2019-11-18 11:52:57 +01:00
|
|
|
(let [result# (deref ~expr)]
|
2016-11-20 20:04:52 +01:00
|
|
|
[nil result#])
|
2019-11-18 11:52:57 +01:00
|
|
|
(catch Exception e#
|
|
|
|
[(handle-error e#) nil])))
|
|
|
|
|
|
|
|
(defmacro try-on!
|
|
|
|
[expr]
|
|
|
|
`(try
|
2020-05-14 13:49:11 +02:00
|
|
|
{:error nil
|
|
|
|
:result ~expr}
|
2019-11-18 11:52:57 +01:00
|
|
|
(catch Exception e#
|
|
|
|
{:error (handle-error e#)
|
|
|
|
:result nil})))
|
|
|
|
|
2019-11-25 15:56:49 +01:00
|
|
|
(defmacro try!
|
|
|
|
[expr]
|
|
|
|
`(try
|
|
|
|
{:error nil
|
|
|
|
:result ~expr}
|
|
|
|
(catch Exception e#
|
|
|
|
{:error (handle-error e#)
|
|
|
|
:result nil})))
|
|
|
|
|
2019-12-09 16:27:01 +01:00
|
|
|
(defn print-error!
|
|
|
|
[error]
|
|
|
|
(let [data (ex-data error)]
|
|
|
|
(cond
|
|
|
|
(= :spec-validation (:code data))
|
|
|
|
(println (:explain data))
|
|
|
|
|
2020-02-17 09:49:04 +01:00
|
|
|
(= :service-error (:type data))
|
|
|
|
(print-error! (.getCause ^Throwable error))
|
|
|
|
|
2019-12-09 16:27:01 +01:00
|
|
|
:else
|
2020-02-17 09:49:04 +01:00
|
|
|
(.printStackTrace ^Throwable error))))
|
2019-12-09 16:27:01 +01:00
|
|
|
|
2019-11-18 11:52:57 +01:00
|
|
|
(defn print-result!
|
|
|
|
[{:keys [error result]}]
|
|
|
|
(if error
|
|
|
|
(do
|
|
|
|
(println "====> START ERROR")
|
2019-12-09 16:27:01 +01:00
|
|
|
(print-error! error)
|
2019-12-01 16:48:41 +01:00
|
|
|
(println "====> END ERROR"))
|
2019-11-18 11:52:57 +01:00
|
|
|
(do
|
|
|
|
(println "====> START RESPONSE")
|
|
|
|
(prn result)
|
|
|
|
(println "====> END RESPONSE"))))
|
|
|
|
|
2016-11-20 20:04:52 +01:00
|
|
|
(defn exception?
|
|
|
|
[v]
|
|
|
|
(instance? Throwable v))
|
|
|
|
|
|
|
|
(defn ex-info?
|
|
|
|
[v]
|
|
|
|
(instance? clojure.lang.ExceptionInfo v))
|
|
|
|
|
|
|
|
(defn ex-of-type?
|
|
|
|
[e type]
|
|
|
|
(let [data (ex-data e)]
|
|
|
|
(= type (:type data))))
|
|
|
|
|
2019-12-09 16:27:01 +01:00
|
|
|
(defn ex-of-code?
|
|
|
|
[e code]
|
|
|
|
(let [data (ex-data e)]
|
|
|
|
(= code (:code data))))
|
|
|
|
|
2016-11-20 20:04:52 +01:00
|
|
|
(defn ex-with-code?
|
|
|
|
[e code]
|
|
|
|
(let [data (ex-data e)]
|
|
|
|
(= code (:code data))))
|
2020-05-14 13:49:11 +02:00
|
|
|
|
|
|
|
(defn tempfile
|
|
|
|
[source]
|
|
|
|
(let [rsc (io/resource source)
|
|
|
|
tmp (fs/create-tempfile)]
|
|
|
|
(io/copy (io/file rsc)
|
|
|
|
(io/file tmp))
|
|
|
|
tmp))
|