Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make Active Model test suite similar to Active Record

  • Loading branch information...
commit 5b1a1bf5bfc520248285b036672146122dd2a815 1 parent 93e2d37
@lifo lifo authored
View
3  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
View
3  activemodel/lib/active_model.rb
@@ -41,4 +41,7 @@ def self.load_all!
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
View
7 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
View
48 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
View
38 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
View
2  activemodel/test/observing_test.rb → activemodel/test/cases/observing_test.rb
@@ -1,4 +1,4 @@
-require 'test_helper'
+require 'cases/helper'
class ObservedModel < ActiveModel::Base
class Observer
View
2  activemodel/test/state_machine/event_test.rb → activemodel/test/cases/state_machine/event_test.rb
@@ -1,4 +1,4 @@
-require 'test_helper'
+require 'cases/helper'
class EventTest < ActiveModel::TestCase
def setup
View
2  activemodel/test/state_machine/machine_test.rb → activemodel/test/cases/state_machine/machine_test.rb
@@ -1,4 +1,4 @@
-require 'test_helper'
+require 'cases/helper'
class MachineTestSubject
include ActiveModel::StateMachine
View
2  activemodel/test/state_machine/state_test.rb → activemodel/test/cases/state_machine/state_test.rb
@@ -1,4 +1,4 @@
-require 'test_helper'
+require 'cases/helper'
class StateTestSubject
include ActiveModel::StateMachine
View
2  ...model/test/state_machine/state_transition_test.rb → ...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
View
2  activemodel/test/state_machine_test.rb → activemodel/test/cases/state_machine_test.rb
@@ -1,4 +1,4 @@
-require 'test_helper'
+require 'cases/helper'
class StateMachineSubject
include ActiveModel::StateMachine
View
3  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"
View
BIN  activemodel/test/fixtures/fixture_database.sqlite3
Binary file not shown
View
41 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
View
22 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
View
21 activemodel/test/test_helper.rb
@@ -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
View
2  activerecord/test/cases/helper.rb
@@ -57,7 +57,7 @@ def try_to_load_dependency_with_silence(*args)
class ActiveSupport::TestCase
include ActiveRecord::TestFixtures
- include ActiveRecord::Testing::RepairHelper
+ include ActiveModel::ValidationsRepairHelper
self.fixture_path = FIXTURES_ROOT
self.use_instantiated_fixtures = false
Please sign in to comment.
Something went wrong with that request. Please try again.