PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/test/cynojure/tests.clj

http://github.com/kriyative/cynojure
Clojure | 148 lines | 128 code | 10 blank | 10 comment | 38 complexity | ca656d96d987abd7c2dc9b24c3cc07f8 MD5 | raw file
  1. ;; tests.clj -- unit tests for cynojure
  2. ;; Ram Krishnan, http://cynojure.posterous.com/
  3. ;; Copyright (c) Ram Krishnan, 2009. All rights reserved. The use and
  4. ;; distribution terms for this software are covered by the Eclipse
  5. ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
  6. ;; which can be found in the file epl-v10.html at the root of this
  7. ;; distribution. By using this software in any fashion, you are
  8. ;; agreeing to be bound by the terms of this license. You must not
  9. ;; remove this notice, or any other, from this software.
  10. (ns cynojure.tests
  11. (:use clojure.contrib.sql)
  12. (:use clojure.contrib.test-is)
  13. (:use cynojure.cl)
  14. (:use cynojure.util)
  15. (:use cynojure.sql))
  16. (deftest test-cl
  17. (is (= (ignore-errors (+ 1 2)) 3))
  18. (is (= (ignore-errors (/ 5 0)) nil))
  19. (is (let [tm 3356647006]
  20. (= (date-to-universal-time (universal-time-to-date tm)) tm))))
  21. (deftest test-util
  22. (is (= (tostr :foo) "foo"))
  23. (is (= (mklist 'a) '(a)))
  24. (is (= (mklist '(a)) '(a)))
  25. (is (= (ip-to-dotted (dotted-to-ip "127.0.0.1")) "127.0.0.1"))
  26. (is (= (md5-sum "foobar") "3858f62230ac3c915f300c664312c63f"))
  27. (is (not (nil? (get-system-classpaths))))
  28. (is (= (csv-escape "foo's, goal") "\"foo''s, goal\""))
  29. (is (not (nil? (uuid))))
  30. (is (= (parse-int "123") 123))
  31. (is (nil? (parse-int "@bar")))
  32. (is (= (.toString (parse-date "Tue, 09 Jun 2009 22:11:40 GMT")) "Tue Jun 09 15:11:40 PDT 2009"))
  33. (is (= (.toString (parse-sql-date "Tue, 09 Jun 2009 22:11:40 GMT")) "2009-06-09"))
  34. (is (= (.toString (parse-sql-timestamp "Tue, 09 Jun 2009 22:11:09 GMT")) "2009-06-09 15:11:09.0"))
  35. (is (= (:host (parse-url "http://www.google.com/")) "www.google.com"))
  36. (is (= (str-trim "abc123" 3) "abc"))
  37. (is (= (str-trim "abc" 5) "abc"))
  38. (is (= (string= "foobar" "FooBar") true))
  39. (is (= (char-numeric? \5) true))
  40. (is (= (char-numeric? \a) false))
  41. )
  42. (deftest test-sql
  43. (is (= (sql [1 2 3]) "(1,2,3)"))
  44. (is (= (sql "foo'bar") "'foo''bar'"))
  45. (is (= (sql [:as :age :foo]) "age AS foo"))
  46. (is (= (sql [:in :age [20 25 30]]) "(age IN (20,25,30))"))
  47. (is (= (sql [:and [:>= :age 20] [:like :name "%smith%"]])
  48. "((age >= 20) AND (name LIKE '%smith%'))"))
  49. (is (= (sql [:and [:>= :age 20] [:= :name "O'Henry"]])
  50. "((age >= 20) AND (name = 'O''Henry'))"))
  51. (is (= (query-str [:count 1]
  52. :from :person
  53. :where [:and [:>= :age 20] [:<= :age 40] [:like :first-name "%smith%"]]
  54. :order-by '((:name :asc) (:id :desc)))
  55. "SELECT COUNT(1) FROM person WHERE ((age >= 20) AND (age <= 40) AND (first_name LIKE '%smith%')) ORDER BY name asc,id desc"))
  56. (is (= (query-str [:as [:count 1] :cnt]
  57. :from :person
  58. :where [:>= :age 20])
  59. "SELECT COUNT(1) AS cnt FROM person WHERE (age >= 20)"))
  60. (is (= (query-str '(:name :age :sex)
  61. :from :person
  62. :where [:and [:>= :age 20] [:<= :age 40]]
  63. :order-by '((:name :asc)))
  64. "SELECT name,age,sex FROM person WHERE ((age >= 20) AND (age <= 40)) ORDER BY name asc"))
  65. (is (= (update-str 'person
  66. {:name "Alice", :age 30},
  67. :where [:= :id 123])
  68. "UPDATE person SET name=?,age=? WHERE (id = 123)"))
  69. (is (= (create-temp-table :report-p-daily-d
  70. '((:p-id :bigint)
  71. (:d :integer :default 0))
  72. :on-commit :drop)
  73. "CREATE TEMP TABLE report_p_daily_d ( p_id bigint,d integer default 0 ) ON COMMIT drop"))
  74. (is (= (create-temp-table :report-p-daily-l1
  75. '((:p-id :bigint)
  76. (:ip-address :bigint)
  77. (:l1 :integer :default 0))
  78. :on-commit :drop)
  79. "CREATE TEMP TABLE report_p_daily_l1 ( p_id bigint,ip_address bigint,l1 integer default 0 ) ON COMMIT drop"))
  80. (is (= (insert-into :report-p-daily-d
  81. '(:p-id :d)
  82. (select '(:p-id [:count 1])
  83. :from :audit-log-entry
  84. :where [:and
  85. [:= :o-id 1]
  86. [:>= :ctime 3468470400]
  87. [:< :ctime 3468556800]]
  88. :group-by :p-id))
  89. "INSERT INTO report_p_daily_d (p_id,d) VALUES (SELECT p_id,COUNT(1) FROM audit_log_entry WHERE ((o_id = 1) AND (ctime >= 3468470400) AND (ctime < 3468556800)) GROUP BY p_id)"))
  90. (is (= (insert-into :report-p-daily-l2
  91. '(:p-id :l2)
  92. (select '(:p-id [:count 1])
  93. :from :audit-log-entry
  94. :where [:and
  95. [:= :o-id 1]
  96. [:>= :ctime 3468470400]
  97. [:< :ctime 3468556800]
  98. [:or [:like :user-agent "Mozilla%"] [:like :user-agent "Opera%"]]]
  99. :group-by :p-id))
  100. "INSERT INTO report_p_daily_l2 (p_id,l2) VALUES (SELECT p_id,COUNT(1) FROM audit_log_entry WHERE ((o_id = 1) AND (ctime >= 3468470400) AND (ctime < 3468556800) AND ((user_agent LIKE 'Mozilla%') OR (user_agent LIKE 'Opera%'))) GROUP BY p_id)"))
  101. (is (= (insert-into :report-p-daily-s
  102. '(:p-id :s)
  103. (select '(:foo.p-id [:count 1])
  104. :from (as (select '(:p-id :ip-address)
  105. :from :audit-log-entry
  106. :where [:and
  107. [:= :o-id 1]
  108. [:>= :ctime 3468470400]
  109. [:< :ctime 3468556800]
  110. [:not [:or [:like :user-agent "Mozilla%"] [:like :user-agent "Opera%"]]]]
  111. :group-by '(:p-id :ip-address))
  112. :foo)
  113. :group-by :foo.p-id))
  114. "INSERT INTO report_p_daily_s (p_id,s) VALUES (SELECT foo.p_id,COUNT(1) FROM (SELECT p_id,ip_address FROM audit_log_entry WHERE ((o_id = 1) AND (ctime >= 3468470400) AND (ctime < 3468556800) AND NOT(((user_agent LIKE 'Mozilla%') OR (user_agent LIKE 'Opera%')))) GROUP BY p_id,ip_address) as foo GROUP BY foo.p_id)"))
  115. (is (= (insert-into :report-p-daily
  116. '(:ctime :p-id :d :s :l2 :l1)
  117. (select '(3468470400
  118. :report-p-daily-d.p-id
  119. :report-p-daily-d.d
  120. :report-p-daily-s.s
  121. :report-p-daily-l2.l2
  122. :report-p-daily-l1.l1)
  123. :from :report-p-daily-d
  124. :full-outer-join '((:report-p-daily-s
  125. [:= :report-p-daily-d.p-id :report-p-daily-s.p-id])
  126. (:report-p-daily-l2
  127. [:= :report-p-daily-d.p-id :report-p-daily-l2.p-id])
  128. (:report-p-daily-l1
  129. [:= :report-p-daily-d.p-id :report-p-daily-l1.p-id]))))
  130. "INSERT INTO report_p_daily (ctime,p_id,d,s,l2,l1) VALUES (SELECT 3468470400,report_p_daily_d.p_id,report_p_daily_d.d,report_p_daily_s.s,report_p_daily_l2.l2,report_p_daily_l1.l1 FROM report_p_daily_d FULL OUTER JOIN report_p_daily_s ON (report_p_daily_d.p_id = report_p_daily_s.p_id) FULL OUTER JOIN report_p_daily_l2 ON (report_p_daily_d.p_id = report_p_daily_l2.p_id) FULL OUTER JOIN report_p_daily_l1 ON (report_p_daily_d.p_id = report_p_daily_l1.p_id))"))
  131. (is (= (insert-into :actor
  132. '(:firstname :surname :password)
  133. [["firstname" "surname" "password"]
  134. ["firstname" "surname" "password"]]
  135. :returning [:a :b])
  136. "INSERT INTO actor (firstname,surname,password) VALUES ('firstname','surname','password'), ('firstname','surname','password') RETURNING (a,b)"))
  137. )
  138. ;; (run-tests 'cynojure.tests)