Skip to content

Commit

Permalink
datomic test fixtures restored, consolidate and export under contrib …
Browse files Browse the repository at this point in the history
…ns for libraries. todo lift all datomic demos/contrib into electric-fiddle
dustingetz committed Aug 5, 2023
1 parent 17098c6 commit 5338531
Showing 20 changed files with 142 additions and 433 deletions.
14 changes: 4 additions & 10 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
:exclusions [org.slf4j/slf4j-api
info.sunng/ring-jetty9-adapter-http3]} ; no need
ring-basic-authentication/ring-basic-authentication {:mvn/version "1.1.1"}
com.datomic/peer {:mvn/version "1.0.6733"} ; for demos
com.datomic/peer {:mvn/version "1.0.6735" :exclusions [org.slf4j/slf4j-nop]} ; for demos
datascript/datascript {:mvn/version "1.3.14"} ; for demos
}
:jvm-opts ["-Xss2m" ; https://github.com/hyperfiddle/electric/issues/11
@@ -56,17 +56,11 @@
info.sunng/ring-jetty9-adapter-http3]} ; no need
ring-basic-authentication/ring-basic-authentication {:mvn/version "1.1.1"}
datascript/datascript {:mvn/version "1.3.14"} ; for demos
com.datomic/peer {:mvn/version "1.0.6733"} ; for demos
com.datomic/peer {:mvn/version "1.0.6733" :exclusions [org.slf4j/slf4j-nop]} ; for demos
}}
:scratch {:extra-deps {com.datomic/dev-local {:mvn/version "1.0.243"}
com.google.guava/guava {:mvn/version "31.1-jre"}}} ; fix conflict - datomic cloud & shadow
:datomic-free {:extra-deps {com.datomic/datomic-free {:mvn/version "0.9.5656" :exclusions [org.slf4j/slf4j-nop com.google.guava/guava]}}}
:datomic-pro {:extra-deps {com.datomic/datomic-pro {:mvn/version "1.0.6527" :exclusions [org.slf4j/slf4j-nop]}}}


:profile {:extra-deps {criterium/criterium {:mvn/version "0.4.6"}
com.clojure-goes-fast/clj-async-profiler {:mvn/version "1.0.2"}}
:jvm-opts ["-Djdk.attach.allowAttachSelf"
"-XX:+UnlockDiagnosticVMOptions"
"-XX:+DebugNonSafepoints"]}}

:mvn/repos {"my.datomic.com" {:url "https://my.datomic.com/repo"}}}
"-XX:+DebugNonSafepoints"]}}}
2 changes: 1 addition & 1 deletion scratch/dustin/y2022/content/livecoding_hfql.cljc
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
[hyperfiddle.photon-dom :as dom]
[hyperfiddle.ui.codemirror :as codemirror]
[hyperfiddle.ui :as ui]
[wip.orders-datascript :refer [orders genders shirt-sizes]]
[contrib.test.orders-datascripttascript :refer [orders genders shirt-sizes]]
dustin.y2022.edn-render)
(:import (hyperfiddle.photon Pending Remote))
#?(:cljs (:require-macros dustin.y2022.livecoding-hfql)))
2 changes: 1 addition & 1 deletion scratch/dustin/y2022/hfql/demo_hfql.cljc
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
[hyperfiddle.photon-dom :as dom]
[hyperfiddle.hfql.ui :as hfui]
[hyperfiddle.hfql.router :refer [router] :rename {router hfql-router}]
#?(:clj [wip.orders-datascript :refer [orders genders shirt-sizes one-order]]))
#?(:clj [contrib.test.orders-datascripttascript :refer [orders genders shirt-sizes one-order]]))
#?(:cljs (:require-macros wip.demo-hfql)))


2 changes: 1 addition & 1 deletion scratch/dustin/y2022/hfql/hfql.cljc
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
[hyperfiddle.hfql.router :as router]
[hyperfiddle.photon :as p]
[hyperfiddle.photon-dom :as dom]
[wip.orders-datascript :refer [orders genders shirt-sizes one-order]]
[contrib.test.orders-datascripttascript :refer [orders genders shirt-sizes one-order]]
[hyperfiddle.photon-ui :as ui]
#?(:cljs [hyperfiddle.router :as html5-router])
[missionary.core :as m]
2 changes: 1 addition & 1 deletion scratch/geoffrey/2022/explorer_tests.cljc
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
[datascript.core :as d]
[clojure.string :as str]
[clojure.spec.alpha :as s]
#?(:clj [wip.orders-datascript :refer [orders order shirt-sizes one-order nav! schema]]))
#?(:clj [contrib.test.teeshirt-orders-datascript :refer [orders order shirt-sizes one-order nav! schema]]))
(:import [hyperfiddle.photon Pending]))

(comment (rcf/enable! true))
145 changes: 1 addition & 144 deletions src-dev/dev.cljc
Original file line number Diff line number Diff line change
@@ -1,147 +1,4 @@
(ns dev
(:require
[datascript.core :as d]
[hyperfiddle.api :as hf]
[hyperfiddle.rcf :refer [tests % tap]]))


(defn fixtures [$]
; portable
(-> $
(d/with [{:db/id 1 :order/type :order/gender :db/ident :order/male}
{:db/id 2 :order/type :order/gender :db/ident :order/female}])
:db-after
(d/with [{:db/id 3 :order/type :order/shirt-size :db/ident :order/mens-small :order/gender :order/male}
{:db/id 4 :order/type :order/shirt-size :db/ident :order/mens-medium :order/gender :order/male}
{:db/id 5 :order/type :order/shirt-size :db/ident :order/mens-large :order/gender :order/male}
{:db/id 6 :order/type :order/shirt-size :db/ident :order/womens-small :order/gender :order/female}
{:db/id 7 :order/type :order/shirt-size :db/ident :order/womens-medium :order/gender :order/female}
{:db/id 8 :order/type :order/shirt-size :db/ident :order/womens-large :order/gender :order/female}])
:db-after
(d/with [{:db/id 9 :order/email "alice@example.com" :order/gender :order/female :order/shirt-size :order/womens-large
:order/tags [:a :b :c]}
{:db/id 10 :order/email "bob@example.com" :order/gender :order/male :order/shirt-size :order/mens-large
:order/tags [:b]}
{:db/id 11 :order/email "charlie@example.com" :order/gender :order/male :order/shirt-size :order/mens-medium}])
:db-after
#_(d/with [{:db/id 12 :order/email "alice@example.com" :order/gender :order/female :order/shirt-size :order/womens-large}
{:order/email "bob@example.com" :order/gender :order/male :order/shirt-size :order/mens-large}
{:order/email "charlie@example.com" :order/gender :order/male :order/shirt-size :order/mens-medium}])

#_:db-after))

;(defn init-datomic []
; (def schema [{:db/ident :order/email :db/valueType :db.type/string :db/cardinality :db.cardinality/one :db/unique :db.unique/identity}
; {:db/ident :order/gender :db/valueType :db.type/ref :db/cardinality :db.cardinality/one}
; {:db/ident :order/shirt-size :db/valueType :db.type/ref :db/cardinality :db.cardinality/one}
; {:db/ident :order/type :db/valueType :db.type/keyword :db/cardinality :db.cardinality/one}
; {:db/ident :order/tags :db/valueType :db.type/keyword :db/cardinality :db.cardinality/many}])
; (d/create-database "datomic:mem://hello-world")
; (def ^:dynamic *$* (-> (d/connect "datomic:mem://hello-world") d/db (d/with schema) :db-after fixtures))
; #_(alter-var-root #'*$* (constantly $)))

(def schema
;; manual db/ids for tests consistency and clarity, not a requirement
[{:db/id 100001 :db/ident :order/email, :db/valueType :db.type/string, :db/cardinality :db.cardinality/one, :db/unique :db.unique/identity}
{:db/id 100002 :db/ident :order/gender, :db/valueType :db.type/ref :db/cardinality :db.cardinality/one}
{:db/id 100003 :db/ident :order/shirt-size, :db/valueType :db.type/ref :db/cardinality :db.cardinality/one}
{:db/id 100004 :db/ident :order/type, :db/valueType :db.type/keyword :db/cardinality :db.cardinality/one}
{:db/id 100005 :db/ident :order/tags, :db/valueType :db.type/keyword :db/cardinality :db.cardinality/many}])

(def db-config {:store {:backend :mem, :id "default"}})

(defn setup-db! []
;; FIXME Datascript doesn’t support :db/valueType, using :hf/valueType in the meantime
(let [-schema {:order/email {:hf/valueType :db.type/string :db/cardinality :db.cardinality/one :db/unique :db.unique/identity}
:order/gender {:db/valueType :db.type/ref :db/cardinality :db.cardinality/one}
:order/shirt-size {:db/valueType :db.type/ref :db/cardinality :db.cardinality/one}
:order/type {#_#_:db/valueType :db.type/keyword :db/cardinality :db.cardinality/one}
:order/tags {#_#_:db/valueType :db.type/keyword :db/cardinality :db.cardinality/many}
:db/ident {:db/unique :db.unique/identity, :hf/valueType :db.type/keyword}}]
#?(:clj (alter-var-root #'schema (constantly -schema))
:cljs (set! schema -schema)))
;(log/info "Initializing Test Database")
(def conn (d/create-conn schema))
(let [$ (-> conn d/db fixtures)]
#?(:clj (alter-var-root #'hf/*$* (constantly $))
:cljs (set! hf/*$* $))))

#?(:clj (setup-db!))

(def db hf/*$*) ; for @(requiring-resolve 'dev/db)

(def male 1 #_:order/male #_17592186045418)
(def female 2 #_:order/female #_17592186045419)
(def m-sm 3 #_17592186045421)
(def m-md 4 #_nil)
(def m-lg 5 #_nil)
(def w-sm 6 #_nil)
(def w-md 7 #_nil)
(def w-lg 8 #_nil)
(def alice 9 #_17592186045428)
(def bob 10 #_nil)
(def charlie 11 #_nil)

(tests
(def e [:order/email "alice@example.com"])

(tests
"(d/pull ['*]) is best for tests"
(d/pull db ['*] e)
:= {:db/id 9,
:order/email "alice@example.com",
:order/shirt-size #:db{:id 8},
:order/gender #:db{:id 2}
:order/tags [:a :b :c]})

(comment #_tests
"careful, entity type is not= to equivalent hashmap"
(d/touch (d/entity db e))
; expected failure
:= {:order/email "alice@example.com",
:order/gender #:db{:id 2},
:order/shirt-size #:db{:id 8},
:order/tags #{:c :b :a},
:db/id 9})

(tests
"entities are not maps"
(type (d/touch (d/entity db e)))
(type *1) := datascript.impl.entity.Entity) ; not a map

(tests
"careful, entity API tests are fragile and (into {}) is insufficient"
(->> (d/touch (d/entity db e)) ; touch is the best way to inspect an entity
(into {})) ; but it's hard to convert to a map...
:= #:order{#_#_:id 9 ; db/id is not present!
:email "alice@example.com",
:gender _ #_#:db{:id 2}, ; entity ref not =
:shirt-size _ #_#:db{:id 8}, ; entity ref not =
:tags #{:c :b :a}}

"select keys doesn't fix the problem as it's not recursive"
(-> (d/touch (d/entity db e))
(select-keys [:order/email :order/shirt-size :order/gender]))
:= #:order{:email "alice@example.com",
:shirt-size _ #_#:db{:id 8}, ; still awkward, need recursive pull
:gender _ #_#:db{:id 2}})

"TLDR is use (d/pull ['*]) like the first example"
(tests
(d/pull db ['*] :order/female)
:= {:db/id female :db/ident :order/female :order/type :order/gender})

(tests
(d/q '[:find [?e ...] :where [_ :order/gender ?e]] db)
:= [2 1] #_[:order/male :order/female])
)

(comment
"CI tests"
#?(:clj (alter-var-root #'hyperfiddle.rcf/*generate-tests* (constantly false)))
(hyperfiddle.rcf/enable!)
(require 'clojure.test)
(clojure.test/run-all-tests #"(hyperfiddle.api|user.orders)"))
(ns dev)

(comment
"Performance profiling, use :profile deps alias"
54 changes: 0 additions & 54 deletions src-dev/test.clj

This file was deleted.

18 changes: 0 additions & 18 deletions src-dev/test/mbrainz.clj

This file was deleted.

27 changes: 13 additions & 14 deletions src-docs/wip/demo_stage_ui4.cljc
Original file line number Diff line number Diff line change
@@ -2,14 +2,13 @@
(:require [contrib.css :refer [css-slugify]]
[contrib.str :refer [pprint-str]]
#?(:clj [contrib.datomic-contrib :as dx])
#?(:clj [datomic.client.api :as d])
#?(:clj [datomic.api :as d])
[hyperfiddle.api :as hf]
[hyperfiddle.electric :as e]
[hyperfiddle.electric-dom2 :as dom]
[hyperfiddle.electric-ui4 :as ui]
[hyperfiddle.popover :refer [Popover]]))

(def cobblestone 536561674378709)
[hyperfiddle.popover :refer [Popover]]
#?(:clj [contrib.test.datomic-peer-mbrainz :as test])))

(def label-form-spec [:db/id
:label/gid
@@ -19,7 +18,7 @@
{:label/country [:db/ident]}
:label/startYear])

(comment (d/pull test/datomic-db ['*] cobblestone))
(comment (d/pull test/db ['*] test/cobblestone))

#?(:clj (defn type-options [db & [needle]]
(->> (d/q '[:find (pull ?e [:db/ident]) :in $ ?needle :where
@@ -31,10 +30,10 @@
(map first))))

(comment
(type-options test/datomic-db "")
(type-options test/datomic-db "prod")
(type-options test/datomic-db "bootleg")
(type-options test/datomic-db nil))
(type-options test/db "")
(type-options test/db "prod")
(type-options test/db "bootleg")
(type-options test/db nil))


(e/defn Form [e]
@@ -86,15 +85,15 @@

(e/defn Page []
#_(e/client (dom/div (if hf/loading "loading" "idle") " " (str (hf/Load-timer.)) "ms"))
(Form. cobblestone)
#_(Form. cobblestone)
(e/client (Popover. "open" (e/fn [] (e/server (Form. cobblestone))))))
(Form. test/cobblestone)
#_(Form. test/cobblestone)
(e/client (Popover. "open" (e/fn [] (e/server (Form. test/cobblestone))))))

(e/defn CrudForm []
(e/client (dom/h1 (dom/text (str `CrudForm))))
(e/server
(let [conn @(requiring-resolve 'test/datomic-conn)
secure-db (d/with-db conn)] ; todo datomic-tx-listener
(let [conn @(requiring-resolve 'contrib.test.datomic-peer-mbrainz/conn)
secure-db (d/db conn)] ; todo datomic-tx-listener
(binding [hf/schema (new (dx/schema> secure-db))
hf/into-tx' hf/into-tx
hf/with (fn [db tx] ; inject datomic
Loading
Oops, something went wrong.

0 comments on commit 5338531

Please sign in to comment.