diff options
Diffstat (limited to 'activemodel/test/cases')
-rw-r--r-- | activemodel/test/cases/attribute_test.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/type/date_test.rb | 16 | ||||
-rw-r--r-- | activemodel/test/cases/type/integer_test.rb | 15 | ||||
-rw-r--r-- | activemodel/test/cases/type/string_test.rb | 8 | ||||
-rw-r--r-- | activemodel/test/cases/type/time_test.rb | 1 | ||||
-rw-r--r-- | activemodel/test/cases/validations/conditional_validation_test.rb | 6 | ||||
-rw-r--r-- | activemodel/test/cases/validations/numericality_validation_test.rb | 13 |
7 files changed, 56 insertions, 5 deletions
diff --git a/activemodel/test/cases/attribute_test.rb b/activemodel/test/cases/attribute_test.rb index 20c02e689c..097db2e923 100644 --- a/activemodel/test/cases/attribute_test.rb +++ b/activemodel/test/cases/attribute_test.rb @@ -204,7 +204,7 @@ module ActiveModel assert_not_predicate unchanged, :changed? end - test "an attribute can not be mutated if it has not been read, + test "an attribute cannot be mutated if it has not been read, and skips expensive calculations" do type_which_raises_from_all_methods = Object.new attribute = Attribute.from_database(:foo, "bar", type_which_raises_from_all_methods) diff --git a/activemodel/test/cases/type/date_test.rb b/activemodel/test/cases/type/date_test.rb index e8cf178612..2dd1a55616 100644 --- a/activemodel/test/cases/type/date_test.rb +++ b/activemodel/test/cases/type/date_test.rb @@ -12,8 +12,22 @@ module ActiveModel assert_nil type.cast(" ") assert_nil type.cast("ABC") - date_string = ::Time.now.utc.strftime("%F") + now = ::Time.now.utc + values_hash = { 1 => now.year, 2 => now.mon, 3 => now.mday } + date_string = now.strftime("%F") assert_equal date_string, type.cast(date_string).strftime("%F") + assert_equal date_string, type.cast(values_hash).strftime("%F") + end + + def test_returns_correct_year + type = Type::Date.new + + time = ::Time.utc(1, 1, 1) + date = ::Date.new(time.year, time.mon, time.mday) + + values_hash_for_multiparameter_assignment = { 1 => 1, 2 => 1, 3 => 1 } + + assert_equal date, type.cast(values_hash_for_multiparameter_assignment) end end end diff --git a/activemodel/test/cases/type/integer_test.rb b/activemodel/test/cases/type/integer_test.rb index df12098974..6c02c01237 100644 --- a/activemodel/test/cases/type/integer_test.rb +++ b/activemodel/test/cases/type/integer_test.rb @@ -50,6 +50,21 @@ module ActiveModel assert_equal 7200, type.cast(2.hours) end + test "casting string for database" do + type = Type::Integer.new + assert_nil type.serialize("wibble") + assert_equal 5, type.serialize("5wibble") + assert_equal 5, type.serialize(" +5") + assert_equal(-5, type.serialize(" -5")) + end + + test "casting empty string" do + type = Type::Integer.new + assert_nil type.cast("") + assert_nil type.serialize("") + assert_nil type.deserialize("") + end + test "changed?" do type = Type::Integer.new diff --git a/activemodel/test/cases/type/string_test.rb b/activemodel/test/cases/type/string_test.rb index 2d85556d20..9cc530e8db 100644 --- a/activemodel/test/cases/type/string_test.rb +++ b/activemodel/test/cases/type/string_test.rb @@ -12,6 +12,14 @@ module ActiveModel assert_equal "123", type.cast(123) end + test "type casting for database" do + type = Type::String.new + object, array, hash = Object.new, [true], { a: :b } + assert_equal object, type.serialize(object) + assert_equal array, type.serialize(array) + assert_equal hash, type.serialize(hash) + end + test "cast strings are mutable" do type = Type::String.new diff --git a/activemodel/test/cases/type/time_test.rb b/activemodel/test/cases/type/time_test.rb index 3fbae1a169..5c6271241d 100644 --- a/activemodel/test/cases/type/time_test.rb +++ b/activemodel/test/cases/type/time_test.rb @@ -16,6 +16,7 @@ module ActiveModel assert_equal ::Time.utc(2000, 1, 1, 16, 45, 54), type.cast("2015-06-13T19:45:54+03:00") assert_equal ::Time.utc(1999, 12, 31, 21, 7, 8), type.cast("06:07:08+09:00") + assert_equal ::Time.utc(2000, 1, 1, 16, 45, 54), type.cast(4 => 16, 5 => 45, 6 => 54) end def test_user_input_in_time_zone diff --git a/activemodel/test/cases/validations/conditional_validation_test.rb b/activemodel/test/cases/validations/conditional_validation_test.rb index 1704db9a48..9674068aff 100644 --- a/activemodel/test/cases/validations/conditional_validation_test.rb +++ b/activemodel/test/cases/validations/conditional_validation_test.rb @@ -49,7 +49,7 @@ class ConditionalValidationTest < ActiveModel::TestCase assert_empty t.errors[:title] end - def test_unless_validation_using_array_of_true_and_felse_methods + def test_unless_validation_using_array_of_true_and_false_methods Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: [:condition_is_true, :condition_is_false]) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert_predicate t, :valid? @@ -111,14 +111,14 @@ class ConditionalValidationTest < ActiveModel::TestCase assert_equal ["hoo 5"], t.errors["title"] end - def test_validation_using_conbining_if_true_and_unless_true_conditions + def test_validation_using_combining_if_true_and_unless_true_conditions Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: :condition_is_true, unless: :condition_is_true) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert_predicate t, :valid? assert_empty t.errors[:title] end - def test_validation_using_conbining_if_true_and_unless_false_conditions + def test_validation_using_combining_if_true_and_unless_false_conditions Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: :condition_is_true, unless: :condition_is_false) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert_predicate t, :invalid? diff --git a/activemodel/test/cases/validations/numericality_validation_test.rb b/activemodel/test/cases/validations/numericality_validation_test.rb index eb4b02df93..16c44762cb 100644 --- a/activemodel/test/cases/validations/numericality_validation_test.rb +++ b/activemodel/test/cases/validations/numericality_validation_test.rb @@ -281,6 +281,19 @@ class NumericalityValidationTest < ActiveModel::TestCase assert_predicate topic, :invalid? end + def test_validates_numericality_with_object_acting_as_numeric + klass = Class.new do + def to_f + 123.54 + end + end + + Topic.validates_numericality_of :price + topic = Topic.new(price: klass.new) + + assert_predicate topic, :valid? + end + def test_validates_numericality_with_invalid_args assert_raise(ArgumentError) { Topic.validates_numericality_of :approved, greater_than_or_equal_to: "foo" } assert_raise(ArgumentError) { Topic.validates_numericality_of :approved, less_than_or_equal_to: "foo" } |