1fd689dd0b7518978c26cc2bc4e4231cfd361139
[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_multi->display();
31         is_deeply($artists, [
32                 {
33                         'artistid' => '1',
34                         'name' => 'Caterwauler McCrae',
35                         'substr' => 'Cat',
36                         'substr2' => 'Cate'
37                 },
38                 {
39                         'artistid' => '2',
40                         'name' => 'Random Boy Band',
41                         'substr' => 'Ran',
42                         'substr2' => 'Rand'
43                 },
44                 {
45                         'artistid' => '3',
46                         'name' => 'We Are Goth',
47                         'substr' => 'We ',
48                         'substr2' => 'We A'
49                 }
50         ], 'display with substring using _with_meta_hash okay');
51 }
52
53 {
54         my $artists = $schema->resultset('Artist')->search({}, { order_by => 'artistid' })->with_substr_key->display();
55         is_deeply($artists, [
56                 {
57                         'artistid' => '1',
58                         'name' => 'Caterwauler McCrae',
59                         'substr' => 'Cat'
60                 },
61                 {
62                         'artistid' => '2',
63                         'name' => 'Random Boy Band',
64                         'substr' => 'Ran'
65                 },
66                 {
67                         'artistid' => '3',
68                         'name' => 'We Are Goth',
69                         'substr' => 'We '
70                 }
71         ], 'display with substring using _with_meta_key okay');
72 }
73
74 # {
75 #       my $artists = $schema->resultset('Artist')->search({}, { order_by => 'artistid' })->with_substr_old->display();
76 #       is_deeply($artists, [
77 #               {
78 #                       'artistid' => '1',
79 #                       'name' => 'Caterwauler McCrae',
80 #                       'substr' => 'Cat'
81 #               },
82 #               {
83 #                       'artistid' => '2',
84 #                       'name' => 'Random Boy Band',
85 #                       'substr' => 'Ran'
86 #               },
87 #               {
88 #                       'artistid' => '3',
89 #                       'name' => 'We Are Goth',
90 #                       'substr' => 'We '
91 #               }
92 #       ], 'display with substring okay');
93 # }
94
95 {
96         my $artists = $schema->resultset('Artist')->search({}, { order_by => 'artistid' })->with_substr_key->search({}, { prefetch => 'cds', rows => 1 })->display();
97         is_deeply($artists, [
98       { 
99         'artistid' => 1,
100         'cds' => [ 
101           { 
102             'cdid' => 3,
103             'artist' => 1,
104             'title' => 'Caterwaulin\' Blues',
105             'year' => '1997'
106           },
107           { 
108             'cdid' => 1,
109             'artist' => 1,
110             'title' => 'Spoonful of bees',
111             'year' => '1999'
112           },
113           { 
114             'cdid' => 2,
115             'artist' => 1,
116             'title' => 'Forkful of bees',
117             'year' => '2001'
118           }
119         ],
120         'name' => 'Caterwauler McCrae',
121         'substr' => 'Cat'
122       }
123         ], 'substring before prefetch okay');
124 }
125
126 done_testing();