aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/activerecord
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-02-09 19:24:22 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-02-09 19:24:22 +0000
commitd0f3e52f0225c864db32fc5e8ddc59afe6d22c0a (patch)
tree108d0b53c369ad9fc286e0986c691cca95ac3a3b /actionpack/test/activerecord
parent5991e5c789da5adc260ceb9ba77da31d65991fbc (diff)
downloadrails-d0f3e52f0225c864db32fc5e8ddc59afe6d22c0a.tar.gz
rails-d0f3e52f0225c864db32fc5e8ddc59afe6d22c0a.tar.bz2
rails-d0f3e52f0225c864db32fc5e8ddc59afe6d22c0a.zip
Move active_record_store_test.rb to test/activerecord/. Closes #3790.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3556 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/test/activerecord')
-rw-r--r--actionpack/test/activerecord/active_record_store_test.rb151
1 files changed, 151 insertions, 0 deletions
diff --git a/actionpack/test/activerecord/active_record_store_test.rb b/actionpack/test/activerecord/active_record_store_test.rb
new file mode 100644
index 0000000000..2038a80119
--- /dev/null
+++ b/actionpack/test/activerecord/active_record_store_test.rb
@@ -0,0 +1,151 @@
+# Unfurl the safety net.
+path_to_ar = File.dirname(__FILE__) + '/../../../activerecord'
+if Object.const_defined?(:ActiveRecord) or File.exist?(path_to_ar)
+ begin
+
+# These tests exercise CGI::Session::ActiveRecordStore, so you're going to
+# need AR in a sibling directory to AP and have SQLite installed.
+
+unless Object.const_defined?(:ActiveRecord)
+ require File.join(path_to_ar, 'lib', 'active_record')
+end
+
+require File.dirname(__FILE__) + '/../abstract_unit'
+require 'action_controller/session/active_record_store'
+
+#ActiveRecord::Base.logger = Logger.new($stdout)
+begin
+ CGI::Session::ActiveRecordStore::Session.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
+ CGI::Session::ActiveRecordStore::Session.connection
+rescue Object
+ $stderr.puts 'SQLite 3 unavailable; falling back to SQLite 2.'
+ begin
+ CGI::Session::ActiveRecordStore::Session.establish_connection(:adapter => 'sqlite', :database => ':memory:')
+ CGI::Session::ActiveRecordStore::Session.connection
+ rescue Object
+ $stderr.puts 'SQLite 2 unavailable; skipping ActiveRecordStore test suite.'
+ raise SystemExit
+ end
+end
+
+
+module CommonActiveRecordStoreTests
+ def test_basics
+ s = session_class.new(:session_id => '1234', :data => { 'foo' => 'bar' })
+ assert_equal 'bar', s.data['foo']
+ assert s.save
+ assert_equal 'bar', s.data['foo']
+
+ assert_not_nil t = session_class.find_by_session_id('1234')
+ assert_not_nil t.data
+ assert_equal 'bar', t.data['foo']
+ end
+
+ def test_reload_same_session
+ @new_session.update
+ reloaded = CGI::Session.new(CGI.new, 'session_id' => @new_session.session_id, 'database_manager' => CGI::Session::ActiveRecordStore)
+ assert_equal 'bar', reloaded['foo']
+ end
+
+ def test_tolerates_close_close
+ assert_nothing_raised do
+ @new_session.close
+ @new_session.close
+ end
+ end
+end
+
+class ActiveRecordStoreTest < Test::Unit::TestCase
+ include CommonActiveRecordStoreTests
+
+ def session_class
+ CGI::Session::ActiveRecordStore::Session
+ end
+
+ def setup
+ session_class.create_table!
+
+ ENV['REQUEST_METHOD'] = 'GET'
+ CGI::Session::ActiveRecordStore.session_class = session_class
+
+ @cgi = CGI.new
+ @new_session = CGI::Session.new(@cgi, 'database_manager' => CGI::Session::ActiveRecordStore, 'new_session' => true)
+ @new_session['foo'] = 'bar'
+ end
+
+ def test_another_instance
+ @another = CGI::Session.new(@cgi, 'session_id' => @new_session.session_id, 'database_manager' => CGI::Session::ActiveRecordStore)
+ assert_equal @new_session.session_id, @another.session_id
+ end
+
+ def test_model_attribute
+ assert_kind_of CGI::Session::ActiveRecordStore::Session, @new_session.model
+ assert_equal({ 'foo' => 'bar' }, @new_session.model.data)
+ end
+
+ def teardown
+ session_class.drop_table!
+ end
+end
+
+class ColumnLimitTest < Test::Unit::TestCase
+ def setup
+ @session_class = CGI::Session::ActiveRecordStore::Session
+ @session_class.create_table!
+ end
+
+ def teardown
+ @session_class.drop_table!
+ end
+
+ def test_protection_from_data_larger_than_column
+ # Can't test this unless there is a limit
+ return unless limit = @session_class.data_column_size_limit
+ too_big = ':(' * limit
+ s = @session_class.new(:session_id => '666', :data => {'foo' => too_big})
+ s.data
+ assert_raise(ActionController::SessionOverflowError) { s.save }
+ end
+end
+
+class DeprecatedActiveRecordStoreTest < ActiveRecordStoreTest
+ def setup
+ session_class.connection.execute 'create table old_sessions (id integer primary key, sessid text unique, data text)'
+ session_class.table_name = 'old_sessions'
+ session_class.send :setup_sessid_compatibility!
+
+ ENV['REQUEST_METHOD'] = 'GET'
+ CGI::Session::ActiveRecordStore.session_class = session_class
+
+ @new_session = CGI::Session.new(CGI.new, 'database_manager' => CGI::Session::ActiveRecordStore, 'new_session' => true)
+ @new_session['foo'] = 'bar'
+ end
+
+ def teardown
+ session_class.connection.execute 'drop table old_sessions'
+ session_class.table_name = 'sessions'
+ end
+end
+
+class SqlBypassActiveRecordStoreTest < ActiveRecordStoreTest
+ def session_class
+ unless @session_class
+ @session_class = CGI::Session::ActiveRecordStore::SqlBypass
+ @session_class.connection = CGI::Session::ActiveRecordStore::Session.connection
+ end
+ @session_class
+ end
+
+ def test_model_attribute
+ assert_kind_of CGI::Session::ActiveRecordStore::SqlBypass, @new_session.model
+ assert_equal({ 'foo' => 'bar' }, @new_session.model.data)
+ end
+end
+
+
+# End of safety net.
+ rescue Object => e
+ $stderr.puts "Skipping CGI::Session::ActiveRecordStore tests: #{e}"
+ #$stderr.puts " #{e.backtrace.join("\n ")}"
+ end
+end