Permalink
Browse files

added validations test, commented out now largely irrelevant when test

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@311 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information...
1 parent bbbd447 commit bf58f8cc01c6721d2a53268bdad9489ef1c98256 dcroak committed Feb 12, 2008
Showing with 1,546 additions and 1,445 deletions.
  1. +1 −1 lib/validations.rb
  2. +7 −11 test/callbacks_test.rb
  3. +4 −0 test/fixtures/company.rb
  4. +101 −0 test/validations_test.rb
  5. +1,433 −1,433 test/when_test.rb
View
2 lib/validations.rb
@@ -4,7 +4,7 @@ module Validations
def self.included(klass)
class << klass
- validations = ActiveRecord::Validations::VALIDATIONS
+ validations = ['validate', 'validate_on_create', 'validate_on_update']
validations.each do |validation|
src = <<-END;
View
18 test/callbacks_test.rb
@@ -6,20 +6,16 @@ def setup
load File.join(File.dirname(__FILE__), 'fixtures', 'company.rb')
end
- basic_callbacks = ['before_create', 'after_create',
- 'before_validation_on_create', 'before_validation',
- 'after_validation_on_create', 'after_validation',
- 'before_save', 'after_save' ]
-
- update_callbacks = ['before_update', 'after_update',
- 'before_validation_on_update', 'after_validation_on_update']
-
+ basic_callbacks = ['before_create', 'after_create',
+ 'before_validation_on_create', 'before_validation',
+ 'after_validation_on_create', 'after_validation',
+ 'before_save', 'after_save' ]
+ update_callbacks = ['before_update', 'after_update',
+ 'before_validation_on_update', 'after_validation_on_update']
destroy_callbacks = ['before_destroy', 'after_destroy']
-
- unique_callbacks = ['after_find', 'after_initialize']
+ unique_callbacks = ['after_find', 'after_initialize']
conditions = ['lambda {|company| company.callback_flag == true}']
-
src = ''
conditions.each do |condition|
View
4 test/fixtures/company.rb
@@ -7,6 +7,10 @@ class Company < ActiveRecord::Base
def change_name
self.name = 'new name' unless self.frozen?
end
+
+ def flag?
+ self.callback_flag == true
+ end
# (1..114).each do |each|
# attr_accessor :"attribute_#{each}"
View
101 test/validations_test.rb
@@ -0,0 +1,101 @@
+require File.join(File.dirname(__FILE__), 'test_helper')
+
+class ValidationsTest < Test::Unit::TestCase
+
+ def setup
+ load File.join(File.dirname(__FILE__), 'fixtures', 'company.rb')
+ end
+
+ validations = ['validate', 'validate_on_create']
+ conditions = ['lambda {|company| company.callback_flag == true}']
+ src = ''
+
+ conditions.each do |condition|
+ validations.each do |validation|
+ src << <<-END;
+ def test_#{validation}_with_if_condition_which_returns_true_should_change_company_name
+ Company.class_eval do
+ #{validation} :change_name, :if => #{condition}
+ end
+ company = Company.new :name => 'thoughtbot', :callback_flag => true
+ assert company.save
+ assert_equal 'new name', company.name
+ end
+
+ def test_#{validation}_with_if_condition_which_returns_false_should_not_change_company_name
+ Company.class_eval do
+ #{validation} :change_name, :if => #{condition}
+ end
+ company = Company.new :name => 'thoughtbot', :callback_flag => false
+ assert company.save
+ assert_equal 'thoughtbot', company.name
+ end
+
+ def test_#{validation}_with_unless_condition_which_returns_true_should_not_change_company_name
+ Company.class_eval do
+ #{validation} :change_name, :unless => #{condition}
+ end
+ company = Company.new :name => 'thoughtbot', :callback_flag => true
+ assert company.save
+ assert_equal 'thoughtbot', company.name
+ end
+
+ def test_#{validation}_with_unless_condition_which_returns_false_should_change_company_name
+ Company.class_eval do
+ #{validation} :change_name, :unless => #{condition}
+ end
+ company = Company.new :name => 'thoughtbot', :callback_flag => false
+ assert company.save
+ assert_equal 'new name', company.name
+ end
+ END
+ end
+
+ src << <<-END;
+ def test_validate_on_update_with_if_condition_which_returns_true_should_change_company_name
+ Company.class_eval do
+ validate_on_update :change_name, :if => #{condition}
+ end
+ company = Company.create :name => 'thoughtbot', :callback_flag => true
+ assert company.save
+ assert_equal 'new name', company.name
+ end
+
+ def test_validate_on_update_with_if_condition_which_returns_false_should_not_change_company_name
+ Company.class_eval do
+ validate_on_update :change_name, :if => #{condition}
+ end
+ company = Company.create :name => 'thoughtbot', :callback_flag => false
+ assert company.save
+ assert_equal 'thoughtbot', company.name
+ end
+
+ def test_validate_on_update_with_unless_condition_which_returns_true_should_not_change_company_name
+ Company.class_eval do
+ validate_on_update :change_name, :unless => #{condition}
+ end
+ company = Company.create :name => 'thoughtbot', :callback_flag => true
+ assert company.save
+ assert_equal 'thoughtbot', company.name
+ end
+
+ def test_validate_on_update_with_unless_condition_which_returns_false_should_change_company_name
+ Company.class_eval do
+ validate_on_update :change_name, :unless => #{condition}
+ end
+ company = Company.create :name => 'thoughtbot', :callback_flag => false
+ assert company.save
+ assert_equal 'new name', company.name
+ end
+ END
+ end
+
+ class_eval src, __FILE__, __LINE__
+
+ def teardown
+ Object.class_eval do
+ remove_const Company.to_s if const_defined? Company.to_s
+ end
+ end
+
+end
View
2,866 test/when_test.rb
1,433 additions, 1,433 deletions not shown because the diff is too large. Please use a local Git client to view these changes.

0 comments on commit bf58f8c

Please sign in to comment.