File: g4_mysql.t

package info (click to toggle)
libcgi-session-perl 4.48-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 616 kB
  • sloc: perl: 1,920; makefile: 5
file content (92 lines) | stat: -r--r--r-- 2,425 bytes parent folder | download | duplicates (4)
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
92
# $Id$

use strict;


my %dsn;
if ($ENV{DBI_DSN} && ($ENV{DBI_DSN} =~ m/^dbi:mysql:/)) {
    %dsn = (
        DataSource  => $ENV{DBI_DSN},
        User        => $ENV{DBI_USER},
        Password    => $ENV{DBI_PASS},
        TableName   => 'sessions'
    );
}
else {
    %dsn = (
        DataSource  => $ENV{CGISESS_MYSQL_DSN},
        User        => $ENV{CGISESS_MYSQL_USER},
        Password    => $ENV{CGISESS_MYSQL_PASS},
        Socket      => $ENV{CGISESS_MYSQL_SOCKET},
        TableName   => 'sessions'
    );
}


use File::Spec;
use Test::More;
use CGI::Session::Test::Default;

for (qw/DBI DBD::mysql/) {
    eval "require $_";
    if ( $@ ) {
        plan(skip_all=>"$_ is NOT available");
        exit(0);
    }
}



require CGI::Session::Driver::mysql;
my $dsnstring = CGI::Session::Driver::mysql->_mk_dsnstr(\%dsn);

my $dbh;
eval { $dbh = DBI->connect($dsnstring, $dsn{User}, $dsn{Password}, {RaiseError=>0, PrintError=>0}) };
if ( $@ ) {
    plan(skip_all=>"Couldn't establish connection with the MySQL server: " . (DBI->errstr || $@));
    exit(0);
}

my $count;
eval { ($count) = $dbh->selectrow_array("SELECT COUNT(*) FROM $dsn{TableName}") };
unless ( defined $count ) {
    unless( $dbh->do(qq|
        CREATE TABLE $dsn{TableName} (
            id CHAR(32) NOT NULL PRIMARY KEY,
            a_session TEXT NULL
        )|) ) {
        plan(skip_all=>"Couldn't create $dsn{TableName}: " . $dbh->errstr);
        exit(0);
    }
}


my $t = CGI::Session::Test::Default->new(
    dsn => "dr:mysql",
    args=>{Handle=>$dbh, TableName=>$dsn{TableName}});


plan tests => $t->number_of_tests + 2;
$t->run();

{
	# This used to test setting the global variable $CGI::Session::MySQL::TABLE_NAME.
	# However, since V 4.29_1, changes to CGI::Session::Driver's new() method mean
	# the unless test in CGI::Session::Driver::mysql's table_name() method was not executed,
	# and so $CGI::Session::MySQL::TABLE_NAME is never used. That 'unless' has been deleted.
	# V 4.32 explicitly documents this new situation. Moral: Don't use global variables.
	# This test was introduced in V 4.00_09.

    my $obj;
    eval {
        require CGI::Session::Driver::mysql;
        $obj = CGI::Session::Driver::mysql->new( {Handle=>$dbh} );
        $obj -> table_name('my_sessions');
    };
    is($@,'', 'survived eval');
    is($obj->table_name, 'my_sessions', "setting table name through the table_name() method works");
}