Minor cleanups after rebase
[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 {
51   no warnings 'redefine';
52   my $old_sth_new=\&DBIx::Class::Storage::DBI::Pg::Sth::new;
53   *DBIx::Class::Storage::DBI::Pg::Sth::new=sub {
54       ++$called;$page_size=$_[4];
55       goto &$old_sth_new;
56   };
57 }
58
59 END {
60     return unless $schema;
61     drop_test_schema($schema);
62 }
63
64 my $start_id = 'populateXaaaaaa';
65 my $rows=1e4;
66 my $offset = 3;
67
68 $called=0;
69 $schema->populate('Artist', [ [ qw/artistid name/ ], map { [ ($_ + $offset) => $start_id++ ] } ( 1 .. $rows ) ] );
70 is ($called,0,'Pg::Sth not created for insert');
71 is (
72     $schema->resultset ('Artist')->search ({ name => { -like => 'populateX%' } })->count,
73     $rows,
74     'populate created correct number of rows with massive AoA bulk insert',
75 );
76
77 {
78     $called=0;
79     my $rs=$schema->resultset('Artist')->search({});
80     my $count=0;
81     $count++ while $rs->next;
82     is($count,$rows,'get all the rows (loop)');
83     is($called,1,'Pg::Sth called once per rs');
84     is($page_size,1000,'default page size used');
85 }
86
87 {
88     $called=0;
89     my $rs=$schema->resultset('Artist')->search({},{cursor_page_size=>10});
90     $rs->first;
91     is($called,1,'Pg::Sth called again per rs');
92     is($page_size,10,'page size from attrs used');
93 }
94
95 {
96     $called=0;
97     my $rs=$schema->resultset('Artist')->search({});
98     $schema->storage->cursor_page_size(20);
99     $rs->first;
100     is($called,1,'Pg::Sth called again per rs');
101     is($page_size,20,'page size from storage used');
102     $schema->storage->cursor_page_size(undef);
103 }
104
105 {
106     $called=0;
107     my $rs=$schema->resultset('Artist')->search({});
108     my @rows=$rs->all;
109     is(scalar(@rows),$rows,'get all the rows (all)');
110     is($called,1,'Pg::Sth called again per rs');
111     is($page_size,1000,'default page size used again');
112 }
113
114 {
115     $called=0;
116     my $rs=$schema->resultset('Artist')->search({});
117     $schema->storage->set_use_dbms_capability('server_cursors',0);
118     my @rows=$rs->all;
119     is(scalar(@rows),$rows,'get all the rows (all)');
120     is($called,0,'Pg::Sth *not* called');
121     $schema->storage->set_use_dbms_capability('server_cursors',1);
122 }
123
124 {
125     $called=0;
126     my $rs=$schema->resultset('Artist')->search({},{server_cursors=>0});
127     my @rows=$rs->all;
128     is(scalar(@rows),$rows,'get all the rows (all)');
129     is($called,0,'Pg::Sth *not* called');
130 }
131