cleanup, moved Pg::Sth to separate file
[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 use Time::HiRes qw(gettimeofday tv_interval);
9
10 my ($dsn, $dbuser, $dbpass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
11
12 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
13   unless ($dsn && $dbuser);
14
15 plan tests => 3;
16
17 sub create_test_schema {
18     my ($schema)=@_;
19     $schema->storage->dbh_do(
20         sub {
21             my (undef,$dbh)=@_;
22             local $dbh->{Warn} = 0;
23             $dbh->do(q[
24           CREATE TABLE artist
25           (
26               artistid       serial       NOT NULL   PRIMARY KEY,
27               name           varchar(100),
28               rank           integer,
29               charfield      char(10)
30           );
31             ],{ RaiseError => 0, PrintError => 0 });
32         });
33 }
34
35 sub drop_test_schema {
36     my ($schema)=@_;
37     $schema->storage->dbh_do(
38         sub {
39             my (undef,$dbh)=@_;
40             local $dbh->{Warn} = 0;
41             eval { $dbh->do('DROP TABLE IF EXISTS artist') };
42             eval { $dbh->do('DROP SEQUENCE public.artist_artistid_seq') };
43         });
44 }
45
46 # copied from 100populate.t
47
48 my $schema = DBICTest::Schema->connection($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
49 drop_test_schema($schema);create_test_schema($schema);
50
51 END {
52     return unless $schema;
53     drop_test_schema($schema);
54 }
55
56 my $start_id = 'populateXaaaaaa';
57 my $rows=1e4;
58 my $offset = 3;
59
60 $schema->populate('Artist', [ [ qw/artistid name/ ], map { [ ($_ + $offset) => $start_id++ ] } ( 1 .. $rows ) ] );
61 is (
62     $schema->resultset ('Artist')->search ({ name => { -like => 'populateX%' } })->count,
63     $rows,
64     'populate created correct number of rows with massive AoA bulk insert',
65 );
66
67 {
68     my $rs=$schema->resultset('Artist')->search({});
69     my $count=0;
70     my $t0=[gettimeofday];
71     $count++ while $rs->next;
72     is($count,$rows,'get all the rows (loop)');
73     diag('Time for all(loop): '.tv_interval($t0));
74 }
75
76 {
77     my $rs=$schema->resultset('Artist')->search({});
78     my $t0=[gettimeofday];
79     $rs->first;
80     diag('Time for first: '.tv_interval($t0));
81 }
82
83 {
84     my $rs=$schema->resultset('Artist')->search({});
85     my $t0=[gettimeofday];
86     my @rows=$rs->all;
87     is(scalar(@rows),$rows,'get all the rows (all)');
88     diag('Time for all: '.tv_interval($t0));
89 }