From e86d1cd621ca62af6f71b04032b1e07a66c06bb6 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 20 Sep 2007 23:22:30 +0000 Subject: Added ActiveRecord::Base#to_json/from_json (currently does not support :include like to_xml) [DHH]. Added ActiveRecord::Base#from_xml [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7519 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/fixtures/contact.rb | 16 ++++++++++ activerecord/test/serialization_test.rb | 47 +++++++++++++++++++++++++++++ activerecord/test/xml_serialization_test.rb | 30 +----------------- 3 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 activerecord/test/fixtures/contact.rb create mode 100644 activerecord/test/serialization_test.rb (limited to 'activerecord/test') diff --git a/activerecord/test/fixtures/contact.rb b/activerecord/test/fixtures/contact.rb new file mode 100644 index 0000000000..c574196d94 --- /dev/null +++ b/activerecord/test/fixtures/contact.rb @@ -0,0 +1,16 @@ +class Contact < ActiveRecord::Base + # mock out self.columns so no pesky db is needed for these tests + def self.column(name, sql_type = nil, options = {}) + @columns ||= [] + @columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, options[:default], sql_type.to_s, options[:null]) + end + + column :name, :string + column :age, :integer + column :avatar, :binary + column :created_at, :datetime + column :awesome, :boolean + column :preferences, :string + + serialize :preferences +end \ No newline at end of file diff --git a/activerecord/test/serialization_test.rb b/activerecord/test/serialization_test.rb new file mode 100644 index 0000000000..b3bdf6b43d --- /dev/null +++ b/activerecord/test/serialization_test.rb @@ -0,0 +1,47 @@ +require 'abstract_unit' +require 'fixtures/contact' + +class SerializationTest < Test::Unit::TestCase + FORMATS = [ :xml, :json ] + + def setup + @contact_attributes = { + :name => 'aaron stack', + :age => 25, + :avatar => 'binarydata', + :created_at => Time.utc(2006, 8, 1), + :awesome => false, + :preferences => { :gem => 'ruby' } + } + + @contact = Contact.new(@contact_attributes) + end + + def test_serialize_should_be_reversible + for format in FORMATS + @serialized = Contact.new.send("to_#{format}") + contact = Contact.new.send("from_#{format}", @serialized) + + assert_equal @contact_attributes.keys.collect(&:to_s).sort, contact.attributes.keys.collect(&:to_s).sort, "For #{format}" + end + end + + def test_serialize_should_allow_attribute_only_filtering + for format in FORMATS + @serialized = Contact.new(@contact_attributes).send("to_#{format}", :only => [ :age, :name ]) + contact = Contact.new.send("from_#{format}", @serialized) + assert_equal @contact_attributes[:name], contact.name, "For #{format}" + assert_nil contact.avatar, "For #{format}" + end + end + + def test_serialize_should_allow_attribute_except_filtering + for format in FORMATS + @serialized = Contact.new(@contact_attributes).send("to_#{format}", :except => [ :age, :name ]) + contact = Contact.new.send("from_#{format}", @serialized) + assert_nil contact.name, "For #{format}" + assert_nil contact.age, "For #{format}" + assert_equal @contact_attributes[:awesome], contact.awesome, "For #{format}" + end + end +end \ No newline at end of file diff --git a/activerecord/test/xml_serialization_test.rb b/activerecord/test/xml_serialization_test.rb index f6e3d0dd9d..a5da55cd8d 100644 --- a/activerecord/test/xml_serialization_test.rb +++ b/activerecord/test/xml_serialization_test.rb @@ -1,26 +1,10 @@ require 'abstract_unit' +require 'fixtures/contact' require 'fixtures/post' require 'fixtures/author' require 'fixtures/tagging' require 'fixtures/comment' -class Contact < ActiveRecord::Base - # mock out self.columns so no pesky db is needed for these tests - def self.columns() @columns ||= []; end - def self.column(name, sql_type = nil, default = nil, null = true) - columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null) - end - - column :name, :string - column :age, :integer - column :avatar, :binary - column :created_at, :datetime - column :awesome, :boolean - column :preferences, :string - - serialize :preferences -end - class XmlSerializationTest < Test::Unit::TestCase def test_should_serialize_default_root @xml = Contact.new.to_xml @@ -47,18 +31,6 @@ class XmlSerializationTest < Test::Unit::TestCase assert_match %r{ [:age, :name] - assert_match %r{ [:age, :name] - assert_no_match %r{