Created
July 8, 2013 07:39
-
-
Save pascalbetz/5946918 to your computer and use it in GitHub Desktop.
Executable Testcase for https://github.com/rails/rails/issues/8020#issuecomment-19958061 Note that this bug is gone in AR 4.0.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'activerecord', '3.2.13' | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :posts do |t| | |
end | |
create_table :comments do |t| | |
t.integer :post_id | |
end | |
end | |
class Post < ActiveRecord::Base | |
has_many :comments | |
end | |
class Comment < ActiveRecord::Base | |
belongs_to :post | |
end | |
class MarshallingTest < MiniTest::Unit::TestCase | |
def setup | |
Post.create! | |
Post.create! | |
end | |
def teardown | |
Post.destroy_all | |
end | |
def test_marshalling_with_touching_associations | |
posts = Post.all | |
posts.each do |post| | |
post.comments | |
end | |
marshalled = Marshal.load(Marshal.dump(posts)) | |
assert_equal Post.all, marshalled | |
end | |
def test_marshalling_without_touching_associations | |
posts = Post.all | |
marshalled = Marshal.load(Marshal.dump(posts)) | |
assert_equal Post.all, marshalled | |
end | |
def test_marshalling_with_arrayifying_associations | |
posts = Post.all | |
posts.each do |post| | |
post.comments.to_a | |
end | |
marshalled = Marshal.load(Marshal.dump(posts)) | |
assert_equal Post.all, marshalled | |
end | |
def test_dumping_twice | |
posts = Post.all | |
posts.each do |post| | |
post.comments.to_a | |
end | |
Marshal.dump(posts) # as noted by a commenter, dumping twice helps... | |
marshalled = Marshal.load(Marshal.dump(posts)) | |
assert_equal Post.all, marshalled | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment