|
|
@@ -1,5 +1,5 @@
|
|
|
(ns logseq.outliner.validate-test
|
|
|
- (:require [cljs.test :refer [deftest is]]
|
|
|
+ (:require [cljs.test :refer [deftest is are testing]]
|
|
|
[datascript.core :as d]
|
|
|
[logseq.outliner.validate :as outliner-validate]
|
|
|
[logseq.db.test.helper :as db-test]))
|
|
|
@@ -70,20 +70,62 @@
|
|
|
(find-block-by-content conn "Fruit")))
|
|
|
"Allow class to have same name as a page")))
|
|
|
|
|
|
+(deftest validate-parent-property
|
|
|
+ (let [conn (db-test/create-conn-with-blocks
|
|
|
+ {:properties {:prop1 {:block/schema {:type :default}}}
|
|
|
+ :classes {:Class1 {} :Class2 {}}
|
|
|
+ :pages-and-blocks
|
|
|
+ [{:page {:block/title "page1"}}
|
|
|
+ {:page {:block/title "page2"}}]})
|
|
|
+ page1 (find-block-by-content conn "page1")
|
|
|
+ page2 (find-block-by-content conn "page2")
|
|
|
+ class1 (find-block-by-content conn "Class1")
|
|
|
+ class2 (find-block-by-content conn "Class2")
|
|
|
+ property (find-block-by-content conn "prop1")]
|
|
|
+
|
|
|
+ (testing "valid parent and child combinations"
|
|
|
+ (is (nil? (outliner-validate/validate-parent-property page1 [page2]))
|
|
|
+ "parent page to child page is valid")
|
|
|
+ (is (nil? (outliner-validate/validate-parent-property class1 [class2]))
|
|
|
+ "parent class to child class is valid"))
|
|
|
+
|
|
|
+ (testing "invalid parent and child combinations"
|
|
|
+ (are [parent child]
|
|
|
+ (thrown-with-msg?
|
|
|
+ js/Error
|
|
|
+ #"Can't set"
|
|
|
+ (outliner-validate/validate-parent-property parent [child]))
|
|
|
+
|
|
|
+ class1 page1
|
|
|
+ page1 class1
|
|
|
+ property page1
|
|
|
+ property class1))))
|
|
|
+
|
|
|
+;; Try as many of the validations against a new graph to confirm
|
|
|
+;; that validations make sense and are valid for a new graph
|
|
|
(deftest new-graph-should-be-valid
|
|
|
- (let [conn (db-test/create-conn)
|
|
|
- pages (d/q '[:find [(pull ?b [*]) ...] :where [?b :block/title] [?b :block/type]] @conn)
|
|
|
- validation-errors (atom {})]
|
|
|
- (doseq [page pages]
|
|
|
- (try
|
|
|
- ;; Try as many of the relevant validations
|
|
|
- (outliner-validate/validate-unique-by-name-tag-and-block-type @conn (:block/title page) page)
|
|
|
- (outliner-validate/validate-page-title (:block/title page) {:node page})
|
|
|
- (outliner-validate/validate-page-title-characters (:block/title page) {:node page})
|
|
|
-
|
|
|
- (catch :default e
|
|
|
- (if (= :notification (:type (ex-data e)))
|
|
|
- (swap! validation-errors update (select-keys page [:block/title :db/ident :block/uuid]) (fnil conj []) e)
|
|
|
- (throw e)))))
|
|
|
- (is (= {} @validation-errors)
|
|
|
- "Default pages shouldn't have any validation errors")))
|
|
|
+ (let [conn (db-test/create-conn)]
|
|
|
+
|
|
|
+ (testing "Validate pages"
|
|
|
+ (let [pages (d/q '[:find [(pull ?b [*]) ...] :where [?b :block/title] [?b :block/type]] @conn)
|
|
|
+ page-errors (atom {})]
|
|
|
+ (doseq [page pages]
|
|
|
+ (try
|
|
|
+ (outliner-validate/validate-unique-by-name-tag-and-block-type @conn (:block/title page) page)
|
|
|
+ (outliner-validate/validate-page-title (:block/title page) {:node page})
|
|
|
+ (outliner-validate/validate-page-title-characters (:block/title page) {:node page})
|
|
|
+
|
|
|
+ (catch :default e
|
|
|
+ (if (= :notification (:type (ex-data e)))
|
|
|
+ (swap! page-errors update (select-keys page [:block/title :db/ident :block/uuid]) (fnil conj []) e)
|
|
|
+ (throw e)))))
|
|
|
+ (is (= {} @page-errors)
|
|
|
+ "Default pages shouldn't have any validation errors")))
|
|
|
+
|
|
|
+ (testing "Validate property relationships"
|
|
|
+ (let [parent-child-pairs (d/q '[:find (pull ?parent [:block/title :block/type])
|
|
|
+ (pull ?child [:block/title :block/type])
|
|
|
+ :where [?child :logseq.property/parent ?parent]] @conn)]
|
|
|
+ (doseq [[parent child] parent-child-pairs]
|
|
|
+ (is (nil? (outliner-validate/validate-parent-property parent [child]))
|
|
|
+ (str "Parent and child page is valid: " (pr-str (:block/title parent)) " " (pr-str (:block/title child)))))))))
|