require "cases/helper" require 'models/contact' require 'models/post' require 'models/author' require 'models/comment' require 'models/company_in_module' require 'models/toy' require 'models/topic' require 'models/reply' require 'models/company' class XmlSerializationTest < ActiveRecord::TestCase def test_should_serialize_default_root @xml = Contact.new.to_xml assert_match %r{^}, @xml assert_match %r{$}, @xml end def test_should_serialize_default_root_with_namespace @xml = Contact.new.to_xml :namespace=>"http://xml.rubyonrails.org/contact" assert_match %r{^}, @xml assert_match %r{$}, @xml end def test_should_serialize_custom_root @xml = Contact.new.to_xml :root => 'xml_contact' assert_match %r{^}, @xml assert_match %r{$}, @xml end def test_should_allow_undasherized_tags @xml = Contact.new.to_xml :root => 'xml_contact', :dasherize => false assert_match %r{^}, @xml assert_match %r{$}, @xml assert_match %r{ 'xml_contact', :camelize => true assert_match %r{^}, @xml assert_match %r{$}, @xml assert_match %r{ 25).to_xml :skip_types => true assert %r{25}.match(@xml) end def test_should_include_yielded_additions @xml = Contact.new.to_xml do |xml| xml.creator "David" end assert_match %r{David}, @xml end def test_to_xml_with_block value = "Rockin' the block" xml = Contact.new.to_xml(:skip_instruct => true) do |_xml| _xml.tag! "arbitrary-element", value end assert_equal "", xml.first(9) assert xml.include?(%(#{value})) end def test_should_skip_instruct_for_included_records @contact = Contact.new @contact.alternative = Contact.new(:name => 'Copa Cabana') @xml = @contact.to_xml(:include => [ :alternative ]) assert_equal @xml.index(' 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time.utc(2006, 8, 1), :awesome => false, :preferences => { :gem => 'ruby' } ) end def test_should_serialize_string assert_match %r{aaron stack}, @contact.to_xml end def test_should_serialize_integer assert_match %r{25}, @contact.to_xml end def test_should_serialize_binary xml = @contact.to_xml assert_match %r{YmluYXJ5ZGF0YQ==\n}, xml assert_match %r{2006-08-01T00:00:00Z}, @contact.to_xml end def test_should_serialize_boolean assert_match %r{false}, @contact.to_xml end def test_should_serialize_hash assert_match %r{\s*ruby\s*}m, @contact.to_xml end def test_uses_serializable_hash_with_only_option def @contact.serializable_hash(options=nil) super(only: %w(name)) end xml = @contact.to_xml assert_match %r{aaron stack}, xml assert_no_match %r{age}, xml assert_no_match %r{awesome}, xml end def test_uses_serializable_hash_with_except_option def @contact.serializable_hash(options=nil) super(except: %w(age)) end xml = @contact.to_xml assert_match %r{aaron stack}, xml assert_match %r{false}, xml assert_no_match %r{age}, xml end def test_does_not_include_inheritance_column_from_sti @contact = ContactSti.new(@contact.attributes) assert_equal 'ContactSti', @contact.type xml = @contact.to_xml assert_match %r{aaron stack}, xml assert_no_match %r{aaron stack}, xml assert_no_match %r{age}, xml assert_no_match %r{ 'Mickey', :updated_at => Time.utc(2006, 8, 1)) assert_match %r{2006-07-31T17:00:00-07:00}, toy.to_xml ensure Time.zone = timezone end def test_should_serialize_datetime_with_timezone_reloaded timezone, Time.zone = Time.zone, "Pacific Time (US & Canada)" toy = Toy.create(:name => 'Minnie', :updated_at => Time.utc(2006, 8, 1)).reload assert_match %r{2006-07-31T17:00:00-07:00}, toy.to_xml ensure Time.zone = timezone end end class NilXmlSerializationTest < ActiveRecord::TestCase def setup @xml = Contact.new.to_xml(:root => 'xml_contact') end def test_should_serialize_string assert_match %r{}, @xml end def test_should_serialize_integer assert %r{}.match(@xml) attributes = $1 assert_match %r{nil="true"}, attributes assert_match %r{type="integer"}, attributes end def test_should_serialize_binary assert %r{}.match(@xml) attributes = $1 assert_match %r{type="binary"}, attributes assert_match %r{encoding="base64"}, attributes assert_match %r{nil="true"}, attributes end def test_should_serialize_datetime assert %r{}.match(@xml) attributes = $1 assert_match %r{nil="true"}, attributes assert_match %r{type="dateTime"}, attributes end def test_should_serialize_boolean assert %r{}.match(@xml) attributes = $1 assert_match %r{type="boolean"}, attributes assert_match %r{nil="true"}, attributes end def test_should_serialize_yaml assert_match %r{}, @xml end end class DatabaseConnectedXmlSerializationTest < ActiveRecord::TestCase fixtures :topics, :companies, :accounts, :authors, :posts, :projects def test_to_xml xml = REXML::Document.new(topics(:first).to_xml(:indent => 0)) bonus_time_in_current_timezone = topics(:first).bonus_time.xmlschema written_on_in_current_timezone = topics(:first).written_on.xmlschema last_read_in_current_timezone = topics(:first).last_read.xmlschema assert_equal "topic", xml.root.name assert_equal "The First Topic" , xml.elements["//title"].text assert_equal "David" , xml.elements["//author-name"].text assert_match "Have a nice day", xml.elements["//content"].text assert_equal "1", xml.elements["//id"].text assert_equal "integer" , xml.elements["//id"].attributes['type'] assert_equal "1", xml.elements["//replies-count"].text assert_equal "integer" , xml.elements["//replies-count"].attributes['type'] assert_equal written_on_in_current_timezone, xml.elements["//written-on"].text assert_equal "dateTime" , xml.elements["//written-on"].attributes['type'] assert_equal "david@loudthinking.com", xml.elements["//author-email-address"].text assert_equal nil, xml.elements["//parent-id"].text assert_equal "integer", xml.elements["//parent-id"].attributes['type'] assert_equal "true", xml.elements["//parent-id"].attributes['nil'] if current_adapter?(:SybaseAdapter) assert_equal last_read_in_current_timezone, xml.elements["//last-read"].text assert_equal "dateTime" , xml.elements["//last-read"].attributes['type'] else # Oracle enhanced adapter allows to define Date attributes in model class (see topic.rb) assert_equal "2004-04-15", xml.elements["//last-read"].text assert_equal "date" , xml.elements["//last-read"].attributes['type'] end # Oracle and DB2 don't have true boolean or time-only fields unless current_adapter?(:OracleAdapter, :DB2Adapter) assert_equal "false", xml.elements["//approved"].text assert_equal "boolean" , xml.elements["//approved"].attributes['type'] assert_equal bonus_time_in_current_timezone, xml.elements["//bonus-time"].text assert_equal "dateTime" , xml.elements["//bonus-time"].attributes['type'] end end def test_except_option xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :except => [:title, :replies_count]) assert_equal "", xml.first(7) assert !xml.include?(%(The First Topic)) assert xml.include?(%(David)) xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :except => [:title, :author_name, :replies_count]) assert !xml.include?(%(The First Topic)) assert !xml.include?(%(David)) end # to_xml used to mess with the hash the user provided which # caused the builder to be reused. This meant the document kept # getting appended to. def test_modules projects = MyApplication::Business::Project.to_a xml = projects.to_xml root = projects.first.class.to_s.underscore.pluralize.tr('/','_').dasherize assert_match "<#{root} type=\"array\">", xml assert_match "", xml end def test_passing_hash_shouldnt_reuse_builder options = {:include=>:posts} david = authors(:david) first_xml_size = david.to_xml(options).size second_xml_size = david.to_xml(options).size assert_equal first_xml_size, second_xml_size end def test_include_uses_association_name xml = authors(:david).to_xml :include=>:hello_posts, :indent => 0 assert_match %r{}, xml assert_match %r{}, xml assert_match %r{}, xml end def test_included_associations_should_skip_types xml = authors(:david).to_xml :include=>:hello_posts, :indent => 0, :skip_types => true assert_match %r{}, xml assert_match %r{}, xml assert_match %r{}, xml end def test_including_has_many_association xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :include => :replies, :except => :replies_count) assert_equal "", xml.first(7) assert xml.include?(%()) assert xml.include?(%(The Second Topic of the day)) end def test_including_belongs_to_association xml = companies(:first_client).to_xml(:indent => 0, :skip_instruct => true, :include => :firm) assert !xml.include?("") xml = companies(:second_client).to_xml(:indent => 0, :skip_instruct => true, :include => :firm) assert xml.include?("") end def test_including_multiple_associations xml = companies(:first_firm).to_xml(:indent => 0, :skip_instruct => true, :include => [ :clients, :account ]) assert_equal "", xml.first(6) assert xml.include?(%()) assert xml.include?(%()) end def test_including_association_with_options xml = companies(:first_firm).to_xml( :indent => 0, :skip_instruct => true, :include => { :clients => { :only => :name } } ) assert_equal "", xml.first(6) assert xml.include?(%(Summit)) assert xml.include?(%()) end def test_methods_are_called_on_object xml = authors(:david).to_xml :methods => :label, :indent => 0 assert_match %r{}, xml end def test_should_not_call_methods_on_associations_that_dont_respond xml = authors(:david).to_xml :include=>:hello_posts, :methods => :label, :indent => 2 assert !authors(:david).hello_posts.first.respond_to?(:label) assert_match %r{^ }, xml assert_no_match %r{^