diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2005-07-04 23:09:49 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2005-07-04 23:09:49 +0000 |
commit | 5ab1df9e376c21db9fd242a7144b7a7abc0d4c65 (patch) | |
tree | b071bdd245ce54f9649b11bdd05605816c4f84ac /actionpack/test/controller | |
parent | 7bcaf2b6894078130ddfd6f855cb20d12f95dee5 (diff) | |
download | rails-5ab1df9e376c21db9fd242a7144b7a7abc0d4c65.tar.gz rails-5ab1df9e376c21db9fd242a7144b7a7abc0d4c65.tar.bz2 rails-5ab1df9e376c21db9fd242a7144b7a7abc0d4c65.zip |
r2820@asus: jeremy | 2005-07-04 21:13:57 -0700
Bring in-mem SQLite goodness to active_record_assertions_test
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1685 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/test/controller')
-rw-r--r-- | actionpack/test/controller/active_record_assertions_test.rb | 240 |
1 files changed, 119 insertions, 121 deletions
diff --git a/actionpack/test/controller/active_record_assertions_test.rb b/actionpack/test/controller/active_record_assertions_test.rb index 915750f667..7b3478e2a8 100644 --- a/actionpack/test/controller/active_record_assertions_test.rb +++ b/actionpack/test/controller/active_record_assertions_test.rb @@ -1,129 +1,127 @@ -path_to_ar = File.dirname(__FILE__) + '/../../../activerecord' +require "#{File.dirname(__FILE__)}/../abstract_unit" +# Unfurl the safety net. +path_to_ar = File.dirname(__FILE__) + '/../../../activerecord' if Object.const_defined?(:ActiveRecord) || File.exist?(path_to_ar) - - # This test is very different than the others. It requires ActiveRecord to - # run. There's a bunch of stuff we are assuming here: - # - # 1. activerecord exists as a sibling directory to actionpack - # (i.e., actionpack/../activerecord) - # 2. you've created the appropriate database to run the active_record unit tests - # 3. you set the appropriate database connection below - begin - driver_to_use = 'native_sqlite3' - - unless Object.const_defined?("ActiveRecord") - require File.join(path_to_ar, 'lib', 'active_record') unless Object.const_defined?(:ActiveRecord) - end - $: << path_to_ar + '/test/' - require "connections/#{driver_to_use}/connection" - require 'fixtures/company' - - # ----------------------------------------------------------------------------- - - # add some validation rules to trip up the assertions - class Company - protected - def validate - errors.add_on_empty('name') - errors.add('rating', 'rating should not be 2') if rating == 2 - errors.add_to_base('oh oh') if rating == 3 - end - end - - # ----------------------------------------------------------------------------- - - require File.dirname(__FILE__) + '/../abstract_unit' - - # a controller class to handle the AR assertions - class ActiveRecordAssertionsController < ActionController::Base - # fail with 1 bad column - def nasty_columns_1 - @company = Company.new - @company.name = "B" - @company.rating = 2 - render_text "snicker...." - end - - # fail with 2 bad column - def nasty_columns_2 - @company = Company.new - @company.name = "" - @company.rating = 2 - render_text "double snicker...." - end - - # this will pass validation - def good_company - @company = Company.new - @company.name = "A" - @company.rating = 69 - render_text "Goodness Gracious!" - end - - # this will fail validation - def bad_company - @company = Company.new - render_text "Who's Bad?" - end - - # the safety dance...... - def rescue_action(e) raise; end - end - - # ----------------------------------------------------------------------------- - - ActiveRecordAssertionsController.template_root = File.dirname(__FILE__) + "/../fixtures/" - - # The test case to try the AR assertions - class ActiveRecordAssertionsControllerTest < Test::Unit::TestCase - # set it up - def setup - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - @controller = ActiveRecordAssertionsController.new - end - - # test for 1 bad apple column - def test_some_invalid_columns - process :nasty_columns_1 - assert_success - assert_invalid_record 'company' - assert_invalid_column_on_record 'company', 'rating' - assert_valid_column_on_record 'company', 'name' - assert_valid_column_on_record 'company', ['name','id'] - end - - # test for 2 bad apples columns - def test_all_invalid_columns - process :nasty_columns_2 - assert_success - assert_invalid_record 'company' - assert_invalid_column_on_record 'company', 'rating' - assert_invalid_column_on_record 'company', 'name' - assert_invalid_column_on_record 'company', ['name','rating'] - end - - # ensure we have no problems with an ActiveRecord - def test_valid_record - process :good_company - assert_success - assert_valid_record 'company' - end - - # ensure we have problems with an ActiveRecord - def test_invalid_record - process :bad_company - assert_success - assert_invalid_record 'company' - end - end +# These tests require Active Record, so you're going to need AR in a +# sibling directory to AP and have SQLite installed. - rescue Object => e - puts "Skipping active record based tests" - puts e.message +unless Object.const_defined?(:ActiveRecord) + require "#{path_to_ar}/lib/active_record" +end + +begin + ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :dbfile => ':memory:') + ActiveRecord::Base.connection +rescue Object + $stderr.puts 'SQLite 3 unavailable; falling to SQLite 2.' + ActiveRecord::Base.establish_connection(:adapter => 'sqlite', :dbfile => ':memory:') + ActiveRecord::Base.connection +end + +# Set up company fixtures. +$LOAD_PATH << "#{path_to_ar}/test" +require 'fixtures/company' +File.read("#{path_to_ar}/test/fixtures/db_definitions/sqlite.sql").split(';').each do |sql| + ActiveRecord::Base.connection.execute(sql) unless sql.blank? +end + +# Add some validation rules to trip up the assertions. +class Company + protected + def validate + errors.add_on_empty('name') + errors.add('rating', 'rating should not be 2') if rating == 2 + errors.add_to_base('oh oh') if rating == 3 + end +end + +# A controller to host the assertions. +class ActiveRecordAssertionsController < ActionController::Base + self.template_root = "#{File.dirname(__FILE__)}/../fixtures/" + + # fail with 1 bad column + def nasty_columns_1 + @company = Company.new + @company.name = "B" + @company.rating = 2 + render_text "snicker...." + end + + # fail with 2 bad columns + def nasty_columns_2 + @company = Company.new + @company.name = "" + @company.rating = 2 + render_text "double snicker...." + end + + # this will pass validation + def good_company + @company = Company.new + @company.name = "A" + @company.rating = 69 + render_text "Goodness Gracious!" end + # this will fail validation + def bad_company + @company = Company.new + render_text "Who's Bad?" + end + + # the safety dance...... + def rescue_action(e) raise; end +end + + +class ActiveRecordAssertionsControllerTest < Test::Unit::TestCase + def setup + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @controller = ActiveRecordAssertionsController.new + end + + # test for 1 bad apple column + def test_some_invalid_columns + process :nasty_columns_1 + assert_success + assert_invalid_record 'company' + assert_invalid_column_on_record 'company', 'rating' + assert_valid_column_on_record 'company', 'name' + assert_valid_column_on_record 'company', %w(name id) + end + + # test for 2 bad apples columns + def test_all_invalid_columns + process :nasty_columns_2 + assert_success + assert_invalid_record 'company' + assert_invalid_column_on_record 'company', 'rating' + assert_invalid_column_on_record 'company', 'name' + assert_invalid_column_on_record 'company', %w(name rating) + end + + # ensure we have no problems with an ActiveRecord + def test_valid_record + process :good_company + assert_success + assert_valid_record 'company' + end + + # ensure we have problems with an ActiveRecord + def test_invalid_record + process :bad_company + assert_success + assert_invalid_record 'company' + end +end + +# End of safety net. + rescue Object => e + $stderr.puts "Skipping Active Record assertion tests: #{e}" + #$stderr.puts " #{e.backtrace.join("\n ")}" + end end |