class SerializationTest
Constants
- FORMATS
Public Instance Methods
setup()
click to toggle source
# File activerecord/test/cases/serialization_test.rb, line 15 def setup @contact_attributes = { name: "aaron stack", age: 25, avatar: "binarydata", created_at: Time.utc(2006, 8, 1), awesome: false, preferences: { gem: "<strong>ruby</strong>" }, alternative_id: nil, id: nil } end
test_except_include()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 163 def test_except_include expected = { "name" => "David", "email" => "david@example.com", "friends" => [{ "name" => "Joe", "email" => "joe@example.com" }, { "name" => "Sue", "email" => "sue@example.com" }] } assert_equal expected, @user.serializable_hash(except: :gender, include: { friends: { except: :gender } }) end
test_find_records_by_serialized_attributes_through_join()
click to toggle source
# File activerecord/test/cases/serialization_test.rb, line 100 def test_find_records_by_serialized_attributes_through_join author = Author.create!(name: "David") author.serialized_posts.create!(title: "Hello") assert_equal 1, Author.joins(:serialized_posts).where(name: "David", serialized_posts: { title: "Hello" }).length end
test_include_option_with_ary()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 127 def test_include_option_with_ary @user.friends = FriendList.new(@user.friends) expected = { "email" => "david@example.com", "gender" => "male", "name" => "David", "friends" => [{ "name" => "Joe", "email" => "joe@example.com", "gender" => "male" }, { "name" => "Sue", "email" => "sue@example.com", "gender" => "female" }] } assert_equal expected, @user.serializable_hash(include: :friends) end
test_include_option_with_empty_association()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 111 def test_include_option_with_empty_association @user.friends = [] expected = { "email" => "david@example.com", "gender" => "male", "name" => "David", "friends" => [] } assert_equal expected, @user.serializable_hash(include: :friends) end
test_include_option_with_plural_association()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 104 def test_include_option_with_plural_association expected = { "email" => "david@example.com", "gender" => "male", "name" => "David", "friends" => [{ "name" => "Joe", "email" => "joe@example.com", "gender" => "male" }, { "name" => "Sue", "email" => "sue@example.com", "gender" => "female" }] } assert_equal expected, @user.serializable_hash(include: :friends) end
test_include_option_with_singular_association()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 98 def test_include_option_with_singular_association expected = { "name" => "David", "gender" => "male", "email" => "david@example.com", "address" => { "street" => "123 Lane", "city" => "Springfield", "state" => "CA", "zip" => 11111 } } assert_equal expected, @user.serializable_hash(include: :address) end
test_include_root_in_json_allows_inheritance()
click to toggle source
# File activerecord/test/cases/serialization_test.rb, line 60 def test_include_root_in_json_allows_inheritance original_root_in_json = ActiveRecord::Base.include_root_in_json ActiveRecord::Base.include_root_in_json = true klazz = Class.new(ActiveRecord::Base) klazz.table_name = "topics" assert klazz.include_root_in_json klazz.include_root_in_json = false assert ActiveRecord::Base.include_root_in_json assert !klazz.include_root_in_json assert !klazz.new.include_root_in_json ensure ActiveRecord::Base.include_root_in_json = original_root_in_json end
test_include_root_in_json_is_false_by_default()
click to toggle source
# File activerecord/test/cases/serialization_test.rb, line 28 def test_include_root_in_json_is_false_by_default assert_equal false, ActiveRecord::Base.include_root_in_json, "include_root_in_json should be false by default but was not" end
test_include_with_options()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 143 def test_include_with_options expected = { "email" => "david@example.com", "gender" => "male", "name" => "David", "address" => { "street" => "123 Lane" } } assert_equal expected, @user.serializable_hash(include: { address: { only: "street" } }) end
test_method_serializable_hash_should_work()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 55 def test_method_serializable_hash_should_work expected = { "name" => "David", "gender" => "male", "email" => "david@example.com" } assert_equal expected, @user.serializable_hash end
test_method_serializable_hash_should_work_with_except_and_methods()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 80 def test_method_serializable_hash_should_work_with_except_and_methods expected = { "gender" => "male", "foo" => "i_am_foo", "bar" => "i_am_bar" } assert_equal expected, @user.serializable_hash(except: [:name, :email], methods: [:foo, :bar]) end
test_method_serializable_hash_should_work_with_except_option()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 65 def test_method_serializable_hash_should_work_with_except_option expected = { "gender" => "male", "email" => "david@example.com" } assert_equal expected, @user.serializable_hash(except: [:name]) end
test_method_serializable_hash_should_work_with_methods_option()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 70 def test_method_serializable_hash_should_work_with_methods_option expected = { "name" => "David", "gender" => "male", "foo" => "i_am_foo", "bar" => "i_am_bar", "email" => "david@example.com" } assert_equal expected, @user.serializable_hash(methods: [:foo, :bar]) end
test_method_serializable_hash_should_work_with_only_and_methods()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 75 def test_method_serializable_hash_should_work_with_only_and_methods expected = { "foo" => "i_am_foo", "bar" => "i_am_bar" } assert_equal expected, @user.serializable_hash(only: [], methods: [:foo, :bar]) end
test_method_serializable_hash_should_work_with_only_option()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 60 def test_method_serializable_hash_should_work_with_only_option expected = { "name" => "David" } assert_equal expected, @user.serializable_hash(only: [:name]) end
test_multiple_includes()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 135 def test_multiple_includes expected = { "email" => "david@example.com", "gender" => "male", "name" => "David", "address" => { "street" => "123 Lane", "city" => "Springfield", "state" => "CA", "zip" => 11111 }, "friends" => [{ "name" => "Joe", "email" => "joe@example.com", "gender" => "male" }, { "name" => "Sue", "email" => "sue@example.com", "gender" => "female" }] } assert_equal expected, @user.serializable_hash(include: [:address, :friends]) end
test_multiple_includes_with_options()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 170 def test_multiple_includes_with_options expected = { "email" => "david@example.com", "gender" => "male", "name" => "David", "address" => { "street" => "123 Lane" }, "friends" => [{ "name" => "Joe", "email" => "joe@example.com", "gender" => "male" }, { "name" => "Sue", "email" => "sue@example.com", "gender" => "female" }] } assert_equal expected, @user.serializable_hash(include: [{ address: { only: "street" } }, :friends]) end
test_nested_include()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 149 def test_nested_include @user.friends.first.friends = [@user] expected = { "email" => "david@example.com", "gender" => "male", "name" => "David", "friends" => [{ "name" => "Joe", "email" => "joe@example.com", "gender" => "male", "friends" => [{ "email" => "david@example.com", "gender" => "male", "name" => "David" }] }, { "name" => "Sue", "email" => "sue@example.com", "gender" => "female", "friends" => [] }] } assert_equal expected, @user.serializable_hash(include: { friends: { include: :friends } }) end
test_only_include()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 158 def test_only_include expected = { "name" => "David", "friends" => [{ "name" => "Joe" }, { "name" => "Sue" }] } assert_equal expected, @user.serializable_hash(only: :name, include: { friends: { only: :name } }) end
test_read_attribute_for_serialization_with_format_after_find()
click to toggle source
# File activerecord/test/cases/serialization_test.rb, line 92 def test_read_attribute_for_serialization_with_format_after_find klazz = Class.new(ActiveRecord::Base) klazz.table_name = "books" book = klazz.find(books(:awdr).id) assert_equal "paperback", book.read_attribute_for_serialization(:format) end
test_read_attribute_for_serialization_with_format_after_init()
click to toggle source
# File activerecord/test/cases/serialization_test.rb, line 84 def test_read_attribute_for_serialization_with_format_after_init klazz = Class.new(ActiveRecord::Base) klazz.table_name = "books" book = klazz.new(format: "paperback") assert_equal "paperback", book.read_attribute_for_serialization(:format) end
test_read_attribute_for_serialization_with_format_without_method_missing()
click to toggle source
# File activerecord/test/cases/serialization_test.rb, line 76 def test_read_attribute_for_serialization_with_format_without_method_missing klazz = Class.new(ActiveRecord::Base) klazz.table_name = "books" book = klazz.new assert_nil book.read_attribute_for_serialization(:format) end
test_serialize_should_allow_attribute_except_filtering()
click to toggle source
# File activerecord/test/cases/serialization_test.rb, line 50 def test_serialize_should_allow_attribute_except_filtering FORMATS.each do |format| @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
test_serialize_should_allow_attribute_only_filtering()
click to toggle source
# File activerecord/test/cases/serialization_test.rb, line 41 def test_serialize_should_allow_attribute_only_filtering FORMATS.each do |format| @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
test_serialize_should_be_reversible()
click to toggle source
# File activerecord/test/cases/serialization_test.rb, line 32 def test_serialize_should_be_reversible FORMATS.each do |format| @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
test_should_raise_NoMethodError_for_non_existing_method()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 85 def test_should_raise_NoMethodError_for_non_existing_method assert_raise(NoMethodError) { @user.serializable_hash(methods: [:nada]) } end
test_should_use_read_attribute_for_serialization()
click to toggle source
# File activemodel/test/cases/serialization_test.rb, line 89 def test_should_use_read_attribute_for_serialization def @user.read_attribute_for_serialization(n) "Jon" end expected = { "name" => "Jon" } assert_equal expected, @user.serializable_hash(only: :name) end