2014-06-19 17:13:19 +00:00
|
|
|
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
|
2014-06-22 22:57:40 +00:00
|
|
|
assert_equal :foo, attributes[:foo].name
|
|
|
|
assert_equal :bar, attributes[:bar].name
|
2014-06-19 17:13:19 +00:00
|
|
|
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
|
|
|
|
|
2014-06-20 17:36:23 +00:00
|
|
|
test "[] returns a null object" do
|
|
|
|
builder = AttributeSet::Builder.new(foo: Type::Float.new)
|
|
|
|
attributes = builder.build_from_database(foo: '3.3')
|
|
|
|
|
|
|
|
assert_equal '3.3', attributes[:foo].value_before_type_cast
|
|
|
|
assert_equal nil, attributes[:bar].value_before_type_cast
|
2014-06-22 22:57:40 +00:00
|
|
|
assert_equal :bar, attributes[:bar].name
|
2014-06-20 17:36:23 +00:00
|
|
|
end
|
|
|
|
|
2014-06-19 17:13:19 +00:00
|
|
|
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
|
2014-06-22 22:25:40 +00:00
|
|
|
duped.write_from_database(:foo, 2)
|
2014-06-19 17:13:19 +00:00
|
|
|
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
|
2014-06-21 19:09:48 +00:00
|
|
|
|
|
|
|
test "to_hash returns a hash of the type cast values" 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({ foo: 1, bar: 2.2 }, attributes.to_hash)
|
|
|
|
assert_equal({ foo: 1, bar: 2.2 }, attributes.to_h)
|
|
|
|
end
|
2014-06-26 09:26:26 +00:00
|
|
|
|
2014-06-21 16:52:19 +00:00
|
|
|
test "values_before_type_cast" do
|
|
|
|
builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Integer.new)
|
|
|
|
attributes = builder.build_from_database(foo: '1.1', bar: '2.2')
|
|
|
|
|
|
|
|
assert_equal({ foo: '1.1', bar: '2.2' }, attributes.values_before_type_cast)
|
|
|
|
end
|
2014-06-26 09:40:08 +00:00
|
|
|
|
2014-06-22 21:38:55 +00:00
|
|
|
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
|
|
|
|
|
2014-07-11 13:50:16 +00:00
|
|
|
test "uninitialized attributes return false for key?" do
|
2014-06-22 21:38:55 +00:00
|
|
|
attributes = attributes_with_uninitialized_key
|
2014-07-11 13:50:16 +00:00
|
|
|
assert attributes.key?(:foo)
|
|
|
|
assert_not attributes.key?(:bar)
|
2014-06-22 21:38:55 +00:00
|
|
|
end
|
|
|
|
|
2014-07-11 13:50:16 +00:00
|
|
|
test "unknown attributes return false for key?" do
|
2014-06-22 21:38:55 +00:00
|
|
|
attributes = attributes_with_uninitialized_key
|
2014-07-11 13:50:16 +00:00
|
|
|
assert_not attributes.key?(:wibble)
|
2014-06-22 21:38:55 +00:00
|
|
|
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
|
2014-11-14 18:58:35 +00:00
|
|
|
assert_nil attributes.fetch_value(:wibble) { "hello" }
|
|
|
|
end
|
|
|
|
|
|
|
|
test "fetch_value returns nil for unknown attributes when types has a default" do
|
|
|
|
types = Hash.new(Type::Value.new)
|
|
|
|
builder = AttributeSet::Builder.new(types)
|
|
|
|
attributes = builder.build_from_database
|
|
|
|
|
|
|
|
assert_nil attributes.fetch_value(:wibble) { "hello" }
|
2014-06-22 21:38:55 +00:00
|
|
|
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
|
|
|
|
|
2014-11-14 18:20:28 +00:00
|
|
|
test "the primary_key is always initialized" do
|
|
|
|
builder = AttributeSet::Builder.new({ foo: Type::Integer.new }, :foo)
|
|
|
|
attributes = builder.build_from_database
|
|
|
|
|
|
|
|
assert attributes.key?(:foo)
|
|
|
|
assert_equal [:foo], attributes.keys
|
|
|
|
assert attributes[:foo].initialized?
|
|
|
|
end
|
|
|
|
|
2014-06-22 22:25:40 +00:00
|
|
|
class MyType
|
|
|
|
def type_cast_from_user(value)
|
|
|
|
return if value.nil?
|
|
|
|
value + " from user"
|
|
|
|
end
|
|
|
|
|
|
|
|
def type_cast_from_database(value)
|
|
|
|
return if value.nil?
|
|
|
|
value + " from database"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "write_from_database sets the attribute with database typecasting" do
|
|
|
|
builder = AttributeSet::Builder.new(foo: MyType.new)
|
|
|
|
attributes = builder.build_from_database
|
|
|
|
|
|
|
|
assert_nil attributes.fetch_value(:foo)
|
|
|
|
|
|
|
|
attributes.write_from_database(:foo, "value")
|
|
|
|
|
|
|
|
assert_equal "value from database", attributes.fetch_value(:foo)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "write_from_user sets the attribute with user typecasting" do
|
|
|
|
builder = AttributeSet::Builder.new(foo: MyType.new)
|
|
|
|
attributes = builder.build_from_database
|
|
|
|
|
|
|
|
assert_nil attributes.fetch_value(:foo)
|
|
|
|
|
|
|
|
attributes.write_from_user(:foo, "value")
|
|
|
|
|
|
|
|
assert_equal "value from user", attributes.fetch_value(:foo)
|
|
|
|
end
|
|
|
|
|
2014-06-22 21:38:55 +00:00
|
|
|
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
|
2014-12-08 19:45:21 +00:00
|
|
|
|
|
|
|
test "freezing doesn't prevent the set from materializing" do
|
|
|
|
builder = AttributeSet::Builder.new(foo: Type::String.new)
|
|
|
|
attributes = builder.build_from_database(foo: "1")
|
|
|
|
|
|
|
|
attributes.freeze
|
|
|
|
assert_equal({ foo: "1" }, attributes.to_hash)
|
|
|
|
end
|
2015-01-20 21:09:53 +00:00
|
|
|
|
|
|
|
test "#accessed_attributes returns only attributes which have been read" do
|
|
|
|
builder = AttributeSet::Builder.new(foo: Type::Value.new, bar: Type::Value.new)
|
|
|
|
attributes = builder.build_from_database(foo: "1", bar: "2")
|
|
|
|
|
|
|
|
assert_equal [], attributes.accessed
|
|
|
|
|
|
|
|
attributes.fetch_value(:foo)
|
|
|
|
|
|
|
|
assert_equal [:foo], attributes.accessed
|
|
|
|
end
|
2014-06-19 17:13:19 +00:00
|
|
|
end
|
|
|
|
end
|