diff options
Diffstat (limited to 'activemodel/test')
14 files changed, 320 insertions, 100 deletions
diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb index 67471ed497..0c6e49bee2 100644 --- a/activemodel/test/cases/attribute_methods_test.rb +++ b/activemodel/test/cases/attribute_methods_test.rb @@ -133,24 +133,6 @@ class AttributeMethodsTest < ActiveModel::TestCase      assert_equal "value of foo bar", ModelWithAttributesWithSpaces.new.send(:'foo bar')    end -  test '#define_attr_method generates attribute method' do -    ModelWithAttributes.define_attr_method(:bar, 'bar') - -    assert_respond_to ModelWithAttributes, :bar -    assert_equal "original bar", ModelWithAttributes.original_bar -    assert_equal "bar", ModelWithAttributes.bar -    ModelWithAttributes.define_attr_method(:bar) -    assert !ModelWithAttributes.bar -  end - -  test '#define_attr_method generates attribute method with invalid identifier characters' do -    ModelWithWeirdNamesAttributes.define_attr_method(:'c?d', 'c?d') - -    assert_respond_to ModelWithWeirdNamesAttributes, :'c?d' -    assert_equal "original c?d", ModelWithWeirdNamesAttributes.send('original_c?d') -    assert_equal "c?d", ModelWithWeirdNamesAttributes.send('c?d') -  end -    test '#alias_attribute works with attributes with spaces in their names' do      ModelWithAttributesWithSpaces.define_attribute_methods([:'foo bar'])      ModelWithAttributesWithSpaces.alias_attribute(:'foo_bar', :'foo bar') diff --git a/activemodel/test/cases/configuration_test.rb b/activemodel/test/cases/configuration_test.rb new file mode 100644 index 0000000000..a172fa26a3 --- /dev/null +++ b/activemodel/test/cases/configuration_test.rb @@ -0,0 +1,154 @@ +require 'cases/helper' + +class ConfigurationOnModuleTest < ActiveModel::TestCase +  def setup +    @mod = mod = Module.new do +      extend ActiveSupport::Concern +      extend ActiveModel::Configuration + +      config_attribute :omg +      self.omg = "default" + +      config_attribute :wtf, global: true +      self.wtf = "default" + +      config_attribute :boolean + +      config_attribute :lol, instance_writer: true +    end + +    @klass = Class.new do +      include mod +    end + +    @subklass = Class.new(@klass) +  end + +  test "default" do +    assert_equal "default", @mod.omg +    assert_equal "default", @klass.omg +    assert_equal "default", @klass.new.omg +  end + +  test "setting" do +    @mod.omg = "lol" +    assert_equal "lol", @mod.omg +  end + +  test "setting on class including the module" do +    @klass.omg = "lol" +    assert_equal "lol", @klass.omg +    assert_equal "lol", @klass.new.omg +    assert_equal "default", @mod.omg +  end + +  test "setting on subclass of class including the module" do +    @subklass.omg = "lol" +    assert_equal "lol", @subklass.omg +    assert_equal "default", @klass.omg +    assert_equal "default", @mod.omg +  end + +  test "setting on instance" do +    assert !@klass.new.respond_to?(:omg=) + +    @klass.lol = "lol" +    obj = @klass.new +    assert_equal "lol", obj.lol +    obj.lol = "omg" +    assert_equal "omg", obj.lol +    assert_equal "lol", @klass.lol +    assert_equal "lol", @klass.new.lol +    obj.lol = false +    assert !obj.lol? +  end + +  test "global attribute" do +    assert_equal "default", @mod.wtf +    assert_equal "default", @klass.wtf + +    @mod.wtf = "wtf" + +    assert_equal "wtf", @mod.wtf +    assert_equal "wtf", @klass.wtf + +    @klass.wtf = "lol" + +    assert_equal "lol", @mod.wtf +    assert_equal "lol", @klass.wtf +  end + +  test "boolean" do +    assert_equal false, @mod.boolean? +    assert_equal false, @klass.new.boolean? +    @mod.boolean = true +    assert_equal true, @mod.boolean? +    assert_equal true, @klass.new.boolean? +  end +end + +class ConfigurationOnClassTest < ActiveModel::TestCase +  def setup +    @klass = Class.new do +      extend ActiveModel::Configuration + +      config_attribute :omg +      self.omg = "default" + +      config_attribute :wtf, global: true +      self.wtf = "default" + +      config_attribute :omg2, instance_writer: true +      config_attribute :wtf2, instance_writer: true, global: true +    end + +    @subklass = Class.new(@klass) +  end + +  test "defaults" do +    assert_equal "default", @klass.omg +    assert_equal "default", @klass.wtf +    assert_equal "default", @subklass.omg +    assert_equal "default", @subklass.wtf +  end + +  test "changing" do +    @klass.omg = "lol" +    assert_equal "lol", @klass.omg +    assert_equal "lol", @subklass.omg +  end + +  test "changing in subclass" do +    @subklass.omg = "lol" +    assert_equal "lol", @subklass.omg +    assert_equal "default", @klass.omg +  end + +  test "changing global" do +    @klass.wtf = "wtf" +    assert_equal "wtf", @klass.wtf +    assert_equal "wtf", @subklass.wtf + +    @subklass.wtf = "lol" +    assert_equal "lol", @klass.wtf +    assert_equal "lol", @subklass.wtf +  end + +  test "instance_writer" do +    obj = @klass.new + +    @klass.omg2 = "omg" +    @klass.wtf2 = "wtf" + +    assert_equal "omg", obj.omg2 +    assert_equal "wtf", obj.wtf2 + +    obj.omg2 = "lol" +    obj.wtf2 = "lol" + +    assert_equal "lol", obj.omg2 +    assert_equal "lol", obj.wtf2 +    assert_equal "omg", @klass.omg2 +    assert_equal "lol", @klass.wtf2 +  end +end diff --git a/activemodel/test/cases/mass_assignment_security/sanitizer_test.rb b/activemodel/test/cases/mass_assignment_security/sanitizer_test.rb index 676937b5e1..3660b9b1e5 100644 --- a/activemodel/test/cases/mass_assignment_security/sanitizer_test.rb +++ b/activemodel/test/cases/mass_assignment_security/sanitizer_test.rb @@ -1,5 +1,5 @@  require "cases/helper" -require 'logger' +require 'active_support/logger'  require 'active_support/core_ext/object/inclusion'  class SanitizerTest < ActiveModel::TestCase @@ -28,7 +28,7 @@ class SanitizerTest < ActiveModel::TestCase    test "debug mass assignment removal with LoggerSanitizer" do      original_attributes = { 'first_name' => 'allowed', 'admin' => 'denied' }      log = StringIO.new -    self.logger = Logger.new(log) +    self.logger = ActiveSupport::Logger.new(log)      @logger_sanitizer.sanitize(original_attributes, @authorizer)      assert_match(/admin/, log.string, "Should log removed attributes: #{log.string}")    end diff --git a/activemodel/test/cases/naming_test.rb b/activemodel/test/cases/naming_test.rb index 5f943729dd..acda989eec 100644 --- a/activemodel/test/cases/naming_test.rb +++ b/activemodel/test/cases/naming_test.rb @@ -34,6 +34,10 @@ class NamingTest < ActiveModel::TestCase    def test_human      assert_equal 'Track back', @model_name.human    end + +  def test_i18n_key +    assert_equal :"post/track_back", @model_name.i18n_key +  end  end  class NamingWithNamespacedModelInIsolatedNamespaceTest < ActiveModel::TestCase @@ -75,8 +79,8 @@ class NamingWithNamespacedModelInIsolatedNamespaceTest < ActiveModel::TestCase      assert_equal 'post', @model_name.param_key    end -  def test_recognizing_namespace -    assert_equal 'Post', Blog::Post.model_name.instance_variable_get("@unnamespaced") +  def test_i18n_key +    assert_equal :"blog/post", @model_name.i18n_key    end  end @@ -118,6 +122,10 @@ class NamingWithNamespacedModelInSharedNamespaceTest < ActiveModel::TestCase    def test_param_key      assert_equal 'blog_post', @model_name.param_key    end + +  def test_i18n_key +    assert_equal :"blog/post", @model_name.i18n_key +  end  end  class NamingWithSuppliedModelNameTest < ActiveModel::TestCase @@ -158,6 +166,48 @@ class NamingWithSuppliedModelNameTest < ActiveModel::TestCase    def test_param_key      assert_equal 'article', @model_name.param_key    end + +  def test_i18n_key +    assert_equal :"article", @model_name.i18n_key +  end +end + +class NamingUsingRelativeModelNameTest < ActiveModel::TestCase +  def setup +    @model_name = Blog::Post.model_name +  end + +  def test_singular +    assert_equal 'blog_post', @model_name.singular +  end + +  def test_plural +    assert_equal 'blog_posts', @model_name.plural +  end + +  def test_element +    assert_equal 'post', @model_name.element +  end + +  def test_collection +    assert_equal 'blog/posts', @model_name.collection +  end + +  def test_human +    assert_equal 'Post', @model_name.human +  end + +  def test_route_key +    assert_equal 'posts', @model_name.route_key +  end + +  def test_param_key +    assert_equal 'post', @model_name.param_key +  end + +  def test_i18n_key +    assert_equal :"blog/post", @model_name.i18n_key +  end  end  class NamingHelpersTest < Test::Unit::TestCase @@ -167,6 +217,7 @@ class NamingHelpersTest < Test::Unit::TestCase      @singular = 'contact'      @plural = 'contacts'      @uncountable = Sheep +    @singular_route_key = 'contact'      @route_key = 'contacts'      @param_key = 'contact'    end @@ -193,10 +244,12 @@ class NamingHelpersTest < Test::Unit::TestCase    def test_route_key      assert_equal @route_key, route_key(@record) +    assert_equal @singular_route_key, singular_route_key(@record)    end    def test_route_key_for_class      assert_equal @route_key, route_key(@klass) +    assert_equal @singular_route_key, singular_route_key(@klass)    end    def test_param_key @@ -212,8 +265,26 @@ class NamingHelpersTest < Test::Unit::TestCase      assert !uncountable?(@klass), "Expected 'contact' to be countable"    end +  def test_uncountable_route_key +    assert_equal "sheep", singular_route_key(@uncountable) +    assert_equal "sheep_index", route_key(@uncountable) +  end +    private      def method_missing(method, *args)        ActiveModel::Naming.send(method, *args)      end  end + +class NameWithAnonymousClassTest < Test::Unit::TestCase +  def test_anonymous_class_without_name_argument +    assert_raises(ArgumentError) do +      ActiveModel::Name.new(Class.new) +    end +  end + +  def test_anonymous_class_with_name_argument +    model_name = ActiveModel::Name.new(Class.new, nil, "Anonymous") +    assert_equal "Anonymous", model_name +  end +end diff --git a/activemodel/test/cases/serializers/json_serialization_test.rb b/activemodel/test/cases/serializers/json_serialization_test.rb index a754d610b9..4ac5fb1779 100644 --- a/activemodel/test/cases/serializers/json_serialization_test.rb +++ b/activemodel/test/cases/serializers/json_serialization_test.rb @@ -14,9 +14,11 @@ class Contact      end    end +  remove_method :attributes if method_defined?(:attributes) +    def attributes      instance_values -  end unless method_defined?(:attributes) +  end  end  class JsonSerializationTest < ActiveModel::TestCase diff --git a/activemodel/test/cases/serializers/xml_serialization_test.rb b/activemodel/test/cases/serializers/xml_serialization_test.rb index fc73d9dcd8..38aecf51ff 100644 --- a/activemodel/test/cases/serializers/xml_serialization_test.rb +++ b/activemodel/test/cases/serializers/xml_serialization_test.rb @@ -9,6 +9,8 @@ class Contact    attr_accessor :address, :friends +  remove_method :attributes if method_defined?(:attributes) +    def attributes      instance_values.except("address", "friends")    end diff --git a/activemodel/test/cases/translation_test.rb b/activemodel/test/cases/translation_test.rb index 1b1d972d5c..54e86d48db 100644 --- a/activemodel/test/cases/translation_test.rb +++ b/activemodel/test/cases/translation_test.rb @@ -56,6 +56,16 @@ class ActiveModelI18nTests < ActiveModel::TestCase      assert_equal 'person gender attribute', Person::Gender.human_attribute_name('attribute')    end +  def test_translated_nested_model_attributes +    I18n.backend.store_translations 'en', :activemodel => {:attributes => {:"person/addresses" => {:street => 'Person Address Street'}}} +    assert_equal 'Person Address Street', Person.human_attribute_name('addresses.street') +  end + +  def test_translated_nested_model_attributes_with_namespace_fallback +    I18n.backend.store_translations 'en', :activemodel => {:attributes => {:addresses => {:street => 'Cool Address Street'}}} +    assert_equal 'Cool Address Street', Person.human_attribute_name('addresses.street') +  end +    def test_translated_model_names      I18n.backend.store_translations 'en', :activemodel => {:models => {:person => 'person model'} }      assert_equal 'person model', Person.model_name.human diff --git a/activemodel/test/cases/validations/exclusion_validation_test.rb b/activemodel/test/cases/validations/exclusion_validation_test.rb index 72a383f128..adab8ccb2b 100644 --- a/activemodel/test/cases/validations/exclusion_validation_test.rb +++ b/activemodel/test/cases/validations/exclusion_validation_test.rb @@ -46,12 +46,12 @@ class ExclusionValidationTest < ActiveModel::TestCase    def test_validates_exclusion_of_with_lambda      Topic.validates_exclusion_of :title, :in => lambda{ |topic| topic.author_name == "sikachu" ? %w( monkey elephant ) : %w( abe wasabi ) } -    p = Topic.new -    p.title = "elephant" -    p.author_name = "sikachu" -    assert p.invalid? +    t = Topic.new +    t.title = "elephant" +    t.author_name = "sikachu" +    assert t.invalid? -    p.title = "wasabi" -    assert p.valid? +    t.title = "wasabi" +    assert t.valid?    end  end diff --git a/activemodel/test/cases/validations/format_validation_test.rb b/activemodel/test/cases/validations/format_validation_test.rb index 2ce714fef0..41a1131bcb 100644 --- a/activemodel/test/cases/validations/format_validation_test.rb +++ b/activemodel/test/cases/validations/format_validation_test.rb @@ -101,25 +101,25 @@ class PresenceValidationTest < ActiveModel::TestCase    def test_validates_format_of_with_lambda      Topic.validates_format_of :content, :with => lambda{ |topic| topic.title == "digit" ? /\A\d+\Z/ : /\A\S+\Z/ } -    p = Topic.new -    p.title = "digit" -    p.content = "Pixies" -    assert p.invalid? +    t = Topic.new +    t.title = "digit" +    t.content = "Pixies" +    assert t.invalid? -    p.content = "1234" -    assert p.valid? +    t.content = "1234" +    assert t.valid?    end    def test_validates_format_of_without_lambda      Topic.validates_format_of :content, :without => lambda{ |topic| topic.title == "characters" ? /\A\d+\Z/ : /\A\S+\Z/ } -    p = Topic.new -    p.title = "characters" -    p.content = "1234" -    assert p.invalid? +    t = Topic.new +    t.title = "characters" +    t.content = "1234" +    assert t.invalid? -    p.content = "Pixies" -    assert p.valid? +    t.content = "Pixies" +    assert t.valid?    end    def test_validates_format_of_for_ruby_class diff --git a/activemodel/test/cases/validations/inclusion_validation_test.rb b/activemodel/test/cases/validations/inclusion_validation_test.rb index 413da92de4..851d345eab 100644 --- a/activemodel/test/cases/validations/inclusion_validation_test.rb +++ b/activemodel/test/cases/validations/inclusion_validation_test.rb @@ -78,12 +78,12 @@ class InclusionValidationTest < ActiveModel::TestCase    def test_validates_inclusion_of_with_lambda      Topic.validates_inclusion_of :title, :in => lambda{ |topic| topic.author_name == "sikachu" ? %w( monkey elephant ) : %w( abe wasabi ) } -    p = Topic.new -    p.title = "wasabi" -    p.author_name = "sikachu" -    assert p.invalid? +    t = Topic.new +    t.title = "wasabi" +    t.author_name = "sikachu" +    assert t.invalid? -    p.title = "elephant" -    assert p.valid? +    t.title = "elephant" +    assert t.valid?    end  end diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb index 44048a9c1d..aa86d9d959 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -260,74 +260,64 @@ class LengthValidationTest < ActiveModel::TestCase    end    def test_validates_length_of_using_minimum_utf8 -    with_kcode('UTF8') do -      Topic.validates_length_of :title, :minimum => 5 +    Topic.validates_length_of :title, :minimum => 5 -      t = Topic.new("title" => "一二三四五", "content" => "whatever") -      assert t.valid? +    t = Topic.new("title" => "一二三四五", "content" => "whatever") +    assert t.valid? -      t.title = "一二三四" -      assert t.invalid? -      assert t.errors[:title].any? -      assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"] -    end +    t.title = "一二三四" +    assert t.invalid? +    assert t.errors[:title].any? +    assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"]    end    def test_validates_length_of_using_maximum_utf8 -    with_kcode('UTF8') do -      Topic.validates_length_of :title, :maximum => 5 +    Topic.validates_length_of :title, :maximum => 5 -      t = Topic.new("title" => "一二三四五", "content" => "whatever") -      assert t.valid? +    t = Topic.new("title" => "一二三四五", "content" => "whatever") +    assert t.valid? -      t.title = "一二34五六" -      assert t.invalid? -      assert t.errors[:title].any? -      assert_equal ["is too long (maximum is 5 characters)"], t.errors["title"] -    end +    t.title = "一二34五六" +    assert t.invalid? +    assert t.errors[:title].any? +    assert_equal ["is too long (maximum is 5 characters)"], t.errors["title"]    end    def test_validates_length_of_using_within_utf8 -    with_kcode('UTF8') do -      Topic.validates_length_of(:title, :content, :within => 3..5) - -      t = Topic.new("title" => "一二", "content" => "12三四五六七") -      assert t.invalid? -      assert_equal ["is too short (minimum is 3 characters)"], t.errors[:title] -      assert_equal ["is too long (maximum is 5 characters)"], t.errors[:content] -      t.title = "一二三" -      t.content  = "12三" -      assert t.valid? -    end +    Topic.validates_length_of(:title, :content, :within => 3..5) + +    t = Topic.new("title" => "一二", "content" => "12三四五六七") +    assert t.invalid? +    assert_equal ["is too short (minimum is 3 characters)"], t.errors[:title] +    assert_equal ["is too long (maximum is 5 characters)"], t.errors[:content] +    t.title = "一二三" +    t.content  = "12三" +    assert t.valid?    end    def test_optionally_validates_length_of_using_within_utf8 -    with_kcode('UTF8') do -      Topic.validates_length_of :title, :within => 3..5, :allow_nil => true +    Topic.validates_length_of :title, :within => 3..5, :allow_nil => true -      t = Topic.new(:title => "一二三四五") -      assert t.valid?, t.errors.inspect +    t = Topic.new(:title => "一二三四五") +    assert t.valid?, t.errors.inspect -      t = Topic.new(:title => "一二三") -      assert t.valid?, t.errors.inspect +    t = Topic.new(:title => "一二三") +    assert t.valid?, t.errors.inspect -      t.title = nil -      assert t.valid?, t.errors.inspect -    end +    t.title = nil +    assert t.valid?, t.errors.inspect    end    def test_validates_length_of_using_is_utf8 -    with_kcode('UTF8') do -      Topic.validates_length_of :title, :is => 5 +    Topic.validates_length_of :title, :is => 5 -      t = Topic.new("title" => "一二345", "content" => "whatever") -      assert t.valid? +    t = Topic.new("title" => "一二345", "content" => "whatever") +    assert t.valid? -      t.title = "一二345六" -      assert t.invalid? -      assert t.errors[:title].any? -      assert_equal ["is the wrong length (should be 5 characters)"], t.errors["title"] -    end +    t.title = "一二345六" +    assert t.invalid? +    assert t.errors[:title].any? +    assert_equal ["is the wrong length (should be 5 characters)"], t.errors["title"]    end    def test_validates_length_of_with_block diff --git a/activemodel/test/cases/validations/numericality_validation_test.rb b/activemodel/test/cases/validations/numericality_validation_test.rb index 08f6169ca5..6742a4bab0 100644 --- a/activemodel/test/cases/validations/numericality_validation_test.rb +++ b/activemodel/test/cases/validations/numericality_validation_test.rb @@ -106,6 +106,13 @@ class NumericalityValidationTest < ActiveModel::TestCase      valid!([2])    end +  def test_validates_numericality_with_other_than +    Topic.validates_numericality_of :approved, :other_than => 0 + +    invalid!([0, 0.0]) +    valid!([-1, 42]) +  end +    def test_validates_numericality_with_proc      Topic.send(:define_method, :min_approved, lambda { 5 })      Topic.validates_numericality_of :approved, :greater_than_or_equal_to => Proc.new {|topic| topic.min_approved } diff --git a/activemodel/test/cases/validations/validates_test.rb b/activemodel/test/cases/validations/validates_test.rb index 779f6c8448..575154ffbd 100644 --- a/activemodel/test/cases/validations/validates_test.rb +++ b/activemodel/test/cases/validations/validates_test.rb @@ -16,6 +16,12 @@ class ValidatesTest < ActiveModel::TestCase      PersonWithValidator.reset_callbacks(:validate)    end +  def test_validates_with_messages_empty +    Person.validates :title, :presence => {:message => "" } +    person = Person.new +    assert !person.valid?, 'person should not be valid.' +  end +    def test_validates_with_built_in_validation      Person.validates :title, :numericality => true      person = Person.new diff --git a/activemodel/test/models/blog_post.rb b/activemodel/test/models/blog_post.rb index d289177259..46eba857df 100644 --- a/activemodel/test/models/blog_post.rb +++ b/activemodel/test/models/blog_post.rb @@ -1,10 +1,6 @@  module Blog -  def self._railtie -    Object.new -  end - -  def self.table_name_prefix -    "blog_" +  def self.use_relative_model_naming? +    true    end    class Post  | 
