Sha256: 82249c7bd05c010011b9c4763ff173d0b878b86bf9952d6b02f53601f22c45ea

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

# -*- ruby encoding: utf-8 -*-

$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0

require 'transaction/simple'
require 'test/unit'

module Transaction::Simple::Test
  class BrokenGraph < Test::Unit::TestCase #:nodoc:
    class Child
      attr_accessor :parent
    end

    class BrokenParent
      include Transaction::Simple

      attr_reader :children
      def initialize
        @children = []
      end

      def <<(child)
        child.parent = self
        @children << child
      end
    end

    class FixedParent < BrokenParent
      # Reconnect the restored children to me, instead of to the bogus me
      # that was restored to them by Marshal::load.
      def _post_transaction_rewind
        @children.each { |child| child.parent = self }
      end
    end

    def test_broken_graph
      parent = BrokenParent.new
      parent << Child.new
      assert_equal(parent.object_id, parent.children[0].parent.object_id)
      parent.start_transaction
      parent << Child.new
      assert_equal(parent.object_id, parent.children[1].parent.object_id)
      parent.abort_transaction
      assert_not_equal(parent.object_id, parent.children[0].parent.object_id)
    end

    def test_fixed_graph
      parent = FixedParent.new
      parent << Child.new
      assert_equal(parent.object_id, parent.children[0].parent.object_id)
      parent.start_transaction
      parent << Child.new
      assert_equal(parent.object_id, parent.children[1].parent.object_id)
      parent.abort_transaction
      assert_equal(parent.object_id, parent.children[0].parent.object_id)
    end
  end
end

# vim: syntax=ruby

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
transaction-simple-1.4.0.2 test/test_broken_graph.rb