e46614377ea0759c4762dbaf17240d38eafbcb2f
[dbsrgits/DBIx-Class-ResultSet-WithMetaData.git] / t / custom_methods.t
1 #!perl
2
3 use Test::More;
4 use lib qw(t/lib);
5 use DBICTest;
6 use Data::Dumper;
7
8 # set up and populate schema
9 ok(my $schema = DBICTest->init_schema(), 'got schema');
10
11 {
12         my $artist_rs = $schema->resultset('Artist')->search({}, { order_by => 'artistid' })->display();
13         is_deeply($artist_rs, [
14                 {
15                         'artistid' => '1',
16                         'name' => 'Caterwauler McCrae'
17                 },
18                 {
19                         'artistid' => '2',
20                         'name' => 'Random Boy Band'
21                 },
22                 {
23                         'artistid' => '3',
24                         'name' => 'We Are Goth'
25                 }
26         ], 'ordered display returned as expected');
27 }
28
29 {
30         my $artists = $schema->resultset('Artist')->search({}, { order_by => 'artistid' })->with_substr->display();
31         is_deeply($artists, [
32                 {
33                         'artistid' => '1',
34                         'name' => 'Caterwauler McCrae',
35                         'substr' => 'Cat'
36                 },
37                 {
38                         'artistid' => '2',
39                         'name' => 'Random Boy Band',
40                         'substr' => 'Ran'
41                 },
42                 {
43                         'artistid' => '3',
44                         'name' => 'We Are Goth',
45                         'substr' => 'We '
46                 }
47         ], 'display with substring okay');
48 }
49
50 {
51         my $artists = $schema->resultset('Artist')->search({}, { order_by => 'artistid' })->with_substr->search({}, { prefetch => 'cds', rows => 1 })->display();
52         is_deeply($artists, [
53       { 
54         'artistid' => 1,
55         'cds' => [ 
56           { 
57             'cdid' => 3,
58             'artist' => 1,
59             'title' => 'Caterwaulin\' Blues',
60             'year' => '1997'
61           },
62           { 
63             'cdid' => 1,
64             'artist' => 1,
65             'title' => 'Spoonful of bees',
66             'year' => '1999'
67           },
68           { 
69             'cdid' => 2,
70             'artist' => 1,
71             'title' => 'Forkful of bees',
72             'year' => '2001'
73           }
74         ],
75         'name' => 'Caterwauler McCrae',
76         'substr' => 'Cat'
77       }
78         ], 'substring before prefetch okay');
79 }
80
81 done_testing();