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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
use Test::More;
use JSON qw/from_json/;
use utf8;
our $test_dburl = $ENV{REDIS_URL} || 'localhost:6379';
our $test_dbnum = $ENV{REDIS_DBNUM} || 15;
our $r; # Redis handle used for asserts
plan skip_all => "Optional modules (Redis) not installed"
unless eval { require Redis; };
plan skip_all => "Redis error : $@"
unless eval {
$r = Redis->new( server => $test_dburl );
$r->select($test_dbnum);
$r->flushall();
};
plan tests => 32;
$package = 'Apache::Session::Browseable::Redis';
use_ok($package);
my $args = {
server => $test_dburl,
database => $test_dbnum,
# Choose your browseable fileds
Index => 'uid sn mail int2',
};
use Data::Dumper;
my $id;
my $json;
is( keys %{ $r->keys('*') }, 0, "Make sure database is empty" );
# Create new session
my %session;
tie %session, $package, $id, $args;
$session{uid} = 'mé';
$session{mail} = 'mé@me.com';
$session{color} = 'zz';
$id = $session{_session_id};
untie %session;
# Make sure it was stored:
ok( $r->exists($id), "Test if new session id exists as a key in Redis" );
ok( $r->exists("uid_mé"), "Test if index exists" );
ok( $json = from_json( $r->get($id) ), "Parse redis value as JSON" );
is( $json->{mail}, 'mé@me.com', "Test if session subkey was correctly stored" );
# Read existing session
tie %session, $package, $id, $args;
is( $session{mail}, 'mé@me.com', "Test if session subkey can be read" );
# Delete session;
tied(%session)->delete;
ok( !$r->exists($id), "Test if new session id was removed" );
ok( !$r->exists("uid_mé"), "Test if index was removed" );
is( keys %{ $r->keys('*') }, 0, "Make sure database is empty after removal" );
untie %session;
# Create a bunch of sessions to search on
my (
%session1, %session2, %session3, %session4, %session5,
$id1, $id2, $id3, $id4, $id5
);
tie %session1, $package, undef, $args;
$session1{uid} = 'obiwan';
$session1{sn} = 'Kenobi';
$session1{color} = 'blue';
$session1{int1} = 10;
$session1{int2} = 20;
$id1 = $session1{_session_id};
untie %session1;
tie %session2, $package, undef, $args;
$session2{uid} = 'darthvader';
$session2{sn} = 'Skywalker';
$session2{color} = 'red';
$session2{int1} = 11;
$session2{int2} = 21;
$id2 = $session2{_session_id};
untie %session2;
tie %session3, $package, undef, $args;
$session3{uid} = 'luke';
$session3{sn} = 'Skywalker';
$session3{color} = 'green';
$session3{int1} = 12;
$session3{int2} = 24;
$id3 = $session3{_session_id};
untie %session3;
tie %session4, $package, undef, $args;
$session4{uid} = 'mace';
$session4{sn} = 'Windu';
$session4{color} = 'purple';
$session4{int1} = 13;
$session4{int2} = 23;
$id4 = $session4{_session_id};
untie %session4;
tie %session5, $package, undef, $args;
$session5{uid} = 'yoda';
$session5{sn} = 'Yoda';
$session5{color} = 'green';
$session5{int1} = 14;
$session5{int2} = 22;
$id5 = $session5{_session_id};
untie %session5;
# Pollutes Redis
my $redis = 'Apache::Session::Browseable::Store::Redis'->_getRedis($args);
$redis->set( 'aaaa', 'bbbb' );
$redis->set( '@aaaa:aa', 'bbbb/bb' );
# Search all keys
my $hash = $package->get_key_from_all_sessions( $args, "uid" );
is( keys %$hash, 5, "Found all 5 session" );
ok( exists( $hash->{$id4} ), "Found 'mace' in result" );
is( $hash->{$id4}->{uid}, 'mace', "Correct value in session 4" );
ok( !defined( $hash->{$id4}->{sn} ), 'only uid is returned' );
$hash = $package->get_key_from_all_sessions($args);
is( keys %$hash, 5, "Found all 5 session" );
ok( exists( $hash->{$id4} ), "Found 'mace' in result" );
is( $hash->{$id4}->{uid}, 'mace', "Correct value in session 4" );
is( $hash->{$id4}->{sn}, 'Windu', 'All fields are returned' );
# Search on indexed field
$hash = $package->searchOn( $args, 'sn', 'Skywalker' );
is( keys %$hash, 2, "Found 2 session" );
is( $hash->{$id2}->{uid}, 'darthvader', "Correct value" );
is( $hash->{$id3}->{uid}, 'luke', "Correct value" );
$hash = $package->searchOnExpr( $args, 'sn', '*walker' );
is( keys %$hash, 2, "Found 2 session" );
is( $hash->{$id2}->{uid}, 'darthvader', "Correct value" );
is( $hash->{$id3}->{uid}, 'luke', "Correct value" );
# Search on unindexed field
$hash = $package->searchOn( $args, 'color', 'green' );
is( keys %$hash, 2, "Found 2 session" );
is( $hash->{$id3}->{uid}, 'luke', "Correct value" );
is( $hash->{$id5}->{uid}, 'yoda', "Correct value" );
$hash = $package->searchOnExpr( $args, 'color', '*ee*', 'uid' );
is( keys %$hash, 2, "Found 2 session" );
is( $hash->{$id3}->{uid}, 'luke', "Correct value" );
is( $hash->{$id5}->{uid}, 'yoda', "Correct value" );
ok( !defined( $hash->{$id3}->{sn} ), 'only uid is returned' );
$package->deleteIfLowerThan(
$args,
{
or => {
int1 => 12,
int2 => 23,
},
not => {
uid => 'yoda',
}
}
);
$hash = $package->get_key_from_all_sessions($args);
is( keys %$hash, 3, "Found 3 session" );
$r->flushall;
|