(ns datomish.test.query #?(:cljs (:require-macros [datomish.pair-chan :refer [go-pair context [conn] (context/make-context (mock-source (d/db conn)))) (defn- expand [find conn] (let [context (conn->context conn) parsed (query/parse find)] (query/find->sql-clause context parsed))) (defn- populate [find conn] (let [context (conn->context conn) parsed (query/parse find)] (query/find-into-context context parsed))) (defn (populate '[:find ?e ?v :in $ :where [?e :foo/int ?v]] conn) :cc :known-types) {'?v :db.type/long '?e :db.type/ref}))) (testing "Numeric entid." (is (= (-> (populate '[:find ?v :in $ :where [6 :foo/int ?v]] conn) :cc :known-types) {'?v :db.type/long}))) (testing "Keyword entity." (is (= (-> (populate '[:find ?v :in $ :where [:my/thing :foo/int ?v]] conn) :cc :known-types) {'?v :db.type/long})))) (deftest-db test-value-constant-constraint-descends-into-not-and-or conn (let [attrs ( :datoms0.tx (sql/param :latest)))] [:= :datoms0.tx :datoms1.e])} (expand '[:find ?timestampMicros ?page :in $ ?latest :where [?page :page/starred true ?t] [?t :db/txInstant ?timestampMicros] (not [(> ?t ?latest)])] conn))))) (deftest-db test-pattern-not-join conn (let [attrs ( :datoms0.tx (sql/param :latest)))] [:= :datoms1.a (:db/txInstant attrs)] [:= :datoms0.tx :datoms1.e] )} (expand '[:find ?timestampMicros ?page :in $ ?latest :where [?page :page/starred true ?t] (not [(> ?t ?latest)]) [?t :db/txInstant ?timestampMicros]] conn))))) (deftest-db test-pattern-not-join-ordering-preserved conn (let [attrs (tag-codes input)] (if (= 1 (count codes)) [:= column (first codes)] (cons :or (map (fn [tag] [:= column tag]) codes))))) (deftest-db test-url-tag conn (let [attrs ( ?date ?then)] [?e :foo/points ?v]] conn)] (is (= (:group-by-vars context) ['?date '?e])) (is (= {:select '([:preag.date :date] [:%max.preag.v :_max_v]) :modifiers [:distinct] :group-by '(:date :e), :with {:preag {:select '([:datoms0.v :date] [:datoms1.v :v] [:datoms0.e :e]), ; Because we need to group on it. :modifiers [:distinct], :from '([:datoms datoms0] [:datoms datoms1]), :where (list :and [:= :datoms0.a (:foo/visitedAt attrs)] (list :> :datoms0.v (sql/param :then)) [:= :datoms1.a (:foo/points attrs)] [:= :datoms0.e :datoms1.e])}} :from [:preag]} (query/context->sql-clause context))))) (deftest-db test-get-else conn (let [attrs ( ?date ?then)] [?e :foo/points ?v]] conn)] (is (thrown-with-msg? ExceptionInfo #"Invalid limit \?x" (query/options-into-context context '?x [[:date :asc]]))) (is (thrown-with-msg? ExceptionInfo #"Ordering expressions must be :asc or :desc" (query/context->sql-clause (query/options-into-context context 10 [[:date :upsidedown]])))) (is (thrown-with-msg? ExceptionInfo #"Ordering vars \#\{:nonexistent\} not a subset" (query/context->sql-clause (query/options-into-context context 10 [[:nonexistent :desc]])))) (is (= {:limit 10} (select-keys (query/context->sql-clause (query/options-into-context context 10 nil)) [:order-by :limit] ))) (is (= {:order-by [[:date :asc]]} (select-keys (query/context->sql-clause (query/options-into-context context nil [[:date :asc]])) [:order-by :limit] ))) (is (= {:limit 10 :order-by [[:date :asc]]} (select-keys (query/context->sql-clause (query/options-into-context context 10 [[:date :asc]])) [:order-by :limit] )))))