2012-07-13 18:32:43 +00:00
( ns honeysql.core-test
2012-08-24 20:50:56 +00:00
( :refer-clojure :exclude [ format ] )
2012-07-13 18:32:43 +00:00
( :require [ clojure.test :refer [ deftest testing is ] ]
2012-08-24 21:08:22 +00:00
[ honeysql.core :as sql ]
2012-08-24 20:50:56 +00:00
[ honeysql.helpers :refer :all ] ) )
2012-07-13 18:32:43 +00:00
;; TODO: more tests
( deftest test-select
2013-08-06 18:14:40 +00:00
( let [ m1 ( -> ( select :f.* :b.baz :c.quux [ :b.bla "bla bla" ]
2013-08-06 18:05:17 +00:00
:%now ( sql/raw "@x := 10" ) )
2012-08-24 21:08:22 +00:00
;;(un-select :c.quux)
2012-08-24 20:50:56 +00:00
( modifiers :distinct )
( from [ :foo :f ] [ :baz :b ] )
2012-10-19 16:41:26 +00:00
( join :draq [ := :f.b :draq.x ] )
( left-join [ :clod :c ] [ := :f.a :c.d ] )
( right-join :bock [ := :bock.z :c.e ] )
2012-08-24 20:50:56 +00:00
( where [ :or
2013-08-06 18:27:56 +00:00
[ :and [ := :f.a "bort" ] [ :not= :b.baz :?param1 ] ]
2012-08-24 20:50:56 +00:00
[ :< 1 2 3 ]
2012-08-25 02:47:54 +00:00
[ :in :f.e [ 1 ( sql/param :param2 ) 3 ] ]
2012-08-24 20:50:56 +00:00
[ :between :f.e 10 20 ] ] )
2012-08-24 21:08:22 +00:00
;;(merge-where [:not= nil :b.bla])
2012-08-24 20:50:56 +00:00
( group :f.a )
( having [ :< 0 :f.e ] )
( order-by [ :b.baz :desc ] :c.quux )
( limit 50 )
2012-08-24 21:08:22 +00:00
( offset 10 ) )
2013-08-06 18:14:40 +00:00
m2 { :select [ :f.* :b.baz :c.quux [ :b.bla "bla bla" ]
2013-08-06 18:05:17 +00:00
:%now ( sql/raw "@x := 10" ) ]
2012-08-24 21:08:22 +00:00
;;:un-select :c.quux
:modifiers :distinct
:from [ [ :foo :f ] [ :baz :b ] ]
2012-10-19 16:41:26 +00:00
:join [ :draq [ := :f.b :draq.x ] ]
:left-join [ [ :clod :c ] [ := :f.a :c.d ] ]
:right-join [ :bock [ := :bock.z :c.e ] ]
2012-08-24 21:08:22 +00:00
:where [ :or
2013-08-06 18:27:56 +00:00
[ :and [ := :f.a "bort" ] [ :not= :b.baz :?param1 ] ]
2012-08-24 21:08:22 +00:00
[ :< 1 2 3 ]
2012-08-25 02:47:54 +00:00
[ :in :f.e [ 1 ( sql/param :param2 ) 3 ] ]
2012-08-24 21:08:22 +00:00
[ :between :f.e 10 20 ] ]
;;:merge-where [:not= nil :b.bla]
:group-by :f.a
:having [ :< 0 :f.e ]
:order-by [ [ :b.baz :desc ] :c.quux ]
:limit 50
:offset 10 }
m3 ( sql/build m2 )
m4 ( apply sql/build ( apply concat m2 ) ) ]
( testing "Various construction methods are consistent"
( is ( = m1 m3 ) ) )
2012-08-24 20:50:56 +00:00
( testing "SQL data formats correctly"
2012-08-25 02:47:54 +00:00
( is ( = ( sql/format m1 { :param1 "gabba" :param2 2 } )
2013-08-06 18:14:40 +00:00
[ "SELECT DISTINCT f.*, b.baz, c.quux, b.bla AS \"bla bla\", now(), @x := 10 FROM foo AS f, baz AS b INNER JOIN draq ON f.b = draq.x LEFT JOIN clod AS c ON f.a = c.d RIGHT JOIN bock ON bock.z = c.e WHERE ((f.a = ? AND b.baz <> ?) OR (1 < 2 AND 2 < 3) OR (f.e in (1, ?, 3)) OR f.e BETWEEN 10 AND 20) GROUP BY f.a HAVING 0 < f.e ORDER BY b.baz DESC, c.quux LIMIT 50 OFFSET 10 "
2012-08-25 02:47:54 +00:00
"bort" "gabba" 2 ] ) ) )
2012-08-24 20:50:56 +00:00
( testing "SQL data prints and reads correctly"
2013-08-06 18:14:40 +00:00
( is ( = m1 ( read-string ( pr-str m1 ) ) ) ) ) ) )