From: Matt S Trout Date: Sun, 24 Nov 2013 01:23:27 +0000 (+0000) Subject: reorganise dq expr tests slightly X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=70a55c691f64fc687c059aabce68c580643217b7;p=dbsrgits%2FDBIx-Class.git reorganise dq expr tests slightly --- diff --git a/t/dq/add_relationship_expr.t b/t/dq/add_relationship_expr.t new file mode 100644 index 0000000..b6ca14b --- /dev/null +++ b/t/dq/add_relationship_expr.t @@ -0,0 +1,35 @@ +use strict; +use warnings; + +use Test::More; +use Test::Exception; +use Test::Warn; +use lib qw(t/lib); +use DBICTest::Schema::Artist; +use Data::Query::ExprDeclare; +BEGIN { + DBICTest::Schema::Artist->has_many( + cds2 => 'DBICTest::Schema::CD', + expr { $_->foreign->artist == $_->self->artistid } + ); + DBICTest::Schema::Artist->has_many( + cds2_pre2k => 'DBICTest::Schema::CD', + expr { + $_->foreign->artist == $_->self->artistid + & $_->foreign->year < 2000 + } + ); +} +use DBICTest; +use DBIC::SqlMakerTest; + +my $schema = DBICTest->init_schema(); + +my $mccrae = $schema->resultset('Artist') + ->find({ name => 'Caterwauler McCrae' }); + +is($mccrae->cds2->count, 3, 'CDs returned from expr join'); + +is($mccrae->cds2_pre2k->count, 2, 'CDs returned from expr w/cond'); + +done_testing; diff --git a/t/dq/join.t b/t/dq/join.t deleted file mode 100644 index 125a0a0..0000000 --- a/t/dq/join.t +++ /dev/null @@ -1,48 +0,0 @@ -use strict; -use warnings; - -use Test::More; -use Test::Exception; -use Test::Warn; -use lib qw(t/lib); -use DBICTest::Schema::Artist; -use Data::Query::ExprDeclare; -BEGIN { - DBICTest::Schema::Artist->has_many( - cds2 => 'DBICTest::Schema::CD', - expr { $_->foreign->artist == $_->self->artistid } - ); - DBICTest::Schema::Artist->has_many( - cds2_pre2k => 'DBICTest::Schema::CD', - expr { - $_->foreign->artist == $_->self->artistid - & $_->foreign->year < 2000 - } - ); -} -use DBICTest; -use DBIC::SqlMakerTest; - -my $schema = DBICTest->init_schema(); - -my $mccrae = $schema->resultset('Artist') - ->find({ name => 'Caterwauler McCrae' }); - -is($mccrae->cds2->count, 3, 'CDs returned from expr join'); - -is($mccrae->cds2_pre2k->count, 2, 'CDs returned from expr w/cond'); - -$schema->source($_)->resultset_class('DBIx::Class::ResultSet::WithDQMethods') - for qw(CD Tag); - -my $cds = $schema->resultset('CD') - ->where(expr { $_->artist->name eq 'Caterwauler McCrae' }); - -is($cds->count, 3, 'CDs via join injection'); - -my $tags = $schema->resultset('Tag') - ->where(expr { $_->cd->artist->name eq 'Caterwauler McCrae' }); - -is($tags->count, 5, 'Tags via two step join injection'); - -done_testing; diff --git a/t/dq/search_expr.t b/t/dq/search_expr.t new file mode 100644 index 0000000..fc33592 --- /dev/null +++ b/t/dq/search_expr.t @@ -0,0 +1,37 @@ +use strict; +use warnings; + +use Test::More; +use Test::Exception; +use Test::Warn; +use lib qw(t/lib); +use DBICTest; +use DBIC::SqlMakerTest; +use Data::Query::ExprDeclare; + +my $schema = DBICTest->init_schema(); + +my $mccrae = $schema->resultset('Artist') + ->find({ name => 'Caterwauler McCrae' }); + +my @cds = $schema->resultset('CD') + ->search(expr { $_->artist == $mccrae->artistid }); + +is(@cds, 3, 'CDs returned from expr search by artistid'); + +my @years = $schema->resultset('CD') + ->search(expr { $_->year < 2000 }) + ->get_column('year') + ->all; + +is_deeply([ sort @years ], [ 1997, 1998, 1999 ], 'Years for < search'); + +my $tag_cond = expr { $_->tag eq 'Blue' }; + +is($schema->resultset('Tag')->search($tag_cond)->count, 4, 'Simple tag cond'); + +$tag_cond &= expr { $_->cd < 4 }; + +is($schema->resultset('Tag')->search($tag_cond)->count, 3, 'Combi tag cond'); + +done_testing; diff --git a/t/dq/where.t b/t/dq/where.t index fc33592..5f8f605 100644 --- a/t/dq/where.t +++ b/t/dq/where.t @@ -5,33 +5,23 @@ use Test::More; use Test::Exception; use Test::Warn; use lib qw(t/lib); +use Data::Query::ExprDeclare; use DBICTest; use DBIC::SqlMakerTest; -use Data::Query::ExprDeclare; my $schema = DBICTest->init_schema(); -my $mccrae = $schema->resultset('Artist') - ->find({ name => 'Caterwauler McCrae' }); - -my @cds = $schema->resultset('CD') - ->search(expr { $_->artist == $mccrae->artistid }); - -is(@cds, 3, 'CDs returned from expr search by artistid'); - -my @years = $schema->resultset('CD') - ->search(expr { $_->year < 2000 }) - ->get_column('year') - ->all; - -is_deeply([ sort @years ], [ 1997, 1998, 1999 ], 'Years for < search'); +$schema->source($_)->resultset_class('DBIx::Class::ResultSet::WithDQMethods') + for qw(CD Tag); -my $tag_cond = expr { $_->tag eq 'Blue' }; +my $cds = $schema->resultset('CD') + ->where(expr { $_->artist->name eq 'Caterwauler McCrae' }); -is($schema->resultset('Tag')->search($tag_cond)->count, 4, 'Simple tag cond'); +is($cds->count, 3, 'CDs via join injection'); -$tag_cond &= expr { $_->cd < 4 }; +my $tags = $schema->resultset('Tag') + ->where(expr { $_->cd->artist->name eq 'Caterwauler McCrae' }); -is($schema->resultset('Tag')->search($tag_cond)->count, 3, 'Combi tag cond'); +is($tags->count, 5, 'Tags via two step join injection'); done_testing;