PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/bricooke/my-biz-expenses
Ruby | 125 lines | 101 code | 21 blank | 3 comment | 0 complexity | 863b97eff343795fd5c5a8162dfbc69d MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause
  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. end
  16. class XmlSerializationTest < Test::Unit::TestCase
  17. def test_should_serialize_default_root
  18. @xml = Contact.new.to_xml
  19. assert_match %r{^<contact>}, @xml
  20. assert_match %r{</contact>$}, @xml
  21. end
  22. def test_should_serialize_default_root_with_namespace
  23. @xml = Contact.new.to_xml :namespace=>"http://xml.rubyonrails.org/contact"
  24. assert_match %r{^<contact xmlns="http://xml.rubyonrails.org/contact">}, @xml
  25. assert_match %r{</contact>$}, @xml
  26. end
  27. def test_should_serialize_custom_root
  28. @xml = Contact.new.to_xml :root => 'xml_contact'
  29. assert_match %r{^<xml-contact>}, @xml
  30. assert_match %r{</xml-contact>$}, @xml
  31. end
  32. def test_should_allow_undasherized_tags
  33. @xml = Contact.new.to_xml :root => 'xml_contact', :dasherize => false
  34. assert_match %r{^<xml_contact>}, @xml
  35. assert_match %r{</xml_contact>$}, @xml
  36. assert_match %r{<created_at}, @xml
  37. end
  38. def test_should_allow_attribute_filtering
  39. @xml = Contact.new.to_xml :only => [:age, :name]
  40. assert_match %r{<name}, @xml
  41. assert_match %r{<age}, @xml
  42. assert_no_match %r{<created-at}, @xml
  43. @xml = Contact.new.to_xml :except => [:age, :name]
  44. assert_no_match %r{<name}, @xml
  45. assert_no_match %r{<age}, @xml
  46. assert_match %r{<created-at}, @xml
  47. end
  48. end
  49. class DefaultXmlSerializationTest < Test::Unit::TestCase
  50. def setup
  51. @xml = Contact.new(:name => 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time.utc(2006, 8, 1), :awesome => false).to_xml
  52. end
  53. def test_should_serialize_string
  54. assert_match %r{<name>aaron stack</name>}, @xml
  55. end
  56. def test_should_serialize_integer
  57. assert_match %r{<age type="integer">25</age>}, @xml
  58. end
  59. def test_should_serialize_binary
  60. assert_match %r{YmluYXJ5ZGF0YQ==\n</avatar>}, @xml
  61. assert_match %r{<avatar(.*)(type="binary")}, @xml
  62. assert_match %r{<avatar(.*)(encoding="base64")}, @xml
  63. end
  64. def test_should_serialize_datetime
  65. assert_match %r{<created-at type=\"datetime\">2006-08-01T00:00:00Z</created-at>}, @xml
  66. end
  67. def test_should_serialize_boolean
  68. assert_match %r{<awesome type=\"boolean\">false</awesome>}, @xml
  69. end
  70. end
  71. class NilXmlSerializationTest < Test::Unit::TestCase
  72. def setup
  73. @xml = Contact.new.to_xml(:root => 'xml_contact')
  74. end
  75. def test_should_serialize_string
  76. assert_match %r{<name></name>}, @xml
  77. end
  78. def test_should_serialize_integer
  79. assert_match %r{<age type="integer"></age>}, @xml
  80. end
  81. def test_should_serialize_binary
  82. assert_match %r{></avatar>}, @xml
  83. assert_match %r{<avatar(.*)(type="binary")}, @xml
  84. assert_match %r{<avatar(.*)(encoding="base64")}, @xml
  85. end
  86. def test_should_serialize_datetime
  87. assert_match %r{<created-at type=\"datetime\"></created-at>}, @xml
  88. end
  89. def test_should_serialize_boolean
  90. assert_match %r{<awesome type=\"boolean\"></awesome>}, @xml
  91. end
  92. end
  93. class DatabaseConnectedXmlSerializationTest < Test::Unit::TestCase
  94. fixtures :authors, :posts
  95. # to_xml used to mess with the hash the user provided which
  96. # caused the builder to be reused
  97. def test_passing_hash_shouldnt_reuse_builder
  98. options = {:include=>:posts}
  99. david = authors(:david)
  100. first_xml_size = david.to_xml(options).size
  101. second_xml_size = david.to_xml(options).size
  102. assert_equal first_xml_size, second_xml_size
  103. end
  104. end