diff --git a/activemodel/Rakefile b/activemodel/Rakefile index 4b60f8d682..283380db96 100755 --- a/activemodel/Rakefile +++ b/activemodel/Rakefile @@ -7,9 +7,8 @@ task :default => :test Rake::TestTask.new do |t| t.libs << "test" - t.pattern = 'test/**/*_test.rb' + t.test_files = Dir.glob("test/cases/**/*_test.rb").sort t.verbose = true - t.warning = true end # Generate the RDoc documentation diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index 9695ac7e3b..0df3719de1 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -41,4 +41,7 @@ module ActiveModel autoload :Validations, 'active_model/validations' autoload :Errors, 'active_model/errors' autoload :DeprecatedErrorMethods, 'active_model/deprecated_error_methods' + autoload :TestCase, 'active_model/test_case' + autoload :StateMachine, 'active_model/state_machine' + autoload :ValidationsRepairHelper, 'active_model/validations_repair_helper' end diff --git a/activemodel/lib/active_model/test_case.rb b/activemodel/lib/active_model/test_case.rb new file mode 100644 index 0000000000..09fb6d707c --- /dev/null +++ b/activemodel/lib/active_model/test_case.rb @@ -0,0 +1,7 @@ +require "active_support/test_case" + +module ActiveModel #:nodoc: + class TestCase < ActiveSupport::TestCase #:nodoc: + include ActiveModel::ValidationsRepairHelper + end +end diff --git a/activemodel/lib/active_model/validations_repair_helper.rb b/activemodel/lib/active_model/validations_repair_helper.rb new file mode 100644 index 0000000000..99e3b2f631 --- /dev/null +++ b/activemodel/lib/active_model/validations_repair_helper.rb @@ -0,0 +1,48 @@ +module ActiveModel + module ValidationsRepairHelper + def self.included(base) + base.class_eval do + extend ClassMethods + end + end + + module Toolbox + def self.record_validations(*model_classes) + model_classes.inject({}) do |repair, klass| + repair[klass] ||= {} + [:validate, :validate_on_create, :validate_on_update].each do |callback| + the_callback = klass.instance_variable_get("@#{callback.to_s}_callbacks") + repair[klass][callback] = (the_callback.nil? ? nil : the_callback.dup) + end + repair + end + end + + def self.reset_validations(recorded) + recorded.each do |klass, repairs| + [:validate, :validate_on_create, :validate_on_update].each do |callback| + klass.instance_variable_set("@#{callback.to_s}_callbacks", repairs[callback]) + end + end + end + end + + module ClassMethods + def repair_validations(*model_classes) + setup do + @validation_repairs = Toolbox.record_validations(*model_classes) + end + teardown do + Toolbox.reset_validations(@validation_repairs) + end + end + end + + def repair_validations(*model_classes, &block) + validation_repairs = Toolbox.record_validations(*model_classes) + return block.call + ensure + Toolbox.reset_validations(validation_repairs) + end + end +end diff --git a/activemodel/test/cases/helper.rb b/activemodel/test/cases/helper.rb new file mode 100644 index 0000000000..6465f4e61f --- /dev/null +++ b/activemodel/test/cases/helper.rb @@ -0,0 +1,38 @@ +$:.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") + +class SqliteError < StandardError +end + +# 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 SqliteError.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. +ActiveSupport::Deprecation.debug = true + +require 'rubygems' +require 'test/unit' +gem 'mocha', '>= 0.9.5' +require 'mocha' + +begin + require 'ruby-debug' + Debugger.start +rescue LoadError +end diff --git a/activemodel/test/observing_test.rb b/activemodel/test/cases/observing_test.rb similarity index 99% rename from activemodel/test/observing_test.rb rename to activemodel/test/cases/observing_test.rb index dc41c9f881..421ac4b4f8 100644 --- a/activemodel/test/observing_test.rb +++ b/activemodel/test/cases/observing_test.rb @@ -1,4 +1,4 @@ -require 'test_helper' +require 'cases/helper' class ObservedModel < ActiveModel::Base class Observer diff --git a/activemodel/test/state_machine/event_test.rb b/activemodel/test/cases/state_machine/event_test.rb similarity index 98% rename from activemodel/test/state_machine/event_test.rb rename to activemodel/test/cases/state_machine/event_test.rb index 64dc8c4875..2a0ef53a3f 100644 --- a/activemodel/test/state_machine/event_test.rb +++ b/activemodel/test/cases/state_machine/event_test.rb @@ -1,4 +1,4 @@ -require 'test_helper' +require 'cases/helper' class EventTest < ActiveModel::TestCase def setup diff --git a/activemodel/test/state_machine/machine_test.rb b/activemodel/test/cases/state_machine/machine_test.rb similarity index 97% rename from activemodel/test/state_machine/machine_test.rb rename to activemodel/test/cases/state_machine/machine_test.rb index d23c223160..2aa954d80c 100644 --- a/activemodel/test/state_machine/machine_test.rb +++ b/activemodel/test/cases/state_machine/machine_test.rb @@ -1,4 +1,4 @@ -require 'test_helper' +require 'cases/helper' class MachineTestSubject include ActiveModel::StateMachine diff --git a/activemodel/test/state_machine/state_test.rb b/activemodel/test/cases/state_machine/state_test.rb similarity index 98% rename from activemodel/test/state_machine/state_test.rb rename to activemodel/test/cases/state_machine/state_test.rb index fbf9ce7b0a..527bfd4c04 100644 --- a/activemodel/test/state_machine/state_test.rb +++ b/activemodel/test/cases/state_machine/state_test.rb @@ -1,4 +1,4 @@ -require 'test_helper' +require 'cases/helper' class StateTestSubject include ActiveModel::StateMachine diff --git a/activemodel/test/state_machine/state_transition_test.rb b/activemodel/test/cases/state_machine/state_transition_test.rb similarity index 99% rename from activemodel/test/state_machine/state_transition_test.rb rename to activemodel/test/cases/state_machine/state_transition_test.rb index b59ff5a6a7..17f9d88be7 100644 --- a/activemodel/test/state_machine/state_transition_test.rb +++ b/activemodel/test/cases/state_machine/state_transition_test.rb @@ -1,4 +1,4 @@ -require 'test_helper' +require 'cases/helper' class StateTransitionTest < ActiveModel::TestCase test 'should set from, to, and opts attr readers' do diff --git a/activemodel/test/state_machine_test.rb b/activemodel/test/cases/state_machine_test.rb similarity index 99% rename from activemodel/test/state_machine_test.rb rename to activemodel/test/cases/state_machine_test.rb index 312d8728ba..f66299741e 100644 --- a/activemodel/test/state_machine_test.rb +++ b/activemodel/test/cases/state_machine_test.rb @@ -1,4 +1,4 @@ -require 'test_helper' +require 'cases/helper' class StateMachineSubject include ActiveModel::StateMachine diff --git a/activemodel/test/config.rb b/activemodel/test/config.rb new file mode 100644 index 0000000000..0b577a9936 --- /dev/null +++ b/activemodel/test/config.rb @@ -0,0 +1,3 @@ +TEST_ROOT = File.expand_path(File.dirname(__FILE__)) +FIXTURES_ROOT = TEST_ROOT + "/fixtures" +SCHEMA_FILE = TEST_ROOT + "/schema.rb" diff --git a/activemodel/test/fixtures/fixture_database.sqlite3 b/activemodel/test/fixtures/fixture_database.sqlite3 new file mode 100644 index 0000000000..3847b16aca Binary files /dev/null and b/activemodel/test/fixtures/fixture_database.sqlite3 differ diff --git a/activemodel/test/fixtures/topics.yml b/activemodel/test/fixtures/topics.yml new file mode 100644 index 0000000000..e4c61ce2d8 --- /dev/null +++ b/activemodel/test/fixtures/topics.yml @@ -0,0 +1,41 @@ +first: + id: 1 + title: The First Topic + author_name: David + author_email_address: david@loudthinking.com + written_on: 2003-07-16t15:28:11.2233+01:00 + last_read: 2004-04-15 + bonus_time: 2005-01-30t15:28:00.00+01:00 + content: Have a nice day + approved: false + replies_count: 1 + +second: + id: 2 + title: The Second Topic of the day + author_name: Mary + written_on: 2004-07-15t15:28:00.0099+01:00 + content: Have a nice day + approved: true + replies_count: 0 + parent_id: 1 + type: Reply + +third: + id: 3 + title: The Third Topic of the day + author_name: Nick + written_on: 2005-07-15t15:28:00.0099+01:00 + content: I'm a troll + approved: true + replies_count: 1 + +fourth: + id: 4 + title: The Fourth Topic of the day + author_name: Carl + written_on: 2006-07-15t15:28:00.0099+01:00 + content: Why not? + approved: true + type: Reply + parent_id: 3 diff --git a/activemodel/test/schema.rb b/activemodel/test/schema.rb new file mode 100644 index 0000000000..3f289c63f8 --- /dev/null +++ b/activemodel/test/schema.rb @@ -0,0 +1,22 @@ +ActiveRecord::Schema.define do + create_table :topics, :force => true do |t| + t.string :title + t.string :author_name + t.string :author_email_address + t.datetime :written_on + t.time :bonus_time + t.date :last_read + t.text :content + t.boolean :approved, :default => true + t.integer :replies_count, :default => 0 + t.integer :parent_id + t.string :type + end + + create_table :developers, :force => true do |t| + t.string :name + t.integer :salary, :default => 70000 + t.datetime :created_at + t.datetime :updated_at + end +end diff --git a/activemodel/test/test_helper.rb b/activemodel/test/test_helper.rb deleted file mode 100644 index 5b5678e42d..0000000000 --- a/activemodel/test/test_helper.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'rubygems' -require 'test/unit' - -gem 'mocha', '>= 0.9.3' -require 'mocha' - -require 'active_model' -require 'active_model/state_machine' - -$:.unshift File.dirname(__FILE__) + "/../../activesupport/lib" -require 'active_support' -require 'active_support/test_case' - -class ActiveModel::TestCase < ActiveSupport::TestCase -end - -begin - require 'ruby-debug' - Debugger.start -rescue LoadError -end diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 1ec52ac24d..a4f05f0c72 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -57,7 +57,7 @@ end class ActiveSupport::TestCase include ActiveRecord::TestFixtures - include ActiveRecord::Testing::RepairHelper + include ActiveModel::ValidationsRepairHelper self.fixture_path = FIXTURES_ROOT self.use_instantiated_fixtures = false