mirror of
https://github.com/penpot/penpot.git
synced 2025-02-08 08:09:14 -05: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))))
|
||||
|
||||
(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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 (= 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)
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
;;
|
||||
;; Copyright (c) 2016-2019 Andrey Antukh <niwi@niwi.nz>
|
||||
|
||||
(ns uxbox.util.exceptions
|
||||
(ns uxbox.common.exceptions
|
||||
"A helpers for work with exceptions."
|
||||
(:require [clojure.spec.alpha :as s]))
|
||||
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue