Make freeze/thaw and dclone work as functions on CDBICompat objects.
[dbsrgits/DBIx-Class.git] / t / 72pg.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
89add887 8{
9 package DBICTest::Schema::Casecheck;
10
11 use strict;
12 use warnings;
13 use base 'DBIx::Class';
14
3ff5b740 15 __PACKAGE__->load_components(qw/Core/);
89add887 16 __PACKAGE__->table('casecheck');
17 __PACKAGE__->add_columns(qw/id name NAME uc_name/);
d9916234 18 __PACKAGE__->column_info_from_storage(1);
89add887 19 __PACKAGE__->set_primary_key('id');
20
21}
22
0567538f 23my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
24
25#warn "$dsn $user $pass";
26
70350518 27plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
ae515736 28 . ' (note: creates and drops tables named artist and casecheck!)' unless ($dsn && $user);
0567538f 29
95ba7ee4 30plan tests => 16;
0567538f 31
89add887 32DBICTest::Schema->load_classes( 'Casecheck' );
3ff5b740 33my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
0567538f 34
114780ee 35# Check that datetime_parser returns correctly before we explicitly connect.
36SKIP: {
37 eval { require DateTime::Format::Pg };
38 skip "DateTime::Format::Pg required", 2 if $@;
39
40 my $store = ref $schema->storage;
41 is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
42
43 my $parser = $schema->storage->datetime_parser;
44 is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
45}
46
3ff5b740 47my $dbh = $schema->storage->dbh;
48$schema->source("Artist")->name("testschema.artist");
4d272ce5 49$dbh->do("CREATE SCHEMA testschema;");
ae515736 50$dbh->do("CREATE TABLE testschema.artist (artistid serial PRIMARY KEY, name VARCHAR(100), charfield CHAR(10));");
51ok ( $dbh->do('CREATE TABLE testschema.casecheck (id serial PRIMARY KEY, "name" VARCHAR(1), "NAME" VARCHAR(2), "UC_NAME" VARCHAR(3));'), 'Creation of casecheck table');
0567538f 52
3ff5b740 53# This is in Core now, but it's here just to test that it doesn't break
54$schema->class('Artist')->load_components('PK::Auto');
0567538f 55
3ff5b740 56my $new = $schema->resultset('Artist')->create({ name => 'foo' });
0567538f 57
b6b65a3e 58is($new->artistid, 1, "Auto-PK worked");
59
3ff5b740 60$new = $schema->resultset('Artist')->create({ name => 'bar' });
b6b65a3e 61
62is($new->artistid, 2, "Auto-PK worked");
63
a953d8d9 64my $test_type_info = {
65 'artistid' => {
103e3e03 66 'data_type' => 'integer',
67 'is_nullable' => 0,
fc22fbac 68 'size' => 4,
a953d8d9 69 },
70 'name' => {
103e3e03 71 'data_type' => 'character varying',
72 'is_nullable' => 1,
ae515736 73 'size' => 100,
fc22fbac 74 'default_value' => undef,
103e3e03 75 },
76 'charfield' => {
77 'data_type' => 'character',
a953d8d9 78 'is_nullable' => 1,
fc22fbac 79 'size' => 10,
80 'default_value' => undef,
103e3e03 81 },
a953d8d9 82};
83
fc22fbac 84
3ff5b740 85my $type_info = $schema->storage->columns_info_for('testschema.artist');
fc22fbac 86my $artistid_defval = delete $type_info->{artistid}->{default_value};
87like($artistid_defval,
4d272ce5 88 qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
fc22fbac 89 'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
90is_deeply($type_info, $test_type_info,
91 'columns_info_for - column data types');
a953d8d9 92
3ff5b740 93my $name_info = $schema->source('Casecheck')->column_info( 'name' );
ae515736 94is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
95
3ff5b740 96my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
ae515736 97is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
98
3ff5b740 99my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
ae515736 100is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
101
95ba7ee4 102# Test SELECT ... FOR UPDATE
103my $HaveSysSigAction = eval "require Sys::SigAction" && !$@;
104if ($HaveSysSigAction) {
105 Sys::SigAction->import( 'set_sig_handler' );
106}
107
108SKIP: {
109 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
110 # create a new schema
111 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
112 $schema2->source("Artist")->name("testschema.artist");
113
114 $schema->txn_do( sub {
115 my $artist = $schema->resultset('Artist')->search(
116 {
117 artistid => 1
118 },
119 {
120 for => 'update'
121 }
122 )->first;
123 is($artist->artistid, 1, "select for update returns artistid = 1");
124
125 my $artist_from_schema2;
126 my $error_ok = 0;
127 eval {
128 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
129 alarm(2);
130 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
131 $artist_from_schema2->name('fooey');
132 $artist_from_schema2->update;
133 alarm(0);
134 };
135 if (my $e = $@) {
136 $error_ok = $e =~ /DBICTestTimeout/;
137 }
138
139 # Make sure that an error was raised, and that the update failed
140 ok($error_ok, "update from second schema times out");
141 ok($artist_from_schema2->is_column_changed('name'), "'name' column is still dirty from second schema");
142 });
143}
144
145SKIP: {
146 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
147 # create a new schema
148 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
149 $schema2->source("Artist")->name("testschema.artist");
150
151 $schema->txn_do( sub {
152 my $artist = $schema->resultset('Artist')->search(
153 {
154 artistid => 1
155 },
156 )->first;
157 is($artist->artistid, 1, "select for update returns artistid = 1");
158
159 my $artist_from_schema2;
160 my $error_ok = 0;
161 eval {
162 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
163 alarm(2);
164 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
165 $artist_from_schema2->name('fooey');
166 $artist_from_schema2->update;
167 alarm(0);
168 };
169 if (my $e = $@) {
170 $error_ok = $e =~ /DBICTestTimeout/;
171 }
172
173 # Make sure that an error was NOT raised, and that the update succeeded
174 ok(! $error_ok, "update from second schema DOES NOT timeout");
175 ok(! $artist_from_schema2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
176 });
177}
178
3ff5b740 179END {
180 if($dbh) {
181 $dbh->do("DROP TABLE testschema.artist;");
182 $dbh->do("DROP TABLE testschema.casecheck;");
183 $dbh->do("DROP SCHEMA testschema;");
184 }
185}
0567538f 186