From 4a395ec76134f67d2299d5f642dfc4e485d805b5 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 25 Nov 2019 15:56:49 +0100 Subject: [PATCH] :bug: More test fixes. --- backend/src/uxbox/services/pages.clj | 2 +- backend/test/uxbox/tests/helpers.clj | 10 ++- .../{test_auth.clj => test_services_auth.clj} | 2 +- ..._kvstore.clj => test_services_kvstore.clj} | 2 +- ...test_pages.clj => test_services_pages.clj} | 21 ++--- ...rojects.clj => test_services_projects.clj} | 2 +- backend/test/uxbox/tests/test_svgparse.clj | 90 ------------------- backend/test/uxbox/tests/test_util_svg.clj | 52 +++++++++++ 8 files changed, 76 insertions(+), 105 deletions(-) rename backend/test/uxbox/tests/{test_auth.clj => test_services_auth.clj} (97%) rename backend/test/uxbox/tests/{test_kvstore.clj => test_services_kvstore.clj} (98%) rename backend/test/uxbox/tests/{test_pages.clj => test_services_pages.clj} (91%) rename backend/test/uxbox/tests/{test_projects.clj => test_services_projects.clj} (98%) delete mode 100644 backend/test/uxbox/tests/test_svgparse.clj create mode 100644 backend/test/uxbox/tests/test_util_svg.clj diff --git a/backend/src/uxbox/services/pages.clj b/backend/src/uxbox/services/pages.clj index 0fb887675..00667307e 100644 --- a/backend/src/uxbox/services/pages.clj +++ b/backend/src/uxbox/services/pages.clj @@ -142,7 +142,7 @@ ;; --- Mutation: Update Page Metadata (s/def ::update-page-metadata - (s/keys :req-un [::data ::user ::project-id ::name ::metadata ::id])) + (s/keys :req-un [::user ::project-id ::name ::metadata ::id])) (sv/defmutation :update-page-metadata {:doc "Update an existing page." diff --git a/backend/test/uxbox/tests/helpers.clj b/backend/test/uxbox/tests/helpers.clj index 79d225375..3abfcd726 100644 --- a/backend/test/uxbox/tests/helpers.clj +++ b/backend/test/uxbox/tests/helpers.clj @@ -137,9 +137,17 @@ {:error (handle-error e#) :result nil}))) +(defmacro try! + [expr] + `(try + {:error nil + :result ~expr} + (catch Exception e# + {:error (handle-error e#) + :result nil}))) + (defn print-result! [{:keys [error result]}] - (if error (do (println "====> START ERROR") diff --git a/backend/test/uxbox/tests/test_auth.clj b/backend/test/uxbox/tests/test_services_auth.clj similarity index 97% rename from backend/test/uxbox/tests/test_auth.clj rename to backend/test/uxbox/tests/test_services_auth.clj index fbec7fef0..50541b13b 100644 --- a/backend/test/uxbox/tests/test_auth.clj +++ b/backend/test/uxbox/tests/test_services_auth.clj @@ -4,7 +4,7 @@ ;; ;; Copyright (c) 2019 Andrey Antukh -(ns uxbox.tests.test-auth +(ns uxbox.tests.test-services-auth (:require [clojure.test :as t] [promesa.core :as p] diff --git a/backend/test/uxbox/tests/test_kvstore.clj b/backend/test/uxbox/tests/test_services_kvstore.clj similarity index 98% rename from backend/test/uxbox/tests/test_kvstore.clj rename to backend/test/uxbox/tests/test_services_kvstore.clj index d6f50c242..f830effe2 100644 --- a/backend/test/uxbox/tests/test_kvstore.clj +++ b/backend/test/uxbox/tests/test_services_kvstore.clj @@ -1,4 +1,4 @@ -(ns uxbox.tests.test-kvstore +(ns uxbox.tests.test-services-kvstore (:require [clojure.spec.alpha :as s] [clojure.test :as t] diff --git a/backend/test/uxbox/tests/test_pages.clj b/backend/test/uxbox/tests/test_services_pages.clj similarity index 91% rename from backend/test/uxbox/tests/test_pages.clj rename to backend/test/uxbox/tests/test_services_pages.clj index 9e01140ef..678a29101 100644 --- a/backend/test/uxbox/tests/test_pages.clj +++ b/backend/test/uxbox/tests/test_services_pages.clj @@ -1,4 +1,4 @@ -(ns uxbox.tests.test-pages +(ns uxbox.tests.test-services-pages (:require [clojure.spec.alpha :as s] [clojure.test :as t] @@ -20,13 +20,14 @@ :project-id (:id proj) :name "test page" :user (:id user)} - [err rsp] (th/try-on (sv/mutation data))] - (t/is (nil? err)) - (t/is (uuid? (:id rsp))) - (t/is (= (:user data) (:user-id rsp))) - (t/is (= (:name data) (:name rsp))) - (t/is (= (:data data) (:data rsp))) - (t/is (= (:metadata data) (:metadata rsp))))) + res (th/try-on! (sv/mutation data))] + (t/is (nil? (:error res))) + (t/is (uuid? (get-in res [:result :id]))) + (let [rsp (:result res)] + (t/is (= (:user data) (:user-id rsp))) + (t/is (= (:name data) (:name rsp))) + (t/is (= (:data data) (:data rsp))) + (t/is (= (:metadata data) (:metadata rsp)))))) (t/deftest test-mutation-update-page (let [user @(th/create-user db/pool 1) @@ -54,7 +55,7 @@ (let [user @(th/create-user db/pool 1) proj @(th/create-project db/pool (:id user) 1) page @(th/create-page db/pool (:id user) (:id proj) 1) - data {::sv/type :update-page + data {::sv/type :update-page-metadata :id (:id page) :metadata {:foo 2} :project-id (:id proj) @@ -91,7 +92,7 @@ :user (:id user)} res (th/try-on! (sv/query data))] - (th/print-result! res) + ;; (th/print-result! res) (t/is (nil? (:error res))) (t/is (vector? (:result res))) (t/is (= 1 (count (:result res)))) diff --git a/backend/test/uxbox/tests/test_projects.clj b/backend/test/uxbox/tests/test_services_projects.clj similarity index 98% rename from backend/test/uxbox/tests/test_projects.clj rename to backend/test/uxbox/tests/test_services_projects.clj index b76acfd84..6440436e7 100644 --- a/backend/test/uxbox/tests/test_projects.clj +++ b/backend/test/uxbox/tests/test_services_projects.clj @@ -1,4 +1,4 @@ -(ns uxbox.tests.test-projects +(ns uxbox.tests.test-services-projects (:require [clojure.test :as t] [promesa.core :as p] diff --git a/backend/test/uxbox/tests/test_svgparse.clj b/backend/test/uxbox/tests/test_svgparse.clj deleted file mode 100644 index 40e719b55..000000000 --- a/backend/test/uxbox/tests/test_svgparse.clj +++ /dev/null @@ -1,90 +0,0 @@ -(ns uxbox.tests.test-svgparse - #_(:require [clojure.test :as t] - [clojure.java.io :as io] - [uxbox.http :as http] - [uxbox.services :as usv] - [uxbox.services.svgparse :as svg] - [uxbox.tests.helpers :as th])) - -;; (t/use-fixtures :once th/state-init) - -;; (t/deftest parse-svg-test -;; (t/testing "parsing valid svg 1" -;; (let [image (slurp (io/resource "uxbox/tests/_files/sample1.svg")) -;; result (svg/parse-string image)] -;; (t/is (contains? result :width)) -;; (t/is (contains? result :height)) -;; (t/is (contains? result :view-box)) -;; (t/is (contains? result :name)) -;; (t/is (contains? result :content)) -;; (t/is (= 500.0 (:width result))) -;; (t/is (= 500.0 (:height result))) -;; (t/is (= [0.0 0.0 500.00001 500.00001] (:view-box result))) -;; (t/is (= "lock.svg" (:name result))))) - -;; (t/testing "parsing valid svg 2" -;; (let [image (slurp (io/resource "uxbox/tests/_files/sample2.svg")) -;; result (svg/parse-string image)] -;; (t/is (contains? result :width)) -;; (t/is (contains? result :height)) -;; (t/is (contains? result :view-box)) -;; (t/is (contains? result :name)) -;; (t/is (contains? result :content)) -;; (t/is (= 500.0 (:width result))) -;; (t/is (= 500.0 (:height result))) -;; (t/is (= [0.0 0.0 500.0 500.00001] (:view-box result))) -;; (t/is (= "play.svg" (:name result))))) - -;; (t/testing "parsing invalid data 1" -;; (let [image (slurp (io/resource "uxbox/tests/_files/sample.jpg")) -;; [e result] (th/try-on (svg/parse-string image))] -;; (t/is (th/exception? e)) -;; (t/is (th/ex-info? e)) -;; (t/is (th/ex-with-code? e :uxbox.services.svgparse/invalid-input)))) - -;; (t/testing "parsing invalid data 2" -;; (let [[e result] (th/try-on (svg/parse-string ""))] -;; (t/is (th/exception? e)) -;; (t/is (th/ex-info? e)) -;; (t/is (th/ex-with-code? e :uxbox.services.svgparse/invalid-input)))) - -;; (t/testing "parsing invalid data 3" -;; (let [[e result] (th/try-on (svg/parse-string ""))] -;; (t/is (th/exception? e)) -;; (t/is (th/ex-info? e)) -;; (t/is (th/ex-with-code? e :uxbox.services.svgparse/invalid-result)))) - -;; ;; (t/testing "valid http request" -;; ;; (with-open [conn (db/connection)] -;; ;; (let [image (slurp (io/resource "uxbox/tests/_files/sample2.svg")) -;; ;; path "/api/svg/parse" -;; ;; user (th/create-user conn 1)] -;; ;; (th/with-server {:handler @http/app} -;; ;; (let [rsp (th/request {:method :post -;; ;; :path path -;; ;; :body image -;; ;; :raw? true})] -;; ;; (t/is (= 200 (:status rsp))) -;; ;; (prn "RESPONSE" rsp) -;; ;; ;; (t/is (contains? (:body rsp) :width)) -;; ;; ;; (t/is (contains? (:body rsp) :height)) -;; ;; ;; (t/is (contains? (:body rsp) :view-box)) -;; ;; ;; (t/is (contains? (:body rsp) :name)) -;; ;; ;; (t/is (contains? (:body rsp) :content)) -;; ;; ;; (t/is (= 500.0 (:width (:body rsp)))) -;; ;; #_(t/is (= 500.0 (:height (:body rsp)))) -;; ;; #_(t/is (= [0.0 0.0 500.0 500.00001] (:view-box (:body rsp)))) -;; ;; #_(t/is (= "play.svg" (:name (:body rsp)))))))) - -;; ;; (t/testing "invalid http request" -;; ;; (let [path "/api/svg/parse" -;; ;; image ""] -;; ;; (with-server {:handler (uft/routes)} -;; ;; (let [rsp (th/request {:method :post -;; ;; :path path -;; ;; :body image -;; ;; :raw? true})] -;; ;; (t/is (= 400 (:status rsp))) -;; ;; (t/is (= :validation (get-in rsp [:body :type]))) -;; ;; (t/is (= ::svg/invalid-result (get-in rsp [:body :code]))))))) -;; ) diff --git a/backend/test/uxbox/tests/test_util_svg.clj b/backend/test/uxbox/tests/test_util_svg.clj new file mode 100644 index 000000000..a58c2c6f6 --- /dev/null +++ b/backend/test/uxbox/tests/test_util_svg.clj @@ -0,0 +1,52 @@ +(ns uxbox.tests.test-util-svg + (:require + [clojure.test :as t] + [clojure.java.io :as io] + [uxbox.http :as http] + [uxbox.util.svg :as svg] + [uxbox.tests.helpers :as th])) + +(t/deftest parse-svg-1 + (let [result (-> (io/resource "uxbox/tests/_files/sample1.svg") + (svg/parse))] + (t/is (contains? result :width)) + (t/is (contains? result :height)) + (t/is (contains? result :view-box)) + (t/is (contains? result :name)) + (t/is (contains? result :content)) + (t/is (= 500.0 (:width result))) + (t/is (= 500.0 (:height result))) + (t/is (= [0.0 0.0 500.00001 500.00001] (:view-box result))) + (t/is (= "lock.svg" (:name result))))) + +(t/deftest parse-svg-2 + (let [result (-> (io/resource "uxbox/tests/_files/sample2.svg") + (svg/parse))] + (t/is (contains? result :width)) + (t/is (contains? result :height)) + (t/is (contains? result :view-box)) + (t/is (contains? result :name)) + (t/is (contains? result :content)) + (t/is (= 500.0 (:width result))) + (t/is (= 500.0 (:height result))) + (t/is (= [0.0 0.0 500.0 500.00001] (:view-box result))) + (t/is (= "play.svg" (:name result))))) + +(t/deftest parse-invalid-svg-1 + (let [image (io/resource "uxbox/tests/_files/sample.jpg") + result (th/try! (svg/parse image))] + (t/is (map? (:error result))) + (t/is (= (get-in result [:error :code]) + ::svg/invalid-input)))) + +(t/deftest parse-invalid-svg-2 + (let [result (th/try! (svg/parse-string ""))] + (t/is (map? (:error result))) + (t/is (= (get-in result [:error :code]) + ::svg/invalid-input)))) + +(t/deftest parse-invalid-svg-3 + (let [result (th/try! (svg/parse-string ""))] + (t/is (map? (:error result))) + (t/is (= (get-in result [:error :code]) + ::svg/invalid-result))))