cursor usage can now be switched on/off
[dbsrgits/DBIx-Class.git] / t / 72pg_cursors.t
CommitLineData
8c194608 1#!perl
2use strict;
3use warnings;
4
5use Test::More;
6use lib qw(t/lib);
7use DBICTest;
8c194608 8
9my ($dsn, $dbuser, $dbpass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
10
11plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
12 unless ($dsn && $dbuser);
13
590a6c43 14plan tests => 10;
8c194608 15
16sub create_test_schema {
17 my ($schema)=@_;
18 $schema->storage->dbh_do(
19 sub {
20 my (undef,$dbh)=@_;
21 local $dbh->{Warn} = 0;
22 $dbh->do(q[
23 CREATE TABLE artist
24 (
25 artistid serial NOT NULL PRIMARY KEY,
26 name varchar(100),
27 rank integer,
28 charfield char(10)
29 );
30 ],{ RaiseError => 0, PrintError => 0 });
31 });
32}
33
34sub drop_test_schema {
35 my ($schema)=@_;
36 $schema->storage->dbh_do(
37 sub {
38 my (undef,$dbh)=@_;
39 local $dbh->{Warn} = 0;
40 eval { $dbh->do('DROP TABLE IF EXISTS artist') };
41 eval { $dbh->do('DROP SEQUENCE public.artist_artistid_seq') };
42 });
43}
44
590a6c43 45my $schema = DBICTest::Schema->connection($dsn, $dbuser, $dbpass, { AutoCommit => 1, use_pg_cursors => 1 });
8c194608 46drop_test_schema($schema);create_test_schema($schema);
47
590a6c43 48my ($called,$page_size)=(0,0);
49my $old_sth_new=\&DBIx::Class::Storage::DBI::Pg::Sth::new;
50*DBIx::Class::Storage::DBI::Pg::Sth::new=sub {
51 ++$called;$page_size=$_[4];
52 goto &$old_sth_new;
53};
54
8c194608 55END {
56 return unless $schema;
57 drop_test_schema($schema);
58}
59
60my $start_id = 'populateXaaaaaa';
61my $rows=1e4;
62my $offset = 3;
63
590a6c43 64$called=0;
8c194608 65$schema->populate('Artist', [ [ qw/artistid name/ ], map { [ ($_ + $offset) => $start_id++ ] } ( 1 .. $rows ) ] );
590a6c43 66is ($called,0,'Pg::Sth not created for insert');
8c194608 67is (
68 $schema->resultset ('Artist')->search ({ name => { -like => 'populateX%' } })->count,
69 $rows,
70 'populate created correct number of rows with massive AoA bulk insert',
71);
72
73{
590a6c43 74 $called=0;
8c194608 75 my $rs=$schema->resultset('Artist')->search({});
76 my $count=0;
8c194608 77 $count++ while $rs->next;
78 is($count,$rows,'get all the rows (loop)');
590a6c43 79 is($called,1,'Pg::Sth called once per rs');
80 is($page_size,$DBIx::Class::Storage::DBI::Pg::DEFAULT_PG_CURSORS_PAGE_SIZE,'default page size used');
8c194608 81}
82
83{
590a6c43 84 $called=0;
85 my $rs=$schema->resultset('Artist')->search({},{pg_cursors_page_size=>10});
8c194608 86 $rs->first;
590a6c43 87 is($called,1,'Pg::Sth called again per rs');
88 is($page_size,10,'page size from attrs used');
8c194608 89}
90
91{
590a6c43 92 $called=0;
8c194608 93 my $rs=$schema->resultset('Artist')->search({});
8c194608 94 my @rows=$rs->all;
95 is(scalar(@rows),$rows,'get all the rows (all)');
590a6c43 96 is($called,1,'Pg::Sth called again per rs');
97 is($page_size,$DBIx::Class::Storage::DBI::Pg::DEFAULT_PG_CURSORS_PAGE_SIZE,'default page size used again');
8c194608 98}
590a6c43 99