aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-26 06:40:08 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-26 06:40:08 -0300
commit2571c3f415ac0c6eacedbc199e31b1c77e7bacc4 (patch)
treee1f9c5ed4433076cec7d2b9e0341f48dcca6fdab /activerecord/test
parent781370998d59bd45b951e0724ccf98871ecb68d2 (diff)
parenta89f8a922d8f37245f3ca60748adc5f4c8ee88d2 (diff)
downloadrails-2571c3f415ac0c6eacedbc199e31b1c77e7bacc4.tar.gz
rails-2571c3f415ac0c6eacedbc199e31b1c77e7bacc4.tar.bz2
rails-2571c3f415ac0c6eacedbc199e31b1c77e7bacc4.zip
Merge pull request #15868 from sgrif/sg-uninitialized-attributes
Move behavior of `read_attribute` to `AttributeSet` Conflicts: activerecord/lib/active_record/attribute_set.rb activerecord/test/cases/attribute_set_test.rb
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/attribute_set_test.rb56
1 files changed, 56 insertions, 0 deletions
diff --git a/activerecord/test/cases/attribute_set_test.rb b/activerecord/test/cases/attribute_set_test.rb
index 1c64f5f1d3..df35a7b384 100644
--- a/activerecord/test/cases/attribute_set_test.rb
+++ b/activerecord/test/cases/attribute_set_test.rb
@@ -68,5 +68,61 @@ module ActiveRecord
assert_equal({ foo: '1.1', bar: '2.2' }, attributes.values_before_type_cast)
end
+
+ test "known columns are built with uninitialized attributes" do
+ attributes = attributes_with_uninitialized_key
+ assert attributes[:foo].initialized?
+ assert_not attributes[:bar].initialized?
+ end
+
+ test "uninitialized attributes are not included in the attributes hash" do
+ attributes = attributes_with_uninitialized_key
+ assert_equal({ foo: 1 }, attributes.to_hash)
+ end
+
+ test "uninitialized attributes are not included in keys" do
+ attributes = attributes_with_uninitialized_key
+ assert_equal [:foo], attributes.keys
+ end
+
+ test "uninitialized attributes return false for include?" do
+ attributes = attributes_with_uninitialized_key
+ assert attributes.include?(:foo)
+ assert_not attributes.include?(:bar)
+ end
+
+ test "unknown attributes return false for include?" do
+ attributes = attributes_with_uninitialized_key
+ assert_not attributes.include?(:wibble)
+ end
+
+ test "fetch_value returns the value for the given initialized attribute" do
+ builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Float.new)
+ attributes = builder.build_from_database(foo: '1.1', bar: '2.2')
+
+ assert_equal 1, attributes.fetch_value(:foo)
+ assert_equal 2.2, attributes.fetch_value(:bar)
+ end
+
+ test "fetch_value returns nil for unknown attributes" do
+ attributes = attributes_with_uninitialized_key
+ assert_nil attributes.fetch_value(:wibble)
+ end
+
+ test "fetch_value uses the given block for uninitialized attributes" do
+ attributes = attributes_with_uninitialized_key
+ value = attributes.fetch_value(:bar) { |n| n.to_s + '!' }
+ assert_equal 'bar!', value
+ end
+
+ test "fetch_value returns nil for uninitialized attributes if no block is given" do
+ attributes = attributes_with_uninitialized_key
+ assert_nil attributes.fetch_value(:bar)
+ end
+
+ def attributes_with_uninitialized_key
+ builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Float.new)
+ builder.build_from_database(foo: '1.1')
+ end
end
end