Add page rename tests

This commit is contained in:
Tienson Qin
2023-12-04 18:15:47 +08:00
parent f4309bdc2b
commit aff14668fc
4 changed files with 147 additions and 32 deletions

View File

@@ -136,12 +136,14 @@
(concat tx right-tx)))
conflicts))
(defn get-conflicts
[db page-id]
(let [parent-left->es (build-parent-left->es db page-id)]
(filter #(> (count (second %)) 1) parent-left->es)))
(defn loop-fix-conflicts
[repo db page-id transact-opts]
(let [get-conflicts (fn [db]
(let [parent-left->es (build-parent-left->es db page-id)]
(filter #(> (count (second %)) 1) parent-left->es)))
conflicts (get-conflicts db)
(let [conflicts (get-conflicts db page-id)
fix-conflicts-tx (when (seq conflicts)
(fix-parent-left-conflicts conflicts))]
(when (seq fix-conflicts-tx)
@@ -149,7 +151,7 @@
(util/pprint fix-conflicts-tx)
(db/transact! repo fix-conflicts-tx transact-opts)
(let [db (db/get-db repo)]
(when (seq (get-conflicts db))
(when (seq (get-conflicts db page-id))
(loop-fix-conflicts repo db page-id transact-opts))))))
(defn fix-page-if-broken!

View File

@@ -117,7 +117,7 @@
TODO: Add other options"
([title]
(create! title {}))
([title {:keys [redirect? create-first-block? format properties split-namespace? journal? uuid rename? persist-op?]
([title {:keys [redirect? create-first-block? format properties split-namespace? journal? uuid rename? persist-op? whiteboard? class?]
:or {redirect? true
create-first-block? true
rename? false
@@ -172,7 +172,7 @@
(when (seq txs)
(db/transact! repo txs {:persist-op? persist-op?})))
(when create-first-block?
(when (and create-first-block? (not (or whiteboard? class?)))
(when (or
(db/page-empty? repo (:db/id (db/entity [:block/name page-name])))
(create-title-property? repo journal? page-name))

View File

@@ -60,7 +60,7 @@
refs)]
tx-data))))
(defn based-merge-pages!
(defn- based-merge-pages!
[from-page-name to-page-name persist-op?]
(when (and (db/page-exists? from-page-name)
(db/page-exists? to-page-name)
@@ -114,30 +114,39 @@
old-page-name (util/page-name-sanity-lc old-name)
page-e (db/entity [:block/name old-page-name])
new-page-name (util/page-name-sanity-lc new-name)
new-page-e (db/entity [:block/name new-page-name])
name-changed? (not= old-name new-name)]
(if (and old-name
new-name
(not (string/blank? new-name))
name-changed?)
(cond
(= old-page-name new-page-name) ; case changed
(db/transact! repo
[{:db/id (:db/id page-e)
:block/original-name new-name}]
{:persist-op? persist-op?})
(cond
(string/blank? new-name)
(do
(notification/show! "Please use a valid name, empty name is not allowed!" :error)
:invalid-empty-name)
(and (not= old-page-name new-page-name)
(db/entity [:block/name new-page-name])) ; merge page
(based-merge-pages! old-page-name new-page-name persist-op?)
(and page-e new-page-e
(or (contains? (:block/type page-e) "whiteboard")
(contains? (:block/type new-page-e) "whiteboard")))
(do
(notification/show! "Can't merge whiteboard pages" :error)
:merge-whiteboard-pages)
:else ; rename
(page-common-handler/create! new-name
{:rename? true
:uuid (:block/uuid page-e)
:redirect? redirect?
:create-first-block? false
:persist-op? persist-op?}))
(and old-name new-name name-changed?)
(do
(cond
(= old-page-name new-page-name) ; case changed
(db/transact! repo
[{:db/id (:db/id page-e)
:block/original-name new-name}]
{:persist-op? persist-op?})
(when (string/blank? new-name)
(notification/show! "Please use a valid name, empty name is not allowed!" :error)))
(ui-handler/re-render-root!))))
(and (not= old-page-name new-page-name)
(db/entity [:block/name new-page-name])) ; merge page
(based-merge-pages! old-page-name new-page-name persist-op?)
:else ; rename
(page-common-handler/create! new-name
{:rename? true
:uuid (:block/uuid page-e)
:redirect? redirect?
:create-first-block? false
:persist-op? persist-op?}))
(ui-handler/re-render-root!))))))

View File

@@ -1 +1,105 @@
(ns frontend.handler.db-based.page-test)
(ns frontend.handler.db-based.page-test
(:require [frontend.handler.db-based.page :as db-page-handler]
[clojure.test :refer [deftest is testing use-fixtures]]
[frontend.test.helper :as test-helper]
[datascript.core :as d]
[frontend.handler.page :as page-handler]
[frontend.db :as db]
[frontend.db.fix :as db-fix]
[frontend.handler.editor :as editor-handler]))
;; FIXME: merge properties from both pages
(def repo test-helper/test-db-name-db-version)
(def init-data (test-helper/initial-test-page-and-blocks))
(def fbid (:block/uuid (second init-data)))
(defn start-and-destroy-db
[f]
(test-helper/db-based-start-and-destroy-db
f
{:init-data (fn [conn] (d/transact! conn init-data))}))
(use-fixtures :each start-and-destroy-db)
(deftest rename-test
(testing "Case change"
(let [page (db/entity [:block/name "test"])]
(db-page-handler/rename! "test" "Test")
(let [entity (db/entity [:block/name "test"])]
(is (= "Test" (:block/original-name entity)))
;; db id not changed
(is (= (:db/id page) (:db/id entity))))))
(testing "Name changed"
(let [page (db/entity [:block/name "test"])]
(db-page-handler/rename! "Test" "New name")
(let [entity (db/entity [:block/name "new name"])]
(is (= "New name" (:block/original-name entity)))
(is (= (:db/id page) (:db/id entity))))))
(testing "Merge existing page"
(page-handler/create! "Existing page" {:redirect? false :create-first-block? true})
(db-page-handler/rename! "New name" "Existing page")
(let [e1 (db/entity [:block/name "new name"])
e2 (db/entity [:block/name "existing page"])]
;; Old page deleted
(is (nil? e1))
;; Blocks from both pages have been merged
(is (= (count (:block/_page e2)) (+ 1 (dec (count init-data)))))
;; Ensure there's no conflicts
(is (empty? (db-fix/get-conflicts (db/get-db) (:db/id e2)))))))
(deftest merge-with-empty-page
(page-handler/create! "Existing page" {:redirect? false :create-first-block? false})
(db-page-handler/rename! "Test" "Existing page")
(let [e1 (db/entity [:block/name "test"])
e2 (db/entity [:block/name "existing page"])]
;; Old page deleted
(is (nil? e1))
;; Blocks from both pages have been merged
(is (= (count (:block/_page e2)) (dec (count init-data))))
;; Ensure there's no conflicts
(is (empty? (db-fix/get-conflicts (db/get-db) (:db/id e2))))))
(deftest rename-a-page-to-existing-whiteboard
(testing "Renaming a page to an existing whiteboard page"
(page-handler/create! "Whiteboard page" {:redirect? false
:whiteboard? true})
(is (= :merge-whiteboard-pages (db-page-handler/rename! "Test" "Whiteboard page")))
(is (= :merge-whiteboard-pages (db-page-handler/rename! "Whiteboard page" "Test")))))
(deftest merge-existing-pages-should-update-ref-ids
(testing "Merge existing page"
(editor-handler/save-block! repo fbid "Block 1 [[Test]]")
(page-handler/create! "Existing page" {:redirect? false :create-first-block? true})
(db-page-handler/rename! "Test" "Existing page")
(let [e1 (db/entity [:block/name "test"])
e2 (db/entity [:block/name "existing page"])]
;; Old page deleted
(is (nil? e1))
;; Blocks from both pages have been merged
(is (= (count (:block/_page e2)) (+ 1 (dec (count init-data)))))
;; Ensure there's no conflicts
(is (empty? (db-fix/get-conflicts (db/get-db) (:db/id e2))))
;; Content updated
(is (= "Block 1 [[Existing page]]" (:block/content (db/entity [:block/uuid fbid])))))))
;; TODO: full coverage
(deftest rename-namespace-pages
(testing "Rename a page to a namespaced one"
(db-page-handler/rename! "Test" "Abc/Def Ghi/Jk")
(let [e1 (db/entity [:block/name "test"])
e2 (db/entity [:block/name "abc/def ghi/jk"])
e3 (db/entity [:block/name "abc/def ghi"])
e4 (db/entity [:block/name "abc"])]
;; Old page deleted
(is (nil? e1))
;; Blocks from both pages have been merged
(is (= (count (:block/_page e2)) (dec (count init-data))))
;; Ensure there's no conflicts
(is (empty? (db-fix/get-conflicts (db/get-db) (:db/id e2))))
(is (= (:db/id e3) (:db/id (:block/namespace e2))))
(is (= (:db/id e4) (:db/id (:block/namespace e3)))))))