Skip to content

Commit

Permalink
tests for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Jul 31, 2023
1 parent 6e447dd commit 25b2de2
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 137 deletions.
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
}

:test {
:extra-paths ["test" "test_storage"]
:extra-paths ["test"]
:extra-deps {
org.clojure/clojurescript {:mvn/version "1.10.520"}
metosin/jsonista {:mvn/version "0.3.3"}
Expand Down
2 changes: 1 addition & 1 deletion dev/user.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require
[clojure.tools.namespace.repl :as ns]))

(ns/set-refresh-dirs "src" "bench" "test" "test_storage" #_"bench_datomic" #_"test_datomic")
(ns/set-refresh-dirs "src" "bench" "test" #_"bench_datomic" #_"test_datomic")

(defn reload []
(set! *warn-on-reflection* true)
Expand Down
3 changes: 3 additions & 0 deletions src/datascript/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -847,3 +847,6 @@
All options are optional."
storage/file-storage))

(defn settings [db]
(set/settings (:eavt db)))
7 changes: 4 additions & 3 deletions src/datascript/storage.clj
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@
:aevt aevt-addr
:avet avet-addr}
(set/settings (:eavt db)))]
(vswap! *store-buffer* conj! [root-addr meta])
(vswap! *store-buffer* conj! [tail-addr []])
(-store (:storage adapter) (persistent! @*store-buffer*))
(when (pos? (count @*store-buffer*))
(vswap! *store-buffer* conj! [root-addr meta])
(vswap! *store-buffer* conj! [tail-addr []])
(-store (:storage adapter) (persistent! @*store-buffer*)))
db)))

(defn store!
Expand Down
3 changes: 2 additions & 1 deletion test/datascript/test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
datascript.test.query-rules
datascript.test.query-v3
datascript.test.serialize
#?(:clj datascript.test.storage)
datascript.test.transact
datascript.test.tuples
datascript.test.validation
Expand All @@ -59,4 +60,4 @@
(comment
(do
(require 'datascript.test :reload-all)
(t/run-all-tests #"datascript\..*")))
(t/run-all-tests #"datascript\..*")))
25 changes: 25 additions & 0 deletions test/datascript/test/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#?(:cljs [cljs.test :as t :refer-macros [is are deftest testing]]
:clj [clojure.test :as t :refer [is are deftest testing]])
[clojure.string :as str]
[cognitect.transit :as transit]
[datascript.core :as d]
[datascript.impl.entity :as de]
[datascript.db :as db #?@(:cljs [:refer-macros [defrecord-updatable]]
Expand Down Expand Up @@ -72,6 +73,30 @@
:cljs
(def no-namespace-maps {:before #(set! *print-namespace-maps* false)}))

(defn transit-write [o type]
#?(:clj
(with-open [os (java.io.ByteArrayOutputStream.)]
(let [writer (transit/writer os type)]
(transit/write writer o)
(.toByteArray os)))
:cljs
(transit/write (transit/writer type) o)))

(defn transit-write-str [o]
#?(:clj (String. ^bytes (transit-write o :json) "UTF-8")
:cljs (transit-write o :json)))

(defn transit-read [s type]
#?(:clj
(with-open [is (java.io.ByteArrayInputStream. s)]
(transit/read (transit/reader is type)))
:cljs
(transit/read (transit/reader type) s)))

(defn transit-read-str [s]
#?(:clj (transit-read (.getBytes ^String s "UTF-8") :json)
:cljs (transit-read s :json)))

;; Core tests

(deftest test-protocols
Expand Down
32 changes: 4 additions & 28 deletions test/datascript/test/serialize.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
[#?(:cljs cljs.reader :clj clojure.edn) :as edn]
#?(:cljs [cljs.test :as t :refer-macros [is are deftest testing]]
:clj [clojure.test :as t :refer [is are deftest testing]])
[cognitect.transit :as transit]
[datascript.core :as d]
[datascript.db :as db]
[datascript.test.core :as tdc]
Expand Down Expand Up @@ -111,40 +110,17 @@
(d/init-db [[:add -1 :name "Ivan"] {:add -1 :age 35}] schema))))))


(defn transit-write [o type]
#?(:clj
(with-open [os (java.io.ByteArrayOutputStream.)]
(let [writer (transit/writer os type)]
(transit/write writer o)
(.toByteArray os)))
:cljs
(transit/write (transit/writer type) o)))

(defn transit-write-str [o]
#?(:clj (String. ^bytes (transit-write o :json) "UTF-8")
:cljs (transit-write o :json)))

(defn transit-read [s type]
#?(:clj
(with-open [is (java.io.ByteArrayInputStream. s)]
(transit/read (transit/reader is type)))
:cljs
(transit/read (transit/reader type) s)))

(defn transit-read-str [s]
#?(:clj (transit-read (.getBytes ^String s "UTF-8") :json)
:cljs (transit-read s :json)))

(deftest serialize
(let [db (d/db-with
(d/empty-db schema)
(map (fn [[e a v]] [:db/add e a v]) data))]
(is (= db (-> db d/serializable d/from-serializable)))
(is (= db (-> db d/serializable pr-str edn/read-string d/from-serializable)))
(is (= db (-> db (d/serializable {:freeze-fn transit-write-str}) pr-str edn/read-string (d/from-serializable {:thaw-fn transit-read-str}))))
(is (= db (-> db (d/serializable {:freeze-fn tdc/transit-write-str}) pr-str edn/read-string (d/from-serializable {:thaw-fn tdc/transit-read-str}))))
(doseq [type [:json :json-verbose #?(:clj :msgpack)]]
(testing type
(is (= db (-> db d/serializable (transit-write type) (transit-read type) d/from-serializable)))))
(is (= db (-> db d/serializable (tdc/transit-write type) (tdc/transit-read type) d/from-serializable)))))
#?(:clj
(is (= db (-> db d/serializable jsonista/write-value-as-string jsonista/read-value d/from-serializable))))
#?(:clj
Expand All @@ -163,10 +139,10 @@
valid? #(#?(:clj Double/isNaN :cljs js/isNaN) (:nan (d/entity % 1)))]
(is (valid? (-> db d/serializable d/from-serializable)))
(is (valid? (-> db d/serializable pr-str edn/read-string d/from-serializable)))
(is (valid? (-> db (d/serializable {:freeze-fn transit-write-str}) pr-str edn/read-string (d/from-serializable {:thaw-fn transit-read-str}))))
(is (valid? (-> db (d/serializable {:freeze-fn tdc/transit-write-str}) pr-str edn/read-string (d/from-serializable {:thaw-fn tdc/transit-read-str}))))
(doseq [type [:json :json-verbose #?(:clj :msgpack)]]
(testing type
(is (valid? (-> db d/serializable (transit-write type) (transit-read type) d/from-serializable)))))
(is (valid? (-> db d/serializable (tdc/transit-write type) (tdc/transit-read type) d/from-serializable)))))
#?(:clj
(is (valid? (-> db d/serializable jsonista/write-value-as-string jsonista/read-value d/from-serializable))))
#?(:clj
Expand Down
Loading

0 comments on commit 25b2de2

Please sign in to comment.