aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2007-09-20 23:22:30 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2007-09-20 23:22:30 +0000
commite86d1cd621ca62af6f71b04032b1e07a66c06bb6 (patch)
tree638a89da26f9066cc333f0ce059c3ce551a97d24 /activerecord/test
parentdc399b96c84bc66b7c20e92fb40e9ed00daf99c2 (diff)
downloadrails-e86d1cd621ca62af6f71b04032b1e07a66c06bb6.tar.gz
rails-e86d1cd621ca62af6f71b04032b1e07a66c06bb6.tar.bz2
rails-e86d1cd621ca62af6f71b04032b1e07a66c06bb6.zip
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
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/fixtures/contact.rb16
-rw-r--r--activerecord/test/serialization_test.rb47
-rw-r--r--activerecord/test/xml_serialization_test.rb30
3 files changed, 64 insertions, 29 deletions
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{<created_at}, @xml
end
- def test_should_allow_attribute_filtering
- @xml = Contact.new.to_xml :only => [:age, :name]
- assert_match %r{<name}, @xml
- assert_match %r{<age}, @xml
- assert_no_match %r{<created-at}, @xml
-
- @xml = Contact.new.to_xml :except => [:age, :name]
- assert_no_match %r{<name}, @xml
- assert_no_match %r{<age}, @xml
- assert_match %r{<created-at}, @xml
- end
-
def test_should_include_yielded_additions
@xml = Contact.new.to_xml do |xml|
xml.creator "David"