@@ -769,4 +769,91 @@ class AssociationsTest < ActiveSupport::TestCase
769769 end
770770 end
771771 end
772+
773+ context "belongs_to associations" do
774+ context "Wotsit and Widget" do
775+ setup { @widget = Widget . create ( name : "widget_0" ) }
776+
777+ context "where the association is created between model versions" do
778+ setup do
779+ @wotsit = @widget . create_wotsit ( name : "wotsit_0" )
780+ Timecop . travel 1 . second . since
781+ @wotsit . update_attributes name : "wotsit_1"
782+ end
783+
784+ context "when reified" do
785+ setup { @wotsit_0 = @wotsit . versions . last . reify ( belongs_to : true ) }
786+
787+ should "see the associated as it was at the time" do
788+ assert_equal "widget_0" , @wotsit_0 . widget . name
789+ end
790+
791+ should "not persist changes to the live association" do
792+ assert_equal @widget , @wotsit . reload . widget
793+ end
794+ end
795+
796+ context "and then the associated is updated between model versions" do
797+ setup do
798+ @widget . update_attributes name : "widget_1"
799+ @widget . update_attributes name : "widget_2"
800+ Timecop . travel 1 . second . since
801+ @wotsit . update_attributes name : "wotsit_2"
802+ @widget . update_attributes name : "widget_3"
803+ end
804+
805+ context "when reified" do
806+ setup { @wotsit_1 = @wotsit . versions . last . reify ( belongs_to : true ) }
807+
808+ should "see the associated as it was at the time" do
809+ assert_equal "widget_2" , @wotsit_1 . widget . name
810+ end
811+
812+ should "not persist changes to the live association" do
813+ assert_equal "widget_3" , @wotsit . reload . widget . name
814+ end
815+ end
816+
817+ context "when reified opting out of belongs_to reification" do
818+ setup { @wotsit_1 = @wotsit . versions . last . reify ( belongs_to : false ) }
819+
820+ should "see the associated as it is live" do
821+ assert_equal "widget_3" , @wotsit_1 . widget . name
822+ end
823+ end
824+ end
825+
826+ context "and then the associated is destroyed" do
827+ setup { @widget . destroy }
828+
829+ context "when reify" do
830+ setup { @wotsit_1 = @wotsit . versions . last . reify ( belongs_to : true ) }
831+
832+ should "see the associated as it was at the time" do
833+ assert_equal @widget , @wotsit_1 . widget
834+ end
835+
836+ should "not persist changes to the live association" do
837+ assert_nil @wotsit . reload . widget
838+ end
839+ end
840+
841+ context "and then the model is updated" do
842+ setup do
843+ Timecop . travel 1 . second . since
844+ @wotsit . update_attributes name : "wotsit_3"
845+ end
846+
847+ context "when reified" do
848+ setup { @wotsit_2 = @wotsit . versions . last . reify ( belongs_to : true ) }
849+
850+ should "see the associated as it was the time" do
851+ assert_nil @wotsit_2 . widget
852+ end
853+ end
854+ end
855+ end
856+ end
857+ end
858+ end
772859end
0 commit comments