Permalink
Browse files

Move relevant validation tests from Active Record to Active Model

  • Loading branch information...
lifo committed Mar 20, 2009
1 parent e945bcf commit 60756ad4ece2298e85353ed50853f1d260e0d27a
Showing with 2,000 additions and 1,733 deletions.
  1. +12 −1 activemodel/lib/active_model/test_case.rb
  2. +1 −16 activemodel/test/cases/helper.rb
  3. +31 −0 activemodel/test/cases/test_database.rb
  4. +73 −0 activemodel/test/cases/validations/acceptance_validation_test.rb
  5. +140 −0 activemodel/test/cases/validations/conditional_validation_test.rb
  6. +51 −0 activemodel/test/cases/validations/confirmation_validation_test.rb
  7. +30 −0 activemodel/test/cases/validations/exclusion_validation_test.rb
  8. +81 −0 activemodel/test/cases/validations/format_validation_test.rb
  9. +174 −0 activemodel/test/cases/validations/i18n_generate_message_validation_test.rb
  10. +4 −247 .../test/cases/validations_i18n_test.rb → activemodel/test/cases/validations/i18n_validation_test.rb
  11. +63 −0 activemodel/test/cases/validations/inclusion_validation_test.rb
  12. +443 −0 activemodel/test/cases/validations/length_validation_test.rb
  13. +163 −0 activemodel/test/cases/validations/numericality_validation_test.rb
  14. +41 −0 activemodel/test/cases/validations/presence_validation_test.rb
  15. +144 −0 activemodel/test/cases/validations_test.rb
  16. +4 −0 activemodel/test/models/developer.rb
  17. +30 −0 activemodel/test/models/reply.rb
  18. +9 −0 activemodel/test/models/topic.rb
  19. +101 −0 activerecord/test/cases/validations/association_validation_test.rb
  20. +37 −0 activerecord/test/cases/validations/i18n_generate_message_validation_test.rb
  21. +89 −0 activerecord/test/cases/validations/i18n_validation_test.rb
  22. +278 −0 activerecord/test/cases/validations/uniqueness_validation_test.rb
  23. +1 −1,469 activerecord/test/cases/validations_test.rb
@@ -2,6 +2,17 @@
module ActiveModel #:nodoc:
class TestCase < ActiveSupport::TestCase #:nodoc:
- include ActiveModel::ValidationsRepairHelper
+ def with_kcode(kcode)
+ if RUBY_VERSION < '1.9'
+ orig_kcode, $KCODE = $KCODE, kcode
+ begin
+ yield
+ ensure
+ $KCODE = orig_kcode
+ end
+ else
+ yield
+ end
+ end
end
end
@@ -1,24 +1,9 @@
$:.unshift(File.dirname(__FILE__) + '/../../lib')
-$:.unshift(File.dirname(__FILE__) + '/../../../activerecord/lib')
$:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib')
require 'config'
-require 'active_model'
-
-require 'active_record'
-require 'logger'
-ActiveRecord::Base.logger = Logger.new("debug.log")
-# Setup database connection
-db_file = "#{FIXTURES_ROOT}/fixture_database.sqlite3"
-ActiveRecord::Base.configurations = { ActiveRecord::Base.name => { :adapter => 'sqlite3', :database => db_file, :timeout => 5000 } }
-unless File.exist?(db_file)
- puts "SQLite3 database not found at #{db_file}. Rebuilding it."
- sqlite_command = %Q{sqlite3 "#{db_file}" "create table a (a integer); drop table a;"}
- puts "Executing '#{sqlite_command}'"
- raise StandardError.new("Seems that there is no sqlite3 executable available") unless system(sqlite_command)
-end
-ActiveRecord::Base.establish_connection(ActiveRecord::Base.name)
+require 'active_model'
# Show backtraces for deprecated behavior for quicker cleanup.
ActiveSupport::Deprecation.debug = true
@@ -0,0 +1,31 @@
+require 'logger'
+
+$:.unshift(File.dirname(__FILE__) + '/../../../activerecord/lib')
+require 'active_record'
+require 'active_record/fixtures'
+
+module ActiveModel
+ module TestDatabase
+ def self.included(base)
+ ActiveRecord::Base.logger = Logger.new("debug.log")
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
+
+ base.send :include, ActiveRecord::TestFixtures
+ base.setup :setup_database
+ end
+
+ def setup_database
+ unless $schema_file_loaded
+ begin
+ # TODO : May the better way be with you
+ original, $stdout = $stdout, StringIO.new
+ load(SCHEMA_FILE)
+ ensure
+ $stdout = original
+ end
+
+ $schema_file_loaded = true
+ end
+ end
+ end
+end
@@ -0,0 +1,73 @@
+# encoding: utf-8
+require 'cases/helper'
+require 'cases/test_database'
+
+require 'models/topic'
+require 'models/reply'
+require 'models/developer'
+
+class AcceptanceValidationTest < ActiveModel::TestCase
+ include ActiveModel::TestDatabase
+ include ActiveModel::ValidationsRepairHelper
+
+ repair_validations(Topic)
+
+ def test_terms_of_service_agreement_no_acceptance
+ Topic.validates_acceptance_of(:terms_of_service, :on => :create)
+
+ t = Topic.create("title" => "We should not be confirmed")
+ assert t.save
+ end
+
+ def test_terms_of_service_agreement
+ Topic.validates_acceptance_of(:terms_of_service, :on => :create)
+
+ t = Topic.create("title" => "We should be confirmed","terms_of_service" => "")
+ assert !t.save
+ assert_equal ["must be accepted"], t.errors[:terms_of_service]
+
+ t.terms_of_service = "1"
+ assert t.save
+ end
+
+ def test_eula
+ Topic.validates_acceptance_of(:eula, :message => "must be abided", :on => :create)
+
+ t = Topic.create("title" => "We should be confirmed","eula" => "")
+ assert !t.save
+ assert_equal ["must be abided"], t.errors[:eula]
+
+ t.eula = "1"
+ assert t.save
+ end
+
+ def test_terms_of_service_agreement_with_accept_value
+ Topic.validates_acceptance_of(:terms_of_service, :on => :create, :accept => "I agree.")
+
+ t = Topic.create("title" => "We should be confirmed", "terms_of_service" => "")
+ assert !t.save
+ assert_equal ["must be accepted"], t.errors[:terms_of_service]
+
+ t.terms_of_service = "I agree."
+ assert t.save
+ end
+
+ def test_validates_acceptance_of_as_database_column
+ repair_validations(Reply) do
+ Reply.validates_acceptance_of(:author_name)
+
+ reply = Reply.create("author_name" => "Dan Brown")
+ assert_equal "Dan Brown", reply["author_name"]
+ end
+ end
+
+ def test_validates_acceptance_of_with_custom_error_using_quotes
+ repair_validations(Developer) do
+ Developer.validates_acceptance_of :salary, :message=> "This string contains 'single' and \"double\" quotes"
+ d = Developer.new
+ d.salary = "0"
+ assert !d.valid?
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors[:salary].last
+ end
+ end
+end
@@ -0,0 +1,140 @@
+# encoding: utf-8
+require 'cases/helper'
+require 'cases/test_database'
+
+require 'models/topic'
+
+class ConditionalValidationTest < ActiveModel::TestCase
+ include ActiveModel::TestDatabase
+ include ActiveModel::ValidationsRepairHelper
+
+ repair_validations(Topic)
+
+ def test_if_validation_using_method_true
+ # When the method returns true
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :if => :condition_is_true )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors[:title].any?
+ assert_equal ["hoo 5"], t.errors["title"]
+ end
+
+ def test_unless_validation_using_method_true
+ # When the method returns true
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :unless => :condition_is_true )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert t.valid?
+ assert !t.errors[:title].any?
+ end
+
+ def test_if_validation_using_method_false
+ # When the method returns false
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :if => :condition_is_true_but_its_not )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert t.valid?
+ assert t.errors[:title].empty?
+ end
+
+ def test_unless_validation_using_method_false
+ # When the method returns false
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :unless => :condition_is_true_but_its_not )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors[:title].any?
+ assert_equal ["hoo 5"], t.errors["title"]
+ end
+
+ def test_if_validation_using_string_true
+ # When the evaluated string returns true
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :if => "a = 1; a == 1" )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors[:title].any?
+ assert_equal ["hoo 5"], t.errors["title"]
+ end
+
+ def test_unless_validation_using_string_true
+ # When the evaluated string returns true
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :unless => "a = 1; a == 1" )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert t.valid?
+ assert t.errors[:title].empty?
+ end
+
+ def test_if_validation_using_string_false
+ # When the evaluated string returns false
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :if => "false")
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert t.valid?
+ assert t.errors[:title].empty?
+ end
+
+ def test_unless_validation_using_string_false
+ # When the evaluated string returns false
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :unless => "false")
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors[:title].any?
+ assert_equal ["hoo 5"], t.errors["title"]
+ end
+
+ def test_if_validation_using_block_true
+ # When the block returns true
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}",
+ :if => Proc.new { |r| r.content.size > 4 } )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors[:title].any?
+ assert_equal ["hoo 5"], t.errors["title"]
+ end
+
+ def test_unless_validation_using_block_true
+ # When the block returns true
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}",
+ :unless => Proc.new { |r| r.content.size > 4 } )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert t.valid?
+ assert t.errors[:title].empty?
+ end
+
+ def test_if_validation_using_block_false
+ # When the block returns false
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}",
+ :if => Proc.new { |r| r.title != "uhohuhoh"} )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert t.valid?
+ assert t.errors[:title].empty?
+ end
+
+ def test_unless_validation_using_block_false
+ # When the block returns false
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}",
+ :unless => Proc.new { |r| r.title != "uhohuhoh"} )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors[:title].any?
+ assert_equal ["hoo 5"], t.errors["title"]
+ end
+
+ # previous implementation of validates_presence_of eval'd the
+ # string with the wrong binding, this regression test is to
+ # ensure that it works correctly
+ def test_validation_with_if_as_string
+ Topic.validates_presence_of(:title)
+ Topic.validates_presence_of(:author_name, :if => "title.to_s.match('important')")
+
+ t = Topic.new
+ assert !t.valid?, "A topic without a title should not be valid"
+ assert !t.errors.invalid?("author_name"), "A topic without an 'important' title should not require an author"
+
+ t.title = "Just a title"
+ assert t.valid?, "A topic with a basic title should be valid"
+
+ t.title = "A very important title"
+ assert !t.valid?, "A topic with an important title, but without an author, should not be valid"
+ assert t.errors.invalid?("author_name"), "A topic with an 'important' title should require an author"
+
+ t.author_name = "Hubert J. Farnsworth"
+ assert t.valid?, "A topic with an important title and author should be valid"
+ end
+end
@@ -0,0 +1,51 @@
+# encoding: utf-8
+require 'cases/helper'
+require 'cases/test_database'
+
+require 'models/topic'
+require 'models/developer'
+
+class ConfirmationValidationTest < ActiveModel::TestCase
+ include ActiveModel::TestDatabase
+ include ActiveModel::ValidationsRepairHelper
+
+ repair_validations(Topic)
+
+ def test_no_title_confirmation
+ Topic.validates_confirmation_of(:title)
+
+ t = Topic.new(:author_name => "Plutarch")
+ assert t.valid?
+
+ t.title_confirmation = "Parallel Lives"
+ assert !t.valid?
+
+ t.title_confirmation = nil
+ t.title = "Parallel Lives"
+ assert t.valid?
+
+ t.title_confirmation = "Parallel Lives"
+ assert t.valid?
+ end
+
+ def test_title_confirmation
+ Topic.validates_confirmation_of(:title)
+
+ t = Topic.create("title" => "We should be confirmed","title_confirmation" => "")
+ assert !t.save
+
+ t.title_confirmation = "We should be confirmed"
+ assert t.save
+ end
+
+ def test_validates_confirmation_of_with_custom_error_using_quotes
+ repair_validations(Developer) do
+ Developer.validates_confirmation_of :name, :message=> "confirm 'single' and \"double\" quotes"
+ d = Developer.new
+ d.name = "John"
+ d.name_confirmation = "Johnny"
+ assert !d.valid?
+ assert_equal ["confirm 'single' and \"double\" quotes"], d.errors[:name]
+ end
+ end
+end
@@ -0,0 +1,30 @@
+# encoding: utf-8
+require 'cases/helper'
+require 'cases/test_database'
+
+require 'models/topic'
+
+class ExclusionValidationTest < ActiveModel::TestCase
+ include ActiveModel::TestDatabase
+ include ActiveModel::ValidationsRepairHelper
+
+ repair_validations(Topic)
+
+ def test_validates_exclusion_of
+ Topic.validates_exclusion_of( :title, :in => %w( abe monkey ) )
+
+ assert Topic.create("title" => "something", "content" => "abc").valid?
+ assert !Topic.create("title" => "monkey", "content" => "abc").valid?
+ end
+
+ def test_validates_exclusion_of_with_formatted_message
+ Topic.validates_exclusion_of( :title, :in => %w( abe monkey ), :message => "option {{value}} is restricted" )
+
+ assert Topic.create("title" => "something", "content" => "abc")
+
+ t = Topic.create("title" => "monkey")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "option monkey is restricted", t.errors.on(:title)
+ end
+end
Oops, something went wrong.

0 comments on commit 60756ad

Please sign in to comment.