From: Matt S Trout Date: Sat, 17 Sep 2005 16:43:31 +0000 (+0000) Subject: Test split to run against normal rels and helper rels (currently just has_one) X-Git-Tag: v0.03001~10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=22b15c96c84ddc1aeddddac637ca4c59a6465dcf;p=dbsrgits%2FDBIx-Class.git Test split to run against normal rels and helper rels (currently just has_one) --- diff --git a/MANIFEST b/MANIFEST index be95530..d1606c0 100644 --- a/MANIFEST +++ b/MANIFEST @@ -43,6 +43,7 @@ lib/DBIx/Class/Relationship.pm lib/DBIx/Class/Relationship/Accessor.pm lib/DBIx/Class/Relationship/Base.pm lib/DBIx/Class/Relationship/CascadeActions.pm +lib/DBIx/Class/Relationship/HasOne.pm lib/DBIx/Class/Relationship/ProxyMethods.pm lib/DBIx/Class/ResultSet.pm lib/DBIx/Class/Row.pm @@ -51,24 +52,31 @@ lib/DBIx/Class/Storage/DBI.pm lib/DBIx/Class/Storage/DBI/Cursor.pm lib/DBIx/Class/Table.pm lib/DBIx/Class/Test/SQLite.pm +Makefile.PL MANIFEST This list of files +META.yml +README script/nextalyzer.pl -t/01core.t t/02pod.t t/03podcoverage.t -t/04db.t -t/05multipk.t -t/06relationship.t -t/07pager.t -t/08inflate.t -t/08inflate_has_a.t -t/09update.t -t/10auto.t -t/11mysql.t -t/12pg.t -t/13oracle.t -t/14mssql.t -t/15limit.t +t/basicrels/01core.t +t/basicrels/04db.t +t/basicrels/05multipk.t +t/basicrels/06relationship.t +t/basicrels/07pager.t +t/basicrels/08inflate.t +t/basicrels/08inflate_has_a.t +t/basicrels/09update.t +t/basicrels/10auto.t +t/basicrels/11mysql.t +t/basicrels/12pg.t +t/basicrels/13oracle.t +t/basicrels/14mssql.t +t/basicrels/15limit.t +t/basicrels/16joins.t +t/basicrels/17join_count.t +t/basicrels/18self_referencial.t +t/cdbi-sweet-t/08pager.t t/cdbi-t/01-columns.t t/cdbi-t/02-Film.t t/cdbi-t/03-subclassing.t @@ -85,19 +93,61 @@ t/cdbi-t/16-reserved.t t/cdbi-t/18-has_a.t t/cdbi-t/19-set_sql.t t/cdbi-t/21-iterator.t +t/cdbi-t/22-self_referential.t t/cdbi-t/30-pager.t t/cdbi-t/98-failure.t +t/helperrels/01core.t +t/helperrels/04db.t +t/helperrels/05multipk.t +t/helperrels/06relationship.t +t/helperrels/07pager.t +t/helperrels/08inflate.t +t/helperrels/08inflate_has_a.t +t/helperrels/09update.t +t/helperrels/10auto.t +t/helperrels/11mysql.t +t/helperrels/12pg.t +t/helperrels/13oracle.t +t/helperrels/14mssql.t +t/helperrels/15limit.t +t/helperrels/16joins.t +t/helperrels/17join_count.t +t/helperrels/18self_referencial.t t/lib/DBICTest.pm +t/lib/DBICTest/BasicRels.pm +t/lib/DBICTest/HelperRels.pm t/lib/DBICTest/Schema.pm t/lib/DBICTest/Schema/Artist.pm +t/lib/DBICTest/Schema/BasicRels.pm t/lib/DBICTest/Schema/CD.pm t/lib/DBICTest/Schema/FourKeys.pm +t/lib/DBICTest/Schema/HelperRels.pm t/lib/DBICTest/Schema/LinerNotes.pm t/lib/DBICTest/Schema/OneKey.pm +t/lib/DBICTest/Schema/SelfRef.pm +t/lib/DBICTest/Schema/SelfRefAlias.pm t/lib/DBICTest/Schema/Tag.pm t/lib/DBICTest/Schema/Track.pm t/lib/DBICTest/Schema/TwoKeys.pm +t/run/01core.tl +t/run/04db.tl +t/run/05multipk.tl +t/run/06relationship.tl +t/run/07pager.tl +t/run/08inflate.tl +t/run/08inflate_has_a.tl +t/run/09update.tl +t/run/10auto.tl +t/run/11mysql.tl +t/run/12pg.tl +t/run/13oracle.tl +t/run/14mssql.tl +t/run/15limit.tl +t/run/16joins.tl +t/run/17join_count.tl +t/run/18self_referencial.tl t/testlib/Actor.pm +t/testlib/ActorAlias.pm t/testlib/Binary.pm t/testlib/Blurb.pm t/testlib/CDBase.pm @@ -114,6 +164,3 @@ t/testlib/MyStarLinkMCPK.pm t/testlib/Order.pm t/testlib/OtherFilm.pm t/testlib/PgBase.pm -META.yml -Makefile.PL -README diff --git a/META.yml b/META.yml index d3204a9..baaca31 100644 --- a/META.yml +++ b/META.yml @@ -1,6 +1,6 @@ --- name: DBIx-Class -version: 0.02 +version: 0.03 author: - Matt S. Trout abstract: Because the brain is a terrible thing to waste. @@ -8,14 +8,21 @@ license: perl requires: DBD::SQLite: 1.08 DBI: 0 + Data::Page: 0 + Module::Find: 0 NEXT: 0 - SQL::Abstract::Limit: 0.033 + SQL::Abstract: 1.2 + SQL::Abstract::Limit: 0.101 + Scalar::Util: 0 + Storable: 0 Tie::IxHash: 0 UNIVERSAL::require: 0 provides: + DBIC::SQL::Abstract: + file: lib/DBIx/Class/Storage/DBI.pm DBIx::Class: file: lib/DBIx/Class.pm - version: 0.02 + version: 0.03 DBIx::Class::AccessorGroup: file: lib/DBIx/Class/AccessorGroup.pm DBIx::Class::CDBICompat: @@ -54,6 +61,8 @@ provides: file: lib/DBIx/Class/CDBICompat/MightHave.pm DBIx::Class::CDBICompat::ObjIndexStubs: file: lib/DBIx/Class/CDBICompat/ObjIndexStubs.pm + DBIx::Class::CDBICompat::Pager: + file: lib/DBIx/Class/CDBICompat/Pager.pm DBIx::Class::CDBICompat::ReadOnly: file: lib/DBIx/Class/CDBICompat/ReadOnly.pm DBIx::Class::CDBICompat::Retrieve: @@ -102,6 +111,8 @@ provides: file: lib/DBIx/Class/Relationship/Base.pm DBIx::Class::Relationship::CascadeActions: file: lib/DBIx/Class/Relationship/CascadeActions.pm + DBIx::Class::Relationship::HasOne: + file: lib/DBIx/Class/Relationship/HasOne.pm DBIx::Class::Relationship::ProxyMethods: file: lib/DBIx/Class/Relationship/ProxyMethods.pm DBIx::Class::ResultSet: diff --git a/lib/DBIx/Class/CDBICompat/HasA.pm b/lib/DBIx/Class/CDBICompat/HasA.pm index c4595c6..b635429 100644 --- a/lib/DBIx/Class/CDBICompat/HasA.pm +++ b/lib/DBIx/Class/CDBICompat/HasA.pm @@ -7,7 +7,7 @@ sub has_a { my ($self, $col, $f_class, %args) = @_; $self->throw( "No such column ${col}" ) unless $self->_columns->{$col}; eval "require $f_class"; - if ($args{'inflate'} || $args{'deflate'}) { + if ($args{'inflate'} || $args{'deflate'}) { # Non-database has_a if (!ref $args{'inflate'}) { my $meth = $args{'inflate'}; $args{'inflate'} = sub { $f_class->$meth(shift); }; @@ -20,11 +20,10 @@ sub has_a { return 1; } my ($pri, $too_many) = keys %{ $f_class->_primaries }; - $self->throw( "has_a only works with a single primary key; ${f_class} has more" ) + $self->throw( "has_a only works with a single primary key; ${f_class} has more. try using a has_one relationship instead of Class::DBI compat rels" ) if $too_many; - $self->add_relationship($col, $f_class, - { "foreign.${pri}" => "self.${col}" }, - { accessor => 'filter' } ); + + $self->has_one($col, $f_class); return 1; } diff --git a/lib/DBIx/Class/Relationship.pm b/lib/DBIx/Class/Relationship.pm index f179120..4b9d64f 100644 --- a/lib/DBIx/Class/Relationship.pm +++ b/lib/DBIx/Class/Relationship.pm @@ -5,7 +5,7 @@ use warnings; use base qw/DBIx::Class Class::Data::Inheritable/; -__PACKAGE__->load_own_components(qw/Accessor CascadeActions ProxyMethods Base/); +__PACKAGE__->load_own_components(qw/Accessor CascadeActions ProxyMethods Base HasOne/); __PACKAGE__->mk_classdata('_relationships', { } ); diff --git a/lib/DBIx/Class/Relationship/HasOne.pm b/lib/DBIx/Class/Relationship/HasOne.pm new file mode 100644 index 0000000..dc2e436 --- /dev/null +++ b/lib/DBIx/Class/Relationship/HasOne.pm @@ -0,0 +1,42 @@ +package DBIx::Class::Relationship::HasOne; + +use strict; +use warnings; + +sub has_one { + my ($class, $acc_name, $f_class, $conds, $args) = @_; + eval "require $f_class"; + # single key relationship + if (not defined $conds && not defined $args) { + my ($pri, $too_many) = keys %{ $f_class->_primaries }; + my $acc_type = ($class->_columns->{$acc_name}) ? 'filter' : 'single'; + $class->add_relationship($acc_name, $f_class, + { "foreign.${pri}" => "self.${acc_name}" }, + { accessor => $acc_type } + ); + } + # multiple key relationship + else { + my %f_primaries = %{ $f_class->_primaries }; + my $conds_rel; + for (keys %$conds) { + $conds_rel->{"foreign.$_"} = "self.".$conds->{$_}; + # primary key usage checks + if (exists $f_primaries{$_}) { + delete $f_primaries{$_}; + } + else + { + $class->throw("non primary key used in join condition: $_"); + } + } + $class->throw("not all primary keys used in multi key relationship!") if keys %f_primaries; + $class->add_relationship($acc_name, $f_class, + $conds_rel, + { accessor => 'single' } + ); + } + return 1; +} + +1; diff --git a/t/01core.t b/t/01core.t deleted file mode 100644 index 5ccdd40..0000000 --- a/t/01core.t +++ /dev/null @@ -1,96 +0,0 @@ -use Test::More; - -plan tests => 23; - -use lib qw(t/lib); - -use_ok('DBICTest'); - -my @art = DBICTest::Artist->search({ }, { order_by => 'name DESC'}); - -cmp_ok(@art, '==', 3, "Three artists returned"); - -my $art = $art[0]; - -is($art->name, 'We Are Goth', "Correct order too"); - -$art->name('We Are In Rehab'); - -is($art->name, 'We Are In Rehab', "Accessor update ok"); - -is($art->get_column("name"), 'We Are In Rehab', 'And via get_column'); - -ok($art->update, 'Update run'); - -@art = DBICTest::Artist->search({ name => 'We Are In Rehab' }); - -cmp_ok(@art, '==', 1, "Changed artist returned by search"); - -cmp_ok($art[0]->artistid, '==', 3,'Correct artist too'); - -$art->delete; - -@art = DBICTest::Artist->search({ }); - -cmp_ok(@art, '==', 2, 'And then there were two'); - -ok(!$art->in_storage, "It knows it's dead"); - -eval { $art->delete; }; - -ok($@, "Can't delete twice: $@"); - -is($art->name, 'We Are In Rehab', 'But the object is still live'); - -$art->insert; - -ok($art->in_storage, "Re-created"); - -@art = DBICTest::Artist->search({ }); - -cmp_ok(@art, '==', 3, 'And now there are three again'); - -my $new = DBICTest::Artist->create({ artistid => 4 }); - -cmp_ok($new->artistid, '==', 4, 'Create produced record ok'); - -@art = DBICTest::Artist->search({ }); - -cmp_ok(@art, '==', 4, "Oh my god! There's four of them!"); - -$new->set_column('name' => 'Man With A Fork'); - -is($new->name, 'Man With A Fork', 'set_column ok'); - -$new->discard_changes; - -ok(!defined $new->name, 'Discard ok'); - -$new->name('Man With A Spoon'); - -$new->update; - -$new_again = DBICTest::Artist->find(4); - -is($new_again->name, 'Man With A Spoon', 'Retrieved correctly'); - -is(DBICTest::Artist->count, 4, 'count ok'); - -# insert_or_update -$new = DBICTest::Track->new( { - trackid => 100, - cd => 1, - position => 1, - title => 'Insert or Update', -} ); -$new->insert_or_update; -ok($new->in_storage, 'insert_or_update insert ok'); - -# test in update mode -$new->position(5); -$new->insert_or_update; -is( DBICTest::Track->find(100)->position, 5, 'insert_or_update update ok'); - -eval { DBICTest::Track->load_components('DoesNotExist'); }; - -ok $@, $@; diff --git a/t/04db.t b/t/04db.t deleted file mode 100644 index 3ab26b7..0000000 --- a/t/04db.t +++ /dev/null @@ -1,45 +0,0 @@ -use Test::More; - -plan tests => 4; - -use lib qw(t/lib); - -use_ok('DBICTest'); - -# add some rows inside a transaction and commit it -# XXX: Is storage->dbh the only way to get a dbh? -DBICTest::Artist->storage->dbh->{AutoCommit} = 0; -for (10..15) { - DBICTest::Artist->create( { - artistid => $_, - name => "artist number $_", - } ); -} -DBICTest::Artist->dbi_commit; -my ($artist) = DBICTest::Artist->find(15); -is($artist->name, 'artist number 15', "Commit ok"); - -# repeat the test using AutoCommit = 1 to force the commit -DBICTest::Artist->storage->dbh->{AutoCommit} = 0; -for (16..20) { - DBICTest::Artist->create( { - artistid => $_, - name => "artist number $_", - } ); -} -DBICTest::Artist->storage->dbh->{AutoCommit} = 1; -($artist) = DBICTest::Artist->find(20); -is($artist->name, 'artist number 20', "Commit using AutoCommit ok"); - -# add some rows inside a transaction and roll it back -DBICTest::Artist->storage->dbh->{AutoCommit} = 0; -for (21..30) { - DBICTest::Artist->create( { - artistid => $_, - name => "artist number $_", - } ); -} -DBICTest::Artist->dbi_rollback; -($artist) = DBICTest::Artist->search( artistid => 25 ); -is($artist, undef, "Rollback ok"); - diff --git a/t/05multipk.t b/t/05multipk.t deleted file mode 100644 index e4d364a..0000000 --- a/t/05multipk.t +++ /dev/null @@ -1,10 +0,0 @@ -use Test::More; - -plan tests => 3; - -use lib qw(t/lib); - -use_ok('DBICTest'); - -ok(DBICTest::FourKeys->find(1,2,3,4), "find multiple pks without hash"); -ok(DBICTest::FourKeys->find(5,4,3,6), "find multiple pks without hash"); diff --git a/t/06relationship.t b/t/06relationship.t deleted file mode 100644 index ec6b3aa..0000000 --- a/t/06relationship.t +++ /dev/null @@ -1,85 +0,0 @@ -use Test::More; - -plan tests => 14; - -use lib qw(t/lib); - -use_ok('DBICTest'); - -# has_a test -my $cd = DBICTest::CD->find(4); -my ($artist) = $cd->search_related('artist'); -is($artist->name, 'Random Boy Band', 'has_a search_related ok'); - -# has_many test with an order_by clause defined -$artist = DBICTest::Artist->find(1); -is( ($artist->search_related('cds'))[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' ); - -# search_related with additional abstract query -my @cds = $artist->search_related('cds', { title => { like => '%of%' } } ); -is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' ); - -# creating a related object -$artist->create_related( 'cds', { - title => 'Big Flop', - year => 2005, -} ); -is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' ); - -# count_related -is( $artist->count_related('cds'), 4, 'count_related ok' ); - -# set_from_related -my $track = DBICTest::Track->create( { - trackid => 1, - cd => 3, - position => 98, - title => 'Hidden Track' -} ); -$track->set_from_related( cd => $cd ); -is( $track->cd, 4, 'set_from_related ok' ); - -# update_from_related, the same as set_from_related, but it calls update afterwards -$track = DBICTest::Track->create( { - trackid => 2, - cd => 3, - position => 99, - title => 'Hidden Track' -} ); -$track->update_from_related( cd => $cd ); -is( (DBICTest::Track->search( cd => 4, position => 99 ))[0]->cd, 4, 'update_from_related ok' ); - -# find_or_create_related with an existing record -$cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } ); -is( $cd->year, 2005, 'find_or_create_related on existing record ok' ); - -# find_or_create_related creating a new record -$cd = $artist->find_or_create_related( 'cds', { - title => 'Greatest Hits', - year => 2006, -} ); -is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' ); -@cds = $artist->search_related('cds'); -is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' ); - -SKIP: { - #skip 'Need to add delete_related', 1; - # delete_related - $artist->delete_related( cds => { title => 'Greatest Hits' }); - cmp_ok( DBICTest::CD->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' ); -}; - -# try to add a bogus relationship using the wrong cols -eval { - $artist->add_relationship( - tracks => 'DBICTest::Track', - { 'foreign.cd' => 'self.cdid' } - ); -}; -like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok'); - -# another bogus relationship using no join condition -eval { - $artist->add_relationship( tracks => 'DBICTest::Track' ); -}; -like($@, qr/join condition/, 'failed when creating a rel without join condition, ok'); diff --git a/t/07pager.t b/t/07pager.t deleted file mode 100644 index 64f33f9..0000000 --- a/t/07pager.t +++ /dev/null @@ -1,66 +0,0 @@ -use Test::More; - -plan tests => 13; - -use lib qw(t/lib); - -use_ok('DBICTest'); - -# first page -my $it = DBICTest::CD->search( - {}, - { order_by => 'title', - rows => 3, - page => 1 } -); - -is( $it->pager->entries_on_this_page, 3, "entries_on_this_page ok" ); - -is( $it->pager->next_page, 2, "next_page ok" ); - -is( $it->count, 3, "count on paged rs ok" ); - -is( $it->next->title, "Caterwaulin' Blues", "iterator->next ok" ); - -$it->next; -$it->next; - -is( $it->next, undef, "next past end of page ok" ); - -# second page, testing with array -my @page2 = DBICTest::CD->search( - {}, - { order_by => 'title', - rows => 3, - page => 2 } -); - -is( $page2[0]->title, "Generic Manufactured Singles", "second page first title ok" ); - -# page a standard resultset -$it = DBICTest::CD->search( - {}, - { order_by => 'title', - rows => 3 } -); -my $page = $it->page(2); - -is( $page->count, 2, "standard resultset paged rs count ok" ); - -is( $page->next->title, "Generic Manufactured Singles", "second page of standard resultset ok" ); - -# test software-based limit paging -$it = DBICTest::CD->search( - {}, - { order_by => 'title', - rows => 3, - page => 2, - software_limit => 1 } -); -is( $it->pager->entries_on_this_page, 2, "software entries_on_this_page ok" ); - -is( $it->pager->previous_page, 1, "software previous_page ok" ); - -is( $it->count, 2, "software count on paged rs ok" ); - -is( $it->next->title, "Generic Manufactured Singles", "software iterator->next ok" ); diff --git a/t/08inflate.t b/t/08inflate.t deleted file mode 100644 index 3ecba70..0000000 --- a/t/08inflate.t +++ /dev/null @@ -1,30 +0,0 @@ -use Test::More; - -eval { require DateTime }; -plan skip_all => "Need DateTime for inflation tests" if $@; - -plan tests => 4; - -use lib qw(t/lib); - -use_ok('DBICTest'); - -DBICTest::CD->inflate_column( 'year', - { inflate => sub { DateTime->new( year => shift ) }, - deflate => sub { shift->year } } -); - -# inflation test -my $cd = DBICTest::CD->find(3); - -is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' ); - -is( $cd->year->month, 1, 'inflated month ok' ); - -# deflate test -my $now = DateTime->now; -$cd->year( $now ); -$cd->update; - -($cd) = DBICTest::CD->search( year => $now->year ); -is( $cd->year->year, $now->year, 'deflate ok' ); diff --git a/t/08inflate_has_a.t b/t/08inflate_has_a.t deleted file mode 100644 index 80678d1..0000000 --- a/t/08inflate_has_a.t +++ /dev/null @@ -1,54 +0,0 @@ -use Test::More; - -eval { require DateTime }; -plan skip_all => "Need DateTime for inflation tests" if $@; - -plan tests => 7; - -use lib qw(t/lib); - -use_ok('DBICTest'); - -DBICTest::CD->load_components(qw/CDBICompat::HasA/); - -DBICTest::CD->has_a( 'year', 'DateTime', - inflate => sub { DateTime->new( year => shift ) }, - deflate => sub { shift->year } -); - -# inflation test -my $cd = DBICTest::CD->find(3); - -is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' ); - -is( $cd->year->month, 1, 'inflated month ok' ); - -# deflate test -my $now = DateTime->now; -$cd->year( $now ); -$cd->update; - -($cd) = DBICTest::CD->search( year => $now->year ); -is( $cd->year->year, $now->year, 'deflate ok' ); - -# re-test using alternate deflate syntax -DBICTest::CD->has_a( 'year', 'DateTime', - inflate => sub { DateTime->new( year => shift ) }, - deflate => 'year' -); - -# inflation test -$cd = DBICTest::CD->find(3); - -is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' ); - -is( $cd->year->month, 1, 'inflated month ok' ); - -# deflate test -$now = DateTime->now; -$cd->year( $now ); -$cd->update; - -($cd) = DBICTest::CD->search( year => $now->year ); -is( $cd->year->year, $now->year, 'deflate ok' ); - diff --git a/t/09update.t b/t/09update.t deleted file mode 100644 index 3b4dbe4..0000000 --- a/t/09update.t +++ /dev/null @@ -1,26 +0,0 @@ -use strict; -use Test::More; - -BEGIN { - eval "use DBD::SQLite"; - plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 4); -} - -use lib qw(t/lib); - -use_ok('DBICTest'); - -my $art = DBICTest::Artist->find(1); - -isa_ok $art => 'DBICTest::Artist'; - -my $name = 'Caterwauler McCrae'; - -ok($art->name($name) eq $name, 'update'); - -{ - my @changed_keys = $art->is_changed; - is( scalar (@changed_keys), 0, 'field changed but same value' ); -} - -$art->discard_changes; diff --git a/t/10auto.t b/t/10auto.t deleted file mode 100644 index 79d32dc..0000000 --- a/t/10auto.t +++ /dev/null @@ -1,14 +0,0 @@ -use Test::More; - -plan tests => 2; - -use lib qw(t/lib); - -use_ok('DBICTest'); - -DBICTest::Artist->load_components(qw/PK::Auto::SQLite/); - -# add an artist without primary key to test Auto -my $artist = DBICTest::Artist->create( { name => 'Auto' } ); -$artist->name( 'Auto Change' ); -ok($artist->update, 'update on object created without PK ok'); diff --git a/t/11mysql.t b/t/11mysql.t deleted file mode 100644 index 3ec842c..0000000 --- a/t/11mysql.t +++ /dev/null @@ -1,49 +0,0 @@ -use lib qw(lib t/lib); -use DBICTest::Schema; - -use Test::More; - -my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}; - -#warn "$dsn $user $pass"; - -plan skip_all, 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test' - unless ($dsn && $user); - -plan tests => 4; - -DBICTest::Schema->compose_connection('MySQLTest' => $dsn, $user, $pass); - -my $dbh = MySQLTest::Artist->storage->dbh; - -$dbh->do("DROP TABLE IF EXISTS artist;"); - -$dbh->do("CREATE TABLE artist (artistid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255));"); - -#'dbi:mysql:host=localhost;database=dbic_test', 'dbic_test', ''); - -MySQLTest::Artist->load_components('PK::Auto::MySQL'); - -# test primary key handling -my $new = MySQLTest::Artist->create({ name => 'foo' }); -ok($new->artistid, "Auto-PK worked"); - -# test LIMIT support -for (1..6) { - MySQLTest::Artist->create({ name => 'Artist ' . $_ }); -} -my $it = MySQLTest::Artist->search( {}, - { rows => 3, - offset => 2, - order_by => 'artistid' } -); -is( $it->count, 3, "LIMIT count ok" ); -is( $it->next->name, "Artist 2", "iterator->next ok" ); -$it->next; -$it->next; -is( $it->next, undef, "next past end of resultset ok" ); - -# clean up our mess -$dbh->do("DROP TABLE artist"); - -1; diff --git a/t/12pg.t b/t/12pg.t deleted file mode 100644 index 7181015..0000000 --- a/t/12pg.t +++ /dev/null @@ -1,31 +0,0 @@ -use lib qw(lib t/lib); -use DBICTest::Schema; - -use Test::More; - -my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/}; - -#warn "$dsn $user $pass"; - -plan skip_all, 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test' - unless ($dsn && $user); - -plan tests => 1; - -DBICTest::Schema->compose_connection('PgTest' => $dsn, $user, $pass); - -my $dbh = PgTest::Artist->storage->dbh; - -eval { - $dbh->do("DROP TABLE artist;"); -}; - -$dbh->do("CREATE TABLE artist (artistid serial PRIMARY KEY, name VARCHAR(255));"); - -PgTest::Artist->load_components('PK::Auto::Pg'); - -my $new = PgTest::Artist->create({ name => 'foo' }); - -ok($new->artistid, "Auto-PK worked"); - -1; diff --git a/t/13oracle.t b/t/13oracle.t deleted file mode 100644 index a327099..0000000 --- a/t/13oracle.t +++ /dev/null @@ -1,63 +0,0 @@ -use lib qw(lib t/lib); -use DBICTest::Schema; - -use Test::More; - -my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/}; - -plan skip_all, 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' . - 'Warning: This test drops and creates a table called \'artist\'' - unless ($dsn && $user && $pass); - -plan tests => 4; - -DBICTest::Schema->compose_connection('OraTest' => $dsn, $user, $pass); - -my $dbh = OraTest::Artist->storage->dbh; - -eval { - $dbh->do("DROP SEQUENCE artist_seq"); - $dbh->do("DROP TABLE artist"); -}; -$dbh->do("CREATE SEQUENCE artist_seq START WITH 1 MAXVALUE 999999 MINVALUE 0"); -$dbh->do("CREATE TABLE artist (artistid NUMBER(12), name VARCHAR(255))"); -$dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))"); -$dbh->do(qq{ - CREATE OR REPLACE TRIGGER artist_insert_trg - BEFORE INSERT ON artist - FOR EACH ROW - BEGIN - IF :new.artistid IS NULL THEN - SELECT artist_seq.nextval - INTO :new.artistid - FROM DUAL; - END IF; - END; -}); - -OraTest::Artist->load_components('PK::Auto::Oracle'); - -# test primary key handling -my $new = OraTest::Artist->create({ name => 'foo' }); -ok($new->artistid, "Oracle Auto-PK worked"); - -# test LIMIT support -for (1..6) { - OraTest::Artist->create({ name => 'Artist ' . $_ }); -} -my $it = OraTest::Artist->search( {}, - { rows => 3, - offset => 2, - order_by => 'artistid' } -); -is( $it->count, 3, "LIMIT count ok" ); -is( $it->next->name, "Artist 2", "iterator->next ok" ); -$it->next; -$it->next; -is( $it->next, undef, "next past end of resultset ok" ); - -# clean up our mess -$dbh->do("DROP SEQUENCE artist_seq"); -$dbh->do("DROP TABLE artist"); - -1; diff --git a/t/14mssql.t b/t/14mssql.t deleted file mode 100644 index a164c2f..0000000 --- a/t/14mssql.t +++ /dev/null @@ -1,48 +0,0 @@ -use lib qw(lib t/lib); -use DBICTest::Schema; - -use Test::More; - -my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/}; - -#warn "$dsn $user $pass"; - -plan skip_all, 'Set $ENV{DBICTEST_MSSQL_DSN}, _USER and _PASS to run this test' - unless ($dsn); - -plan tests => 4; - -DBICTest::Schema->compose_connection( 'MSSQLTest' => $dsn, $user, $pass ); - -my $dbh = MSSQLTest::Artist->storage->dbh; - -$dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL - DROP TABLE artist"); - -$dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(255));"); - -MSSQLTest::Artist->load_components('PK::Auto::MSSQL'); - -# Test PK -my $new = MSSQLTest::Artist->create( { name => 'foo' } ); -ok($new->artistid, "Auto-PK worked"); - -# Test LIMIT -for (1..6) { - MSSQLTest::Artist->create( { name => 'Artist ' . $_ } ); -} - -my $it = MSSQLTest::Artist->search( { }, - { rows => 3, - offset => 2, - order_by => 'artistid' - } -); - -is( $it->count, 3, "LIMIT count ok" ); -is( $it->next->name, "Artist 2", "iterator->next ok" ); -$it->next; -$it->next; -is( $it->next, undef, "next past end of resultset ok" ); - -1; diff --git a/t/15limit.t b/t/15limit.t deleted file mode 100644 index 98fca7a..0000000 --- a/t/15limit.t +++ /dev/null @@ -1,67 +0,0 @@ -use strict; -use Test::More; - -BEGIN { - eval "use DBD::SQLite"; - plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 10); -} - -use lib qw(t/lib); - -use_ok('DBICTest'); - -# test LIMIT -my $it = DBICTest::CD->search( {}, - { rows => 3, - order_by => 'title' } -); -is( $it->count, 3, "count ok" ); -is( $it->next->title, "Caterwaulin' Blues", "iterator->next ok" ); -$it->next; -$it->next; -is( $it->next, undef, "next past end of resultset ok" ); - -# test OFFSET -my @cds = DBICTest::CD->search( {}, - { rows => 2, - offset => 2, - order_by => 'year' } -); -is( $cds[0]->title, "Spoonful of bees", "offset ok" ); - -# test software-based limiting -$it = DBICTest::CD->search( {}, - { rows => 3, - software_limit => 1, - order_by => 'title' } -); -is( $it->count, 3, "software limit count ok" ); -is( $it->next->title, "Caterwaulin' Blues", "software iterator->next ok" ); -$it->next; -$it->next; -is( $it->next, undef, "software next past end of resultset ok" ); - -@cds = DBICTest::CD->search( {}, - { rows => 2, - offset => 2, - software_limit => 1, - order_by => 'year' } -); -is( $cds[0]->title, "Spoonful of bees", "software offset ok" ); - -# based on a failing criteria submitted by waswas -# requires SQL::Abstract >= 1.20 -$it = DBICTest::CD->search( - { title => [ - -and => - { - -like => '%bees' - }, - { - -not_like => 'Forkful%' - } - ] - }, - { rows => 5 } -); -is( $it->count, 1, "complex abstract count ok" ); diff --git a/t/16joins.t b/t/16joins.t deleted file mode 100644 index 1e27cea..0000000 --- a/t/16joins.t +++ /dev/null @@ -1,155 +0,0 @@ -use strict; -use Test::More; -use IO::File; - -BEGIN { - eval "use DBD::SQLite"; - plan $@ - ? ( skip_all => 'needs DBD::SQLite for testing' ) - : ( tests => 22 ); -} - -use lib qw(t/lib); - -use_ok('DBICTest'); - -# test the abstract join => SQL generator -my $sa = new DBIC::SQL::Abstract; - -my @j = ( - { child => 'person' }, - [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ], - [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ], -); -my $match = 'person child JOIN person father ON ( father.person_id = ' - . 'child.father_id ) JOIN person mother ON ( mother.person_id ' - . '= child.mother_id )' - ; -is( $sa->_recurse_from(@j), $match, 'join 1 ok' ); - -my @j2 = ( - { mother => 'person' }, - [ [ { child => 'person' }, - [ { father => 'person' }, - { 'father.person_id' => 'child.father_id' } - ] - ], - { 'mother.person_id' => 'child.mother_id' } - ], -); -$match = 'person mother JOIN (person child JOIN person father ON (' - . ' father.person_id = child.father_id )) ON ( mother.person_id = ' - . 'child.mother_id )' - ; -is( $sa->_recurse_from(@j2), $match, 'join 2 ok' ); - -my @j3 = ( - { child => 'person' }, - [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ], - [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ], -); -$match = 'person child INNER JOIN person father ON ( father.person_id = ' - . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id ' - . '= child.mother_id )' - ; - -is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok'); - -my $rs = DBICTest::CD->search( - { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, - { from => [ { 'me' => 'cd' }, - [ - { artist => 'artist' }, - { 'me.artist' => 'artist.artistid' } - ] ] } - ); - -cmp_ok( $rs->count, '==', 1, "Single record in resultset"); - -is($rs->first->title, 'Forkful of bees', 'Correct record returned'); - -$rs = DBICTest::CD->search( - { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, - { join => 'artist' }); - -cmp_ok( $rs->count, '==', 1, "Single record in resultset"); - -is($rs->first->title, 'Forkful of bees', 'Correct record returned'); - -$rs = DBICTest::CD->search( - { 'artist.name' => 'We Are Goth', - 'liner_notes.notes' => 'Kill Yourself!' }, - { join => [ qw/artist liner_notes/ ] }); - -cmp_ok( $rs->count, '==', 1, "Single record in resultset"); - -is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned'); - -$rs = DBICTest::Artist->search( - { 'liner_notes.notes' => 'Kill Yourself!' }, - { join => { 'cds' => 'liner_notes' } }); - -cmp_ok( $rs->count, '==', 1, "Single record in resultset"); - -is($rs->first->name, 'We Are Goth', 'Correct record returned'); - -DBICTest::Schema::CD->add_relationship( - artist => 'DBICTest::Schema::Artist', - { 'foreign.artistid' => 'self.artist' }, - { accessor => 'filter' }, -); - -DBICTest::Schema::CD->add_relationship( - liner_notes => 'DBICTest::Schema::LinerNotes', - { 'foreign.liner_id' => 'self.cdid' }, - { join_type => 'LEFT', accessor => 'single' }); - -$rs = DBICTest::CD->search( - { 'artist.name' => 'Caterwauler McCrae' }, - { prefetch => [ qw/artist liner_notes/ ], - order_by => 'me.cdid' }); - -cmp_ok($rs->count, '==', 3, 'Correct number of records returned'); - -# start test for prefetch SELECT count -unlink 't/var/dbic.trace' if -e 't/var/dbic.trace'; -DBI->trace(1, 't/var/dbic.trace'); - -my @cd = $rs->all; - -is($cd[0]->title, 'Spoonful of bees', 'First record returned ok'); - -ok(!exists $cd[0]->{_relationship_data}{liner_notes}, 'No prefetch for NULL LEFT JOIN'); - -is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN'); - -is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok'); - -# count the SELECTs -DBI->trace(0); -my $selects = 0; -my $trace = IO::File->new('t/var/dbic.trace', '<') - or die "Unable to read trace file"; -while (<$trace>) { - $selects++ if /SELECT/; -} -$trace->close; -unlink 't/var/dbic.trace'; -is($selects, 1, 'prefetch ran only 1 select statement'); - -my ($artist) = DBICTest::Artist->search({ 'cds.year' => 2001 }, - { order_by => 'artistid DESC', join => 'cds' }); - -is($artist->name, 'Random Boy Band', "Join search by object ok"); - -my @cds = DBICTest::CD->search({ 'liner_notes.notes' => 'Buy Merch!' }, - { join => 'liner_notes' }); - -cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have"); - -is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved"); - -my @artists = DBICTest::Artist->search({ 'tags.tag' => 'Shiny' }, - { join => { 'cds' => 'tags' } }); - -cmp_ok( @artists, '==', 2, "two-join search ok" ); diff --git a/t/17join_count.t b/t/17join_count.t deleted file mode 100644 index 063da72..0000000 --- a/t/17join_count.t +++ /dev/null @@ -1,30 +0,0 @@ -use strict; -use warnings; - -use Test::More; - -eval "use DBD::SQLite"; -plan skip_all => 'needs DBD::SQLite for testing' if $@; -plan tests => 5; - -use lib 't/lib'; - -use_ok('DBICTest'); - -cmp_ok(DBICTest::CD->count({ 'artist.name' => 'Caterwauler McCrae' }, - { join => 'artist' }), - '==', 3, 'Count by has_a ok'); - -cmp_ok(DBICTest::CD->count({ 'tags.tag' => 'Blue' }, { join => 'tags' }), - '==', 4, 'Count by has_many ok'); - -cmp_ok(DBICTest::CD->count( - { 'liner_notes.notes' => { '!=' => undef } }, - { join => 'liner_notes' }), - '==', 3, 'Count by might_have ok'); - -cmp_ok(DBICTest::CD->count( - { 'year' => { '>', 1998 }, 'tags.tag' => 'Cheesy', - 'liner_notes.notes' => { 'like' => 'Buy%' } }, - { join => [ qw/tags liner_notes/ ] } ), - '==', 2, "Mixed count ok"); diff --git a/t/18self_referencial.t b/t/18self_referencial.t deleted file mode 100644 index 65af217..0000000 --- a/t/18self_referencial.t +++ /dev/null @@ -1,38 +0,0 @@ -use Test::More; - -# this test will check to see if you can have 2 columns -# in the same class pointing at the same other class -# -# example: -# -# +---------+ +--------------+ -# | SelfRef | | SelfRefAlias | -# +---------+ 1-M +--------------+ -# | id |-------| self_ref | --+ -# | name | | alias | --+ -# +---------+ +--------------+ | -# /|\ | -# | | -# +--------------------------------+ -# -# see http://use.perl.org/~LTjake/journal/24876 for the -# issue with CDBI - -plan tests => 5; - -use lib qw( t/lib ); - -use_ok( 'DBICTest' ); - -my $item = DBICTest::SelfRef->find( 1 ); -is( $item->name, 'First', 'proper start item' ); - -my @aliases = $item->aliases; - -is( scalar @aliases, 1, 'proper number of aliases' ); - -my $orig = $aliases[ 0 ]->self_ref; -my $alias = $aliases[ 0 ]->alias; - -is( $orig->name, 'First', 'proper original' ); -is( $alias->name, 'Second', 'proper alias' ); \ No newline at end of file diff --git a/t/19quotes.t b/t/19quotes.t index a312e35..abc1283 100644 --- a/t/19quotes.t +++ b/t/19quotes.t @@ -6,13 +6,15 @@ BEGIN { eval "use DBD::SQLite"; plan $@ ? ( skip_all => 'needs DBD::SQLite for testing' ) - : ( tests => 2 ); + : ( tests => 3 ); } use lib qw(t/lib); use_ok('DBICTest'); +use_ok('DBICTest::HelperRels'); + DBICTest::_db->storage->sql_maker->{'quote_char'} = q!'!; DBICTest::_db->storage->sql_maker->{'name_sep'} = '.'; diff --git a/t/lib/DBICTest/Schema/Artist.pm b/t/lib/DBICTest/Schema/Artist.pm index 2fd5da4..dd0cfda 100644 --- a/t/lib/DBICTest/Schema/Artist.pm +++ b/t/lib/DBICTest/Schema/Artist.pm @@ -5,18 +5,5 @@ use base 'DBIx::Class::Core'; DBICTest::Schema::Artist->table('artist'); DBICTest::Schema::Artist->add_columns(qw/artistid name/); DBICTest::Schema::Artist->set_primary_key('artistid'); -DBICTest::Schema::Artist->add_relationship( - cds => 'DBICTest::Schema::CD', - { 'foreign.artist' => 'self.artistid' }, - { order_by => 'year' } -); -DBICTest::Schema::Artist->add_relationship( - twokeys => 'DBICTest::Schema::TwoKeys', - { 'foreign.artist' => 'self.artistid' } -); -DBICTest::Schema::Artist->add_relationship( - onekeys => 'DBICTest::Schema::OneKey', - { 'foreign.artist' => 'self.artistid' } -); 1; diff --git a/t/lib/DBICTest/Schema/CD.pm b/t/lib/DBICTest/Schema/CD.pm index 457f8ac..512d8a1 100644 --- a/t/lib/DBICTest/Schema/CD.pm +++ b/t/lib/DBICTest/Schema/CD.pm @@ -5,22 +5,5 @@ use base 'DBIx::Class::Core'; DBICTest::Schema::CD->table('cd'); DBICTest::Schema::CD->add_columns(qw/cdid artist title year/); DBICTest::Schema::CD->set_primary_key('cdid'); -DBICTest::Schema::CD->add_relationship( - artist => 'DBICTest::Schema::Artist', - { 'foreign.artistid' => 'self.artist' }, -); -DBICTest::Schema::CD->add_relationship( - tracks => 'DBICTest::Schema::Track', - { 'foreign.cd' => 'self.cdid' } -); -DBICTest::Schema::CD->add_relationship( - tags => 'DBICTest::Schema::Tag', - { 'foreign.cd' => 'self.cdid' } -); -#DBICTest::Schema::CD->might_have(liner_notes => 'DBICTest::Schema::LinerNotes' => qw/notes/); -DBICTest::Schema::CD->add_relationship( - liner_notes => 'DBICTest::Schema::LinerNotes', - { 'foreign.liner_id' => 'self.cdid' }, - { join_type => 'LEFT' }); 1; diff --git a/t/lib/DBICTest/Schema/SelfRef.pm b/t/lib/DBICTest/Schema/SelfRef.pm index f51d97e..8c065c2 100644 --- a/t/lib/DBICTest/Schema/SelfRef.pm +++ b/t/lib/DBICTest/Schema/SelfRef.pm @@ -5,10 +5,5 @@ use base 'DBIx::Class::Core'; __PACKAGE__->table('self_ref'); __PACKAGE__->add_columns(qw/id name/); __PACKAGE__->set_primary_key('id'); -__PACKAGE__->add_relationship( - aliases => 'DBICTest::Schema::SelfRefAlias', - { 'foreign.self_ref' => 'self.id' }, - { accessor => 'multi' } -); 1; diff --git a/t/lib/DBICTest/Schema/SelfRefAlias.pm b/t/lib/DBICTest/Schema/SelfRefAlias.pm index 785fcc6..736e418 100644 --- a/t/lib/DBICTest/Schema/SelfRefAlias.pm +++ b/t/lib/DBICTest/Schema/SelfRefAlias.pm @@ -5,16 +5,5 @@ use base 'DBIx::Class::Core'; __PACKAGE__->table('self_ref_alias'); __PACKAGE__->add_columns(qw/self_ref alias/); __PACKAGE__->set_primary_key('self_ref alias'); -__PACKAGE__->add_relationship( - self_ref => 'DBICTest::Schema::SelfRef', - { 'foreign.id' => 'self.self_ref' }, - { accessor => 'single' } - -); -__PACKAGE__->add_relationship( - alias => 'DBICTest::Schema::SelfRef', - { 'foreign.id' => 'self.alias' }, - { accessor => 'single' } -); 1; diff --git a/t/lib/DBICTest/Schema/Tag.pm b/t/lib/DBICTest/Schema/Tag.pm index 4a3c4d2..a356d8f 100644 --- a/t/lib/DBICTest/Schema/Tag.pm +++ b/t/lib/DBICTest/Schema/Tag.pm @@ -5,9 +5,5 @@ use base qw/DBIx::Class::Core/; DBICTest::Schema::Tag->table('tags'); DBICTest::Schema::Tag->add_columns(qw/tagid cd tag/); DBICTest::Schema::Tag->set_primary_key('tagid'); -DBICTest::Schema::Tag->add_relationship( - cd => 'DBICTest::Schema::CD', - { 'foreign.cdid' => 'self.cd' } -); 1; diff --git a/t/lib/DBICTest/Schema/Track.pm b/t/lib/DBICTest/Schema/Track.pm index 97cb6f4..2e6fa7f 100644 --- a/t/lib/DBICTest/Schema/Track.pm +++ b/t/lib/DBICTest/Schema/Track.pm @@ -5,9 +5,5 @@ use base 'DBIx::Class::Core'; DBICTest::Schema::Track->table('track'); DBICTest::Schema::Track->add_columns(qw/trackid cd position title/); DBICTest::Schema::Track->set_primary_key('trackid'); -DBICTest::Schema::Track->add_relationship( - cd => 'DBICTest::Schema::CD', - { 'foreign.cdid' => 'self.cd' } -); 1; diff --git a/t/lib/DBICTest/Schema/TwoKeys.pm b/t/lib/DBICTest/Schema/TwoKeys.pm index f7442c1..ea6fa92 100755 --- a/t/lib/DBICTest/Schema/TwoKeys.pm +++ b/t/lib/DBICTest/Schema/TwoKeys.pm @@ -5,13 +5,5 @@ use base 'DBIx::Class::Core'; DBICTest::Schema::TwoKeys->table('twokeys'); DBICTest::Schema::TwoKeys->add_columns(qw/artist cd/); DBICTest::Schema::TwoKeys->set_primary_key(qw/artist cd/); -DBICTest::Schema::TwoKeys->add_relationship( - artist => 'DBICTest::Schema::Artist', - { 'foreign.artistid' => 'self.artist' } -); -DBICTest::Schema::TwoKeys->add_relationship( - cd => 'DBICTest::Schema::CD', - { 'foreign.cdid' => 'self.cd' } -); 1;