X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F72pg.t;h=7ca06c71fea09499e4e3cf374162c0ed9add6ef0;hb=efd389947b511e24b2d660c5b233121dbd1ad835;hp=1156f21f25ae1a1daad2219197e5fd13b0240050;hpb=f2b70f86c9c44eb3367bd2d4d629f7e201032652;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/t/72pg.t b/t/72pg.t index 1156f21..7ca06c7 100644 --- a/t/72pg.t +++ b/t/72pg.t @@ -1,60 +1,73 @@ use strict; -use warnings; +use warnings; use Test::More; use Test::Exception; use lib qw(t/lib); use DBICTest; -{ - package DBICTest::Schema::Casecheck; + +BEGIN { + package DBICTest::Schema::ArrayTest; use strict; use warnings; use base 'DBIx::Class'; __PACKAGE__->load_components(qw/Core/); - __PACKAGE__->table('testschema.casecheck'); - __PACKAGE__->add_columns(qw/id name NAME uc_name storecolumn/); + __PACKAGE__->table('testschema.array_test'); + __PACKAGE__->add_columns(qw/id arrayfield/); __PACKAGE__->column_info_from_storage(1); __PACKAGE__->set_primary_key('id'); - sub store_column { - my ($self, $name, $value) = @_; - $value = '#'.$value if($name eq "storecolumn"); - $self->maybe::next::method($name, $value); - } } -{ - package DBICTest::Schema::ArrayTest; +my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/}; + +plan skip_all => <load_components(qw/Core/); - __PACKAGE__->table('testschema.array_test'); - __PACKAGE__->add_columns(qw/id arrayfield/); + __PACKAGE__->table('testschema.casecheck'); + __PACKAGE__->add_columns(qw/id name NAME uc_name storecolumn/); __PACKAGE__->column_info_from_storage(1); __PACKAGE__->set_primary_key('id'); + sub store_column { + my ($self, $name, $value) = @_; + $value = '#'.$value if($name eq "storecolumn"); + $self->maybe::next::method($name, $value); + } } -my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/}; - -plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test '. - '(note: This test drops and creates tables called \'artist\', \'casecheck\', \'array_test\' and \'sequence_test\''. - ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''. - ' as well as following schemas: \'testschema\',\'anothertestschema\'!)' - unless ($dsn && $user); - +DBICTest::Schema->load_classes( 'Casecheck', 'ArrayTest' ); -plan tests => 41; +# make sure sqlt_type overrides work (::Storage::DBI::Pg does this) +{ + my $schema = DBICTest::Schema->connect($dsn, $user, $pass); -DBICTest::Schema->load_classes( 'Casecheck', 'ArrayTest' ); -my $schema = DBICTest::Schema->connect($dsn, $user, $pass,); + ok (!$schema->storage->_dbh, 'definitely not connected'); + is ($schema->storage->sqlt_type, 'PostgreSQL', 'sqlt_type correct pre-connection'); +} +my $schema = DBICTest::Schema->connect($dsn, $user, $pass); # Check that datetime_parser returns correctly before we explicitly connect. SKIP: { eval { require DateTime::Format::Pg }; @@ -96,21 +109,44 @@ EOS $dbh->do("CREATE SCHEMA yetanothertestschema;"); $dbh->do("CREATE TABLE yetanothertestschema.artist $artist_table_def;"); $dbh->do('set search_path=testschema,public'); -} - -# store_column is called once for create() for non sequence columns + $dbh->do("CREATE SCHEMA unq_nextval_schema;"); + $dbh->do("CREATE SCHEMA unq_nextval_schema2;"); + $dbh->do(<do('set search_path=public,testschema,yetanothertestschema'); + $dbh->do('create sequence public.artist_artistid_seq'); #< in the public schema + $dbh->do(<do('set search_path=testschema,public'); -ok(my $storecolumn = $schema->resultset('Casecheck')->create({'storecolumn' => 'a'})); +} -is($storecolumn->storecolumn, '#a'); # was '##a' -# This is in Core now, but it's here just to test that it doesn't break -$schema->class('Artist')->load_components('PK::Auto'); +{ + # This is in Core now, but it's here just to test that it doesn't break + $schema->class('Artist')->load_components('PK::Auto'); + cmp_ok( $schema->resultset('Artist')->count, '==', 0, 'this should start with an empty artist table'); -{ #test that auto-pk also works with the defined search path by un-schema-qualifying - #the table name + # test that auto-pk also works with the defined search path by + # un-schema-qualifying the table name my $artist_name_save = $schema->source("Artist")->name; $schema->source("Artist")->name("artist"); @@ -121,16 +157,69 @@ $schema->class('Artist')->load_components('PK::Auto'); is($unq_new && $unq_new->artistid, 1, "and got correct artistid"); - $schema->source("Artist")->name($artist_name_save); -} + my @test_schemas = ( [qw| anothertestschema 1 |], + [qw| yetanothertestschema 1 |], + ); + foreach my $t ( @test_schemas ) { + my ($sch_name, $start_num) = @$t; + #test with anothertestschema + $schema->source('Artist')->name("$sch_name.artist"); + $schema->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache + my $another_new; + lives_ok { + $another_new = $schema->resultset('Artist')->create({ name => 'Tollbooth Willy'}); + is( $another_new->artistid,$start_num, "got correct artistid for $sch_name") + or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || ''); + } "$sch_name liid 1 did not die" + or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || ''); + lives_ok { + $another_new = $schema->resultset('Artist')->create({ name => 'Adam Sandler'}); + is( $another_new->artistid,$start_num+1, "got correct artistid for $sch_name") + or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || ''); + } "$sch_name liid 2 did not die" + or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || ''); + + } -my $new = $schema->resultset('Artist')->create({ name => 'foo' }); -is($new->artistid, 2, "Auto-PK worked"); + my @todo_schemas = ( + [qw| unq_nextval_schema 2 |], + [qw| unq_nextval_schema2 1 |], + ); + + foreach my $t ( @todo_schemas ) { + my ($sch_name, $start_num) = @$t; + + #test with anothertestschema + $schema->source('Artist')->name("$sch_name.artist"); + $schema->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache + my $another_new; + lives_ok { + $another_new = $schema->resultset('Artist')->create({ name => 'Tollbooth Willy'}); + is( $another_new->artistid,$start_num, "got correct artistid for $sch_name") + or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || ''); + } "$sch_name liid 1 did not die" + or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || ''); + + lives_ok { + $another_new = $schema->resultset('Artist')->create({ name => 'Adam Sandler'}); + is( $another_new->artistid,$start_num+1, "got correct artistid for $sch_name") + or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || ''); + } "$sch_name liid 2 did not die" + or diag "USED SEQUENCE: ".($schema->source('Artist')->column_info('artistid')->{sequence} || ''); + } -$new = $schema->resultset('Artist')->create({ name => 'bar' }); + $schema->source('Artist')->column_info('artistid')->{sequence} = undef; #< clear sequence name cache + $schema->source("Artist")->name($artist_name_save); +} -is($new->artistid, 3, "Auto-PK worked"); +my $new; +lives_ok { + $new = $schema->resultset('Artist')->create({ name => 'foo' }); + is($new->artistid, 4, "Auto-PK worked"); + $new = $schema->resultset('Artist')->create({ name => 'bar' }); + is($new->artistid, 5, "Auto-PK worked"); +} 'old auto-pk tests did not die either'; my $test_type_info = { @@ -204,6 +293,10 @@ SKIP: { } +# store_column is called once for create() for non sequence columns +ok(my $storecolumn = $schema->resultset('Casecheck')->create({'storecolumn' => 'a'})); +is($storecolumn->storecolumn, '#a'); # was '##a' + my $name_info = $schema->source('Casecheck')->column_info( 'name' ); is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" ); @@ -299,10 +392,21 @@ for (1..5) { my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 }); is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually"); +#_cleanup ($dbh); + +done_testing; + + sub _cleanup { my $dbh = shift or return; + $dbh->ping or return; for my $stat ( + 'DROP TABLE unq_nextval_schema2.artist', + 'DROP SCHEMA unq_nextval_schema2', + 'DROP SEQUENCE public.artist_artistid_seq', + 'DROP TABLE unq_nextval_schema.artist', + 'DROP SCHEMA unq_nextval_schema', 'DROP TABLE testschema.artist', 'DROP TABLE testschema.casecheck', 'DROP TABLE testschema.sequence_test', @@ -317,6 +421,7 @@ sub _cleanup { 'DROP SCHEMA yetanothertestschema', ) { eval { $dbh->do ($stat) }; + diag $@ if $@; } }