PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/wheels/tests/model/crud/transactions.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 198 lines | 171 code | 27 blank | 0 comment | 0 complexity | 782e1b924b23a59b3ccfc0c714bfda7a MD5 | raw file
Possible License(s): Apache-2.0, CPL-1.0
  1. <cfcomponent extends="wheelsMapping.Test">
  2. <cffunction name="setup">
  3. <cfset application.wheels.transactionMode = "commit">
  4. </cffunction>
  5. <cffunction name="teardown">
  6. <cfset application.wheels.transactionMode = "none">
  7. </cffunction>
  8. <cffunction name="test_create_rollbacks_when_callback_returns_false">
  9. <cfset loc.tag = model("tagFalseCallbacks").create(name="Kermit", description="The Frog")>
  10. <cfset loc.tag = model("tagFalseCallbacks").findOne(where="name='Kermit'")>
  11. <cfset assert("loc.tag IS false")>
  12. </cffunction>
  13. <cffunction name="test_update_rollbacks_when_callback_returns_false">
  14. <cfset loc.tag = model("tagFalseCallbacks").findOne(where="description='testdesc'")>
  15. <cfset loc.tag.update(name="Kermit")>
  16. <cfset loc.tag = model("tagFalseCallbacks").findOne(where="description='testdesc'")>
  17. <cfset assert("loc.tag.name IS 'releases'")>
  18. </cffunction>
  19. <cffunction name="test_save_rollbacks_when_callback_returns_false">
  20. <cfset loc.tag = model("tagFalseCallbacks").findOne(where="description='testdesc'")>
  21. <cfset loc.tag.name = "Kermit">
  22. <cfset loc.tag.save()>
  23. <cfset loc.tag = model("tagFalseCallbacks").findOne(where="description='testdesc'")>
  24. <cfset assert("loc.tag.name IS 'releases'")>
  25. </cffunction>
  26. <cffunction name="test_delete_rollbacks_when_callback_returns_false">
  27. <cfset loc.tag = model("tagFalseCallbacks").findOne(where="description='testdesc'")>
  28. <cfset loc.tag.delete()>
  29. <cfset loc.tag = model("tagFalseCallbacks").findOne(where="description='testdesc'")>
  30. <cfset assert("IsObject(loc.tag)")>
  31. </cffunction>
  32. <cffunction name="test_deleteAll_with_instantiate_rollbacks_when_callback_returns_false">
  33. <cfset model("tagFalseCallbacks").deleteAll(instantiate=true)>
  34. <cfset loc.results = model("tagFalseCallbacks").findAll()>
  35. <cfset assert("loc.results.recordcount IS 8")>
  36. </cffunction>
  37. <cffunction name="test_updateAll_with_instantiate_rollbacks_when_callback_returns_false">
  38. <cfset model("tagFalseCallbacks").updateAll(name="Kermit", instantiate=true)>
  39. <cfset loc.results = model("tagFalseCallbacks").findAll(where="name = 'Kermit'")>
  40. <cfset assert("loc.results.recordcount IS 0")>
  41. </cffunction>
  42. <cffunction name="test_create_with_rollback">
  43. <cfset loc.tag = model("tag").create(name="Kermit", description="The Frog", transaction="rollback")>
  44. <cfset loc.tag = model("tag").findOne(where="name='Kermit'")>
  45. <cfset assert("not IsObject(loc.tag)")>
  46. </cffunction>
  47. <cffunction name="test_update_with_rollback">
  48. <cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
  49. <cfset loc.tag.update(name="Kermit", transaction="rollback")>
  50. <cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
  51. <cfset assert("loc.tag.name IS 'releases'")>
  52. </cffunction>
  53. <cffunction name="test_save_with_rollback">
  54. <cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
  55. <cfset loc.tag.name = "Kermit">
  56. <cfset loc.tag.save(transaction="rollback")>
  57. <cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
  58. <cfset assert("loc.tag.name IS 'releases'")>
  59. </cffunction>
  60. <cffunction name="test_delete_with_rollback">
  61. <cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
  62. <cfset loc.tag.delete(transaction="rollback")>
  63. <cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
  64. <cfset assert("IsObject(loc.tag)")>
  65. </cffunction>
  66. <cffunction name="test_deleteAll_with_rollback">
  67. <cfset model("tag").deleteAll(instantiate=true, transaction="rollback")>
  68. <cfset loc.results = model("tag").findAll()>
  69. <cfset assert("loc.results.recordcount IS 8")>
  70. </cffunction>
  71. <cffunction name="test_updateAll_with_rollback">
  72. <cfset model("tag").updateAll(name="Kermit", instantiate=true, transaction="rollback")>
  73. <cfset loc.results = model("tag").findAll(where="name = 'Kermit'")>
  74. <cfset assert("loc.results.recordcount IS 0")>
  75. </cffunction>
  76. <cffunction name="test_create_with_transactions_disabled">
  77. <cftransaction>
  78. <cfset loc.tag = model("tag").create(name="Kermit", description="The Frog", transaction="none")>
  79. <cfset loc.tag = model("tag").findOne(where="name='Kermit'")>
  80. <cfset assert("IsObject(loc.tag)")>
  81. <cftransaction action="rollback" />
  82. </cftransaction>
  83. </cffunction>
  84. <cffunction name="test_update_with_transactions_disabled">
  85. <cftransaction>
  86. <cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
  87. <cfset loc.tag.update(name="Kermit", transaction="none")>
  88. <cfset loc.tag.reload()>
  89. <cfset assert("loc.tag.name IS 'Kermit'")>
  90. <cftransaction action="rollback" />
  91. </cftransaction>
  92. </cffunction>
  93. <cffunction name="test_save_with_transactions_disabled">
  94. <cftransaction>
  95. <cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
  96. <cfset loc.tag.name = "Kermit">
  97. <cfset loc.tag.save(transaction="none")>
  98. <cfset loc.tag.reload()>
  99. <cfset assert("loc.tag.name IS 'Kermit'")>
  100. <cftransaction action="rollback" />
  101. </cftransaction>
  102. </cffunction>
  103. <cffunction name="test_delete_with_transactions_disabled">
  104. <cftransaction>
  105. <cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
  106. <cfset loc.tag.delete(transaction="none")>
  107. <cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
  108. <cfset assert("not IsObject(loc.tag)")>
  109. <cftransaction action="rollback" />
  110. </cftransaction>
  111. </cffunction>
  112. <cffunction name="test_deleteAll_with_transactions_disabled">
  113. <cftransaction>
  114. <cfset model("tag").deleteAll(instantiate=true, transaction="none")>
  115. <cfset loc.results = model("tag").findAll()>
  116. <cfset assert("loc.results.recordcount IS 0")>
  117. <cftransaction action="rollback" />
  118. </cftransaction>
  119. </cffunction>
  120. <cffunction name="test_updateAll_with_transactions_disabled">
  121. <cftransaction>
  122. <cfset model("tag").updateAll(name="Kermit", instantiate=true, transaction="none")>
  123. <cfset loc.results = model("tag").findAll(where="name = 'Kermit'")>
  124. <cfset assert("loc.results.recordcount IS 8")>
  125. <cftransaction action="rollback" />
  126. </cftransaction>
  127. </cffunction>
  128. <cffunction name="test_nested_transaction_within_callback">
  129. <cfset loc.tag = model("tagWithDataCallbacks").create(name="Kermit", description="The Frog", transaction="rollback")>
  130. <cfset assert("IsObject(loc.tag)")>
  131. </cffunction>
  132. <cffunction name="test_nested_transaction_within_callback_with_transactions_disabled">
  133. <cftransaction>
  134. <cfset loc.tag = model("tagWithDataCallbacks").create(name="Kermit", description="The Frog", transaction="none")>
  135. <cfset assert("IsObject(loc.tag)")>
  136. <cftransaction action="rollback" />
  137. </cftransaction>
  138. </cffunction>
  139. <cffunction name="test_transaction_closed_after_rollback">
  140. <cfset loc.hash = model("tag").$hashedConnectionArgs()>
  141. <cfset loc.tag = model("tagWithDataCallbacks").create(name="Kermit", description="The Frog", transaction="rollback")>
  142. <cfset assert('request.wheels.transactions[loc.hash] eq false')>
  143. </cffunction>
  144. <cffunction name="test_transaction_closed_after_none">
  145. <cfset loc.hash = model("tag").$hashedConnectionArgs()>
  146. <cftransaction>
  147. <cfset loc.tag = model("tagWithDataCallbacks").create(name="Kermit", description="The Frog", transaction="none")>
  148. <cftransaction action="rollback" />
  149. </cftransaction>
  150. <cfset assert('request.wheels.transactions[loc.hash] eq false')>
  151. </cffunction>
  152. <cffunction name="test_transaction_closed_when_error_raised">
  153. <cfset loc.hash = model("tag").$hashedConnectionArgs()>
  154. <cftry>
  155. <cfset loc.tag = model("tag").create(id="", name="Kermit", description="The Frog", transaction="rollback")>
  156. <cfcatch type="any"></cfcatch>
  157. </cftry>
  158. <cfset assert('request.wheels.transactions[loc.hash] eq false')>
  159. </cffunction>
  160. <cffunction name="test_rollback_when_error_raised">
  161. <cfset loc.tag = Duplicate(model("tagWithDataCallbacks").new(name="Kermit", description="The Frog"))>
  162. <cfset loc.tag.afterSave(methods="crashMe")>
  163. <cftry>
  164. <cfset loc.tag.save()>
  165. <cfcatch>
  166. <cfset loc.results = model("tag").findAll(where="name = 'Kermit'")>
  167. </cfcatch>
  168. </cftry>
  169. <cfset assert("loc.results.recordcount IS 0")>
  170. </cffunction>
  171. </cfcomponent>