PageRenderTime 195ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wheels/tests/model/crud/finders.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 214 lines | 177 code | 37 blank | 0 comment | 1 complexity | e6286d6648f950e32f5e9eb28baed2ed MD5 | raw file
Possible License(s): Apache-2.0, CPL-1.0
  1. <cfcomponent extends="wheelsMapping.Test">
  2. <cffunction name="setup">
  3. <cfset loc.user = model("user")>
  4. </cffunction>
  5. <cffunction name="test_select_distinct_addresses">
  6. <cfset loc.q = loc.user.findAll(select="address", distinct="true", order="address")>
  7. <cfset assert('loc.q.recordcount eq 4')>
  8. <cfset loc.e = "123 Petruzzi St.|456 Peters Dr.|789 Djurner Ave.|987 Riera Blvd.">
  9. <cfset loc.r = valuelist(loc.q.address, "|")>
  10. <cfset assert('loc.e eq loc.r')>
  11. </cffunction>
  12. <cffunction name="test_select_users_groupby_address">
  13. <cfset loc.q = loc.user.findAll(select="address", group="address", order="address", result="loc.result")>
  14. <cfset assert('loc.q.recordcount eq 4')>
  15. <cfset loc.e = "123 Petruzzi St.|456 Peters Dr.|789 Djurner Ave.|987 Riera Blvd.">
  16. <cfset loc.r = valuelist(loc.q.address, "|")>
  17. <cfset assert('loc.e eq loc.r')>
  18. </cffunction>
  19. <cffunction name="test_findByKey">
  20. <cfset loc.e = loc.user.findOne(where="lastname = 'Petruzzi'")>
  21. <cfset loc.q = loc.user.findByKey(loc.e.id)>
  22. <cfset assert('loc.q.id eq loc.e.id')>
  23. </cffunction>
  24. <cffunction name="test_findByKey_returns_false_when_record_not_found">
  25. <cfset loc.q = loc.user.findByKey(999999999)>
  26. <cfset assert('loc.q eq false')>
  27. </cffunction>
  28. <cffunction name="test_findByKey_returns_false_when_passed_blank_string">
  29. <cfset loc.q = loc.user.findByKey("")>
  30. <cfset assert('loc.q eq false')>
  31. </cffunction>
  32. <cffunction name="test_findByKey_returns_empty_query_when_record_not_found_with_return_as_equal_query">
  33. <cfset loc.q = loc.user.findByKey(key=999999999, returnAs="query")>
  34. <cfset assert('loc.q.RecordCount eq false')>
  35. </cffunction>
  36. <cffunction name="test_findOne">
  37. <cfset loc.e = loc.user.findOne(where="lastname = 'Petruzzi'")>
  38. <cfset assert('isobject(loc.e)')>
  39. </cffunction>
  40. <cffunction name="test_findOne_returns_false_when_record_not_found">
  41. <cfset loc.e = loc.user.findOne(where="lastname = 'somenamenotfound'")>
  42. <cfset assert('loc.e eq false')>
  43. </cffunction>
  44. <cffunction name="test_findOne_returns_empty_query_when_record_not_found_with_return_as_equal_query">
  45. <cfset loc.e = loc.user.findOne(where="lastname = 'somenamenotfound'", returnAs="query")>
  46. <cfset assert('loc.e.RecordCount eq false')>
  47. </cffunction>
  48. <cffunction name="test_findOne_returns_false_when_record_not_found_with_inner_join_include">
  49. <cfset loc.e = loc.user.findOne(where="lastname= = 'somenamenotfound'", include="galleries") />
  50. <cfset assert('loc.e eq false')>
  51. </cffunction>
  52. <cffunction name="test_findOne_returns_false_when_record_not_found_with_outer_join_include">
  53. <cfset loc.e = loc.user.findOne(where="lastname= = 'somenamenotfound'", include="outerjoinphotogalleries") />
  54. <cfset assert('loc.e eq false')>
  55. </cffunction>
  56. <cffunction name="test_findAll">
  57. <cfset loc.q = loc.user.findAll()>
  58. <cfset assert('loc.q.recordcount eq 5')>
  59. <cfset loc.q = loc.user.findAll(where="lastname = 'Petruzzi' OR lastname = 'Peters'", order="lastname")>
  60. <cfset assert('loc.q.recordcount eq 2')>
  61. <cfset assert('valuelist(loc.q.lastname) eq "peters,Petruzzi"')>
  62. </cffunction>
  63. <cffunction name="test_findAllByXXX">
  64. <cfset loc.q = loc.user.findAllByZipcode(value="22222", order="id")>
  65. <cfset assert('loc.q.recordcount eq 2')>
  66. <cfset loc.q = loc.user.findAllByZipcode(value="11111", order="id")>
  67. <cfset assert('loc.q.recordcount eq 1')>
  68. </cffunction>
  69. <cffunction name="test_findByKey_norecords_returns_correct_type">
  70. <cfset loc.q = loc.user.findByKey("0")>
  71. <cfset debug('loc.q', false)>
  72. <cfset assert('isboolean(loc.q) and loc.q eq false')>
  73. <cfset loc.q = loc.user.findByKey(key="0", returnas="query")>
  74. <cfset debug('loc.q', false)>
  75. <cfset assert('isquery(loc.q) and loc.q.recordcount eq 0')>
  76. <cfset loc.q = loc.user.findByKey(key="0", returnas="object")>
  77. <cfset debug('loc.q', false)>
  78. <cfset assert('isboolean(loc.q) and loc.q eq false')>
  79. <!--- readd when we have implemented the code to throw an error when an incorrect returnAs value is passed in
  80. <cfset loc.q = raised('loc.user.findByKey(key="0", returnas="objects")')>
  81. <cfset loc.r = "Wheels.IncorrectArgumentValue">
  82. <cfset debug('loc.q', false)>
  83. <cfset assert('loc.q eq loc.r')> --->
  84. </cffunction>
  85. <cffunction name="test_findOne_norecords_returns_correct_type">
  86. <cfset loc.q = loc.user.findOne(where="id = 0")>
  87. <cfset debug('loc.q', false)>
  88. <cfset assert('isboolean(loc.q) and loc.q eq false')>
  89. <cfset loc.q = loc.user.findOne(where="id = 0", returnas="query")>
  90. <cfset debug('loc.q', false)>
  91. <cfset assert('isquery(loc.q) and loc.q.recordcount eq 0')>
  92. <cfset loc.q = loc.user.findOne(where="id = 0", returnas="object")>
  93. <cfset debug('loc.q', false)>
  94. <cfset assert('isboolean(loc.q) and loc.q eq false')>
  95. <!--- readd when we have implemented the code to throw an error when an incorrect returnAs value is passed in
  96. <cfset loc.q = raised('loc.user.findOne(where="id = 0", returnas="objects")')>
  97. <cfset loc.r = "Wheels.IncorrectArgumentValue">
  98. <cfset debug('loc.q', false)>
  99. <cfset assert('loc.q eq loc.r')> --->
  100. </cffunction>
  101. <cffunction name="test_findAll_returnAs_query_noRecords_returns_correct_type">
  102. <cfset loc.q = loc.user.findAll(where="id = 0", returnas="query")>
  103. <cfset debug('loc.q', false)>
  104. <cfset assert('isquery(loc.q) and loc.q.recordcount eq 0')>
  105. </cffunction>
  106. <cffunction name="test_findAll_returnAs_structs_noRecords_returns_correct_type">
  107. <cfset loc.q = loc.user.findAll(where="id = 0", returnAs="structs")>
  108. <cfset debug('loc.q', false)>
  109. <cfset assert('isarray(loc.q) and arrayisempty(loc.q)')>
  110. </cffunction>
  111. <cffunction name="test_findAll_returnAs_objects_noRecords_returns_correct_type">
  112. <cfset loc.q = loc.user.findAll(where="id = 0", returnas="objects")>
  113. <cfset debug('loc.q', false)>
  114. <cfset assert('isarray(loc.q) and arrayisempty(loc.q)')>
  115. </cffunction>
  116. <cffunction name="test_findAll_returnAs_invalid_throws_error">
  117. <cfset loc.q = raised('loc.user.findAll(where="id = 1", returnas="notvalid")')>
  118. <cfset loc.r = "Wheels.IncorrectArgumentValue">
  119. <cfset debug('loc.q', false)>
  120. <cfset assert('loc.q eq loc.r')>
  121. </cffunction>
  122. <cffunction name="test_exists_by_key_valid">
  123. <cfset loc.e = loc.user.findOne(where="lastname = 'Petruzzi'")>
  124. <cfset loc.r = loc.user.exists(loc.e.id)>
  125. <cfset assert('loc.r eq true')>
  126. </cffunction>
  127. <cffunction name="test_exists_by_key_invalid">
  128. <cfset loc.r = loc.user.exists(0)>
  129. <cfset assert('loc.r eq false')>
  130. </cffunction>
  131. <cffunction name="test_exists_by_where_one_record_valid">
  132. <cfset loc.r = loc.user.exists(where="lastname = 'Petruzzi'")>
  133. <cfset assert('loc.r eq true')>
  134. </cffunction>
  135. <cffunction name="test_exists_by_where_one_record_invalid">
  136. <cfset loc.r = loc.user.exists(where="lastname = 'someoneelse'")>
  137. <cfset assert('loc.r eq false')>
  138. </cffunction>
  139. <cffunction name="test_exists_by_where_two_records_valid">
  140. <cfset loc.r = loc.user.exists(where="zipcode = '22222'")>
  141. <cfset assert('loc.r eq true')>
  142. </cffunction>
  143. <cffunction name="test_allow_negative_values_in_where_clause">
  144. <cfset loc.r = loc.user.exists(where="id = -1")>
  145. <cfset assert('loc.r eq false')>
  146. </cffunction>
  147. <cffunction name="test_findByKey_with_include_soft_deletes">
  148. <cftransaction action="begin">
  149. <cfset loc.post1 = model("Post").findOne()>
  150. <cfset loc.post1.delete(transaction="none")>
  151. <cfset loc.post2 = model("Post").findByKey(key=loc.post1.id, includeSoftDeletes=true)>
  152. <cftransaction action="rollback" />
  153. </cftransaction>
  154. <cfset assert('IsObject(loc.post2) is true')>
  155. </cffunction>
  156. <cffunction name="test_findOne_with_include_soft_deletes">
  157. <cftransaction action="begin">
  158. <cfset loc.post1 = model("Post").findOne()>
  159. <cfset loc.post1.delete(transaction="none")>
  160. <cfset loc.post2 = model("Post").findOne(where="id=#loc.post1.id#", includeSoftDeletes=true)>
  161. <cftransaction action="rollback" />
  162. </cftransaction>
  163. <cfset assert('IsObject(loc.post2) is true')>
  164. </cffunction>
  165. <cffunction name="test_findAll_with_include_soft_deletes">
  166. <cftransaction action="begin">
  167. <cfset model("Post").deleteAll()>
  168. <cfset loc.allPosts = model("Post").findAll(includeSoftDeletes=true)>
  169. <cftransaction action="rollback" />
  170. </cftransaction>
  171. <cfset assert('loc.allPosts.recordcount eq 4')>
  172. </cffunction>
  173. <cffunction name="test_findOne_returns_empty_array_for_included_model_when_none_exist">
  174. <cfset loc.e = model("author").findOne(where="lastname = 'Bellenie'", include="posts")>
  175. <cfset assert('IsArray(loc.e.posts) && ArrayIsEmpty(loc.e.posts)')>
  176. </cffunction>
  177. </cfcomponent>