Mojo-Pg-Che

 view release on metacpan or  search on metacpan

t/04-select.t  view on Meta::CPAN

use Mojo::Base -strict;

use Test::More;
use Mojo::Util qw(dumper);

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::Pg::Che;

my $class = 'Mojo::Pg::Che';

# 1
my $pg = $class->connect($dsn, $user, $pw, max_connections=>20);#{pg_enable_utf8 => 1,}

$pg->on(connection=>sub {$_[1]->do('set datestyle to "DMY, ISO";');});

subtest 'blocking pg select' => sub {
  my $r = $pg->selectrow_hashref('select now() as now',);
  like($r->{now}, qr/\d{4}-\d{2}-\d{2}/, 'blocking pg select');
};

subtest 'blocking db select' => sub {
  my $db = $pg->db;
  my $result = $db->selectrow_hashref('select now() as now',);
  like($result->{now}, qr/\d{4}-\d{2}-\d{2}/, 'blocking db select');
  
};

=pod
subtest 'attr Async=>1' => sub {
  my $sth = $pg->prepare('select now() as now, pg_sleep(?)');
  like $pg->selectrow_hashref($sth, undef, (1))->{now}, qr/\d{4}-\d{2}-\d{2}/, 'async sth pg selectrow_hashref';
  my $cb = $pg->selectrow_hashref($sth, {Async=>1}, ($_,));
  Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
  like $$cb->()->hash->{now}, qr/\d{4}-\d{2}-\d{2}/, "right convert to async sth";
  #~ my @cb = ();
  #~ for (1..5) {
    #~ my $rand = rand;
    #~ push @cb, [(my $cb = $pg->selectrow_hashref('select now() as now, pg_sleep(?), ?::numeric as rand', {Async=>1}, (10-$_,$rand))), $rand];
  #~ }
  #~ Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
  #~ for (@cb) {
    #~ my $r = ${$_->[0]}->()->fetchrow_hashref();
    #~ like $r->{now}, qr/\d{4}-\d{2}-\d{2}/, 'async sth pg selectrow_hashref';
    #~ is $r->{rand}, $_->[1], 'right result';
  #~ }
  
  #~ eval {local $sth; my $res = $pg->selectrow_hashref($sth, undef, (1))->{now};};
  #~ like $@, qr/.+/, 'blocking sth error after async query';
  
  #~ $sth->finish();
  $sth->{pg_async} = 0;
  like $pg->selectrow_hashref($sth, undef, (1))->{now}, qr/\d{4}-\d{2}-\d{2}/, 'right convert from async sth';
};
=cut

subtest 'blocking selectrow_array' => sub {
  my @result;
  for (142..144) {
    push @result, $pg->selectrow_array('select ?::int, 100', undef, ($_));
  }
  is scalar @result, 6, 'blocking pg selectrow_array';
};

#~ subtest 'async selectrow_arrayref' => sub {
  #~ my @cb;



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