Guest User

Untitled

a guest
Apr 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. require 'mongo_mapper'
  2. require 'spec'
  3. require 'extlib'
  4.  
  5. MongoMapper.connection = Mongo::Connection.new('localhost')
  6. MongoMapper.database = 'tmp'
  7.  
  8. class Bar
  9. include MongoMapper::EmbeddedDocument
  10. key :title, String
  11. end
  12.  
  13. class Foo
  14. include MongoMapper::Document
  15. key :name, String
  16. key :alpha, Hash
  17. key :group, Array
  18. many :bars
  19. key :name_updated, Time
  20. key :alpha_updated, Time
  21. key :group_updated, Time
  22. key :bars_updated, Time
  23.  
  24. def before_save
  25. self.name_updated = Time.now if self.name_changed?
  26. self.alpha_updated = Time.now if self.alpha_changed?
  27. self.group_updated = Time.now if self.group_changed?
  28. # bars_changed? is undefined
  29. # self.bars_updated = Time.now if self.bars_changed?
  30. end
  31. end
  32.  
  33. describe "test changed? behaviour" do
  34.  
  35. before :each do
  36. Foo.collection.clear
  37. end
  38.  
  39. it "should indicate string changed" do
  40. foo = Foo.create
  41. foo.name_updated.should be_nil
  42. foo.name = 'one'
  43. foo.changed.include?('name').should be_true
  44. foo.save
  45. foo.name_updated.should_not be_nil
  46. end
  47.  
  48. it "should indicate a hash was reassigned" do
  49. foo = Foo.create
  50. foo.alpha_updated.should be_nil
  51. foo.alpha = {:a => 1}
  52. foo.changed.include?('alpha').should be_true
  53. foo.save
  54. foo.alpha_updated.should_not be_nil
  55. end
  56.  
  57. it "should indicate hash changed" do
  58. foo = Foo.create
  59. foo.alpha_updated.should be_nil
  60. foo.alpha['a'] = 1
  61. foo.changed.include?('alpha').should be_true #fails as only the hash contents changed, not the hash object
  62. foo.save
  63. foo.alpha_updated.should_not be_nil #fails
  64. end
  65.  
  66. it "should indicate an array was reassigned" do
  67. foo = Foo.create
  68. foo.group_updated.should be_nil
  69. foo.group = %w(one two three)
  70. foo.changed.include?('group').should be_true
  71. foo.save
  72. foo.group_updated.should_not be_nil
  73. end
  74.  
  75. it "should indicate array changed" do
  76. foo = Foo.create
  77. foo.group_updated.should be_nil
  78. foo.group << 'one'
  79. foo.group << 'two'
  80. foo.group << 'three'
  81. foo.changed.include?('group').should be_true #fails as only the array contents changed, not the array object
  82. foo.save
  83. foo.group_updated.should_not be_nil #fails
  84. end
  85.  
  86. it "should indicate embedded document changed" do
  87. foo = Foo.create
  88. foo.bars_updated.should be_nil
  89. foo.bars << Bar.new(:title => 'boss')
  90. foo.save
  91. foo.bars_updated.should_not be_nil #fails
  92. end
  93.  
  94. end
  95.  
  96. __END__
  97.  
  98.  
  99. royw-macbook:tmp royw$ spec changed.rb
  100. ..F.FF
  101.  
  102. 1)
  103. 'test changed? behaviour should indicate hash changed' FAILED
  104. expected true, got false
  105. ./changed.rb:62:
  106.  
  107. 2)
  108. 'test changed? behaviour should indicate array changed' FAILED
  109. expected true, got false
  110. ./changed.rb:82:
  111.  
  112. 3)
  113. 'test changed? behaviour should indicate embedded document changed' FAILED
  114. expected not nil, got nil
  115. ./changed.rb:92:
  116.  
  117. Finished in 0.027555 seconds
  118.  
  119. 6 examples, 3 failures
Add Comment
Please, Sign In to add comment