|
|
# 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 SQLite3 installed.
unless Object.const_defined?(:ActiveRecord)
require "#{File.dirname(__FILE__)}/../../../activerecord/lib/active_record"
end
require File.dirname(__FILE__) + '/../abstract_unit'
require 'action_controller/session/active_record_store'
CGI::Session::ActiveRecordStore::Session.establish_connection(:adapter => 'sqlite3', :dbfile => ':memory:')
class ActiveRecordStoreTest < Test::Unit::TestCase
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
@new_session = CGI::Session.new(CGI.new, 'database_manager' => CGI::Session::ActiveRecordStore, 'new_session' => true)
@new_session['foo'] = 'bar'
end
def teardown
session_class.drop_table!
end
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
end
class SqlBypassActiveRecordStoreTest < Test::Unit::TestCase
def session_class
CGI::Session::ActiveRecordStore::SqlBypass
end
def setup
session_class.connection = CGI::Session::ActiveRecordStore::SqlBypass.connection
session_class.create_table!
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)
end
def teardown
session_class.drop_table!
end
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
end
# End of safety net.
rescue Object => e
$stderr.puts "Skipping CGI::Session::ActiveRecordStore tests: #{e}"
#$stderr.puts " #{e.backtrace.join("\n ")}"
end
end
|