Apache-Session-Browseable

 view release on metacpan or  search on metacpan

t/Apache-Session-Browseable-Redis.t  view on Meta::CPAN

$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' );

# Test that updating an indexed field cleans up the old index
tie %session1, $package, $id1, $args;
ok( $r->sismember( "uid_obiwan", $id1 ),
    "Before update: id1 is in uid_obiwan index" );
$session1{uid} = 'benkenobi';
untie %session1;

ok( !$r->sismember( "uid_obiwan", $id1 ),
    "After update: id1 removed from old uid_obiwan index" );
ok( $r->sismember( "uid_benkenobi", $id1 ),
    "After update: id1 added to new uid_benkenobi index" );

# Verify searchOn uses the updated index
$hash = $package->searchOn( $args, 'uid', 'obiwan' );
is( keys %$hash, 0, "searchOn old value returns nothing" );

$hash = $package->searchOn( $args, 'uid', 'benkenobi' );
is( keys %$hash,            1,          "searchOn new value returns 1" );
is( $hash->{$id1}->{uid}, 'benkenobi', "Correct updated value" );

# Restore original value for subsequent tests
tie %session1, $package, $id1, $args;
$session1{uid} = 'obiwan';
untie %session1;

# Test that setting an indexed field to empty removes the index entry
tie %session1, $package, $id1, $args;
$session1{sn} = 'Kenobi';
untie %session1;

ok( $r->sismember( "sn_Kenobi", $id1 ), "sn_Kenobi index contains id1" );

tie %session1, $package, $id1, $args;
$session1{sn} = '';
untie %session1;

ok( !$r->sismember( "sn_Kenobi", $id1 ),
    "After clearing sn, old index entry removed" );

# Restore for deleteIfLowerThan test
tie %session1, $package, $id1, $args;
$session1{sn} = 'Kenobi';
untie %session1;

# Test deleteIfLowerThan cleans up indexes
$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 sessions after deleteIfLowerThan" );

# Verify that deleted sessions were removed from indexes
ok( !$r->sismember( "uid_obiwan", $id1 ),
    "deleteIfLowerThan cleaned uid index for deleted session" );
ok( !$r->sismember( "sn_Kenobi", $id1 ),
    "deleteIfLowerThan cleaned sn index for deleted session" );
ok( !$r->sismember( "uid_darthvader", $id2 ),
    "deleteIfLowerThan cleaned uid index for session 2" );
ok( $r->sismember( "uid_luke", $id3 ),
    "Surviving session still in uid index" );
ok( $r->sismember( "uid_yoda", $id5 ),
    "Excluded session (yoda) still in uid index" );

# Test that deleteIfLowerThan purges sessions missing required fields
# Create a session with no _utime (simulates an empty/interrupted session)
my %session_empty;
tie %session_empty, $package, undef, $args;
my $id_empty = $session_empty{_session_id};
$session_empty{uid} = 'ghost';



( run in 1.661 second using v1.01-cache-2.11-cpan-bbe5e583499 )