aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-19 16:14:55 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-19 16:14:55 -0300
commit70de7dda8c09c1e02e0281879ef6fb8f372ada5e (patch)
tree999d08ae19eb59d738909c94287833b7e86824ad /activerecord/test
parentc36e77a93e286f4546a36e79d43f543e9f00d6be (diff)
parent099af48d31bb2b5fc8e3fff39b825efff7964d55 (diff)
downloadrails-70de7dda8c09c1e02e0281879ef6fb8f372ada5e.tar.gz
rails-70de7dda8c09c1e02e0281879ef6fb8f372ada5e.tar.bz2
rails-70de7dda8c09c1e02e0281879ef6fb8f372ada5e.zip
Merge pull request #15818 from sgrif/sg-attribute-set
Introduce an object to aid in creation and management of `@attributes`
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/attribute_set_test.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/activerecord/test/cases/attribute_set_test.rb b/activerecord/test/cases/attribute_set_test.rb
new file mode 100644
index 0000000000..091f7e396a
--- /dev/null
+++ b/activerecord/test/cases/attribute_set_test.rb
@@ -0,0 +1,49 @@
+require 'cases/helper'
+
+module ActiveRecord
+ class AttributeSetTest < ActiveRecord::TestCase
+ test "building a new set from raw attributes" 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[:foo].value
+ assert_equal 2.2, attributes[:bar].value
+ end
+
+ test "building with custom types" do
+ builder = AttributeSet::Builder.new(foo: Type::Float.new)
+ attributes = builder.build_from_database({ foo: '3.3', bar: '4.4' }, { bar: Type::Integer.new })
+
+ assert_equal 3.3, attributes[:foo].value
+ assert_equal 4, attributes[:bar].value
+ end
+
+ test "duping creates a new hash and dups each attribute" do
+ builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::String.new)
+ attributes = builder.build_from_database(foo: 1, bar: 'foo')
+
+ # Ensure the type cast value is cached
+ attributes[:foo].value
+ attributes[:bar].value
+
+ duped = attributes.dup
+ duped[:foo] = Attribute.from_database(2, Type::Integer.new)
+ duped[:bar].value << 'bar'
+
+ assert_equal 1, attributes[:foo].value
+ assert_equal 2, duped[:foo].value
+ assert_equal 'foo', attributes[:bar].value
+ assert_equal 'foobar', duped[:bar].value
+ end
+
+ test "freezing cloned set does not freeze original" do
+ attributes = AttributeSet.new({})
+ clone = attributes.clone
+
+ clone.freeze
+
+ assert clone.frozen?
+ assert_not attributes.frozen?
+ end
+ end
+end