PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/rails/activerecord/test/xml_serialization_test.rb

https://github.com/millbanksystems/hansard
Ruby | 153 lines | 121 code | 28 blank | 4 comment | 0 complexity | 86906ee8525811590b188654e3b44b91 MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception
  1. require 'abstract_unit'
  2. require 'fixtures/post'
  3. require 'fixtures/author'
  4. class Contact < ActiveRecord::Base
  5. # mock out self.columns so no pesky db is needed for these tests
  6. def self.columns() @columns ||= []; end
  7. def self.column(name, sql_type = nil, default = nil, null = true)
  8. columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
  9. end
  10. column :name, :string
  11. column :age, :integer
  12. column :avatar, :binary
  13. column :created_at, :datetime
  14. column :awesome, :boolean
  15. column :preferences, :string
  16. serialize :preferences
  17. end
  18. class XmlSerializationTest < Test::Unit::TestCase
  19. def test_should_serialize_default_root
  20. @xml = Contact.new.to_xml
  21. assert_match %r{^<contact>}, @xml
  22. assert_match %r{</contact>$}, @xml
  23. end
  24. def test_should_serialize_default_root_with_namespace
  25. @xml = Contact.new.to_xml :namespace=>"http://xml.rubyonrails.org/contact"
  26. assert_match %r{^<contact xmlns="http://xml.rubyonrails.org/contact">}, @xml
  27. assert_match %r{</contact>$}, @xml
  28. end
  29. def test_should_serialize_custom_root
  30. @xml = Contact.new.to_xml :root => 'xml_contact'
  31. assert_match %r{^<xml-contact>}, @xml
  32. assert_match %r{</xml-contact>$}, @xml
  33. end
  34. def test_should_allow_undasherized_tags
  35. @xml = Contact.new.to_xml :root => 'xml_contact', :dasherize => false
  36. assert_match %r{^<xml_contact>}, @xml
  37. assert_match %r{</xml_contact>$}, @xml
  38. assert_match %r{<created_at}, @xml
  39. end
  40. def test_should_allow_attribute_filtering
  41. @xml = Contact.new.to_xml :only => [:age, :name]
  42. assert_match %r{<name}, @xml
  43. assert_match %r{<age}, @xml
  44. assert_no_match %r{<created-at}, @xml
  45. @xml = Contact.new.to_xml :except => [:age, :name]
  46. assert_no_match %r{<name}, @xml
  47. assert_no_match %r{<age}, @xml
  48. assert_match %r{<created-at}, @xml
  49. end
  50. def test_should_include_yielded_additions
  51. @xml = Contact.new.to_xml do |xml|
  52. xml.creator "David"
  53. end
  54. assert_match %r{<creator>David</creator>}, @xml
  55. end
  56. end
  57. class DefaultXmlSerializationTest < Test::Unit::TestCase
  58. def setup
  59. @xml = Contact.new(:name => 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time.utc(2006, 8, 1), :awesome => false, :preferences => { :gem => 'ruby' }).to_xml
  60. end
  61. def test_should_serialize_string
  62. assert_match %r{<name>aaron stack</name>}, @xml
  63. end
  64. def test_should_serialize_integer
  65. assert_match %r{<age type="integer">25</age>}, @xml
  66. end
  67. def test_should_serialize_binary
  68. assert_match %r{YmluYXJ5ZGF0YQ==\n</avatar>}, @xml
  69. assert_match %r{<avatar(.*)(type="binary")}, @xml
  70. assert_match %r{<avatar(.*)(encoding="base64")}, @xml
  71. end
  72. def test_should_serialize_datetime
  73. assert_match %r{<created-at type=\"datetime\">2006-08-01T00:00:00Z</created-at>}, @xml
  74. end
  75. def test_should_serialize_boolean
  76. assert_match %r{<awesome type=\"boolean\">false</awesome>}, @xml
  77. end
  78. def test_should_serialize_yaml
  79. assert_match %r{<preferences type=\"yaml\">--- \n:gem: ruby\n</preferences>}, @xml
  80. end
  81. end
  82. class NilXmlSerializationTest < Test::Unit::TestCase
  83. def setup
  84. @xml = Contact.new.to_xml(:root => 'xml_contact')
  85. end
  86. def test_should_serialize_string
  87. assert_match %r{<name></name>}, @xml
  88. end
  89. def test_should_serialize_integer
  90. assert_match %r{<age type="integer"></age>}, @xml
  91. end
  92. def test_should_serialize_binary
  93. assert_match %r{></avatar>}, @xml
  94. assert_match %r{<avatar(.*)(type="binary")}, @xml
  95. assert_match %r{<avatar(.*)(encoding="base64")}, @xml
  96. end
  97. def test_should_serialize_datetime
  98. assert_match %r{<created-at type=\"datetime\"></created-at>}, @xml
  99. end
  100. def test_should_serialize_boolean
  101. assert_match %r{<awesome type=\"boolean\"></awesome>}, @xml
  102. end
  103. def test_should_serialize_yaml
  104. assert_match %r{<preferences type=\"yaml\"></preferences>}, @xml
  105. end
  106. end
  107. class DatabaseConnectedXmlSerializationTest < Test::Unit::TestCase
  108. fixtures :authors, :posts
  109. # to_xml used to mess with the hash the user provided which
  110. # caused the builder to be reused. This meant the document kept
  111. # getting appended to.
  112. def test_passing_hash_shouldnt_reuse_builder
  113. options = {:include=>:posts}
  114. david = authors(:david)
  115. first_xml_size = david.to_xml(options).size
  116. second_xml_size = david.to_xml(options).size
  117. assert_equal first_xml_size, second_xml_size
  118. end
  119. def test_include_uses_association_name
  120. xml = authors(:david).to_xml :include=>:hello_posts, :indent=>0
  121. assert_match %r{<hello-posts>}, xml
  122. assert_match %r{<post>}, xml
  123. assert_match %r{<sti-post>}, xml
  124. end
  125. end