Skip to content
This repository
Browse code

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
  • Loading branch information...
commit 5ab1df9e376c21db9fd242a7144b7a7abc0d4c65 1 parent 7bcaf2b
Jeremy Kemper jeremy authored

Showing 1 changed file with 119 additions and 121 deletions. Show diff stats Hide diff stats

  1. +119 121 actionpack/test/controller/active_record_assertions_test.rb
240 actionpack/test/controller/active_record_assertions_test.rb
... ... @@ -1,129 +1,127 @@
1   -path_to_ar = File.dirname(__FILE__) + '/../../../activerecord'
  1 +require "#{File.dirname(__FILE__)}/../abstract_unit"
2 2
  3 +# Unfurl the safety net.
  4 +path_to_ar = File.dirname(__FILE__) + '/../../../activerecord'
3 5 if Object.const_defined?(:ActiveRecord) || File.exist?(path_to_ar)
4   -
5   - # This test is very different than the others. It requires ActiveRecord to
6   - # run. There's a bunch of stuff we are assuming here:
7   - #
8   - # 1. activerecord exists as a sibling directory to actionpack
9   - # (i.e., actionpack/../activerecord)
10   - # 2. you've created the appropriate database to run the active_record unit tests
11   - # 3. you set the appropriate database connection below
12   -
13 6 begin
14 7
15   - driver_to_use = 'native_sqlite3'
16   -
17   - unless Object.const_defined?("ActiveRecord")
18   - require File.join(path_to_ar, 'lib', 'active_record') unless Object.const_defined?(:ActiveRecord)
19   - end
20   - $: << path_to_ar + '/test/'
21   - require "connections/#{driver_to_use}/connection"
22   - require 'fixtures/company'
23   -
24   - # -----------------------------------------------------------------------------
25   -
26   - # add some validation rules to trip up the assertions
27   - class Company
28   - protected
29   - def validate
30   - errors.add_on_empty('name')
31   - errors.add('rating', 'rating should not be 2') if rating == 2
32   - errors.add_to_base('oh oh') if rating == 3
33   - end
34   - end
35   -
36   - # -----------------------------------------------------------------------------
37   -
38   - require File.dirname(__FILE__) + '/../abstract_unit'
39   -
40   - # a controller class to handle the AR assertions
41   - class ActiveRecordAssertionsController < ActionController::Base
42   - # fail with 1 bad column
43   - def nasty_columns_1
44   - @company = Company.new
45   - @company.name = "B"
46   - @company.rating = 2
47   - render_text "snicker...."
48   - end
49   -
50   - # fail with 2 bad column
51   - def nasty_columns_2
52   - @company = Company.new
53   - @company.name = ""
54   - @company.rating = 2
55   - render_text "double snicker...."
56   - end
57   -
58   - # this will pass validation
59   - def good_company
60   - @company = Company.new
61   - @company.name = "A"
62   - @company.rating = 69
63   - render_text "Goodness Gracious!"
64   - end
65   -
66   - # this will fail validation
67   - def bad_company
68   - @company = Company.new
69   - render_text "Who's Bad?"
70   - end
71   -
72   - # the safety dance......
73   - def rescue_action(e) raise; end
74   - end
75   -
76   - # -----------------------------------------------------------------------------
77   -
78   - ActiveRecordAssertionsController.template_root = File.dirname(__FILE__) + "/../fixtures/"
79   -
80   - # The test case to try the AR assertions
81   - class ActiveRecordAssertionsControllerTest < Test::Unit::TestCase
82   - # set it up
83   - def setup
84   - @request = ActionController::TestRequest.new
85   - @response = ActionController::TestResponse.new
86   - @controller = ActiveRecordAssertionsController.new
87   - end
88   -
89   - # test for 1 bad apple column
90   - def test_some_invalid_columns
91   - process :nasty_columns_1
92   - assert_success
93   - assert_invalid_record 'company'
94   - assert_invalid_column_on_record 'company', 'rating'
95   - assert_valid_column_on_record 'company', 'name'
96   - assert_valid_column_on_record 'company', ['name','id']
97   - end
98   -
99   - # test for 2 bad apples columns
100   - def test_all_invalid_columns
101   - process :nasty_columns_2
102   - assert_success
103   - assert_invalid_record 'company'
104   - assert_invalid_column_on_record 'company', 'rating'
105   - assert_invalid_column_on_record 'company', 'name'
106   - assert_invalid_column_on_record 'company', ['name','rating']
107   - end
108   -
109   - # ensure we have no problems with an ActiveRecord
110   - def test_valid_record
111   - process :good_company
112   - assert_success
113   - assert_valid_record 'company'
114   - end
115   -
116   - # ensure we have problems with an ActiveRecord
117   - def test_invalid_record
118   - process :bad_company
119   - assert_success
120   - assert_invalid_record 'company'
121   - end
122   - end
  8 +# These tests require Active Record, so you're going to need AR in a
  9 +# sibling directory to AP and have SQLite installed.
123 10
124   - rescue Object => e
125   - puts "Skipping active record based tests"
126   - puts e.message
  11 +unless Object.const_defined?(:ActiveRecord)
  12 + require "#{path_to_ar}/lib/active_record"
  13 +end
  14 +
  15 +begin
  16 + ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :dbfile => ':memory:')
  17 + ActiveRecord::Base.connection
  18 +rescue Object
  19 + $stderr.puts 'SQLite 3 unavailable; falling to SQLite 2.'
  20 + ActiveRecord::Base.establish_connection(:adapter => 'sqlite', :dbfile => ':memory:')
  21 + ActiveRecord::Base.connection
  22 +end
  23 +
  24 +# Set up company fixtures.
  25 +$LOAD_PATH << "#{path_to_ar}/test"
  26 +require 'fixtures/company'
  27 +File.read("#{path_to_ar}/test/fixtures/db_definitions/sqlite.sql").split(';').each do |sql|
  28 + ActiveRecord::Base.connection.execute(sql) unless sql.blank?
  29 +end
  30 +
  31 +# Add some validation rules to trip up the assertions.
  32 +class Company
  33 + protected
  34 + def validate
  35 + errors.add_on_empty('name')
  36 + errors.add('rating', 'rating should not be 2') if rating == 2
  37 + errors.add_to_base('oh oh') if rating == 3
  38 + end
  39 +end
  40 +
  41 +# A controller to host the assertions.
  42 +class ActiveRecordAssertionsController < ActionController::Base
  43 + self.template_root = "#{File.dirname(__FILE__)}/../fixtures/"
  44 +
  45 + # fail with 1 bad column
  46 + def nasty_columns_1
  47 + @company = Company.new
  48 + @company.name = "B"
  49 + @company.rating = 2
  50 + render_text "snicker...."
  51 + end
  52 +
  53 + # fail with 2 bad columns
  54 + def nasty_columns_2
  55 + @company = Company.new
  56 + @company.name = ""
  57 + @company.rating = 2
  58 + render_text "double snicker...."
  59 + end
  60 +
  61 + # this will pass validation
  62 + def good_company
  63 + @company = Company.new
  64 + @company.name = "A"
  65 + @company.rating = 69
  66 + render_text "Goodness Gracious!"
127 67 end
128 68
  69 + # this will fail validation
  70 + def bad_company
  71 + @company = Company.new
  72 + render_text "Who's Bad?"
  73 + end
  74 +
  75 + # the safety dance......
  76 + def rescue_action(e) raise; end
  77 +end
  78 +
  79 +
  80 +class ActiveRecordAssertionsControllerTest < Test::Unit::TestCase
  81 + def setup
  82 + @request = ActionController::TestRequest.new
  83 + @response = ActionController::TestResponse.new
  84 + @controller = ActiveRecordAssertionsController.new
  85 + end
  86 +
  87 + # test for 1 bad apple column
  88 + def test_some_invalid_columns
  89 + process :nasty_columns_1
  90 + assert_success
  91 + assert_invalid_record 'company'
  92 + assert_invalid_column_on_record 'company', 'rating'
  93 + assert_valid_column_on_record 'company', 'name'
  94 + assert_valid_column_on_record 'company', %w(name id)
  95 + end
  96 +
  97 + # test for 2 bad apples columns
  98 + def test_all_invalid_columns
  99 + process :nasty_columns_2
  100 + assert_success
  101 + assert_invalid_record 'company'
  102 + assert_invalid_column_on_record 'company', 'rating'
  103 + assert_invalid_column_on_record 'company', 'name'
  104 + assert_invalid_column_on_record 'company', %w(name rating)
  105 + end
  106 +
  107 + # ensure we have no problems with an ActiveRecord
  108 + def test_valid_record
  109 + process :good_company
  110 + assert_success
  111 + assert_valid_record 'company'
  112 + end
  113 +
  114 + # ensure we have problems with an ActiveRecord
  115 + def test_invalid_record
  116 + process :bad_company
  117 + assert_success
  118 + assert_invalid_record 'company'
  119 + end
  120 +end
  121 +
  122 +# End of safety net.
  123 + rescue Object => e
  124 + $stderr.puts "Skipping Active Record assertion tests: #{e}"
  125 + #$stderr.puts " #{e.backtrace.join("\n ")}"
  126 + end
129 127 end

0 comments on commit 5ab1df9

Please sign in to comment.
Something went wrong with that request. Please try again.