Skip to content

Commit

Permalink
added specs for conext 'where the association is changed between mode…
Browse files Browse the repository at this point in the history
…l versions'
  • Loading branch information
theRealNG committed Mar 14, 2016
1 parent e12b9aa commit 4fbac03
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions test/unit/associations_test.rb
Expand Up @@ -776,16 +776,16 @@ class AssociationsTest < ActiveSupport::TestCase

context "where the association is created between model versions" do
setup do
@wotsit = @widget.create_wotsit(name: "wotsit_0")
@wotsit = Wotsit.create(name: "wotsit_0")
Timecop.travel 1.second.since
@wotsit.update_attributes name: "wotsit_1"
@wotsit.update_attributes widget_id: @widget.id, name: "wotsit_1"
end

context "when reified" do
setup { @wotsit_0 = @wotsit.versions.last.reify(belongs_to: true) }

should "see the associated as it was at the time" do
assert_equal "widget_0", @wotsit_0.widget.name
assert_equal nil, @wotsit_0.widget
end

should "not persist changes to the live association" do
Expand Down Expand Up @@ -824,13 +824,16 @@ class AssociationsTest < ActiveSupport::TestCase
end

context "and then the associated is destroyed" do
setup { @widget.destroy }
setup do
@wotsit.update_attributes name: "wotsit_2"
@widget.destroy
end

context "when reify" do
setup { @wotsit_1 = @wotsit.versions.last.reify(belongs_to: true) }
context "when reified" do
setup { @wotsit_2 = @wotsit.versions.last.reify(belongs_to: true) }

should "see the associated as it was at the time" do
assert_equal @widget, @wotsit_1.widget
assert_equal @widget, @wotsit_2.widget
end

should "not persist changes to the live association" do
Expand All @@ -854,6 +857,38 @@ class AssociationsTest < ActiveSupport::TestCase
end
end
end

context "where the association is changed between model versions" do
setup do
@wotsit = @widget.create_wotsit(name: "wotsit_0")
Timecop.travel 1.second.since
@new_widget = Widget.create(name: "new_widget")
@wotsit.update_attributes(widget_id: @new_widget.id, name: "wotsit_1")
end

context "when reified" do
setup { @wotsit_0 = @wotsit.versions.last.reify(belongs_to: true) }

should "see the association as it was at the time" do
assert_equal "widget_0", @wotsit_0.widget.name
end

should "not persist changes to the live association" do
assert_equal @new_widget, @wotsit.reload.widget
end
end

context "when reified with option mark_for_destruction" do
setup do
@wotsit_0 = @wotsit.versions.last.
reify(belongs_to: true, mark_for_destruction: true)
end

should "should not mark the new associated for destruction" do
assert_equal false, @new_widget.marked_for_destruction?
end
end
end
end
end
end

0 comments on commit 4fbac03

Please sign in to comment.