414199996a31107d6cff950f9155340c061be499
[dbsrgits/DBIx-Class.git] / t / 72pg_cursors.t
1 #!perl
2 use strict;
3 use warnings;
4
5 use Test::More;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 my ($dsn, $dbuser, $dbpass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
10
11 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
12   unless ($dsn && $dbuser);
13
14 plan tests => 16;
15
16 sub 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
34 sub 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
45 my $schema = DBICTest::Schema->connection($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
46 $schema->storage->set_use_dbms_capability('server_cursors',1);
47 drop_test_schema($schema);create_test_schema($schema);
48
49 my ($called,$page_size)=(0,0);
50 my $old_sth_new=\&DBIx::Class::Storage::DBI::Pg::Sth::new;
51 *DBIx::Class::Storage::DBI::Pg::Sth::new=sub {
52     ++$called;$page_size=$_[4];
53     goto &$old_sth_new;
54 };
55
56 END {
57     return unless $schema;
58     drop_test_schema($schema);
59 }
60
61 my $start_id = 'populateXaaaaaa';
62 my $rows=1e4;
63 my $offset = 3;
64
65 $called=0;
66 $schema->populate('Artist', [ [ qw/artistid name/ ], map { [ ($_ + $offset) => $start_id++ ] } ( 1 .. $rows ) ] );
67 is ($called,0,'Pg::Sth not created for insert');
68 is (
69     $schema->resultset ('Artist')->search ({ name => { -like => 'populateX%' } })->count,
70     $rows,
71     'populate created correct number of rows with massive AoA bulk insert',
72 );
73
74 {
75     $called=0;
76     my $rs=$schema->resultset('Artist')->search({});
77     my $count=0;
78     $count++ while $rs->next;
79     is($count,$rows,'get all the rows (loop)');
80     is($called,1,'Pg::Sth called once per rs');
81     is($page_size,1000,'default page size used');
82 }
83
84 {
85     $called=0;
86     my $rs=$schema->resultset('Artist')->search({},{cursor_page_size=>10});
87     $rs->first;
88     is($called,1,'Pg::Sth called again per rs');
89     is($page_size,10,'page size from attrs used');
90 }
91
92 {
93     $called=0;
94     my $rs=$schema->resultset('Artist')->search({});
95     $schema->storage->cursor_page_size(20);
96     $rs->first;
97     is($called,1,'Pg::Sth called again per rs');
98     is($page_size,20,'page size from storage used');
99     $schema->storage->cursor_page_size(undef);
100 }
101
102 {
103     $called=0;
104     my $rs=$schema->resultset('Artist')->search({});
105     my @rows=$rs->all;
106     is(scalar(@rows),$rows,'get all the rows (all)');
107     is($called,1,'Pg::Sth called again per rs');
108     is($page_size,1000,'default page size used again');
109 }
110
111 {
112     $called=0;
113     my $rs=$schema->resultset('Artist')->search({});
114     $schema->storage->set_use_dbms_capability('server_cursors',0);
115     my @rows=$rs->all;
116     is(scalar(@rows),$rows,'get all the rows (all)');
117     is($called,0,'Pg::Sth *not* called');
118     $schema->storage->set_use_dbms_capability('server_cursors',1);
119 }
120
121 {
122     $called=0;
123     my $rs=$schema->resultset('Artist')->search({},{server_cursors=>0});
124     my @rows=$rs->all;
125     is(scalar(@rows),$rows,'get all the rows (all)');
126     is($called,0,'Pg::Sth *not* called');
127 }
128