From: Peter Rabbitson Date: Sun, 12 Dec 2010 07:14:10 +0000 (+0100) Subject: Untodoify tests - these are not 'nice to have', they must work X-Git-Tag: v0.08190~1^2~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=eb3bb737db34965596d2875f9b21bb4912a35160;p=dbsrgits%2FDBIx-Class.git Untodoify tests - these are not 'nice to have', they must work --- diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index 4e9408a..834555a 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -1554,26 +1554,14 @@ sub _resolve_condition { my ($self, $cond, $as, $for, $rel) = @_; if (ref $cond eq 'CODE') { - # heuristic for the actual relname - if (! defined $rel) { - if (!ref $as) { - $rel = $as; - } - elsif (!ref $for) { - $rel = $for; - } - } - - if (! defined $rel) { - $self->throw_exception ('Unable to determine relationship name for condition resolution'); - } + my $obj_rel = !!ref $for; return $cond->({ - self_alias => ref $for ? $as : $for, - foreign_alias => ref $for ? $self->related_source($rel)->resultset->current_source_alias : $as, + self_alias => $obj_rel ? $as : $for, + foreign_alias => $obj_rel ? 'me' : $as, self_resultsource => $self, - foreign_relname => $rel, - self_rowobj => ref $for ? $for : undef + foreign_relname => $rel || ($obj_rel ? $as : $for), + self_rowobj => $obj_rel ? $for : undef }); } elsif (ref $cond eq 'HASH') { diff --git a/t/relationship/custom.t b/t/relationship/custom.t index 4fb96ff..68936e3 100644 --- a/t/relationship/custom.t +++ b/t/relationship/custom.t @@ -35,50 +35,49 @@ foreach my $artwork (@artworks) { my $cds_80s_rs = $artist->cds_80s; -is_same_sql_bind($cds_80s_rs->as_query, - '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me'. - ' WHERE ( ( me.artist = ? AND ( me.year < ? AND me.year > ? ) ) ))', - [ - [ 'me.artist' => 4 ], - [ 'me.year' => 1990 ], - [ 'me.year' => 1979 ], - ]); +is_same_sql_bind( + $cds_80s_rs->as_query, + '( + SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track + FROM cd me + WHERE ( ( me.artist = ? AND ( me.year < ? AND me.year > ? ) ) ) + )', + [ + [ 'me.artist' => 21 ], + [ 'me.year' => 1990 ], + [ 'me.year' => 1979 ], + ] +); my @cds_80s = $cds_80s_rs->all; is(@cds_80s, 6, '6 80s cds found (1980 - 1985)'); map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s; -# this is the current version, enhanced version below. -my $cds_90s_rs = $artist2->cds_90s; -is_same_sql_bind($cds_90s_rs->as_query, - '(SELECT cds_90s.cdid, cds_90s.artist, cds_90s.title, cds_90s.year, cds_90s.genreid,'. - ' cds_90s.single_track FROM artist artist__row JOIN cd cds_90s ON ( cds_90s.artist = artist__row.artistid'. - ' AND ( cds_90s.year < ? AND cds_90s.year > ? ) ) WHERE ( artist__row.artistid = ? ))', - [ - [ 'cds_90s.year' => 2000 ], - [ 'cds_90s.year' => 1989 ], - [ 'artist__row.artistid' => 5 ], - ]); -TODO: { - local $TODO = 'enhanced aliasing in search_related'; - my $cds_90s_rs = $artist2->cds_90s; - is_same_sql_bind($cds_90s_rs->as_query, - '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid,'. - ' me.single_track FROM artist artist__row JOIN cd me ON ( me.artist = artist__row.artistid'. - ' AND ( me.year < ? AND me.year > ? ) ) WHERE ( artist__row.artistid = ? ))', - [ - [ 'me.year' => 2000 ], - [ 'me.year' => 1989 ], - [ 'artist__row.artistid' => 5 ], - ]); -} +my $cds_90s_rs = $artist2->cds_90s; +is_same_sql_bind( + $cds_90s_rs->as_query, + '( + SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track + FROM artist artist__row + JOIN cd me + ON ( me.artist = artist__row.artistid AND ( me.year < ? AND me.year > ? ) ) + WHERE ( artist__row.artistid = ? ) + )', + [ + [ 'me.year' => 2000 ], + [ 'me.year' => 1989 ], + [ 'artist__row.artistid' => 22 ], + ] +); my @cds_90s = $cds_90s_rs->all; is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search'); map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s; -my @cds_90s_95 = $artist2->cds_90s->search({ 'year' => 1995 }); -is(@cds_90s_95, 1, '1 90s (95) cds found even with non-optimized search'); -map { ok($_->year == 1995) } @cds_90s_95; +lives_ok { + my @cds_90s_95 = $artist2->cds_90s->search({ 'me.year' => 1995 }); + is(@cds_90s_95, 1, '1 90s (95) cds found even with non-optimized search'); + map { ok($_->year == 1995) } @cds_90s_95; +} 'should preserve chain-head "me" alias (API-consistency)'; # search for all artists prefetching published cds in the 80s... my @all_artists_with_80_cds = $schema->resultset("Artist")->search