Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add find-record, which is (first (find-records ...)). Also rename som…

…e tests.
  • Loading branch information...
commit 185165d5db0d2431bc0b17a137fba71477626761 1 parent 1df96f4
John Hume authored
6  src/clj_record/core.clj
@@ -102,6 +102,10 @@
102 102
         select-query (format "select * from %s where %s" (table-name model-name) parameterized-where)]
103 103
     (find-by-sql model-name (apply vector select-query values))))
104 104
 
  105
+(defn find-record
  106
+  [model-name attributes-or-where-params]
  107
+  (first (find-records model-name attributes-or-where-params)))
  108
+
105 109
 (defn update
106 110
   "Updates by (partial-record :id), updating only those columns included in partial-record."
107 111
   [model-name partial-record]
@@ -167,6 +171,8 @@
167 171
         (get-record ~model-name id#))
168 172
       (defn ~'find-records [attributes#]
169 173
         (find-records ~model-name attributes#))
  174
+      (defn ~'find-record [attributes#]
  175
+        (find-record ~model-name attributes#))
170 176
       (defn ~'find-by-sql [select-query-and-values#]
171 177
         (find-by-sql ~model-name select-query-and-values#))
172 178
       (defn ~'create [attributes#]
21  src/clj_record/test/core_test.clj
@@ -21,23 +21,38 @@
21 21
         acme (manufacturer/get-record id)]
22 22
     (is (= "ACME" (acme :name)))))
23 23
 
24  
-(defdbtest get-record-by-id
  24
+(defdbtest get-record-does-a-lookup-by-id
25 25
   (let [humedai (manufacturer/create (valid-manufacturer-with {:name "Humedai Motors"}))]
26 26
     (is (= humedai (manufacturer/get-record (humedai :id))))))
27 27
 
28 28
 (defdbtest get-record-throws-if-not-found
29 29
   (is (thrown? IllegalArgumentException (manufacturer/get-record -1))))
30 30
 
31  
-(defdbtest find-records-by-attribute-equality-conditions
  31
+(defdbtest find-records-can-do-lookup-by-attribute-equality-conditions
32 32
   (let [humedai (manufacturer/create (valid-manufacturer-with {:name "Humedai Motors"}))
33 33
         other-1 (manufacturer/create (valid-manufacturer-with {:name "Some Other"}))]
34 34
     (is (= [humedai] (manufacturer/find-records {:name "Humedai Motors"})))))
35 35
 
36  
-(defdbtest find-records-by-SQL-conditions
  36
+(defdbtest find-records-can-do-lookup-by-a-vector-of-SQL-conditions-followed-by-values
37 37
   (let [humedai (manufacturer/create (valid-manufacturer-with {:name "Humedai Motors"}))
38 38
         other-1 (manufacturer/create (valid-manufacturer-with {:name "Some Other"}))]
39 39
     (is (= [humedai] (manufacturer/find-records ["name = ?" "Humedai Motors"])))))
40 40
 
  41
+(defdbtest find-record-can-do-lookup-by-attribute-equality-conditions
  42
+  (let [humedai (manufacturer/create (valid-manufacturer-with {:name "Humedai Motors"}))
  43
+        other-1 (manufacturer/create (valid-manufacturer-with {:name "Some Other"}))]
  44
+    (is (= humedai (manufacturer/find-record {:name "Humedai Motors"})))))
  45
+
  46
+(defdbtest find-record-can-do-lookup-by-a-vector-of-SQL-conditions-followed-by-values
  47
+  (let [humedai (manufacturer/create (valid-manufacturer-with {:name "Humedai Motors"}))
  48
+        other-1 (manufacturer/create (valid-manufacturer-with {:name "Some Other"}))]
  49
+    (is (= humedai (manufacturer/find-record ["name = ?" "Humedai Motors"])))))
  50
+
  51
+(defdbtest find-record-returns-the-first-matching-record
  52
+  (let [humedai (manufacturer/create (valid-manufacturer-with {:grade 90}))
  53
+        other-1 (manufacturer/create (valid-manufacturer-with {:grade 90}))]
  54
+    (is (= humedai (manufacturer/find-record {:grade 90})))))
  55
+
41 56
 (defdbtest find-by-sql-uses-a-complete-query
42 57
   (let [humedai (manufacturer/create (valid-manufacturer-with {:name "Humedai Motors"}))]
43 58
     (is (= (manufacturer/find-records ["name = ?" "Humedai Motors"])

0 notes on commit 185165d

Please sign in to comment.
Something went wrong with that request. Please try again.