aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2012-05-22 07:31:40 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2012-05-22 07:31:40 -0700
commitd3ee0a24498b1d54eefbf484720cd63a2b653c65 (patch)
tree4acf1350a0608996d2ba3ded3e74d3d71d35a4ff /activerecord/test
parent6b9f077c85e375bc8089fdc14be6574b9236d7ee (diff)
parent940c135175cba6e6611cb67a93b9370da4e81fd9 (diff)
downloadrails-d3ee0a24498b1d54eefbf484720cd63a2b653c65.tar.gz
rails-d3ee0a24498b1d54eefbf484720cd63a2b653c65.tar.bz2
rails-d3ee0a24498b1d54eefbf484720cd63a2b653c65.zip
Merge pull request #6368 from Antiarchitect/store-indifferent-access
Support migrating from Hash to HashWithIndifferentAccess for accessing attributes.
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/store_test.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/activerecord/test/cases/store_test.rb b/activerecord/test/cases/store_test.rb
index e1d0f1f799..3a5d84df9f 100644
--- a/activerecord/test/cases/store_test.rb
+++ b/activerecord/test/cases/store_test.rb
@@ -41,6 +41,40 @@ class StoreTest < ActiveRecord::TestCase
assert_equal false, @john.remember_login
end
+ test "preserve store attributes data in HashWithIndifferentAccess format without any conversion" do
+ @john.json_data = HashWithIndifferentAccess.new(:height => 'tall', 'weight' => 'heavy')
+ @john.height = 'low'
+ assert_equal true, @john.json_data.instance_of?(HashWithIndifferentAccess)
+ assert_equal 'low', @john.json_data[:height]
+ assert_equal 'low', @john.json_data['height']
+ assert_equal 'heavy', @john.json_data[:weight]
+ assert_equal 'heavy', @john.json_data['weight']
+ end
+
+ test "convert store attributes from Hash to HashWithIndifferentAccess saving the data and access attributes indifferently" do
+ @john.json_data = { :height => 'tall', 'weight' => 'heavy' }
+ assert_equal true, @john.json_data.instance_of?(Hash)
+ assert_equal 'tall', @john.json_data[:height]
+ assert_equal nil, @john.json_data['height']
+ assert_equal nil, @john.json_data[:weight]
+ assert_equal 'heavy', @john.json_data['weight']
+ @john.height = 'low'
+ assert_equal true, @john.json_data.instance_of?(HashWithIndifferentAccess)
+ assert_equal 'low', @john.json_data[:height]
+ assert_equal 'low', @john.json_data['height']
+ assert_equal 'heavy', @john.json_data[:weight]
+ assert_equal 'heavy', @john.json_data['weight']
+ end
+
+ test "convert store attributes from any format other than Hash or HashWithIndifferent access losing the data" do
+ @john.json_data = "somedata"
+ @john.height = 'low'
+ assert_equal true, @john.json_data.instance_of?(HashWithIndifferentAccess)
+ assert_equal 'low', @john.json_data[:height]
+ assert_equal 'low', @john.json_data['height']
+ assert_equal false, @john.json_data.delete_if { |k, v| k == 'height' }.any?
+ end
+
test "reading store attributes through accessors encoded with JSON" do
assert_equal 'tall', @john.height
assert_nil @john.weight