Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move relevant validation tests from Active Record to Active Model
- Loading branch information
Showing
23 changed files
with
2,000 additions
and
1,733 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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
73
activemodel/test/cases/validations/acceptance_validation_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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
140
activemodel/test/cases/validations/conditional_validation_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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
51
activemodel/test/cases/validations/confirmation_validation_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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
30
activemodel/test/cases/validations/exclusion_validation_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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.