Guest User

Untitled

a guest
Apr 26th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. require 'test_helper'
  2.  
  3. module CallbackModelTemplate
  4. def self.included(model)
  5. model.class_eval do
  6. key :name, String
  7. has_many :sons, :class_name => 'CallbacksTest::Son'
  8.  
  9. [ :before_validation_on_create, :before_validation_on_update,
  10. :before_validation, :after_validation,
  11. :before_create, :after_create,
  12. :before_update, :after_update,
  13. :before_save, :after_save,
  14. :before_destroy, :after_destroy ].each do |callback|
  15. callback_method = "#{callback}_callback"
  16. send(callback, callback_method)
  17. define_method(callback_method) do
  18. history << callback.to_sym
  19. end
  20. end
  21. end
  22. end
  23.  
  24. def history
  25. @history ||= []
  26. end
  27.  
  28. def clear_history
  29. @history = nil
  30. sons.each { |son| son.clear_history }
  31. begin
  32. wife.clear_history
  33. rescue
  34. end
  35. end
  36. end
  37.  
  38. class CallbacksTest < Test::Unit::TestCase
  39. CreateOrder = [:before_validation, :before_validation_on_create, :after_validation, :before_save, :before_create, :after_create, :after_save]
  40. UpdateOrder = [:before_validation, :before_validation_on_update, :after_validation, :before_save, :before_update, :after_update, :after_save]
  41.  
  42. class Wife
  43. include MongoMapper::EmbeddedDocument
  44. include CallbackModelTemplate
  45. end
  46.  
  47. class Father
  48. include MongoMapper::Document
  49. include CallbackModelTemplate
  50. key :wife, Wife
  51. end
  52.  
  53. class Son
  54. include MongoMapper::EmbeddedDocument
  55. include CallbackModelTemplate
  56. end
  57.  
  58. context "Defining and running callbacks" do
  59. setup do
  60. grandson = Son.new(:name => 'John Nunemaker III')
  61. @son = Son.new(:name => 'John Nunemaker Jr.', :sons => [grandson])
  62. @wife = Wife.new(:name => 'Stephanie')
  63. clear_all_collections
  64. end
  65.  
  66. context ": Creating a document" do
  67. setup do
  68. @father = Father.create(:name => 'John Nunemaker', :wife => @wife, :sons => [@son])
  69. @order
  70. end
  71.  
  72. should "get the order right for documents" do
  73. @father.history.should == CreateOrder
  74. @father.wife.history.should == CreateOrder
  75. @father.sons.first.history.should == CreateOrder
  76. @father.sons.first.sons.first.history.should == CreateOrder
  77. end
  78. end
  79.  
  80. context ": Updating a document" do
  81. setup do
  82. @father = Father.create(:name => 'John Nunemaker', :sons => [@son])
  83. @father.clear_history
  84. @father.name = "John"
  85. @father.save
  86. end
  87.  
  88. should "get the order right for all documents" do
  89. @father.history.should == UpdateOrder
  90. @father.sons.first.history.should == UpdateOrder
  91. @father.sons.first.sons.first.history.should == UpdateOrder
  92. end
  93.  
  94. context ": With a new embedded document" do
  95. setup do
  96. @father.clear_history
  97. @father.wife = @wife
  98. grandson = Son.new(:name => 'Jimmy Nunemaker Jr.')
  99. @father.sons << Son.new(:name => 'Jimmy Nunemaker', :sons => [grandson])
  100. @father.save
  101. end
  102.  
  103. should "perform create callbacks on new embedded docs" do
  104. @father.wife.history.should == CreateOrder
  105. @father.sons.last.history.should == CreateOrder
  106. @father.sons.last.sons.first.history.should == CreateOrder
  107. end
  108.  
  109. should "perform update callbacks on old embedded docs" do
  110. @father.sons.first.history.should == UpdateOrder
  111. @father.sons.first.sons.first.history.should == UpdateOrder
  112. end
  113. end
  114. end
  115.  
  116. should "work for before and after validation" do
  117. doc = Father.new(:name => 'John Nunemaker')
  118. doc.valid?
  119. doc.history.should include(:before_validation)
  120. doc.history.should include(:after_validation)
  121. end
  122.  
  123. should "work for before and after create" do
  124. doc = Father.create(:name => 'John Nunemaker')
  125. doc.history.should include(:before_create)
  126. doc.history.should include(:after_create)
  127. end
  128.  
  129. should "work for before and after update" do
  130. doc = Father.create(:name => 'John Nunemaker')
  131. doc.name = 'John Doe'
  132. doc.save
  133. doc.history.should include(:before_update)
  134. doc.history.should include(:after_update)
  135. end
  136.  
  137. should "work for before and after save" do
  138. doc = Father.new
  139. doc.name = 'John Doe'
  140. doc.save
  141. doc.history.should include(:before_save)
  142. doc.history.should include(:after_save)
  143. end
  144.  
  145. should "work for before and after destroy" do
  146. doc = Father.create(:name => 'John Nunemaker')
  147. doc.destroy
  148. doc.history.should include(:before_destroy)
  149. doc.history.should include(:after_destroy)
  150. end
  151. end
  152. end
Add Comment
Please, Sign In to add comment