aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/yaml_serialization_test.rb
blob: 0b54c309d18739e5910c7b57148d604ca47adadc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require "cases/helper"
require 'models/topic'

class YamlSerializationTest < ActiveRecord::TestCase
  fixtures :topics

  def test_to_yaml_with_time_with_zone_should_not_raise_exception
    Time.zone = ActiveSupport::TimeZone["Pacific Time (US & Canada)"]
    ActiveRecord::Base.time_zone_aware_attributes = true
    topic = Topic.new(:written_on => DateTime.now)
    assert_nothing_raised { topic.to_yaml }
  end

  def test_roundtrip
    topic = Topic.first
    assert topic
    t = YAML.load YAML.dump topic
    assert_equal topic, t
  end

  def test_encode_with_coder
    topic = Topic.first
    coder = {}
    topic.encode_with coder
    assert_equal({'attributes' => topic.attributes}, coder)
  end

  begin
    require 'psych'

    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
    end
  rescue LoadError
  end
end