mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
be9b68038e
This method can be used to see all of the fields on a model which have been read. This can be useful during development mode to quickly find out which fields need to be selected. For performance critical pages, if you are not using all of the fields of a database, an easy performance win is only selecting the fields which you need. By calling this method at the end of a controller action, it's easy to determine which fields need to be selected. While writing this, I also noticed a place for an easy performance win internally which I had been wanting to introduce. You cannot mutate a field which you have not read. Therefore, we can skip the calculation of in place changes if we have never read from the field. This can significantly speed up methods like `#changed?` if any of the fields have an expensive mutable type (like `serialize`) ``` Calculating ------------------------------------- #changed? with serialized column (before) 391.000 i/100ms #changed? with serialized column (after) 1.514k i/100ms ------------------------------------------------- #changed? with serialized column (before) 4.243k (± 3.7%) i/s - 21.505k #changed? with serialized column (after) 16.789k (± 3.2%) i/s - 84.784k ```
201 lines
6.8 KiB
Ruby
201 lines
6.8 KiB
Ruby
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
|
|
assert_equal :foo, attributes[:foo].name
|
|
assert_equal :bar, attributes[:bar].name
|
|
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 "[] 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
|
|
assert_equal :bar, attributes[:bar].name
|
|
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.write_from_database(:foo, 2)
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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 key?" do
|
|
attributes = attributes_with_uninitialized_key
|
|
assert attributes.key?(:foo)
|
|
assert_not attributes.key?(:bar)
|
|
end
|
|
|
|
test "unknown attributes return false for key?" do
|
|
attributes = attributes_with_uninitialized_key
|
|
assert_not attributes.key?(: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) { "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" }
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
end
|
|
end
|