2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/topic"
|
|
|
|
require "models/reply"
|
|
|
|
require "models/post"
|
|
|
|
require "models/author"
|
2009-12-11 16:08:00 -05:00
|
|
|
|
|
|
|
class YamlSerializationTest < ActiveRecord::TestCase
|
2017-04-27 02:44:25 -04:00
|
|
|
fixtures :topics, :authors, :author_addresses, :posts
|
2010-12-02 11:44:31 -05:00
|
|
|
|
2009-12-11 16:08:00 -05:00
|
|
|
def test_to_yaml_with_time_with_zone_should_not_raise_exception
|
2013-10-24 15:26:23 -04:00
|
|
|
with_timezone_config aware_attributes: true, zone: "Pacific Time (US & Canada)" do
|
2016-08-06 13:37:57 -04:00
|
|
|
topic = Topic.new(written_on: DateTime.now)
|
2013-10-24 15:26:23 -04:00
|
|
|
assert_nothing_raised { topic.to_yaml }
|
|
|
|
end
|
2009-12-11 16:08:00 -05:00
|
|
|
end
|
2010-12-02 11:44:31 -05:00
|
|
|
|
|
|
|
def test_roundtrip
|
|
|
|
topic = Topic.first
|
|
|
|
assert topic
|
|
|
|
t = YAML.load YAML.dump topic
|
|
|
|
assert_equal topic, t
|
|
|
|
end
|
2011-01-05 17:01:47 -05:00
|
|
|
|
2011-11-30 20:15:16 -05:00
|
|
|
def test_roundtrip_serialized_column
|
2016-08-16 03:30:11 -04:00
|
|
|
topic = Topic.new(content: { omg: :lol })
|
|
|
|
assert_equal({ omg: :lol }, YAML.load(YAML.dump(topic)).content)
|
2011-11-30 20:15:16 -05:00
|
|
|
end
|
|
|
|
|
2012-01-04 11:30:57 -05:00
|
|
|
def test_psych_roundtrip
|
|
|
|
topic = Topic.first
|
|
|
|
assert topic
|
|
|
|
t = Psych.load Psych.dump topic
|
|
|
|
assert_equal topic, t
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_psych_roundtrip_new_object
|
|
|
|
topic = Topic.new
|
|
|
|
assert topic
|
|
|
|
t = Psych.load Psych.dump topic
|
|
|
|
assert_equal topic.attributes, t.attributes
|
2011-01-05 17:01:47 -05:00
|
|
|
end
|
2013-11-03 09:35:44 -05:00
|
|
|
|
|
|
|
def test_active_record_relation_serialization
|
|
|
|
[Topic.all].to_yaml
|
|
|
|
end
|
2014-05-30 16:06:05 -04:00
|
|
|
|
|
|
|
def test_raw_types_are_not_changed_on_round_trip
|
|
|
|
topic = Topic.new(parent_id: "123")
|
|
|
|
assert_equal "123", topic.parent_id_before_type_cast
|
|
|
|
assert_equal "123", YAML.load(YAML.dump(topic)).parent_id_before_type_cast
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cast_types_are_not_changed_on_round_trip
|
|
|
|
topic = Topic.new(parent_id: "123")
|
|
|
|
assert_equal 123, topic.parent_id
|
|
|
|
assert_equal 123, YAML.load(YAML.dump(topic)).parent_id
|
|
|
|
end
|
2014-05-31 09:01:59 -04:00
|
|
|
|
|
|
|
def test_new_records_remain_new_after_round_trip
|
|
|
|
topic = Topic.new
|
|
|
|
|
|
|
|
assert topic.new_record?, "Sanity check that new records are new"
|
|
|
|
assert YAML.load(YAML.dump(topic)).new_record?, "Record should be new after deserialization"
|
|
|
|
|
|
|
|
topic.save!
|
|
|
|
|
|
|
|
assert_not topic.new_record?, "Saved records are not new"
|
|
|
|
assert_not YAML.load(YAML.dump(topic)).new_record?, "Saved record should not be new after deserialization"
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
topic = Topic.select("title").last
|
2014-05-31 09:01:59 -04:00
|
|
|
|
|
|
|
assert_not topic.new_record?, "Loaded records without ID are not new"
|
|
|
|
assert_not YAML.load(YAML.dump(topic)).new_record?, "Record should not be new after deserialization"
|
|
|
|
end
|
2014-06-10 10:42:47 -04:00
|
|
|
|
|
|
|
def test_types_of_virtual_columns_are_not_changed_on_round_trip
|
2016-08-06 12:26:20 -04:00
|
|
|
author = Author.select("authors.*, count(posts.id) as posts_count")
|
2014-06-10 10:42:47 -04:00
|
|
|
.joins(:posts)
|
2016-08-06 12:26:20 -04:00
|
|
|
.group("authors.id")
|
2014-06-10 10:42:47 -04:00
|
|
|
.first
|
|
|
|
dumped = YAML.load(YAML.dump(author))
|
|
|
|
|
|
|
|
assert_equal 5, author.posts_count
|
|
|
|
assert_equal 5, dumped.posts_count
|
|
|
|
end
|
2015-03-10 13:35:09 -04:00
|
|
|
|
|
|
|
def test_a_yaml_version_is_provided_for_future_backwards_compat
|
|
|
|
coder = {}
|
|
|
|
Topic.first.encode_with(coder)
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert coder["active_record_yaml_version"]
|
2015-03-10 13:35:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_deserializing_rails_41_yaml
|
2015-03-10 13:54:17 -04:00
|
|
|
topic = YAML.load(yaml_fixture("rails_4_1"))
|
2015-03-10 13:35:09 -04:00
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate topic, :new_record?
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil topic.id
|
2015-03-10 13:35:09 -04:00
|
|
|
assert_equal "The First Topic", topic.title
|
|
|
|
assert_equal({ omg: :lol }, topic.content)
|
|
|
|
end
|
|
|
|
|
2015-03-10 13:54:17 -04:00
|
|
|
def test_deserializing_rails_4_2_0_yaml
|
|
|
|
topic = YAML.load(yaml_fixture("rails_4_2_0"))
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate topic, :new_record?
|
2015-03-10 13:54:17 -04:00
|
|
|
assert_equal 1, topic.id
|
|
|
|
assert_equal "The First Topic", topic.title
|
|
|
|
assert_equal("Have a nice day", topic.content)
|
|
|
|
end
|
|
|
|
|
2016-05-31 14:44:38 -04:00
|
|
|
def test_yaml_encoding_keeps_mutations
|
|
|
|
author = Author.first
|
|
|
|
author.name = "Sean"
|
|
|
|
dumped = YAML.load(YAML.dump(author))
|
|
|
|
|
|
|
|
assert_equal "Sean", dumped.name
|
|
|
|
assert_equal author.name_was, dumped.name_was
|
|
|
|
assert_equal author.changes, dumped.changes
|
|
|
|
end
|
|
|
|
|
2017-06-22 10:35:40 -04:00
|
|
|
def test_yaml_encoding_keeps_false_values
|
|
|
|
topic = Topic.first
|
|
|
|
topic.approved = false
|
|
|
|
dumped = YAML.load(YAML.dump(topic))
|
|
|
|
|
|
|
|
assert_equal false, dumped.approved
|
|
|
|
end
|
|
|
|
|
2015-03-10 13:54:17 -04:00
|
|
|
private
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def yaml_fixture(file_name)
|
|
|
|
path = File.expand_path(
|
2017-05-15 10:17:28 -04:00
|
|
|
"../support/yaml_compatibility_fixtures/#{file_name}.yml",
|
|
|
|
__dir__
|
2016-08-06 13:55:02 -04:00
|
|
|
)
|
|
|
|
File.read(path)
|
|
|
|
end
|
2009-12-11 16:08:00 -05:00
|
|
|
end
|