mirror of
https://github.com/penpot/penpot.git
synced 2025-08-07 14:38:33 +02:00
🚧 Add basic tests for project-page-update mutation.
This commit is contained in:
parent
db768f356b
commit
5d7fba1955
7 changed files with 83 additions and 37 deletions
|
@ -97,11 +97,11 @@
|
||||||
(p/then' su/constantly-nil))))
|
(p/then' su/constantly-nil))))
|
||||||
|
|
||||||
(defn- insert-page-snapshot
|
(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)
|
(let [sql "insert into project_page_snapshots (user_id, page_id, version, data, operations)
|
||||||
values ($1, $2, $3, $4, $5)
|
values ($1, $2, $3, $4, $5)
|
||||||
returning id, version, operations"]
|
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
|
;; --- Mutation: Rename Page
|
||||||
|
|
||||||
|
@ -131,7 +131,7 @@
|
||||||
;; A generic, Ops based (granular) page update method.
|
;; A generic, Ops based (granular) page update method.
|
||||||
|
|
||||||
(s/def ::operations
|
(s/def ::operations
|
||||||
(s/coll-of ::cp/opeation :kind vector?))
|
(s/coll-of vector? :kind vector?))
|
||||||
|
|
||||||
(s/def ::update-project-page
|
(s/def ::update-project-page
|
||||||
(s/keys :opt-un [::id ::user ::version ::operations]))
|
(s/keys :opt-un [::id ::user ::version ::operations]))
|
||||||
|
@ -148,8 +148,8 @@
|
||||||
|
|
||||||
(defn- update-project-page
|
(defn- update-project-page
|
||||||
[conn page params]
|
[conn page params]
|
||||||
(when (> (:version page)
|
(when (> (:version params)
|
||||||
(:version params))
|
(:version page))
|
||||||
(ex/raise :type :validation
|
(ex/raise :type :validation
|
||||||
:code :version-conflict
|
:code :version-conflict
|
||||||
:hint "The incoming version is greater that stored version."
|
:hint "The incoming version is greater that stored version."
|
||||||
|
@ -161,6 +161,7 @@
|
||||||
(cp/process-ops ops)
|
(cp/process-ops ops)
|
||||||
(blob/encode))
|
(blob/encode))
|
||||||
page (assoc page
|
page (assoc page
|
||||||
|
:user-id (:user params)
|
||||||
:data data
|
:data data
|
||||||
:version (inc (:version page))
|
:version (inc (:version page))
|
||||||
:operations (blob/encode ops))]
|
:operations (blob/encode ops))]
|
||||||
|
@ -169,7 +170,7 @@
|
||||||
(p/then (fn [s] (retrieve-lagged-operations conn s params))))))
|
(p/then (fn [s] (retrieve-lagged-operations conn s params))))))
|
||||||
|
|
||||||
(su/defstr sql:lagged-snapshots
|
(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
|
s.created_at, s.modified_at, s.user_id
|
||||||
from project_page_snapshots as s
|
from project_page_snapshots as s
|
||||||
where s.page_id = $1
|
where s.page_id = $1
|
||||||
|
|
|
@ -97,7 +97,9 @@
|
||||||
:file-id file-id
|
:file-id file-id
|
||||||
:name (str "page" i)
|
:name (str "page" i)
|
||||||
:ordering i
|
:ordering i
|
||||||
:data {}
|
:data {:shapes []
|
||||||
|
:canvas []
|
||||||
|
:shapes-by-id {}}
|
||||||
:metadata {}}))
|
:metadata {}}))
|
||||||
|
|
||||||
(defn handle-error
|
(defn handle-error
|
||||||
|
|
|
@ -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 <niwi@niwi.nz>
|
|
||||||
|
|
||||||
(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))))
|
|
||||||
|
|
||||||
|
|
|
@ -75,6 +75,56 @@
|
||||||
(t/is (= (:id data) (get-in out [:result :id])))
|
(t/is (= (:id data) (get-in out [:result :id])))
|
||||||
(t/is (= 1 (get-in out [:result :version])))))
|
(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
|
(t/deftest mutation-delete-project-page
|
||||||
(let [user @(th/create-user db/pool 1)
|
(let [user @(th/create-user db/pool 1)
|
||||||
proj @(th/create-project db/pool (:id user) 1)
|
proj @(th/create-project db/pool (:id user) 1)
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
;;
|
;;
|
||||||
;; Copyright (c) 2016-2019 Andrey Antukh <niwi@niwi.nz>
|
;; Copyright (c) 2016-2019 Andrey Antukh <niwi@niwi.nz>
|
||||||
|
|
||||||
(ns uxbox.util.exceptions
|
(ns uxbox.common.exceptions
|
||||||
"A helpers for work with exceptions."
|
"A helpers for work with exceptions."
|
||||||
(:require [clojure.spec.alpha :as s]))
|
(:require [clojure.spec.alpha :as s]))
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@
|
||||||
::background
|
::background
|
||||||
::background-opacity]))
|
::background-opacity]))
|
||||||
|
|
||||||
(s/def ::opeation
|
(s/def ::operation
|
||||||
(s/or :mod-shape (s/cat :name #(= % :mod-shape)
|
(s/or :mod-shape (s/cat :name #(= % :mod-shape)
|
||||||
:id uuid?
|
:id uuid?
|
||||||
:attr keyword?
|
:attr keyword?
|
||||||
|
@ -60,6 +60,10 @@
|
||||||
:del-shape (s/cat :name #(= % :del-shape)
|
:del-shape (s/cat :name #(= % :del-shape)
|
||||||
:id uuid?)))
|
:id uuid?)))
|
||||||
|
|
||||||
|
(s/def ::operations
|
||||||
|
(s/coll-of ::operation :kind vector?))
|
||||||
|
|
||||||
|
|
||||||
;; --- Operations Processing Impl
|
;; --- Operations Processing Impl
|
||||||
|
|
||||||
(declare process-operation)
|
(declare process-operation)
|
||||||
|
@ -69,7 +73,8 @@
|
||||||
|
|
||||||
(defn process-ops
|
(defn process-ops
|
||||||
[data operations]
|
[data operations]
|
||||||
(reduce process-operation data operations))
|
(->> (cs/conform ::operations operations)
|
||||||
|
(reduce process-operation data)))
|
||||||
|
|
||||||
(defn- process-operation
|
(defn- process-operation
|
||||||
[data operation]
|
[data operation]
|
||||||
|
|
|
@ -6,9 +6,11 @@
|
||||||
|
|
||||||
(ns uxbox.common.spec
|
(ns uxbox.common.spec
|
||||||
(:require
|
(:require
|
||||||
|
#?(:clj [datoteka.core :as fs])
|
||||||
[clojure.spec.alpha :as s]
|
[clojure.spec.alpha :as s]
|
||||||
[cuerdas.core :as str]
|
[cuerdas.core :as str]
|
||||||
#?(:clj [datoteka.core :as fs])))
|
[expound.alpha :as expound]
|
||||||
|
[uxbox.common.exceptions :as ex]))
|
||||||
|
|
||||||
(s/check-asserts true)
|
(s/check-asserts true)
|
||||||
|
|
||||||
|
@ -20,8 +22,18 @@
|
||||||
(def uuid-rx
|
(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}$")
|
#"^[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
|
;; --- Public API
|
||||||
#"^[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$")
|
|
||||||
|
(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
|
;; --- Predicates
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue