Apache-Session-Browseable

 view release on metacpan or  search on metacpan

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

#!/usr/bin/perl

use strict;
use warnings;
use Test::More;

plan skip_all => "Optional modules (DBD::Pg, DBI) not installed"
  unless eval {
    require DBI;
    require DBD::Pg;
  };

plan tests => 34;

my $package = 'Apache::Session::Browseable::Store::Patroni';

use_ok($package);

my $store = $package->new;
isa_ok( $store, $package );

# Test _buildDataSource function
{
    no warnings 'once';

    # Basic case: append host/port
    is(
        Apache::Session::Browseable::Store::Patroni::_buildDataSource(
            'dbi:Pg:dbname=sessions', { host => '10.0.0.1', port => 5432 }
        ),
        'dbi:Pg:dbname=sessions;host=10.0.0.1;port=5432',
        '_buildDataSource: basic append'
    );

    # Replace existing host/port (semicolon separated)
    is(
        Apache::Session::Browseable::Store::Patroni::_buildDataSource(
            'dbi:Pg:dbname=sessions;host=old.host;port=1234',
            { host => '10.0.0.2', port => 5433 }
        ),
        'dbi:Pg:dbname=sessions;host=10.0.0.2;port=5433',
        '_buildDataSource: replace existing host/port'
    );

    # Handle trailing colon in DSN
    is(
        Apache::Session::Browseable::Store::Patroni::_buildDataSource(
            'dbi:Pg:', { host => '10.0.0.3', port => 5434 }
        ),
        'dbi:Pg:host=10.0.0.3;port=5434',
        '_buildDataSource: trailing colon'
    );

    # Complex DSN with other params
    is(
        Apache::Session::Browseable::Store::Patroni::_buildDataSource(
            'dbi:Pg:dbname=mydb;host=127.0.0.1;port=5432;sslmode=require',
            { host => '192.168.1.1', port => 5435 }
        ),
        'dbi:Pg:dbname=mydb;sslmode=require;host=192.168.1.1;port=5435',
        '_buildDataSource: complex DSN with other params'
    );
}

# Test cache structure (multi-source support)
{
    no warnings 'once';

    # Clear cache
    %Apache::Session::Browseable::Store::Patroni::patroniCache = ();

    my $ds1 = 'dbi:Pg:dbname=db1';
    my $ds2 = 'dbi:Pg:dbname=db2';

    # Simulate caching for first datasource
    $Apache::Session::Browseable::Store::Patroni::patroniCache{$ds1} = {
        leader => {
            host => '10.0.0.1',
            port => 5432,
            time => time()
        }
    };

    # Simulate caching for second datasource
    $Apache::Session::Browseable::Store::Patroni::patroniCache{$ds2} = {
        leader => {
            host => '10.0.0.2',
            port => 5433,
            time => time()
        }
    };

    # Verify they are independent
    is(
        $Apache::Session::Browseable::Store::Patroni::patroniCache{$ds1}
          ->{leader}->{host},
        '10.0.0.1', 'Multi-source: first datasource has correct host'
    );
    is(
        $Apache::Session::Browseable::Store::Patroni::patroniCache{$ds2}
          ->{leader}->{host},
        '10.0.0.2', 'Multi-source: second datasource has correct host'
    );

    # Verify ports are independent
    is(
        $Apache::Session::Browseable::Store::Patroni::patroniCache{$ds1}
          ->{leader}->{port},
        5432, 'Multi-source: first datasource has correct port'
    );
    is(
        $Apache::Session::Browseable::Store::Patroni::patroniCache{$ds2}
          ->{leader}->{port},
        5433, 'Multi-source: second datasource has correct port'
    );

    # Clear cache
    %Apache::Session::Browseable::Store::Patroni::patroniCache = ();
}



( run in 0.749 second using v1.01-cache-2.11-cpan-acf6aa7dc9e )