move join inference cleverness into a role
[dbsrgits/DBIx-Class.git] / t / dq / join.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use Test::Warn;
7 use lib qw(t/lib);
8 use DBICTest::Schema::Artist;
9 use Data::Query::ExprDeclare;
10 BEGIN {
11   DBICTest::Schema::Artist->has_many(
12     cds2 => 'DBICTest::Schema::CD',
13     expr { $_->foreign->artist == $_->self->artistid }
14   );
15   DBICTest::Schema::Artist->has_many(
16     cds2_pre2k => 'DBICTest::Schema::CD',
17     expr {
18       $_->foreign->artist == $_->self->artistid
19       & $_->foreign->year < 2000
20     }
21   );
22 }
23 use DBICTest;
24 use DBIC::SqlMakerTest;
25
26 my $schema = DBICTest->init_schema();
27
28 my $mccrae = $schema->resultset('Artist')
29                     ->find({ name => 'Caterwauler McCrae' });
30
31 is($mccrae->cds2->count, 3, 'CDs returned from expr join');
32
33 is($mccrae->cds2_pre2k->count, 2, 'CDs returned from expr w/cond');
34
35 $schema->source($_)->resultset_class('DBIx::Class::ResultSet::WithDQMethods')
36   for qw(CD Tag);
37
38 my $cds = $schema->resultset('CD')
39                  ->where(expr { $_->artist->name eq 'Caterwauler McCrae' });
40
41 is($cds->count, 3, 'CDs via join injection');
42
43 my $tags = $schema->resultset('Tag')
44                   ->where(expr { $_->cd->artist->name eq 'Caterwauler McCrae' });
45
46 is($tags->count, 5, 'Tags via two step join injection');
47
48 done_testing;