DBIx::Class::Bundled
[dbsrgits/DBIx-Class.git] / t / dq / remap.t
CommitLineData
1f005975 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use Test::Warn;
7use lib qw(t/lib);
1f005975 8use DBICTest;
9use DBIC::SqlMakerTest;
f4a8b21e 10use Data::Query::ExprDeclare;
11use Data::Query::ExprHelpers;
1f005975 12
13my $schema = DBICTest->init_schema();
14
15$schema->source($_)->resultset_class('DBIx::Class::ResultSet::WithDQMethods')
16 for qw(CD Tag);
17
18my $cds = $schema->resultset('CD');
19
0f900f81 20throws_ok {
21 $cds->_remap_identifiers(Identifier('name'))
22} qr/Invalid name on me: name/;
23
1f005975 24is_deeply(
0f900f81 25 [ $cds->_remap_identifiers(Identifier('title')) ],
26 [ Identifier('me', 'title'), [] ],
1f005975 27 'Remap column on me'
28);
29
1d3ef20f 30throws_ok {
31 $cds->_remap_identifiers(Identifier('artist'))
32} qr/Invalid name on me: artist is a relationship/;
33
1f005975 34is_deeply(
35 [ $cds->_remap_identifiers(Identifier('artist', 'name')) ],
36 [ Identifier('artist', 'name'), [ { artist => {} } ] ],
37 'Remap column on rel'
38);
39
40is_deeply(
41 [ $cds->search({}, { join => { single_track => { cd => 'artist' } } })
42 ->_remap_identifiers(Identifier('artist', 'name')) ],
43 [ Identifier('artist_2', 'name'), [ { artist => {} } ] ],
44 'Remap column on rel with re-alias'
45);
46
0f900f81 47is_deeply(
48 [ $cds->_remap_identifiers(Identifier('artist_id')) ],
49 [ Identifier('me', 'artist'), [] ],
50 'Remap column w/column name rename'
51);
52
53my $double_name = expr { $_->artist->name == $_->artist->name }->{expr};
54
55is_deeply(
56 [ $cds->_remap_identifiers($double_name) ],
57 [ $double_name, [ { artist => {} } ] ],
58 'Remap column on rel only adds rel once'
59);
60
1f005975 61done_testing;