mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make Active Model test suite similar to Active Record
This commit is contained in:
parent
93e2d378df
commit
5b1a1bf5bf
17 changed files with 170 additions and 30 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
7
activemodel/lib/active_model/test_case.rb
Normal file
7
activemodel/lib/active_model/test_case.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require "active_support/test_case"
|
||||
|
||||
module ActiveModel #:nodoc:
|
||||
class TestCase < ActiveSupport::TestCase #:nodoc:
|
||||
include ActiveModel::ValidationsRepairHelper
|
||||
end
|
||||
end
|
48
activemodel/lib/active_model/validations_repair_helper.rb
Normal file
48
activemodel/lib/active_model/validations_repair_helper.rb
Normal file
|
@ -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
|
38
activemodel/test/cases/helper.rb
Normal file
38
activemodel/test/cases/helper.rb
Normal file
|
@ -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
|
|
@ -1,4 +1,4 @@
|
|||
require 'test_helper'
|
||||
require 'cases/helper'
|
||||
|
||||
class ObservedModel < ActiveModel::Base
|
||||
class Observer
|
|
@ -1,4 +1,4 @@
|
|||
require 'test_helper'
|
||||
require 'cases/helper'
|
||||
|
||||
class EventTest < ActiveModel::TestCase
|
||||
def setup
|
|
@ -1,4 +1,4 @@
|
|||
require 'test_helper'
|
||||
require 'cases/helper'
|
||||
|
||||
class MachineTestSubject
|
||||
include ActiveModel::StateMachine
|
|
@ -1,4 +1,4 @@
|
|||
require 'test_helper'
|
||||
require 'cases/helper'
|
||||
|
||||
class StateTestSubject
|
||||
include ActiveModel::StateMachine
|
|
@ -1,4 +1,4 @@
|
|||
require 'test_helper'
|
||||
require 'cases/helper'
|
||||
|
||||
class StateTransitionTest < ActiveModel::TestCase
|
||||
test 'should set from, to, and opts attr readers' do
|
|
@ -1,4 +1,4 @@
|
|||
require 'test_helper'
|
||||
require 'cases/helper'
|
||||
|
||||
class StateMachineSubject
|
||||
include ActiveModel::StateMachine
|
3
activemodel/test/config.rb
Normal file
3
activemodel/test/config.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
TEST_ROOT = File.expand_path(File.dirname(__FILE__))
|
||||
FIXTURES_ROOT = TEST_ROOT + "/fixtures"
|
||||
SCHEMA_FILE = TEST_ROOT + "/schema.rb"
|
BIN
activemodel/test/fixtures/fixture_database.sqlite3
vendored
Normal file
BIN
activemodel/test/fixtures/fixture_database.sqlite3
vendored
Normal file
Binary file not shown.
41
activemodel/test/fixtures/topics.yml
vendored
Normal file
41
activemodel/test/fixtures/topics.yml
vendored
Normal file
|
@ -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
|
22
activemodel/test/schema.rb
Normal file
22
activemodel/test/schema.rb
Normal file
|
@ -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
|
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue