aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r--activerecord/test/cases/aggregations_test.rb2
-rw-r--r--activerecord/test/cases/associations/eager_test.rb12
-rw-r--r--activerecord/test/cases/attribute_methods_test.rb9
-rw-r--r--activerecord/test/cases/base_test.rb200
-rw-r--r--activerecord/test/cases/connection_adapters/connection_handler_test.rb15
-rw-r--r--activerecord/test/cases/dirty_test.rb12
-rw-r--r--activerecord/test/cases/nested_attributes_test.rb54
-rw-r--r--activerecord/test/cases/persistence_test.rb40
-rw-r--r--activerecord/test/cases/relations_test.rb8
-rw-r--r--activerecord/test/cases/serialization_test.rb12
-rw-r--r--activerecord/test/cases/serialized_attribute_test.rb205
-rw-r--r--activerecord/test/cases/session_store/session_test.rb81
-rw-r--r--activerecord/test/cases/session_store/sql_bypass_test.rb75
13 files changed, 331 insertions, 394 deletions
diff --git a/activerecord/test/cases/aggregations_test.rb b/activerecord/test/cases/aggregations_test.rb
index 5bd8f76ba2..48b06a767f 100644
--- a/activerecord/test/cases/aggregations_test.rb
+++ b/activerecord/test/cases/aggregations_test.rb
@@ -114,6 +114,8 @@ class AggregationsTest < ActiveRecord::TestCase
customers(:david).save
customers(:david).reload
assert_nil customers(:david).non_blank_gps_location
+ ensure
+ Customer.gps_conversion_was_run = nil
end
def test_nil_return_from_converter_results_in_failure_when_allow_nil_is_false
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index da4eeb3844..124bf65d3a 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -980,6 +980,18 @@ class EagerAssociationTest < ActiveRecord::TestCase
assert_equal posts(:welcome, :thinking), posts
end
+ def test_preload_has_many_with_association_condition_and_default_scope
+ post = Post.create!(:title => 'Beaches', :body => "I like beaches!")
+ Reader.create! :person => people(:david), :post => post
+ LazyReader.create! :person => people(:susan), :post => post
+
+ assert_equal 1, post.lazy_readers.to_a.size
+ assert_equal 2, post.lazy_readers_skimmers_or_not.to_a.size
+
+ post_with_readers = Post.includes(:lazy_readers_skimmers_or_not).find(post.id)
+ assert_equal 2, post_with_readers.lazy_readers_skimmers_or_not.to_a.size
+ end
+
def test_eager_loading_with_conditions_on_string_joined_table_preloads
posts = assert_queries(2) do
Post.all.merge!(:select => 'distinct posts.*', :includes => :author, :joins => "INNER JOIN comments on comments.post_id = posts.id", :where => "comments.body like 'Thank you%'", :order => 'posts.id').to_a
diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb
index 4bc68acd13..ea58a624a1 100644
--- a/activerecord/test/cases/attribute_methods_test.rb
+++ b/activerecord/test/cases/attribute_methods_test.rb
@@ -542,10 +542,10 @@ class AttributeMethodsTest < ActiveRecord::TestCase
val = t.send attr_name unless attr_name == "type"
if attribute_gets_cached
assert cached_columns.include?(attr_name)
- assert_equal val, cache[attr_name]
+ assert_equal val, cache[attr_name.to_sym]
else
assert uncached_columns.include?(attr_name)
- assert !cache.include?(attr_name)
+ assert !cache.include?(attr_name.to_sym)
end
end
end
@@ -729,11 +729,6 @@ class AttributeMethodsTest < ActiveRecord::TestCase
Object.send(:undef_method, :title) # remove test method from object
end
- def test_list_of_serialized_attributes
- assert_equal %w(content), Topic.serialized_attributes.keys
- assert_equal %w(preferences), Contact.serialized_attributes.keys
- end
-
def test_instance_method_should_be_defined_on_the_base_class
subklass = Class.new(Topic)
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index 63981a68a9..c6f7e8cf0f 100644
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -26,7 +26,6 @@ require 'models/bird'
require 'models/teapot'
require 'rexml/document'
require 'active_support/core_ext/exception'
-require 'bcrypt'
class FirstAbstractClass < ActiveRecord::Base
self.abstract_class = true
@@ -467,13 +466,13 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_singular_table_name_guesses_for_individual_table
- CreditCard.pluralize_table_names = false
- CreditCard.reset_table_name
- assert_equal "credit_card", CreditCard.table_name
+ Post.pluralize_table_names = false
+ Post.reset_table_name
+ assert_equal "post", Post.table_name
assert_equal "categories", Category.table_name
ensure
- CreditCard.pluralize_table_names = true
- CreditCard.reset_table_name
+ Post.pluralize_table_names = true
+ Post.reset_table_name
end
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
@@ -1297,195 +1296,6 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal 0, replies.size
end
- MyObject = Struct.new :attribute1, :attribute2
-
- def test_serialized_attribute
- Topic.serialize("content", MyObject)
-
- myobj = MyObject.new('value1', 'value2')
- topic = Topic.create("content" => myobj)
- assert_equal(myobj, topic.content)
-
- topic.reload
- assert_equal(myobj, topic.content)
- end
-
- def test_serialized_attribute_in_base_class
- Topic.serialize("content", Hash)
-
- hash = { 'content1' => 'value1', 'content2' => 'value2' }
- important_topic = ImportantTopic.create("content" => hash)
- assert_equal(hash, important_topic.content)
-
- important_topic.reload
- assert_equal(hash, important_topic.content)
- end
-
- # This test was added to fix GH #4004. Obviously the value returned
- # is not really the value 'before type cast' so we should maybe think
- # about changing that in the future.
- def test_serialized_attribute_before_type_cast_returns_unserialized_value
- klass = Class.new(ActiveRecord::Base)
- klass.table_name = "topics"
- klass.serialize :content, Hash
-
- t = klass.new(:content => { :foo => :bar })
- assert_equal({ :foo => :bar }, t.content_before_type_cast)
- t.save!
- t.reload
- assert_equal({ :foo => :bar }, t.content_before_type_cast)
- end
-
- def test_serialized_attribute_calling_dup_method
- klass = Class.new(ActiveRecord::Base)
- klass.table_name = "topics"
- klass.serialize :content, JSON
-
- t = klass.new(:content => { :foo => :bar }).dup
- assert_equal({ :foo => :bar }, t.content_before_type_cast)
- end
-
- def test_serialized_attribute_declared_in_subclass
- hash = { 'important1' => 'value1', 'important2' => 'value2' }
- important_topic = ImportantTopic.create("important" => hash)
- assert_equal(hash, important_topic.important)
-
- important_topic.reload
- assert_equal(hash, important_topic.important)
- assert_equal(hash, important_topic.read_attribute(:important))
- end
-
- def test_serialized_time_attribute
- myobj = Time.local(2008,1,1,1,0)
- topic = Topic.create("content" => myobj).reload
- assert_equal(myobj, topic.content)
- end
-
- def test_serialized_string_attribute
- myobj = "Yes"
- topic = Topic.create("content" => myobj).reload
- assert_equal(myobj, topic.content)
- end
-
- def test_nil_serialized_attribute_without_class_constraint
- topic = Topic.new
- assert_nil topic.content
- end
-
- def test_nil_not_serialized_without_class_constraint
- assert Topic.new(:content => nil).save
- assert_equal 1, Topic.where(:content => nil).count
- end
-
- def test_nil_not_serialized_with_class_constraint
- Topic.serialize :content, Hash
- assert Topic.new(:content => nil).save
- assert_equal 1, Topic.where(:content => nil).count
- ensure
- Topic.serialize(:content)
- end
-
- def test_should_raise_exception_on_serialized_attribute_with_type_mismatch
- myobj = MyObject.new('value1', 'value2')
- topic = Topic.new(:content => myobj)
- assert topic.save
- Topic.serialize(:content, Hash)
- assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).reload.content }
- ensure
- Topic.serialize(:content)
- end
-
- def test_serialized_attribute_with_class_constraint
- settings = { "color" => "blue" }
- Topic.serialize(:content, Hash)
- topic = Topic.new(:content => settings)
- assert topic.save
- assert_equal(settings, Topic.find(topic.id).content)
- ensure
- Topic.serialize(:content)
- end
-
- def test_serialized_default_class
- Topic.serialize(:content, Hash)
- topic = Topic.new
- assert_equal Hash, topic.content.class
- assert_equal Hash, topic.read_attribute(:content).class
- topic.content["beer"] = "MadridRb"
- assert topic.save
- topic.reload
- assert_equal Hash, topic.content.class
- assert_equal "MadridRb", topic.content["beer"]
- ensure
- Topic.serialize(:content)
- end
-
- def test_serialized_no_default_class_for_object
- topic = Topic.new
- assert_nil topic.content
- end
-
- def test_serialized_boolean_value_true
- Topic.serialize(:content)
- topic = Topic.new(:content => true)
- assert topic.save
- topic = topic.reload
- assert_equal topic.content, true
- end
-
- def test_serialized_boolean_value_false
- Topic.serialize(:content)
- topic = Topic.new(:content => false)
- assert topic.save
- topic = topic.reload
- assert_equal topic.content, false
- end
-
- def test_serialize_with_coder
- coder = Class.new {
- # Identity
- def load(thing)
- thing
- end
-
- # base 64
- def dump(thing)
- [thing].pack('m')
- end
- }.new
-
- Topic.serialize(:content, coder)
- s = 'hello world'
- topic = Topic.new(:content => s)
- assert topic.save
- topic = topic.reload
- assert_equal [s].pack('m'), topic.content
- ensure
- Topic.serialize(:content)
- end
-
- def test_serialize_with_bcrypt_coder
- crypt_coder = Class.new {
- def load(thing)
- return unless thing
- BCrypt::Password.new thing
- end
-
- def dump(thing)
- BCrypt::Password.create(thing).to_s
- end
- }.new
-
- Topic.serialize(:content, crypt_coder)
- password = 'password'
- topic = Topic.new(:content => password)
- assert topic.save
- topic = topic.reload
- assert_kind_of BCrypt::Password, topic.content
- assert_equal(true, topic.content == password, 'password should equal')
- ensure
- Topic.serialize(:content)
- end
-
def test_quote
author_name = "\\ \001 ' \n \\n \""
topic = Topic.create('author_name' => author_name)
diff --git a/activerecord/test/cases/connection_adapters/connection_handler_test.rb b/activerecord/test/cases/connection_adapters/connection_handler_test.rb
index 17cb447105..e1579d037f 100644
--- a/activerecord/test/cases/connection_adapters/connection_handler_test.rb
+++ b/activerecord/test/cases/connection_adapters/connection_handler_test.rb
@@ -4,16 +4,11 @@ module ActiveRecord
module ConnectionAdapters
class ConnectionHandlerTest < ActiveRecord::TestCase
def setup
+ @klass = Class.new { include Model::Tag }
+ @subklass = Class.new(@klass) { include Model::Tag }
+
@handler = ConnectionHandler.new
- @handler.establish_connection 'america', Base.connection_pool.spec
- @klass = Class.new do
- include Model::Tag
- def self.name; 'america'; end
- end
- @subklass = Class.new(@klass) do
- include Model::Tag
- def self.name; 'north america'; end
- end
+ @handler.establish_connection @klass, Base.connection_pool.spec
end
def test_retrieve_connection
@@ -42,6 +37,8 @@ module ActiveRecord
def test_retrieve_connection_pool_uses_superclass_pool_after_subclass_establish_and_remove
@handler.establish_connection 'north america', Base.connection_pool.spec
+ assert_same @handler.retrieve_connection_pool(@klass),
+ @handler.retrieve_connection_pool(@subklass)
@handler.remove_connection @subklass
assert_same @handler.retrieve_connection_pool(@klass),
diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb
index 92677b9926..9a2a5a4e3c 100644
--- a/activerecord/test/cases/dirty_test.rb
+++ b/activerecord/test/cases/dirty_test.rb
@@ -509,6 +509,16 @@ class DirtyTest < ActiveRecord::TestCase
assert_not_nil pirate.previous_changes['updated_on'][1]
assert !pirate.previous_changes.key?('parrot_id')
assert !pirate.previous_changes.key?('created_on')
+
+ pirate = Pirate.find_by_catchphrase("Ahoy!")
+ pirate.update_attribute(:catchphrase, "Ninjas suck!")
+
+ assert_equal 2, pirate.previous_changes.size
+ assert_equal ["Ahoy!", "Ninjas suck!"], pirate.previous_changes['catchphrase']
+ assert_not_nil pirate.previous_changes['updated_on'][0]
+ assert_not_nil pirate.previous_changes['updated_on'][1]
+ assert !pirate.previous_changes.key?('parrot_id')
+ assert !pirate.previous_changes.key?('created_on')
end
if ActiveRecord::Base.connection.supports_migrations?
@@ -534,7 +544,7 @@ class DirtyTest < ActiveRecord::TestCase
pirate = target.create(:created_on => created_on)
pirate.reload # Here mysql truncate the usec value to 0
-
+
pirate.created_on = created_on
assert !pirate.created_on_changed?
end
diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb
index 3a234f0cc1..07862ca4ca 100644
--- a/activerecord/test/cases/nested_attributes_test.rb
+++ b/activerecord/test/cases/nested_attributes_test.rb
@@ -771,9 +771,9 @@ module NestedAttributesOnACollectionAssociationTests
assert !man.errors[:"interests.man"].empty?
end
end
- # restore :inverse_of
+ ensure
Man.reflect_on_association(:interests).options[:inverse_of] = :man
- Interest.reflect_on_association(:man).options[:inverse_of] = :interests
+ Interest.reflect_on_association(:man).options[:inverse_of] = :interests
end
def test_can_use_symbols_as_object_identifier
@@ -783,12 +783,14 @@ module NestedAttributesOnACollectionAssociationTests
def test_numeric_colum_changes_from_zero_to_no_empty_string
Man.accepts_nested_attributes_for(:interests)
- Interest.validates_numericality_of(:zine_id)
- man = Man.create(:name => 'John')
- interest = man.interests.create(:topic=>'bar',:zine_id => 0)
- assert interest.save
- assert !man.update_attributes({:interests_attributes => { :id => interest.id, :zine_id => 'foo' }})
+ repair_validations(Interest) do
+ Interest.validates_numericality_of(:zine_id)
+ man = Man.create(name: 'John')
+ interest = man.interests.create(topic: 'bar', zine_id: 0)
+ assert interest.save
+ assert !man.update_attributes({interests_attributes: { id: interest.id, zine_id: 'foo' }})
+ end
end
private
@@ -846,13 +848,7 @@ class TestNestedAttributesOnAHasAndBelongsToManyAssociation < ActiveRecord::Test
include NestedAttributesOnACollectionAssociationTests
end
-class TestNestedAttributesLimit < ActiveRecord::TestCase
- def setup
- Pirate.accepts_nested_attributes_for :parrots, :limit => 2
-
- @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
- end
-
+module NestedAttributesLimitTests
def teardown
Pirate.accepts_nested_attributes_for :parrots, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
end
@@ -876,6 +872,36 @@ class TestNestedAttributesLimit < ActiveRecord::TestCase
end
end
+class TestNestedAttributesLimitNumeric < ActiveRecord::TestCase
+ def setup
+ Pirate.accepts_nested_attributes_for :parrots, :limit => 2
+
+ @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+ end
+
+ include NestedAttributesLimitTests
+end
+
+class TestNestedAttributesLimitSymbol < ActiveRecord::TestCase
+ def setup
+ Pirate.accepts_nested_attributes_for :parrots, :limit => :parrots_limit
+
+ @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?", :parrots_limit => 2)
+ end
+
+ include NestedAttributesLimitTests
+end
+
+class TestNestedAttributesLimitProc < ActiveRecord::TestCase
+ def setup
+ Pirate.accepts_nested_attributes_for :parrots, :limit => proc { 2 }
+
+ @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+ end
+
+ include NestedAttributesLimitTests
+end
+
class TestNestedAttributesWithNonStandardPrimaryKeys < ActiveRecord::TestCase
fixtures :owners, :pets
diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb
index 72b8219782..4ffa4836e0 100644
--- a/activerecord/test/cases/persistence_test.rb
+++ b/activerecord/test/cases/persistence_test.rb
@@ -371,10 +371,50 @@ class PersistencesTest < ActiveRecord::TestCase
assert_raise(ActiveSupport::FrozenObjectError) { client.name = "something else" }
end
+ def test_update_attribute
+ assert !Topic.find(1).approved?
+ Topic.find(1).update_attribute("approved", true)
+ assert Topic.find(1).approved?
+
+ Topic.find(1).update_attribute(:approved, false)
+ assert !Topic.find(1).approved?
+ end
+
def test_update_attribute_does_not_choke_on_nil
assert Topic.find(1).update_attributes(nil)
end
+ def test_update_attribute_for_readonly_attribute
+ minivan = Minivan.find('m1')
+ assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_attribute(:color, 'black') }
+ end
+
+ def test_update_attribute_with_one_updated
+ t = Topic.first
+ t.update_attribute(:title, 'super_title')
+ assert_equal 'super_title', t.title
+ assert !t.changed?, "topic should not have changed"
+ assert !t.title_changed?, "title should not have changed"
+ assert_nil t.title_change, 'title change should be nil'
+
+ t.reload
+ assert_equal 'super_title', t.title
+ end
+
+ def test_update_attribute_for_updated_at_on
+ developer = Developer.find(1)
+ prev_month = Time.now.prev_month
+
+ developer.update_attribute(:updated_at, prev_month)
+ assert_equal prev_month, developer.updated_at
+
+ developer.update_attribute(:salary, 80001)
+ assert_not_equal prev_month, developer.updated_at
+
+ developer.reload
+ assert_not_equal prev_month, developer.updated_at
+ end
+
def test_update_column
topic = Topic.find(1)
topic.update_column("approved", true)
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 684538940a..b91423351e 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -656,6 +656,14 @@ class RelationTest < ActiveRecord::TestCase
assert_raises(ActiveRecord::ActiveRecordError) { Author.limit(10).delete_all }
end
+ def test_select_takes_a_variable_list_of_args
+ david = developers(:david)
+
+ developer = Developer.where(id: david.id).select(:name, :salary).first
+ assert_equal david.name, developer.name
+ assert_equal david.salary, developer.salary
+ end
+
def test_select_argument_error
assert_raises(ArgumentError) { Developer.select }
end
diff --git a/activerecord/test/cases/serialization_test.rb b/activerecord/test/cases/serialization_test.rb
index 10d8ccc711..25b860878a 100644
--- a/activerecord/test/cases/serialization_test.rb
+++ b/activerecord/test/cases/serialization_test.rb
@@ -18,12 +18,6 @@ class SerializationTest < ActiveRecord::TestCase
}
end
- def test_serialized_init_with
- topic = Topic.allocate
- topic.init_with('attributes' => { 'content' => '--- foo' })
- assert_equal 'foo', topic.content
- end
-
def test_serialize_should_be_reversible
FORMATS.each do |format|
@serialized = Contact.new.send("to_#{format}")
@@ -51,10 +45,4 @@ class SerializationTest < ActiveRecord::TestCase
assert_equal @contact_attributes[:awesome], contact.awesome, "For #{format}"
end
end
-
- def test_serialized_attributes_are_class_level_settings
- topic = Topic.new
- assert_raise(NoMethodError) { topic.serialized_attributes = [] }
- assert_deprecated { topic.serialized_attributes }
- end
end
diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb
new file mode 100644
index 0000000000..f24ee54cd2
--- /dev/null
+++ b/activerecord/test/cases/serialized_attribute_test.rb
@@ -0,0 +1,205 @@
+require "cases/helper"
+require 'models/topic'
+require 'bcrypt'
+
+class SerializedAttributeTest < ActiveRecord::TestCase
+ fixtures :topics
+
+ MyObject = Struct.new :attribute1, :attribute2
+
+ def teardown
+ super
+ Topic.serialize("content")
+ end
+
+ def test_list_of_serialized_attributes
+ assert_equal %w(content), Topic.serialized_attributes.keys
+ end
+
+ def test_serialized_attributes_are_class_level_settings
+ topic = Topic.new
+ assert_raise(NoMethodError) { topic.serialized_attributes = [] }
+ assert_deprecated { topic.serialized_attributes }
+ end
+
+ def test_serialized_attribute
+ Topic.serialize("content", MyObject)
+
+ myobj = MyObject.new('value1', 'value2')
+ topic = Topic.create("content" => myobj)
+ assert_equal(myobj, topic.content)
+
+ topic.reload
+ assert_equal(myobj, topic.content)
+ end
+
+ def test_serialized_attribute_init_with
+ topic = Topic.allocate
+ topic.init_with('attributes' => { 'content' => '--- foo' })
+ assert_equal 'foo', topic.content
+ end
+
+ def test_serialized_attribute_in_base_class
+ Topic.serialize("content", Hash)
+
+ hash = { 'content1' => 'value1', 'content2' => 'value2' }
+ important_topic = ImportantTopic.create("content" => hash)
+ assert_equal(hash, important_topic.content)
+
+ important_topic.reload
+ assert_equal(hash, important_topic.content)
+ end
+
+ # This test was added to fix GH #4004. Obviously the value returned
+ # is not really the value 'before type cast' so we should maybe think
+ # about changing that in the future.
+ def test_serialized_attribute_before_type_cast_returns_unserialized_value
+ Topic.serialize :content, Hash
+
+ t = Topic.new(:content => { :foo => :bar })
+ assert_equal({ :foo => :bar }, t.content_before_type_cast)
+ t.save!
+ t.reload
+ assert_equal({ :foo => :bar }, t.content_before_type_cast)
+ end
+
+ def test_serialized_attribute_calling_dup_method
+ Topic.serialize :content, JSON
+
+ t = Topic.new(:content => { :foo => :bar }).dup
+ assert_equal({ :foo => :bar }, t.content_before_type_cast)
+ end
+
+ def test_serialized_attribute_declared_in_subclass
+ hash = { 'important1' => 'value1', 'important2' => 'value2' }
+ important_topic = ImportantTopic.create("important" => hash)
+ assert_equal(hash, important_topic.important)
+
+ important_topic.reload
+ assert_equal(hash, important_topic.important)
+ assert_equal(hash, important_topic.read_attribute(:important))
+ end
+
+ def test_serialized_time_attribute
+ myobj = Time.local(2008,1,1,1,0)
+ topic = Topic.create("content" => myobj).reload
+ assert_equal(myobj, topic.content)
+ end
+
+ def test_serialized_string_attribute
+ myobj = "Yes"
+ topic = Topic.create("content" => myobj).reload
+ assert_equal(myobj, topic.content)
+ end
+
+ def test_nil_serialized_attribute_without_class_constraint
+ topic = Topic.new
+ assert_nil topic.content
+ end
+
+ def test_nil_not_serialized_without_class_constraint
+ assert Topic.new(:content => nil).save
+ assert_equal 1, Topic.where(:content => nil).count
+ end
+
+ def test_nil_not_serialized_with_class_constraint
+ Topic.serialize :content, Hash
+ assert Topic.new(:content => nil).save
+ assert_equal 1, Topic.where(:content => nil).count
+ end
+
+ def test_serialized_attribute_should_raise_exception_on_save_with_wrong_type
+ Topic.serialize(:content, Hash)
+ topic = Topic.new(:content => "string")
+ assert_raise(ActiveRecord::SerializationTypeMismatch) { topic.save }
+ end
+
+ def test_should_raise_exception_on_serialized_attribute_with_type_mismatch
+ myobj = MyObject.new('value1', 'value2')
+ topic = Topic.new(:content => myobj)
+ assert topic.save
+ Topic.serialize(:content, Hash)
+ assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).content }
+ end
+
+ def test_serialized_attribute_with_class_constraint
+ settings = { "color" => "blue" }
+ Topic.serialize(:content, Hash)
+ topic = Topic.new(:content => settings)
+ assert topic.save
+ assert_equal(settings, Topic.find(topic.id).content)
+ end
+
+ def test_serialized_default_class
+ Topic.serialize(:content, Hash)
+ topic = Topic.new
+ assert_equal Hash, topic.content.class
+ assert_equal Hash, topic.read_attribute(:content).class
+ topic.content["beer"] = "MadridRb"
+ assert topic.save
+ topic.reload
+ assert_equal Hash, topic.content.class
+ assert_equal "MadridRb", topic.content["beer"]
+ end
+
+ def test_serialized_no_default_class_for_object
+ topic = Topic.new
+ assert_nil topic.content
+ end
+
+ def test_serialized_boolean_value_true
+ topic = Topic.new(:content => true)
+ assert topic.save
+ topic = topic.reload
+ assert_equal topic.content, true
+ end
+
+ def test_serialized_boolean_value_false
+ topic = Topic.new(:content => false)
+ assert topic.save
+ topic = topic.reload
+ assert_equal topic.content, false
+ end
+
+ def test_serialize_with_coder
+ coder = Class.new {
+ # Identity
+ def load(thing)
+ thing
+ end
+
+ # base 64
+ def dump(thing)
+ [thing].pack('m')
+ end
+ }.new
+
+ Topic.serialize(:content, coder)
+ s = 'hello world'
+ topic = Topic.new(:content => s)
+ assert topic.save
+ topic = topic.reload
+ assert_equal [s].pack('m'), topic.content
+ end
+
+ def test_serialize_with_bcrypt_coder
+ crypt_coder = Class.new {
+ def load(thing)
+ return unless thing
+ BCrypt::Password.new thing
+ end
+
+ def dump(thing)
+ BCrypt::Password.create(thing).to_s
+ end
+ }.new
+
+ Topic.serialize(:content, crypt_coder)
+ password = 'password'
+ topic = Topic.new(:content => password)
+ assert topic.save
+ topic = topic.reload
+ assert_kind_of BCrypt::Password, topic.content
+ assert_equal(true, topic.content == password, 'password should equal')
+ end
+end
diff --git a/activerecord/test/cases/session_store/session_test.rb b/activerecord/test/cases/session_store/session_test.rb
deleted file mode 100644
index a3b8ab74d9..0000000000
--- a/activerecord/test/cases/session_store/session_test.rb
+++ /dev/null
@@ -1,81 +0,0 @@
-require 'cases/helper'
-require 'action_dispatch'
-require 'active_record/session_store'
-
-module ActiveRecord
- class SessionStore
- class SessionTest < ActiveRecord::TestCase
- self.use_transactional_fixtures = false
-
- attr_reader :session_klass
-
- def setup
- super
- ActiveRecord::Base.connection.schema_cache.clear!
- Session.drop_table! if Session.table_exists?
- @session_klass = Class.new(Session)
- end
-
- def test_data_column_name
- # default column name is 'data'
- assert_equal 'data', Session.data_column_name
- end
-
- def test_table_name
- assert_equal 'sessions', Session.table_name
- end
-
- def test_accessible_attributes
- assert Session.accessible_attributes.include?(:session_id)
- assert Session.accessible_attributes.include?(:data)
- assert Session.accessible_attributes.include?(:marshaled_data)
- end
-
- def test_create_table!
- assert !Session.table_exists?
- Session.create_table!
- assert Session.table_exists?
- Session.drop_table!
- assert !Session.table_exists?
- end
-
- def test_find_by_sess_id_compat
- Session.reset_column_information
- klass = Class.new(Session) do
- def self.session_id_column
- 'sessid'
- end
- end
- klass.create_table!
-
- assert klass.columns_hash['sessid'], 'sessid column exists'
- session = klass.new(:data => 'hello')
- session.sessid = "100"
- session.save!
-
- found = klass.find_by_session_id("100")
- assert_equal session, found
- assert_equal session.sessid, found.session_id
- ensure
- klass.drop_table!
- Session.reset_column_information
- end
-
- def test_find_by_session_id
- Session.create_table!
- session_id = "10"
- s = session_klass.create!(:data => 'world', :session_id => session_id)
- t = session_klass.find_by_session_id(session_id)
- assert_equal s, t
- assert_equal s.data, t.data
- Session.drop_table!
- end
-
- def test_loaded?
- Session.create_table!
- s = Session.new
- assert !s.loaded?, 'session is not loaded'
- end
- end
- end
-end
diff --git a/activerecord/test/cases/session_store/sql_bypass_test.rb b/activerecord/test/cases/session_store/sql_bypass_test.rb
deleted file mode 100644
index b8cf4cf2cc..0000000000
--- a/activerecord/test/cases/session_store/sql_bypass_test.rb
+++ /dev/null
@@ -1,75 +0,0 @@
-require 'cases/helper'
-require 'action_dispatch'
-require 'active_record/session_store'
-
-module ActiveRecord
- class SessionStore
- class SqlBypassTest < ActiveRecord::TestCase
- def setup
- super
- Session.drop_table! if Session.table_exists?
- end
-
- def test_create_table
- assert !Session.table_exists?
- SqlBypass.create_table!
- assert Session.table_exists?
- SqlBypass.drop_table!
- assert !Session.table_exists?
- end
-
- def test_new_record?
- s = SqlBypass.new :data => 'foo', :session_id => 10
- assert s.new_record?, 'this is a new record!'
- end
-
- def test_persisted?
- s = SqlBypass.new :data => 'foo', :session_id => 10
- assert !s.persisted?, 'this is a new record!'
- end
-
- def test_not_loaded?
- s = SqlBypass.new({})
- assert !s.loaded?, 'it is not loaded'
- end
-
- def test_loaded?
- s = SqlBypass.new :data => 'hello'
- assert s.loaded?, 'it is loaded'
- end
-
- def test_save
- SqlBypass.create_table! unless Session.table_exists?
- session_id = 20
- s = SqlBypass.new :data => 'hello', :session_id => session_id
- s.save
- t = SqlBypass.find_by_session_id session_id
- assert_equal s.session_id, t.session_id
- assert_equal s.data, t.data
- end
-
- def test_destroy
- SqlBypass.create_table! unless Session.table_exists?
- session_id = 20
- s = SqlBypass.new :data => 'hello', :session_id => session_id
- s.save
- s.destroy
- assert_nil SqlBypass.find_by_session_id session_id
- end
-
- def test_data_column
- SqlBypass.drop_table! if exists = Session.table_exists?
- old, SqlBypass.data_column = SqlBypass.data_column, 'foo'
- SqlBypass.create_table!
-
- session_id = 20
- SqlBypass.new(:data => 'hello', :session_id => session_id).save
- assert_equal 'hello', SqlBypass.find_by_session_id(session_id).data
- ensure
- SqlBypass.drop_table!
- SqlBypass.data_column = old
- SqlBypass.create_table! if exists
- end
- end
- end
-end