From 7d63c2185d3a99bfce5694f509922e11d0ee34f3 Mon Sep 17 00:00:00 2001 From: Richard Newman Date: Fri, 19 Aug 2016 09:15:16 -0700 Subject: [PATCH] Rework query tests to use a live DB. Fixes #35. --- test/datomish/test/query.cljc | 626 ++++++++++++++++++++-------------- 1 file changed, 369 insertions(+), 257 deletions(-) diff --git a/test/datomish/test/query.cljc b/test/datomish/test/query.cljc index 4fde079a..77036bbb 100644 --- a/test/datomish/test/query.cljc +++ b/test/datomish/test/query.cljc @@ -1,19 +1,32 @@ (ns datomish.test.query + #?(:cljs + (:require-macros + [datomish.pair-chan :refer [go-pair DatomsSource - {:table :datoms - :fulltext-table :fulltext_values - :fulltext-view :all_datoms - :columns [:e :a :v :tx :added] - :attribute-transform transforms/attribute-transform-string - :constant-transform transforms/constant-transform-default - :table-alias (comp (make-predictable-gensym) name) - :schema (schema/map->Schema - {:schema schema - :rschema nil}) - :make-constraints nil})) +(def page-schema + [{:db/id (d/id-literal :db.part/user) + :db.install/_attribute :db.part/db + :db/ident :page/loves + :db/valueType :db.type/ref + :db/cardinality :db.cardinality/many} + {:db/id (d/id-literal :db.part/user) + :db.install/_attribute :db.part/db + :db/ident :page/likes + :db/valueType :db.type/ref + :db/cardinality :db.cardinality/many} + {:db/id (d/id-literal :db.part/user) + :db.install/_attribute :db.part/db + :db/ident :page/url + :db/valueType :db.type/string + :db/unique :db.unique/identity + :db/cardinality :db.cardinality/one} + {:db/id (d/id-literal :db.part/user) + :db.install/_attribute :db.part/db + :db/ident :page/title + :db/valueType :db.type/string + :db/cardinality :db.cardinality/one} + {:db/id (d/id-literal :db.part/user) + :db.install/_attribute :db.part/db + :db/ident :page/starred + :db/valueType :db.type/boolean + :db/cardinality :db.cardinality/one}]) -(defn- expand [find schema] - (let [context (context/->Context (mock-source nil schema) nil nil) +(def schema-with-page + (concat + simple-schema + page-schema)) + +(defn mock-source [db] + (assoc (datomish.db/datoms-source db) + :table-alias (comp (make-predictable-gensym) name))) + +(defn conn->context [conn] + (context/->Context (mock-source (d/db conn)) nil nil)) + +(defn- expand [find conn] + (let [context (conn->context conn) parsed (query/parse find)] (query/find->sql-clause context parsed))) -(defn- populate [find schema] - (let [context (context/->Context (mock-source nil schema) nil nil) +(defn- populate [find conn] + (let [context (conn->context conn) parsed (query/parse find)] (query/find-into-context context parsed))) -(deftest test-type-extraction +(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 (= (:known-types (:cc (populate '[:find ?v :in $ :where [6 :foo/int ?v]] simple-schema))) - {'?v :db.type/integer}))) + (is (= (-> + (populate '[:find ?v :in $ :where [6 :foo/int ?v]] conn) + :cc :known-types) + {'?v :db.type/long}))) + (testing "Keyword entity." - (is (= (:known-types (:cc (populate '[:find ?v :in $ :where [:my/thing :foo/int ?v]] simple-schema))) - {'?v :db.type/integer})))) + (is (= (-> + (populate '[:find ?v :in $ :where [:my/thing :foo/int ?v]] conn) + :cc :known-types) + {'?v :db.type/long})))) -(deftest test-value-constant-constraint-descends-into-not-and-or - (testing "Elision of types inside a join." - (is (= '{:select ([:datoms0.e :e] - [:datoms0.v :v]), - :modifiers [:distinct], - :from [[:datoms datoms0]], - :where (:and - [:= :datoms0.a "foo/int"] - [:not - [:exists - {:select [1], - :from [[:all_datoms all_datoms1]], - :where (:and - [:= :all_datoms1.e 15] - [:= :datoms0.v :all_datoms1.v])}]])} - (expand - '[:find ?e ?v :in $ :where - [?e :foo/int ?v] - (not [15 ?a ?v])] - simple-schema)))) +(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)])] - simple-schema)))) + conn)))))) -(deftest test-pattern-not-join - (is (= '{:select ([:datoms1.v :timestampMicros] [:datoms0.e :page]), - :modifiers [:distinct], - :from [[:datoms datoms0] - [:datoms datoms1]], - :where (:and - [:= :datoms0.a "page/starred"] +(deftest-db test-basic-join conn + ;; Note that we use a schema without :page/starred, so we + ;; don't know what type it is. + (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"] - [:= :datoms0.tx :datoms1.e] - )} - (expand - '[:find ?timestampMicros ?page :in $ ?latest :where - [?page :page/starred true ?t] - (not [(> ?t ?latest)]) - [?t :db/txInstant ?timestampMicros]] - simple-schema)))) - -(deftest test-pattern-not-join-ordering-preserved - (is (= '{:select ([:datoms2.v :timestampMicros] [:datoms0.e :page]), - :modifiers [:distinct], - :from [[:datoms datoms0] - [:datoms datoms2]], - :where (:and - [:= :datoms0.a "page/starred"] - [:= :datoms0.value_type_tag 1] ; boolean +(deftest-db test-not-clause-ordering-preserved 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 [?page :foo/bar _]) - [?t :db/txInstant ?timestampMicros]] - simple-schema)))) + (expand + '[:find ?timestampMicros ?page :in $ ?latest :where + [?page :page/starred true ?t] + (not [(> ?t ?latest)]) + [?t :db/txInstant ?timestampMicros]] + conn))))) -(deftest test-single-or - (is (= '{:select ([:datoms0.e :page]), - :modifiers [:distinct], - :from ([:datoms datoms0] [:datoms datoms1] [:datoms datoms2]), - :where (:and - [:= :datoms0.a "page/url"] - [:= :datoms0.value_type_tag 10] - [:= :datoms0.v "http://example.com/"] - [:= :datoms1.a "page/title"] - [:= :datoms2.a "page/loves"] - [:= :datoms0.e :datoms1.e] - [:= :datoms0.e :datoms2.v])} - (expand - '[:find ?page :in $ ?latest :where - [?page :page/url "http://example.com/"] - [?page :page/title ?title] - (or - [?entity :page/loves ?page])] - simple-schema)))) +(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 (