Mojo-Pg-Che

 view release on metacpan or  search on metacpan

t/09-base-database.t  view on Meta::CPAN

use Mojo::Base -strict;

BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }

use Test::More;

#~ plan skip_all => 'set TEST_ONLINE to enable this test' unless $ENV{TEST_ONLINE};
plan skip_all => 'set env TEST_PG="dbname=<...>/<pg_user>/<passwd>" to enable this test' unless $ENV{TEST_PG};

my ($dsn, $user, $pw) = split m|[/]|, $ENV{TEST_PG};


use Mojo::IOLoop;
use Mojo::JSON 'true';
use Mojo::Pg::Che;
use Scalar::Util 'refaddr';

# Connected
#~ my $pg = Mojo::Pg::Che->new($ENV{TEST_ONLINE});
my $pg = Mojo::Pg::Che->connect($dsn, $user, $pw)
  ->pg;#max_connections=>20
ok $pg->db->ping, 'connected';

# Custom search_path
#~ $pg = Mojo::Pg::Che->new($ENV{TEST_ONLINE})->search_path(['$user', 'foo', 'bar']);
$pg = Mojo::Pg::Che->connect($dsn, $user, $pw, search_path=>['$user', 'foo', 'bar'])
  ->pg;#max_connections=>20
is_deeply $pg->db->query('show search_path')->hash,
  {search_path => '"$user", foo, bar'}, 'right structure';
#~ $pg = Mojo::Pg::Che->new($ENV{TEST_ONLINE});
$pg = Mojo::Pg::Che->connect($dsn, $user, $pw, max_connections=>1)
  ->pg;#20

# Blocking select
is_deeply $pg->db->query('select 1 as one, 2 as two, 3 as three')->hash,
  {one => 1, two => 2, three => 3}, 'right structure';

# Non-blocking select
my ($fail, $result);
my $same;
my $db = $pg->db;
$db->query(
  'select 1 as one, 2 as two, 3 as three' => sub {
    my ($db, $err, $results) = @_;
    $fail   = $err;
    $result = $results->hash;
    $same   = $db->dbh eq $results->db->dbh;
    Mojo::IOLoop->stop;
  }
);
Mojo::IOLoop->start;
ok $same, 'same database handles';
ok !$fail, 'no error';
is_deeply $result, {one => 1, two => 2, three => 3}, 'right structure';

# Concurrent non-blocking selects
($fail, $result) = ();
Mojo::IOLoop->delay(
  sub {
    my $delay = shift;
    $pg->db->query('select 1 as one' => $delay->begin);
    $pg->db->query('select 2 as two' => $delay->begin);
    $pg->db->query('select 2 as two' => $delay->begin);
  },
  sub {
    my ($delay, $err_one, $one, $err_two, $two, $err_again, $again) = @_;
    $fail   = $err_one || $err_two || $err_again;
    $result = [$one->hashes->first, $two->hashes->first, $again->hashes->first];
  }
)->wait;



( run in 0.566 second using v1.01-cache-2.11-cpan-71847e10f99 )