Browse Source

Make http-kit db test comply with verification

1. Include id value.
2. Preserve case of randomNumber colum.
3. Clamp query count between 1 and 500 as required.
Keith R. Gustafson 11 years ago
parent
commit
6c9c09e8e3
1 changed files with 14 additions and 2 deletions
  1. 14 2
      http-kit/hello/src/hello/handler.clj

+ 14 - 2
http-kit/hello/src/hello/handler.clj

@@ -19,7 +19,9 @@
 ;; Query a random World record from the database
 (defn get-world []
   (let [id (inc (rand-int 9999))] ; Num between 1 and 10,000
-    (db/query "select id randomNumber from world where id = ?" id)))
+    ; Set a naming strategy to preserve column name case
+    (clojure.java.jdbc/with-naming-strategy {:keyword identity}
+      (db/query "select * from world where id = ?" id))))
 
 ;; Run the specified number of queries, return the results
 (defn run-queries [queries]
@@ -28,6 +30,16 @@
      queries ; Number of queries to run
      (repeatedly get-world))))
 
+(defn get-query-count [queries]
+  "Parse provided string value of query count, clamping values to between 1 and 500."
+  (let [q (try (Integer/parseInt queries)
+               (catch Exception e 1))] ; default to 1 on parse failure
+    (if (> q 500)
+      500 ; clamp to 500 max
+      (if (< q 1)
+        1 ; clamp to 1 min
+        q)))) ; otherwise use provided value
+
 ;; Define route handlers
 (defroutes app-routes
   (GET "/http-kit/" [] "Hello, World!")
@@ -35,7 +47,7 @@
   (GET "/http-kit/db" []
        (response (first (run-queries 1))))
   (GET "/http-kit/db/:queries" [queries]
-       (response (run-queries (Integer/parseInt queries))))
+       (response (run-queries (get-query-count queries))))
   (route/not-found "Not Found"))