Skip to content

Commit

Permalink
Move relevant validation tests from Active Record to Active Model
Browse files Browse the repository at this point in the history
  • Loading branch information
lifo committed Mar 20, 2009
1 parent e945bcf commit 60756ad
Show file tree
Hide file tree
Showing 23 changed files with 2,000 additions and 1,733 deletions.
13 changes: 12 additions & 1 deletion activemodel/lib/active_model/test_case.rb
Expand Up @@ -2,6 +2,17 @@


module ActiveModel #:nodoc: module ActiveModel #:nodoc:
class TestCase < ActiveSupport::TestCase #: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
end end
17 changes: 1 addition & 16 deletions activemodel/test/cases/helper.rb
@@ -1,24 +1,9 @@
$:.unshift(File.dirname(__FILE__) + '/../../lib') $:.unshift(File.dirname(__FILE__) + '/../../lib')
$:.unshift(File.dirname(__FILE__) + '/../../../activerecord/lib')
$:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib') $:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib')


require 'config' require 'config'
require 'active_model'

require 'active_record'
require 'logger'
ActiveRecord::Base.logger = Logger.new("debug.log")


# Setup database connection require 'active_model'
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)


# Show backtraces for deprecated behavior for quicker cleanup. # Show backtraces for deprecated behavior for quicker cleanup.
ActiveSupport::Deprecation.debug = true ActiveSupport::Deprecation.debug = true
Expand Down
31 changes: 31 additions & 0 deletions activemodel/test/cases/test_database.rb
@@ -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
73 changes: 73 additions & 0 deletions activemodel/test/cases/validations/acceptance_validation_test.rb
@@ -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
140 changes: 140 additions & 0 deletions activemodel/test/cases/validations/conditional_validation_test.rb
@@ -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
51 changes: 51 additions & 0 deletions activemodel/test/cases/validations/confirmation_validation_test.rb
@@ -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
30 changes: 30 additions & 0 deletions activemodel/test/cases/validations/exclusion_validation_test.rb
@@ -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

0 comments on commit 60756ad

Please sign in to comment.