PageRenderTime 23ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/rails/activerecord/test/callbacks_test.rb

http://github.com/benburkert/cruisecontrolrb
Ruby | 377 lines | 337 code | 39 blank | 1 comment | 2 complexity | 3cb72f5654642d8f9c2142ae61ce2f29 MD5 | raw file
Possible License(s): Apache-2.0
  1. require 'abstract_unit'
  2. class CallbackDeveloper < ActiveRecord::Base
  3. set_table_name 'developers'
  4. class << self
  5. def callback_string(callback_method)
  6. "history << [#{callback_method.to_sym.inspect}, :string]"
  7. end
  8. def callback_proc(callback_method)
  9. Proc.new { |model| model.history << [callback_method, :proc] }
  10. end
  11. def define_callback_method(callback_method)
  12. define_method("#{callback_method}_method") do |model|
  13. model.history << [callback_method, :method]
  14. end
  15. end
  16. def callback_object(callback_method)
  17. klass = Class.new
  18. klass.send(:define_method, callback_method) do |model|
  19. model.history << [callback_method, :object]
  20. end
  21. klass.new
  22. end
  23. end
  24. ActiveRecord::Callbacks::CALLBACKS.each do |callback_method|
  25. callback_method_sym = callback_method.to_sym
  26. define_callback_method(callback_method_sym)
  27. send(callback_method, callback_method_sym)
  28. send(callback_method, callback_string(callback_method_sym))
  29. send(callback_method, callback_proc(callback_method_sym))
  30. send(callback_method, callback_object(callback_method_sym))
  31. send(callback_method) { |model| model.history << [callback_method_sym, :block] }
  32. end
  33. def history
  34. @history ||= []
  35. end
  36. # after_initialize and after_find are invoked only if instance methods have been defined.
  37. def after_initialize
  38. end
  39. def after_find
  40. end
  41. end
  42. class RecursiveCallbackDeveloper < ActiveRecord::Base
  43. set_table_name 'developers'
  44. before_save :on_before_save
  45. after_save :on_after_save
  46. attr_reader :on_before_save_called, :on_after_save_called
  47. def on_before_save
  48. @on_before_save_called ||= 0
  49. @on_before_save_called += 1
  50. save unless @on_before_save_called > 1
  51. end
  52. def on_after_save
  53. @on_after_save_called ||= 0
  54. @on_after_save_called += 1
  55. save unless @on_after_save_called > 1
  56. end
  57. end
  58. class ImmutableDeveloper < ActiveRecord::Base
  59. set_table_name 'developers'
  60. validates_inclusion_of :salary, :in => 50000..200000
  61. before_save :cancel
  62. before_destroy :cancel
  63. def cancelled?
  64. @cancelled == true
  65. end
  66. private
  67. def cancel
  68. @cancelled = true
  69. false
  70. end
  71. end
  72. class ImmutableMethodDeveloper < ActiveRecord::Base
  73. set_table_name 'developers'
  74. validates_inclusion_of :salary, :in => 50000..200000
  75. def cancelled?
  76. @cancelled == true
  77. end
  78. def before_save
  79. @cancelled = true
  80. false
  81. end
  82. def before_destroy
  83. @cancelled = true
  84. false
  85. end
  86. end
  87. class CallbackCancellationDeveloper < ActiveRecord::Base
  88. set_table_name 'developers'
  89. def before_create
  90. false
  91. end
  92. end
  93. class CallbacksTest < Test::Unit::TestCase
  94. fixtures :developers
  95. def test_initialize
  96. david = CallbackDeveloper.new
  97. assert_equal [
  98. [ :after_initialize, :string ],
  99. [ :after_initialize, :proc ],
  100. [ :after_initialize, :object ],
  101. [ :after_initialize, :block ],
  102. ], david.history
  103. end
  104. def test_find
  105. david = CallbackDeveloper.find(1)
  106. assert_equal [
  107. [ :after_find, :string ],
  108. [ :after_find, :proc ],
  109. [ :after_find, :object ],
  110. [ :after_find, :block ],
  111. [ :after_initialize, :string ],
  112. [ :after_initialize, :proc ],
  113. [ :after_initialize, :object ],
  114. [ :after_initialize, :block ],
  115. ], david.history
  116. end
  117. def test_new_valid?
  118. david = CallbackDeveloper.new
  119. david.valid?
  120. assert_equal [
  121. [ :after_initialize, :string ],
  122. [ :after_initialize, :proc ],
  123. [ :after_initialize, :object ],
  124. [ :after_initialize, :block ],
  125. [ :before_validation, :string ],
  126. [ :before_validation, :proc ],
  127. [ :before_validation, :object ],
  128. [ :before_validation, :block ],
  129. [ :before_validation_on_create, :string ],
  130. [ :before_validation_on_create, :proc ],
  131. [ :before_validation_on_create, :object ],
  132. [ :before_validation_on_create, :block ],
  133. [ :after_validation, :string ],
  134. [ :after_validation, :proc ],
  135. [ :after_validation, :object ],
  136. [ :after_validation, :block ],
  137. [ :after_validation_on_create, :string ],
  138. [ :after_validation_on_create, :proc ],
  139. [ :after_validation_on_create, :object ],
  140. [ :after_validation_on_create, :block ]
  141. ], david.history
  142. end
  143. def test_existing_valid?
  144. david = CallbackDeveloper.find(1)
  145. david.valid?
  146. assert_equal [
  147. [ :after_find, :string ],
  148. [ :after_find, :proc ],
  149. [ :after_find, :object ],
  150. [ :after_find, :block ],
  151. [ :after_initialize, :string ],
  152. [ :after_initialize, :proc ],
  153. [ :after_initialize, :object ],
  154. [ :after_initialize, :block ],
  155. [ :before_validation, :string ],
  156. [ :before_validation, :proc ],
  157. [ :before_validation, :object ],
  158. [ :before_validation, :block ],
  159. [ :before_validation_on_update, :string ],
  160. [ :before_validation_on_update, :proc ],
  161. [ :before_validation_on_update, :object ],
  162. [ :before_validation_on_update, :block ],
  163. [ :after_validation, :string ],
  164. [ :after_validation, :proc ],
  165. [ :after_validation, :object ],
  166. [ :after_validation, :block ],
  167. [ :after_validation_on_update, :string ],
  168. [ :after_validation_on_update, :proc ],
  169. [ :after_validation_on_update, :object ],
  170. [ :after_validation_on_update, :block ]
  171. ], david.history
  172. end
  173. def test_create
  174. david = CallbackDeveloper.create('name' => 'David', 'salary' => 1000000)
  175. assert_equal [
  176. [ :after_initialize, :string ],
  177. [ :after_initialize, :proc ],
  178. [ :after_initialize, :object ],
  179. [ :after_initialize, :block ],
  180. [ :before_validation, :string ],
  181. [ :before_validation, :proc ],
  182. [ :before_validation, :object ],
  183. [ :before_validation, :block ],
  184. [ :before_validation_on_create, :string ],
  185. [ :before_validation_on_create, :proc ],
  186. [ :before_validation_on_create, :object ],
  187. [ :before_validation_on_create, :block ],
  188. [ :after_validation, :string ],
  189. [ :after_validation, :proc ],
  190. [ :after_validation, :object ],
  191. [ :after_validation, :block ],
  192. [ :after_validation_on_create, :string ],
  193. [ :after_validation_on_create, :proc ],
  194. [ :after_validation_on_create, :object ],
  195. [ :after_validation_on_create, :block ],
  196. [ :before_save, :string ],
  197. [ :before_save, :proc ],
  198. [ :before_save, :object ],
  199. [ :before_save, :block ],
  200. [ :before_create, :string ],
  201. [ :before_create, :proc ],
  202. [ :before_create, :object ],
  203. [ :before_create, :block ],
  204. [ :after_create, :string ],
  205. [ :after_create, :proc ],
  206. [ :after_create, :object ],
  207. [ :after_create, :block ],
  208. [ :after_save, :string ],
  209. [ :after_save, :proc ],
  210. [ :after_save, :object ],
  211. [ :after_save, :block ]
  212. ], david.history
  213. end
  214. def test_save
  215. david = CallbackDeveloper.find(1)
  216. david.save
  217. assert_equal [
  218. [ :after_find, :string ],
  219. [ :after_find, :proc ],
  220. [ :after_find, :object ],
  221. [ :after_find, :block ],
  222. [ :after_initialize, :string ],
  223. [ :after_initialize, :proc ],
  224. [ :after_initialize, :object ],
  225. [ :after_initialize, :block ],
  226. [ :before_validation, :string ],
  227. [ :before_validation, :proc ],
  228. [ :before_validation, :object ],
  229. [ :before_validation, :block ],
  230. [ :before_validation_on_update, :string ],
  231. [ :before_validation_on_update, :proc ],
  232. [ :before_validation_on_update, :object ],
  233. [ :before_validation_on_update, :block ],
  234. [ :after_validation, :string ],
  235. [ :after_validation, :proc ],
  236. [ :after_validation, :object ],
  237. [ :after_validation, :block ],
  238. [ :after_validation_on_update, :string ],
  239. [ :after_validation_on_update, :proc ],
  240. [ :after_validation_on_update, :object ],
  241. [ :after_validation_on_update, :block ],
  242. [ :before_save, :string ],
  243. [ :before_save, :proc ],
  244. [ :before_save, :object ],
  245. [ :before_save, :block ],
  246. [ :before_update, :string ],
  247. [ :before_update, :proc ],
  248. [ :before_update, :object ],
  249. [ :before_update, :block ],
  250. [ :after_update, :string ],
  251. [ :after_update, :proc ],
  252. [ :after_update, :object ],
  253. [ :after_update, :block ],
  254. [ :after_save, :string ],
  255. [ :after_save, :proc ],
  256. [ :after_save, :object ],
  257. [ :after_save, :block ]
  258. ], david.history
  259. end
  260. def test_destroy
  261. david = CallbackDeveloper.find(1)
  262. david.destroy
  263. assert_equal [
  264. [ :after_find, :string ],
  265. [ :after_find, :proc ],
  266. [ :after_find, :object ],
  267. [ :after_find, :block ],
  268. [ :after_initialize, :string ],
  269. [ :after_initialize, :proc ],
  270. [ :after_initialize, :object ],
  271. [ :after_initialize, :block ],
  272. [ :before_destroy, :string ],
  273. [ :before_destroy, :proc ],
  274. [ :before_destroy, :object ],
  275. [ :before_destroy, :block ],
  276. [ :after_destroy, :string ],
  277. [ :after_destroy, :proc ],
  278. [ :after_destroy, :object ],
  279. [ :after_destroy, :block ]
  280. ], david.history
  281. end
  282. def test_delete
  283. david = CallbackDeveloper.find(1)
  284. CallbackDeveloper.delete(david.id)
  285. assert_equal [
  286. [ :after_find, :string ],
  287. [ :after_find, :proc ],
  288. [ :after_find, :object ],
  289. [ :after_find, :block ],
  290. [ :after_initialize, :string ],
  291. [ :after_initialize, :proc ],
  292. [ :after_initialize, :object ],
  293. [ :after_initialize, :block ],
  294. ], david.history
  295. end
  296. def test_before_save_returning_false
  297. david = ImmutableDeveloper.find(1)
  298. assert david.valid?
  299. assert !david.save
  300. assert_raises(ActiveRecord::RecordNotSaved) { david.save! }
  301. david = ImmutableDeveloper.find(1)
  302. david.salary = 10_000_000
  303. assert !david.valid?
  304. assert !david.save
  305. assert_raises(ActiveRecord::RecordInvalid) { david.save! }
  306. end
  307. def test_before_create_returning_false
  308. someone = CallbackCancellationDeveloper.new
  309. assert someone.valid?
  310. assert !someone.save
  311. end
  312. def test_before_destroy_returning_false
  313. david = ImmutableDeveloper.find(1)
  314. assert !david.destroy
  315. assert_not_nil ImmutableDeveloper.find_by_id(1)
  316. end
  317. def test_zzz_callback_returning_false # must be run last since we modify CallbackDeveloper
  318. david = CallbackDeveloper.find(1)
  319. CallbackDeveloper.before_validation proc { |model| model.history << [:before_validation, :returning_false]; return false }
  320. CallbackDeveloper.before_validation proc { |model| model.history << [:before_validation, :should_never_get_here] }
  321. david.save
  322. assert_equal [
  323. [ :after_find, :string ],
  324. [ :after_find, :proc ],
  325. [ :after_find, :object ],
  326. [ :after_find, :block ],
  327. [ :after_initialize, :string ],
  328. [ :after_initialize, :proc ],
  329. [ :after_initialize, :object ],
  330. [ :after_initialize, :block ],
  331. [ :before_validation, :string ],
  332. [ :before_validation, :proc ],
  333. [ :before_validation, :object ],
  334. [ :before_validation, :block ],
  335. [ :before_validation, :returning_false ]
  336. ], david.history
  337. end
  338. end