Source

SQLRat / src / test / clj / org / bituf / sqlrat / test / clause.clj

(ns org.bituf.sqlrat.test.clause
  (:use org.bituf.sqlrat.clause)
  (:use org.bituf.sqlrat.test.util)
  (:use clojure.test))


(deftest test-as-clause
  (testing "Takes string"
    (let [c (as-clause "hello")]
      (is (= c ["hello"]))))
  (testing "Takes vector (or sequence, list, set)"
    (let [c (as-clause ["hello" 45])]
      (is (= c ["hello" 45]))))
  (testing "Rejects everything else"
    (is (thrown? IllegalArgumentException
          (as-clause {"hello" 45})))))


(deftest test-clause?-and-assert-clause
  (testing "String clause"
    (let [c (as-clause "hello")]
      (is (true? (clause? c)))
      (assert-clause c)))
  (testing "String non-clause"
    (let [c "hello"]
      (is (false? (clause? c)))
      (is (thrown? IllegalArgumentException
            (assert-clause c)))))
  (testing "Vector clause"
    (let [c (as-clause ["hello" 45])]
      (is (true? (clause? c)))
      (assert-clause c))
  (testing "Vector non-clause"
    (let [c ["hello" 45]]
      (is (false? (clause? c)))
      (is (thrown? IllegalArgumentException
            (assert-clause c)))))))


(deftest test-merge-clauses
  (testing "Merge parameter-less"
    (let [c1 "hello"
          c2 "world"
          delim ":"
          cc ["hello:world"]]
      (is (= cc (merge-clauses c1 delim c2)))))
  (testing "Merge parameter-less with parameter-ful"
    (let [c1 (as-clause "hello")
          c2 (as-clause ["world" 45])
          delim "+"
          cc ["hello+world" 45]]
      (is (= cc (merge-clauses c1 delim c2)))))
  (testing "Merge parameter-ful and parameter-less"
    (let [c1 (as-clause ["hello" 78 889])
          c2 (as-clause "world")
          delim ":"
          cc ["hello:world" 78 889]]
      (is (= cc (merge-clauses c1 delim c2)))))
  (testing "Merge parameter-ful and parameter-ful"
    (let [c1 (as-clause ["hello" 78 889])
          c2 (as-clause ["world" "horse"])
          cc ["helloworld" 78 889 "horse"]]
      (is (= cc (merge-clauses c1 c2))))))


(deftest test-merge-key-clauses
  (testing "Merge WHERE clause"
    (let [c (=? :id 45)]
      (is (= [" WHERE id=?" 45]
            (merge-key-clauses " " :where c)))))
  (testing "Merge WHERE with empty clause"
    (let [c (as-clause "")]
      (is (= [""]
            (merge-key-clauses " " :where c)))))
  (testing "Merge GROUP BY clause"
    (let [c (csnames [:qty :cost])]
      (is (= [" GROUP BY qty, cost"]
            (merge-key-clauses " " :group-by c))))))


(deftest test-op2-with-value
  (let [k :id
        v 34
        s=?  ["id=?" v]
        s<>? ["id<>?" v]
        s!=? ["id!=?" v]
        s>?  ["id>?" v]
        s<?  ["id<?" v]
        s>=? ["id>=?" v]
        s<=? ["id<=?" v]
        s-like? ["id LIKE ?" v]
        s-begins-with? ["id LIKE ?" (str v "%")]
        s-ends-with?   ["id LIKE ?" (str "%" v)]
        s-includes?    ["id LIKE ?" (str "%" v "%")]]
    (testing "Equals"         (is (= s=?  (=?  k v))))
    (testing "Does not equal" (is (= s<>? (<>? k v))))
    (testing "Does not equal" (is (= s!=? (!=? k v))))
    (testing "Greater than"   (is (= s>?  (>? k v))))
    (testing "Less than"      (is (= s<?  (<? k v))))
    (testing "Greater than or equals" (is (= s>=? (>=? k v))))
    (testing "Less than or equals"    (is (= s<=? (<=? k v))))
    (testing "Like"        (is (= s-like?        (like?        k v))))
    (testing "Begins with" (is (= s-begins-with? (begins-with? k v))))
    (testing "Ends with"   (is (= s-ends-with?   (ends-with?   k v))))
    (testing "Includes"    (is (= s-includes?    (includes?    k v))))))


(deftest test-op2-with-subquery
  (let [k :id
        ;; sub-query
        qs "SELECT someid FROM sometable WHERE q=?" ;; query string
        qp 10      ;; query parameter
        q  [qs qp] ;; (sub)query
        v (as-clause q)
        e (str "(" qs ")") ;; enclosed sub-query
        s=?  [(str "id="  e) qp]
        s<>? [(str "id<>" e) qp]
        s!=? [(str "id!=" e) qp]
        s>?  [(str "id>"  e) qp]
        s<?  [(str "id<"  e) qp]
        s>=? [(str "id>=" e) qp]
        s<=? [(str "id<=" e) qp]]
    (testing "Equals"         (is (= s=?  (=?  k v))))
    (testing "Does not equal" (is (= s<>? (<>? k v))))
    (testing "Does not equal" (is (= s!=? (!=? k v))))
    (testing "Greater than"   (is (= s>?  (>? k v))))
    (testing "Less than"      (is (= s<?  (<? k v))))
    (testing "Greater than or equals" (is (= s>=? (>=? k v))))
    (testing "Less than or equals"    (is (= s<=? (<=? k v))))))


(deftest test-multi-op
  (testing "Between"
    (is (= ["code BETWEEN ? AND ?" 100 200]
          (between? :code 100 200))))
  (testing "In (values)"
    (is (= ["code IN (?, ?, ?)" 20 30 40]
          (in? :code [20 30 40])))))


(deftest test-enclose
  ;(testing "With parameters"
  ;  (is (= ["1hello2"] (enclose "1" "hello" "2"))))
  (testing "Without parameters"
    (is (= ["(hello)"] (enclose "hello")))))


(deftest test-map-to-clauses
  (testing "Map to clauses"
    (is (= [["a=?" 10] ["b=?" 20]]
          (map-to-clauses =? {:a 10 :b 20})))))


(deftest test-one-operand-fns
  (testing "Is Null"
    (is (= (=? :c nil) ["c IS NULL"])))
  (testing "Not Null"
    (is (= (<>? :c nil) ["c NOT NULL"]))))


(deftest test-complex-clauses
  (testing "Sample 1"
    (is (= ["((id>=? OR cnt BETWEEN ? AND ?) AND name IN (?, ?, ?))"
            45 67 89 "Foo" "Bar" "Baz"]
          (AND
            (OR (>=? :id 45) (between? :cnt 67 89))
            (in? :name ["Foo" "Bar" "Baz"]))))))


(defn test-ns-hook []
  (test-as-clause)
  (test-clause?-and-assert-clause)
  (test-merge-clauses)
  (test-merge-key-clauses)
  (test-op2-with-value)
  (test-op2-with-subquery)
  (test-multi-op)
  (test-enclose)
  (test-map-to-clauses)
  (test-one-operand-fns)
  (test-complex-clauses))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.