From 5d7fba1955228a83f604f9a2a1c0e0a81f9c4ea6 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sat, 14 Dec 2019 23:07:00 +0100 Subject: [PATCH] :construction: Add basic tests for project-page-update mutation. --- .../services/mutations/project_pages.clj | 13 ++--- backend/test/uxbox/tests/helpers.clj | 4 +- backend/test/uxbox/tests/main.clj | 24 --------- .../tests/test_services_project_pages.clj | 50 +++++++++++++++++++ common/uxbox/common/exceptions.cljc | 2 +- common/uxbox/common/pages.cljc | 9 +++- common/uxbox/common/spec.cljc | 18 +++++-- 7 files changed, 83 insertions(+), 37 deletions(-) delete mode 100644 backend/test/uxbox/tests/main.clj diff --git a/backend/src/uxbox/services/mutations/project_pages.clj b/backend/src/uxbox/services/mutations/project_pages.clj index 5080383f70..191f654ca2 100644 --- a/backend/src/uxbox/services/mutations/project_pages.clj +++ b/backend/src/uxbox/services/mutations/project_pages.clj @@ -97,11 +97,11 @@ (p/then' su/constantly-nil)))) (defn- insert-page-snapshot - [conn {:keys [user id version data operations]}] + [conn {:keys [user-id id version data operations]}] (let [sql "insert into project_page_snapshots (user_id, page_id, version, data, operations) values ($1, $2, $3, $4, $5) returning id, version, operations"] - (db/query-one conn [sql user id version data operations]))) + (db/query-one conn [sql user-id id version data operations]))) ;; --- Mutation: Rename Page @@ -131,7 +131,7 @@ ;; A generic, Ops based (granular) page update method. (s/def ::operations - (s/coll-of ::cp/opeation :kind vector?)) + (s/coll-of vector? :kind vector?)) (s/def ::update-project-page (s/keys :opt-un [::id ::user ::version ::operations])) @@ -148,8 +148,8 @@ (defn- update-project-page [conn page params] - (when (> (:version page) - (:version params)) + (when (> (:version params) + (:version page)) (ex/raise :type :validation :code :version-conflict :hint "The incoming version is greater that stored version." @@ -161,6 +161,7 @@ (cp/process-ops ops) (blob/encode)) page (assoc page + :user-id (:user params) :data data :version (inc (:version page)) :operations (blob/encode ops))] @@ -169,7 +170,7 @@ (p/then (fn [s] (retrieve-lagged-operations conn s params)))))) (su/defstr sql:lagged-snapshots - "select s.id, s.version, s.operations, + "select s.id, s.page_id, s.version, s.operations, s.created_at, s.modified_at, s.user_id from project_page_snapshots as s where s.page_id = $1 diff --git a/backend/test/uxbox/tests/helpers.clj b/backend/test/uxbox/tests/helpers.clj index ea2bda3165..2f1a8fcbff 100644 --- a/backend/test/uxbox/tests/helpers.clj +++ b/backend/test/uxbox/tests/helpers.clj @@ -97,7 +97,9 @@ :file-id file-id :name (str "page" i) :ordering i - :data {} + :data {:shapes [] + :canvas [] + :shapes-by-id {}} :metadata {}})) (defn handle-error diff --git a/backend/test/uxbox/tests/main.clj b/backend/test/uxbox/tests/main.clj deleted file mode 100644 index 00a4975eb8..0000000000 --- a/backend/test/uxbox/tests/main.clj +++ /dev/null @@ -1,24 +0,0 @@ -;; This Source Code Form is subject to the terms of the Mozilla Public -;; License, v. 2.0. If a copy of the MPL was not distributed with this -;; file, You can obtain one at http://mozilla.org/MPL/2.0/. -;; -;; Copyright (c) 2019 Andrey Antukh - -(ns uxbox.tests.main - (:require [clojure.test :as test])) - -(defn -main - [& args] - ;; (require 'uxbox.tests.test-projects) - ;; (require 'uxbox.tests.test-pages) - ;; (require 'uxbox.tests.test-images) - ;; (require 'uxbox.tests.test-icons) - (require 'uxbox.tests.test-users) - (require 'uxbox.tests.test-auth) - ;; (require 'uxbox.tests.test-kvstore) - (let [{:keys [fail]} (test/run-all-tests #"^uxbox.tests.*")] - (if (pos? fail) - (System/exit fail) - (System/exit 0)))) - - diff --git a/backend/test/uxbox/tests/test_services_project_pages.clj b/backend/test/uxbox/tests/test_services_project_pages.clj index 2a1feacb81..2c8036a7db 100644 --- a/backend/test/uxbox/tests/test_services_project_pages.clj +++ b/backend/test/uxbox/tests/test_services_project_pages.clj @@ -75,6 +75,56 @@ (t/is (= (:id data) (get-in out [:result :id]))) (t/is (= 1 (get-in out [:result :version]))))) +(t/deftest mutation-update-project-page-1 + (let [user @(th/create-user db/pool 1) + proj @(th/create-project db/pool (:id user) 1) + file @(th/create-project-file db/pool (:id user) (:id proj) 1) + page @(th/create-project-page db/pool (:id user) (:id file) 1) + + data {::sm/type :update-project-page + :id (:id page) + :version 99 + :user (:id user) + :operations []} + + out (th/try-on! (sm/handle data))] + + ;; (th/print-result! out) + + (let [error (:error out)] + (t/is (th/ex-info? error)) + (t/is (th/ex-of-type? error :service-error))) + + (let [error (ex-cause (:error out))] + (t/is (th/ex-info? error)) + (t/is (th/ex-of-type? error :validation)) + (t/is (th/ex-of-code? error :version-conflict))))) + +(t/deftest mutation-update-project-page-2 + (let [user @(th/create-user db/pool 1) + proj @(th/create-project db/pool (:id user) 1) + file @(th/create-project-file db/pool (:id user) (:id proj) 1) + page @(th/create-project-page db/pool (:id user) (:id file) 1) + + sid (uuid/next) + data {::sm/type :update-project-page + :id (:id page) + :version 0 + :user (:id user) + :operations [[:add-shape sid {:id sid :type :rect}]]} + + out (th/try-on! (sm/handle data))] + + ;; (th/print-result! out) + (t/is (nil? (:error out))) + (t/is (= 1 (count (:result out)))) + (t/is (= (:id data) (get-in out [:result 0 :page-id]))) + (t/is (= 1 (count (get-in out [:result 0 :operations])))) + (t/is (= :add-shape (get-in out [:result 0 :operations 0 0]))) + (t/is (= sid (get-in out [:result 0 :operations 0 1]))) + )) + + (t/deftest mutation-delete-project-page (let [user @(th/create-user db/pool 1) proj @(th/create-project db/pool (:id user) 1) diff --git a/common/uxbox/common/exceptions.cljc b/common/uxbox/common/exceptions.cljc index 17c7cf3b8c..5d1ca920ab 100644 --- a/common/uxbox/common/exceptions.cljc +++ b/common/uxbox/common/exceptions.cljc @@ -4,7 +4,7 @@ ;; ;; Copyright (c) 2016-2019 Andrey Antukh -(ns uxbox.util.exceptions +(ns uxbox.common.exceptions "A helpers for work with exceptions." (:require [clojure.spec.alpha :as s])) diff --git a/common/uxbox/common/pages.cljc b/common/uxbox/common/pages.cljc index 3eee32b886..8c2964467e 100644 --- a/common/uxbox/common/pages.cljc +++ b/common/uxbox/common/pages.cljc @@ -49,7 +49,7 @@ ::background ::background-opacity])) -(s/def ::opeation +(s/def ::operation (s/or :mod-shape (s/cat :name #(= % :mod-shape) :id uuid? :attr keyword? @@ -60,6 +60,10 @@ :del-shape (s/cat :name #(= % :del-shape) :id uuid?))) +(s/def ::operations + (s/coll-of ::operation :kind vector?)) + + ;; --- Operations Processing Impl (declare process-operation) @@ -69,7 +73,8 @@ (defn process-ops [data operations] - (reduce process-operation data operations)) + (->> (cs/conform ::operations operations) + (reduce process-operation data))) (defn- process-operation [data operation] diff --git a/common/uxbox/common/spec.cljc b/common/uxbox/common/spec.cljc index 84dab7391a..250ffbb16c 100644 --- a/common/uxbox/common/spec.cljc +++ b/common/uxbox/common/spec.cljc @@ -6,9 +6,11 @@ (ns uxbox.common.spec (:require + #?(:clj [datoteka.core :as fs]) [clojure.spec.alpha :as s] [cuerdas.core :as str] - #?(:clj [datoteka.core :as fs]))) + [expound.alpha :as expound] + [uxbox.common.exceptions :as ex])) (s/check-asserts true) @@ -20,8 +22,18 @@ (def uuid-rx #"^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$") -(def number-rx - #"^[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$") +;; --- Public API + +(defn conform + [spec data] + (let [result (s/conform spec data)] + (when (= result ::s/invalid) + (ex/raise :type :validation + :code :spec-validation + :explain (with-out-str + (expound/printer data)) + :data (::s/problems data))) + result)) ;; --- Predicates