aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/active_record_store_test.rb
blob: 6701b4dd295c2c1837fd504bdf72520fc5afd07c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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:')

def setup_session_schema(connection, table_name = 'sessions', session_id_column_name = 'sessid', data_column_name = 'data')
  connection.execute <<-end_sql
    create table #{table_name} (
      id integer primary key,
      #{connection.quote_column_name(session_id_column_name)} text unique,
      #{connection.quote_column_name(data_column_name)} text
    )
  end_sql
end

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
    session_id = @new_session.session_id
    @new_session.close
    found = session_class.find_by_session_id(session_id)
    assert_not_nil found
    assert_equal 'bar', found.data['foo']
  end
end


class SqlBypassActiveRecordStoreTest < Test::Unit::TestCase
  def session_class
    CGI::Session::ActiveRecordStore::SqlBypass
  end

  def setup
    session_class.connection = CGI::Session::ActiveRecordStore::Session.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
    session_id = @new_session.session_id
    @new_session.close
    found = session_class.find_by_session_id(session_id)
    assert_not_nil found
    assert_equal 'bar', found.data['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