"Fossies" - the Fresh Open Source Software Archive

Member "Apache-Session-1.93/t/99mysql.t" (8 Apr 2014, 5538 Bytes) of package /linux/www/Apache-Session-1.93.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 use Test::More;
    2 use Test::Deep;
    3 
    4 #plan skip_all => "Not running RDBM tests without APACHE_SESSION_MAINTAINER=1"
    5 #  unless ($ENV{APACHE_SESSION_MAINTAINER} || $ENV{TRAVIS});
    6 plan skip_all => "Optional modules (Test::Database, DBD::mysql, DBI) not installed"
    7   unless eval {
    8                require Test::Database;
    9                require DBD::mysql;
   10                require DBI;
   11               };
   12 
   13 my $dbd_mysq_ver = DBD::mysql->VERSION();
   14 plan skip_all => "Version $dbd_mysq_ver of DBD::mysql has serious problems on Windows"
   15   if ($^O eq 'MSWin32' && $dbd_mysq_ver >= '4.021' && $dbd_mysq_ver <= '4.023');
   16 
   17 if ($ENV{TRAVIS}) {
   18     my $cfg = << 'EOT';
   19     driver_dsn  = dbi:mysql:
   20     username    = root
   21 EOT
   22     Test::Database->load_config(\$cfg);
   23 }
   24 
   25 my @db_handles = Test::Database->handles('mysql');
   26 
   27 plan skip_all => "No mysql handle reported by Test::Database"
   28   unless @db_handles;
   29 
   30 my $mysql = $db_handles[0];
   31 my $dsn = $mysql->dsn();
   32 my $uname = $mysql->username();
   33 my $upass = $mysql->password();
   34 diag "DBD::mysql version ".DBD::mysql->VERSION();
   35 
   36 plan skip_all => "Test::Database handle->driver is undef. Probably it was not possible to establish connection."
   37   if !defined($mysql->driver);
   38 
   39 diag "Mysql version ".$mysql->driver->version;
   40 
   41 plan tests => 23;
   42 
   43 my $package = 'Apache::Session::MySQL';
   44 use_ok $package;
   45 
   46 my @tables_used = qw/sessions s/;
   47 sub drop_tables {
   48     my $dbh = shift;
   49     my $dblist = join(', ', @_);
   50     my $res = $dbh->do("DROP TABLE IF EXISTS $dblist");
   51     if (!defined $res and $dbh->errstr =~ /Cannot delete or update a parent row: a foreign key constraint/) {
   52       my $ary_ref = $dbh->selectcol_arrayref('SHOW TABLES');
   53       $dblist = join(', ', @$ary_ref);
   54       diag "Found foreign key constraint, trying to drop all tables from DB";
   55       
   56       $dbh->do("SET foreign_key_checks = 0");
   57       $dbh->do("DROP TABLE IF EXISTS $dblist");
   58       $dbh->do("SET foreign_key_checks = 1");
   59     }
   60 }
   61 
   62 {
   63     my $dbh1 = $mysql->dbh();
   64     drop_tables($dbh1, @tables_used);
   65     foreach my $table (@tables_used) {
   66         $dbh1->do(<<"EOT");
   67   CREATE TABLE $table (
   68     id char(32) not null primary key,
   69     a_session blob
   70   );
   71 EOT
   72     }
   73 }
   74 
   75 my $session = {};
   76 
   77 tie %{$session}, $package, undef, {
   78     DataSource     => $dsn,
   79     UserName       => $uname,
   80     Password       => $upass,
   81     LockDataSource => $dsn,
   82     LockUserName   => $uname,
   83     LockPassword   => $upass,
   84 };
   85 
   86 ok tied(%{$session}), 'session tied';
   87 
   88 ok exists($session->{_session_id}), 'session id exists';
   89 
   90 my $id = $session->{_session_id};
   91 
   92 my $foo = $session->{foo} = 'bar';
   93 my $baz = $session->{baz} = ['tom', 'dick', 'harry'];
   94 my $test_value = $session->{'test'} = 12; #test for RT#50896
   95 
   96 untie %{$session};
   97 undef $session;
   98 $session = {};
   99 
  100 tie %{$session}, $package, $id, {
  101     DataSource     => $dsn,
  102     UserName       => $uname,
  103     Password       => $upass,
  104     LockDataSource => $dsn,
  105     LockUserName   => $uname,
  106     LockPassword   => $upass,
  107 };
  108 
  109 ok tied(%{$session}), 'session tied';
  110 
  111 is $session->{_session_id}, $id, 'id retrieved matches one stored';
  112 
  113 cmp_deeply $session->{foo}, $foo, "Foo matches";
  114 cmp_deeply $session->{baz}, $baz, "Baz matches";
  115 cmp_deeply $session->{test}, $test_value, "test matches";
  116 
  117 untie %{$session};
  118 undef $session;
  119 $session = {};
  120 
  121 {
  122 
  123 tie %{$session}, $package, undef, {
  124     TableName      => 's',
  125     DataSource     => $dsn,
  126     UserName       => $uname,
  127     Password       => $upass,
  128     LockDataSource => $dsn,
  129     LockUserName   => $uname,
  130     LockPassword   => $upass,
  131 };
  132 
  133 ok tied(%{$session}), 'session tied';
  134 
  135 ok exists($session->{_session_id}), 'session id exists';
  136 my $id1 = $session->{_session_id};
  137 
  138 $session{'test'} = 13;
  139 
  140 untie %{$session};
  141 undef $session;
  142 $session = {};
  143 
  144 tie %{$session}, $package, $id1, {
  145     TableName      => 's',
  146     DataSource     => $dsn,
  147     UserName       => $uname,
  148     Password       => $upass,
  149     LockDataSource => $dsn,
  150     LockUserName   => $uname,
  151     LockPassword   => $upass,
  152 };
  153 
  154 ok tied(%{$session}), 'session tied';
  155 
  156 ok exists($session->{_session_id}), 'session id exists';
  157 
  158 is($session->{_session_id}, $id1, 'session id is correct');
  159 is($session{'test'}, 13, 'correct value retrieved');
  160 
  161 untie %{$session};
  162 undef $session;
  163 $session = {};
  164 
  165 tie %{$session}, $package, $id1, { #test for RT#50896
  166     TableName      => 's',
  167     DataSource     => $dsn,
  168     UserName       => $uname,
  169     Password       => $upass,
  170     LockDataSource => $dsn,
  171     LockUserName   => $uname,
  172     LockPassword   => $upass,
  173 };
  174 
  175 ok tied(%{$session}), 'session tied';
  176 
  177 ok exists($session->{_session_id}), 'session id exists';
  178 
  179 is($session->{_session_id}, $id1, 'session id is correct');
  180 is($session{'test'}, 13, 'correct value retrieved');
  181 
  182 }
  183 
  184 
  185 untie %{$session};
  186 undef $session;
  187 $session = {};
  188 
  189 my $dbh = DBI->connect($dsn, $uname, $upass, {RaiseError => 1});
  190 
  191 tie %{$session}, $package, $id, {
  192     Handle     => $dbh,
  193     LockHandle => $dbh,
  194 };
  195 
  196 ok tied(%{$session}), 'session tied';
  197 
  198 is $session->{_session_id}, $id, 'id retrieved matches one stored';
  199 
  200 cmp_deeply $session->{foo}, $foo, "Foo matches";
  201 cmp_deeply $session->{baz}, $baz, "Baz matches";
  202 cmp_deeply $session->{'test'}, $test_value, "test matches";
  203 
  204 tied(%{$session})->delete;
  205 untie %{$session};
  206 $dbh->disconnect;
  207 
  208 unless ($ENV{TRAVIS}) {
  209     my $dbh1 = $mysql->dbh();
  210     drop_tables($dbh1, @tables_used);
  211 }
  212