diff options
Diffstat (limited to 'activerecord/test/cases/attributes_test.rb')
-rw-r--r-- | activerecord/test/cases/attributes_test.rb | 98 |
1 files changed, 84 insertions, 14 deletions
diff --git a/activerecord/test/cases/attributes_test.rb b/activerecord/test/cases/attributes_test.rb index 2991ca8b76..f4620ae2da 100644 --- a/activerecord/test/cases/attributes_test.rb +++ b/activerecord/test/cases/attributes_test.rb @@ -1,10 +1,10 @@ -require 'cases/helper' +require "cases/helper" class OverloadedType < ActiveRecord::Base attribute :overloaded_float, :integer attribute :overloaded_string_with_limit, :string, limit: 50 attribute :non_existent_decimal, :decimal - attribute :string_with_default, :string, default: 'the overloaded default' + attribute :string_with_default, :string, default: "the overloaded default" end class ChildOfOverloadedType < OverloadedType @@ -15,7 +15,7 @@ class GrandchildOfOverloadedType < ChildOfOverloadedType end class UnoverloadedType < ActiveRecord::Base - self.table_name = 'overloaded_types' + self.table_name = "overloaded_types" end module ActiveRecord @@ -38,20 +38,20 @@ module ActiveRecord data.reload assert_equal 2, data.overloaded_float - assert_kind_of Fixnum, OverloadedType.last.overloaded_float + assert_kind_of Integer, OverloadedType.last.overloaded_float assert_equal 2.0, UnoverloadedType.last.overloaded_float assert_kind_of Float, UnoverloadedType.last.overloaded_float end test "properties assigned in constructor" do - data = OverloadedType.new(overloaded_float: '3.3') + data = OverloadedType.new(overloaded_float: "3.3") assert_equal 3, data.overloaded_float end test "overloaded properties with limit" do - assert_equal 50, OverloadedType.type_for_attribute('overloaded_string_with_limit').limit - assert_equal 255, UnoverloadedType.type_for_attribute('overloaded_string_with_limit').limit + assert_equal 50, OverloadedType.type_for_attribute("overloaded_string_with_limit").limit + assert_equal 255, UnoverloadedType.type_for_attribute("overloaded_string_with_limit").limit end test "nonexistent attribute" do @@ -63,26 +63,35 @@ module ActiveRecord end end + test "model with nonexistent attribute with default value can be saved" do + klass = Class.new(OverloadedType) do + attribute :non_existent_string_with_default, :string, default: "nonexistent" + end + + model = klass.new + assert model.save + end + test "changing defaults" do data = OverloadedType.new unoverloaded_data = UnoverloadedType.new - assert_equal 'the overloaded default', data.string_with_default - assert_equal 'the original default', unoverloaded_data.string_with_default + assert_equal "the overloaded default", data.string_with_default + assert_equal "the original default", unoverloaded_data.string_with_default end test "defaults are not touched on the columns" do - assert_equal 'the original default', OverloadedType.columns_hash['string_with_default'].default + assert_equal "the original default", OverloadedType.columns_hash["string_with_default"].default end test "children inherit custom properties" do - data = ChildOfOverloadedType.new(overloaded_float: '4.4') + data = ChildOfOverloadedType.new(overloaded_float: "4.4") assert_equal 4, data.overloaded_float end test "children can override parents" do - data = GrandchildOfOverloadedType.new(overloaded_float: '4.4') + data = GrandchildOfOverloadedType.new(overloaded_float: "4.4") assert_equal 4.4, data.overloaded_float end @@ -97,13 +106,13 @@ module ActiveRecord assert_equal 6, klass.attribute_types.length assert_equal 6, klass.column_defaults.length - assert_not klass.attribute_types.include?('wibble') + assert_not klass.attribute_types.include?("wibble") klass.attribute :wibble, Type::Value.new assert_equal 7, klass.attribute_types.length assert_equal 7, klass.column_defaults.length - assert klass.attribute_types.include?('wibble') + assert_includes klass.attribute_types, "wibble" end test "the given default value is cast from user" do @@ -135,6 +144,17 @@ module ActiveRecord assert_equal 2, klass.new.counter end + test "procs are memoized before type casting" do + klass = Class.new(OverloadedType) do + @@counter = 0 + attribute :counter, :integer, default: -> { @@counter += 1 } + end + + model = klass.new + assert_equal 1, model.counter_before_type_cast + assert_equal 1, model.counter_before_type_cast + end + test "user provided defaults are persisted even if unchanged" do model = OverloadedType.create! @@ -185,5 +205,55 @@ module ActiveRecord assert_equal(:bar, child.new(foo: :bar).foo) end + + test "attributes not backed by database columns are not dirty when unchanged" do + refute OverloadedType.new.non_existent_decimal_changed? + end + + test "attributes not backed by database columns are always initialized" do + OverloadedType.create! + model = OverloadedType.first + + assert_nil model.non_existent_decimal + model.non_existent_decimal = "123" + assert_equal 123, model.non_existent_decimal + end + + test "attributes not backed by database columns return the default on models loaded from database" do + child = Class.new(OverloadedType) do + attribute :non_existent_decimal, :decimal, default: 123 + end + child.create! + model = child.first + + assert_equal 123, model.non_existent_decimal + end + + test "attributes not backed by database columns properly interact with mutation and dirty" do + child = Class.new(ActiveRecord::Base) do + self.table_name = "topics" + attribute :foo, :string, default: "lol" + end + child.create! + model = child.first + + assert_equal "lol", model.foo + + model.foo << "asdf" + assert_equal "lolasdf", model.foo + assert model.foo_changed? + + model.reload + assert_equal "lol", model.foo + + model.foo = "lol" + refute model.changed? + end + + test "attributes not backed by database columns appear in inspect" do + inspection = OverloadedType.new.inspect + + assert_includes inspection, "non_existent_decimal" + end end end |