;; Copyright 2016 Mozilla ;; ;; Licensed under the Apache License, Version 2.0 (the "License"); you may not use ;; this file except in compliance with the License. You may obtain a copy of the ;; License at http://www.apache.org/licenses/LICENSE-2.0 ;; Unless required by applicable law or agreed to in writing, software distributed ;; under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR ;; CONDITIONS OF ANY KIND, either express or implied. See the License for the ;; specific language governing permissions and limitations under the License. (ns datomish.schema-changes-test #?(:cljs (:require-macros [datomish.pair-chan :refer [go-pair schema-fragment]] [datomish.schema :as ds] [datomish.sqlite :as s] [datomish.util :as util #?(:cljs :refer-macros :clj :refer) [raise cond-let]] [datomish.db :as dm] #?@(:clj [[datomish.jdbc-sqlite] [datomish.pair-chan :refer [go-pair !]]]) #?@(:cljs [[datomish.js-sqlite] [datomish.pair-chan] [datomish.test-macros :refer-macros [deftest-async deftest-db]] [datomish.node-tempfile :refer [tempfile]] [cljs.test :as t :refer-macros [is are deftest testing async]] [cljs.core.async :as a :refer [!]]])) #?(:clj (:import [clojure.lang ExceptionInfo])) #?(:clj (:import [datascript.db DB]))) #?(:cljs (def Throwable js/Error)) (deftest test-datoms->schema-fragment (let [tx 10101 ->datom (fn [xs] (apply datom (conj xs tx)))] (are [i o] (= (datoms->schema-fragment (map ->datom i)) o) ;; Base case. [] {} ;; No matches. [[0 :not-db/add :not-db/install]] {} ;; Interesting case. [[:db.part/db :db.install/attribute 1] [:db.part/db :db.install/attribute 2] [1 :db/ident :test/attr1] [1 :db/valueType :db.value/string] [1 :db/cardinalty :db.cardinality/one] [1 :db/unique :db.unique/identity] [2 :db/ident :test/attr2] [2 :db/valueType :db.value/integer] [2 :db/cardinalty :db.cardinality/many]] {:test/attr1 {:db/valueType :db.value/string :db/cardinalty :db.cardinality/one :db/unique :db.unique/identity} :test/attr2 {:db/valueType :db.value/integer :db/cardinalty :db.cardinality/many}}) ;; :db/ident, :db/valueType, and :db/cardinality are required. valueType and cardinality are ;; enforced at the schema level. (testing "Fails without entity" (is (thrown-with-msg? ExceptionInfo #":db.install/attribute requires :db/ident, got \{\} for 1" (->> [[:db.part/db :db.install/attribute 1]] (map ->datom) (datoms->schema-fragment))))) (testing "Fails without :db/ident" (is (thrown-with-msg? ExceptionInfo #":db.install/attribute requires :db/ident, got \{:db/valueType :db.value/string\} for 1" (->> [[:db.part/db :db.install/attribute 1] [1 :db/valueType :db.value/string]] (map ->datom) (datoms->schema-fragment))))))) (deftest-db test-add-and-change-ident conn ;; Passes through on failure. (is (= :test/ident (d/entid (d/db conn) :test/ident))) (let [report (