|
| 1 | + (ns chrondb.query.ast-test |
| 2 | + (:require [clojure.test :refer [deftest is testing]] |
| 3 | + [chrondb.query.ast :as ast])) |
| 4 | + |
| 5 | + (deftest match-and-term-clauses-test |
| 6 | + (testing "match-all returns the expected clause" |
| 7 | + (is (= {:type :match-all} |
| 8 | + (ast/match-all)))) |
| 9 | + |
| 10 | + (testing "term clauses require both field and value" |
| 11 | + (is (= {:type :term :field "status" :value "active"} |
| 12 | + (ast/term :status "active"))) |
| 13 | + (is (= {:type :term :field "score" :value "42"} |
| 14 | + (ast/term :score 42))) |
| 15 | + (is (nil? (ast/term nil "active"))) |
| 16 | + (is (nil? (ast/term :status nil)))) |
| 17 | + |
| 18 | + (testing "wildcard clauses normalize field and value" |
| 19 | + (is (= {:type :wildcard :field "name" :value "foo"} |
| 20 | + (ast/wildcard :name "foo"))) |
| 21 | + (is (nil? (ast/wildcard :name nil)))) |
| 22 | + |
| 23 | + (testing "prefix delegates to wildcard with appended asterisk" |
| 24 | + (is (= (ast/wildcard :path "logs*") |
| 25 | + (ast/prefix :path "logs"))) |
| 26 | + (is (nil? (ast/prefix :path nil)))) |
| 27 | + |
| 28 | + (testing "full-text search clauses include analyzer metadata" |
| 29 | + (is (= {:type :fts :field "content" :value "history" :analyzer :fts} |
| 30 | + (ast/fts :content "history"))) |
| 31 | + (is (nil? (ast/fts :content nil)))) |
| 32 | + |
| 33 | + (testing "exists and missing clauses reject nil fields" |
| 34 | + (is (= {:type :exists :field "updated_at"} |
| 35 | + (ast/exists :updated_at))) |
| 36 | + (is (nil? (ast/exists nil))) |
| 37 | + (is (= {:type :missing :field "deleted_at"} |
| 38 | + (ast/missing :deleted_at))) |
| 39 | + (is (nil? (ast/missing nil))))) |
| 40 | + |
| 41 | + (deftest range-clauses-test |
| 42 | + (testing "default range is inclusive with stringified bounds" |
| 43 | + (is (= {:type :range |
| 44 | + :field "timestamp" |
| 45 | + :lower "10" |
| 46 | + :upper "20" |
| 47 | + :include-lower? true |
| 48 | + :include-upper? true |
| 49 | + :value-type nil} |
| 50 | + (ast/range :timestamp 10 20)))) |
| 51 | + |
| 52 | + (testing "range supports open bounds and option overrides" |
| 53 | + (is (= {:type :range |
| 54 | + :field "created_at" |
| 55 | + :lower nil |
| 56 | + :upper "100" |
| 57 | + :include-lower? false |
| 58 | + :include-upper? false |
| 59 | + :value-type :string} |
| 60 | + (ast/range :created_at nil 100 |
| 61 | + {:include-lower? false |
| 62 | + :include-upper? false |
| 63 | + :type :string})))) |
| 64 | + |
| 65 | + (testing "numeric helper ranges set the value type" |
| 66 | + (is (= {:type :range |
| 67 | + :field "age" |
| 68 | + :lower "18" |
| 69 | + :upper "65" |
| 70 | + :include-lower? true |
| 71 | + :include-upper? false |
| 72 | + :value-type :long} |
| 73 | + (ast/range-long :age 18 65 {:include-upper? false}))) |
| 74 | + (is (= {:type :range |
| 75 | + :field "score" |
| 76 | + :lower "0.1" |
| 77 | + :upper "0.9" |
| 78 | + :include-lower? true |
| 79 | + :include-upper? true |
| 80 | + :value-type :double} |
| 81 | + (ast/range-double :score 0.1 0.9 {}))))) |
| 82 | + |
| 83 | + (deftest boolean-node-test |
| 84 | + (testing "empty boolean collapses to match-all" |
| 85 | + (is (= (ast/match-all) |
| 86 | + (ast/boolean {})))) |
| 87 | + |
| 88 | + (testing "single should clause collapses to the clause itself" |
| 89 | + (let [clause (ast/term :status "active")] |
| 90 | + (is (= clause |
| 91 | + (ast/boolean {:should [clause]}))))) |
| 92 | + |
| 93 | + (testing "must-not clauses receive an implicit match-all must" |
| 94 | + (let [clause (ast/term :status "inactive")] |
| 95 | + (is (= {:type :boolean |
| 96 | + :must [(ast/match-all)] |
| 97 | + :should [] |
| 98 | + :must-not [clause] |
| 99 | + :filter []} |
| 100 | + (ast/boolean {:must [] |
| 101 | + :should [] |
| 102 | + :must-not [clause] |
| 103 | + :filter []}))))) |
| 104 | + |
| 105 | + (testing "general boolean vectorizes inputs and removes nils" |
| 106 | + (let [must-a (ast/term :status "active") |
| 107 | + should-a (ast/term :role "admin") |
| 108 | + result (ast/boolean {:must [must-a nil] |
| 109 | + :should [nil should-a] |
| 110 | + :must-not [nil] |
| 111 | + :filter nil})] |
| 112 | + (is (= {:type :boolean |
| 113 | + :must [must-a] |
| 114 | + :should [should-a] |
| 115 | + :must-not [] |
| 116 | + :filter []} |
| 117 | + result))))) |
| 118 | + |
| 119 | + (deftest logical-combinators-test |
| 120 | + (testing "and collapses edge cases" |
| 121 | + (let [clause (ast/term :status "active")] |
| 122 | + (is (= (ast/match-all) (ast/and))) |
| 123 | + (is (= clause (ast/and clause))) |
| 124 | + (is (= {:type :boolean |
| 125 | + :must [clause (ast/match-all)] |
| 126 | + :should [] |
| 127 | + :must-not [] |
| 128 | + :filter []} |
| 129 | + (ast/and clause nil (ast/match-all)))))) |
| 130 | + |
| 131 | + (testing "or collapses edge cases" |
| 132 | + (let [clause (ast/term :role "admin")] |
| 133 | + (is (= (ast/match-all) (ast/or))) |
| 134 | + (is (= clause (ast/or clause))) |
| 135 | + (is (= {:type :boolean |
| 136 | + :must [] |
| 137 | + :should [clause (ast/match-all)] |
| 138 | + :must-not [] |
| 139 | + :filter []} |
| 140 | + (ast/or clause nil (ast/match-all)))))) |
| 141 | + |
| 142 | + (testing "not wraps the clause with must-not and implicit must" |
| 143 | + (let [clause (ast/term :status "inactive")] |
| 144 | + (is (= {:type :boolean |
| 145 | + :must [(ast/match-all)] |
| 146 | + :should [] |
| 147 | + :must-not [clause] |
| 148 | + :filter []} |
| 149 | + (ast/not clause)))) |
| 150 | + |
| 151 | + (testing "not defaults to match-all when clause is nil" |
| 152 | + (is (= {:type :boolean |
| 153 | + :must [(ast/match-all)] |
| 154 | + :should [] |
| 155 | + :must-not [(ast/match-all)] |
| 156 | + :filter []} |
| 157 | + (ast/not nil))))) |
| 158 | + |
| 159 | + (deftest sort-and-query-metadata-test |
| 160 | + (testing "sort descriptors infer defaults" |
| 161 | + (is (= {:field "timestamp" :direction :asc :type :string} |
| 162 | + (ast/sort-by :timestamp))) |
| 163 | + (is (= {:field "timestamp" :direction :desc :type :string} |
| 164 | + (ast/sort-by :timestamp :desc))) |
| 165 | + (is (= {:field "timestamp" :direction :desc :type :long} |
| 166 | + (ast/sort-by :timestamp :desc :long)))) |
| 167 | + |
| 168 | + (testing "query wraps clauses and optional metadata" |
| 169 | + (let [clause (ast/term :status "active") |
| 170 | + sort-desc (ast/sort-by :timestamp :desc :long) |
| 171 | + query (ast/query [clause] |
| 172 | + {:sort [sort-desc] |
| 173 | + :limit 25 |
| 174 | + :offset 10 |
| 175 | + :branch "feature" |
| 176 | + :hints {:refresh true} |
| 177 | + :after {:doc 42}})] |
| 178 | + (is (= {:clauses [clause] |
| 179 | + :sort [sort-desc] |
| 180 | + :limit 25 |
| 181 | + :offset 10 |
| 182 | + :branch "feature" |
| 183 | + :hints {:refresh true} |
| 184 | + :after {:doc 42}} |
| 185 | + query)))) |
| 186 | + |
| 187 | + (testing "query vectorizes sort descriptors when provided" |
| 188 | + (let [clause (ast/match-all) |
| 189 | + sort-desc (ast/sort-by :timestamp) |
| 190 | + query (ast/query [clause] {:sort [sort-desc]})] |
| 191 | + (is (= [sort-desc] (:sort query)))))) |
| 192 | + |
| 193 | +(deftest query-transformers-test |
| 194 | + (let [base-query (ast/query [(ast/match-all)]) |
| 195 | + sort-desc (ast/sort-by :timestamp :desc :long)] |
| 196 | + (testing "with-branch replaces the branch" |
| 197 | + (is (= "main" |
| 198 | + (:branch (ast/with-branch base-query "main"))))) |
| 199 | + |
| 200 | + (testing "with-hints merges successive hint maps" |
| 201 | + (is (= {:refresh true :track-total-hits true} |
| 202 | + (:hints (-> base-query |
| 203 | + (ast/with-hints {:refresh true}) |
| 204 | + (ast/with-hints {:track-total-hits true}))))) |
| 205 | + (is (nil? (:hints (ast/with-hints base-query nil))))) |
| 206 | + |
| 207 | + (testing "with-search-after attaches the cursor" |
| 208 | + (is (= {:doc 9 :score 0.42} |
| 209 | + (:after (ast/with-search-after base-query {:doc 9 :score 0.42}))))) |
| 210 | + |
| 211 | + (testing "with-pagination conditionally sets pagination keys" |
| 212 | + (is (= {:limit 100 :offset 20 :after {:doc 1}} |
| 213 | + (select-keys (ast/with-pagination base-query |
| 214 | + {:limit 100 |
| 215 | + :offset 20 |
| 216 | + :after {:doc 1}}) |
| 217 | + [:limit :offset :after]))) |
| 218 | + (is (= base-query (ast/with-pagination base-query {})))) |
| 219 | + |
| 220 | + (testing "with-sort vectorizes descriptors" |
| 221 | + (is (= [sort-desc] |
| 222 | + (:sort (ast/with-sort base-query sort-desc)))) |
| 223 | + (is (= [sort-desc] |
| 224 | + (:sort (ast/with-sort base-query [sort-desc])))))) |
| 225 | +)) |
| 226 | + |
0 commit comments