Browse Source

enhance(dev): cljs tests can run with or without repeatable idents

Gabriel Horner 5 months ago
parent
commit
28675be54b

+ 1 - 1
.github/workflows/graph-parser.yml

@@ -76,7 +76,7 @@ jobs:
         run: yarn install --frozen-lockfile
 
       - name: Run ClojureScript tests
-        run: clojure -M:test
+        run: REPEATABLE_IDENTS=true clojure -M:test
 
       - name: Run nbb-logseq tests
         run: yarn test

+ 4 - 3
deps/db/src/logseq/db/frontend/db_ident.cljc

@@ -58,7 +58,8 @@
 
 (defn create-db-ident-from-name
   "Creates a :db/ident for a class or property by sanitizing the given name.
-  The created ident should obey clojure's rules for keywords.
+  The created ident must obey clojure's rules for keywords i.e.
+  be a valid symbol per https://clojure.org/reference/reader#_symbols
 
    NOTE: Only use this when creating a db-ident for a new class/property. Using
    this in read-only contexts like querying can result in db-ident conflicts"
@@ -66,8 +67,8 @@
   {:pre [(or (keyword? user-namespace) (string? user-namespace)) (string? name-string)]}
   (assert (not (re-find #"^(logseq|block)(\.|$)" (name user-namespace)))
           "New ident is not allowed to use an internal namespace")
-  (if #?(:org.babashka/nbb (some? js/process)
-         :cljs (exists? js/process)
+  (if #?(:org.babashka/nbb true
+         :cljs js/process.env.REPEATABLE_IDENTS
          :default false)
     ;; Used for contexts where we want repeatable idents e.g. tests and CLIs
     (keyword user-namespace (-> name-string (string/replace #"[/()]|\s+" "-") (string/replace-first #"^(\d)" "NUM-$1")))

+ 10 - 0
deps/db/test/logseq/db_test.cljs

@@ -83,3 +83,13 @@
     (is (= nil
            (ldb/page-exists? @conn "movie" #{:logseq.class/Property}))
         "Class pages correctly not found for given class")))
+
+(deftest get-classes-with-property-test
+  (let [conn (db-test/create-conn-with-blocks
+              {:properties {:prop1 {:logseq.property/type :default}}
+               :classes
+               {:Class1 {:build/class-properties [:prop1]}
+                :Class2 {:build/class-properties [:prop1]}}})
+        classes (ldb/get-classes-with-property @conn :user.property/prop1)]
+    (is (= ["Class1" "Class2"]
+           (map :block/title classes)))))

+ 1 - 1
deps/graph-parser/README.md

@@ -56,7 +56,7 @@ $ yarn test -i focus
 
 ClojureScript tests use https://github.com/Olical/cljs-test-runner. To run tests:
 ```
-clojure -M:test
+REPEATABLE_IDENTS=true clojure -M:test
 ```
 
 To see available options that can run specific tests or namespaces: `clojure -M:test --help`

+ 1 - 15
src/test/frontend/db/db_based_model_test.cljs

@@ -2,12 +2,10 @@
   (:require [cljs.test :refer [use-fixtures deftest is testing]]
             [datascript.core :as d]
             [frontend.db :as db]
-            [frontend.db.conn :as conn]
             [frontend.db.model :as model]
             [frontend.test.helper :as test-helper]
             [logseq.db :as ldb]
-            [logseq.db.frontend.class :as db-class]
-            [logseq.db.test.helper :as db-test]))
+            [logseq.db.frontend.class :as db-class]))
 
 (def repo test-helper/test-db-name-db-version)
 
@@ -54,18 +52,6 @@
              [(:db/id (db/entity [:block/uuid fbid]))
               (:db/id (db/entity [:block/uuid sbid]))])))))
 
-(deftest get-classes-with-property-test
-  (let [conn (db-test/create-conn-with-blocks
-              {:properties {:prop1 {:logseq.property/type :default}}
-               :classes
-               {:Class1 {:build/class-properties [:prop1]}
-                :Class2 {:build/class-properties [:prop1]}}})
-        property (d/entity @conn :user.property/prop1)
-        classes (with-redefs [conn/get-db (constantly @conn)]
-                  (model/get-classes-with-property (:db/ident property)))]
-    (is (= ["Class1" "Class2"]
-           (map :block/title classes)))))
-
 (deftest hidden-page-test
   (let [opts {:redirect? false :create-first-block? false}
         _ (test-helper/create-page! "page 1" opts)]

+ 1 - 1
src/test/frontend/worker/handler/page/db_based/page_test.cljs

@@ -18,7 +18,7 @@
 
 (deftest create-namespace-pages
   (let [conn (db-test/create-conn-with-blocks
-              {:properties {:property1 {:logseq.property/type :default}}
+              {:properties {:user.property/property1 {:logseq.property/type :default}}
                :classes {:class1 {}}
                :pages-and-blocks [{:page {:block/title "page1"}}]})]