d8b70ddfcfaeab7182cc11380f0c2a63404a8ad8
[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_key_obj->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 using _with_meta_key with object okay');
93 }
94
95 # {
96 #       my $artists = $schema->resultset('Artist')->search({}, { order_by => 'artistid' })->with_substr_old->display();
97 #       is_deeply($artists, [
98 #               {
99 #                       'artistid' => '1',
100 #                       'name' => 'Caterwauler McCrae',
101 #                       'substr' => 'Cat'
102 #               },
103 #               {
104 #                       'artistid' => '2',
105 #                       'name' => 'Random Boy Band',
106 #                       'substr' => 'Ran'
107 #               },
108 #               {
109 #                       'artistid' => '3',
110 #                       'name' => 'We Are Goth',
111 #                       'substr' => 'We '
112 #               }
113 #       ], 'display with substring okay');
114 # }
115
116 {
117         my $artists = $schema->resultset('Artist')->search({}, { order_by => 'artistid' })->with_substr_key->search({}, { prefetch => 'cds', rows => 1 })->display();
118         is_deeply($artists, [
119       { 
120         'artistid' => 1,
121         'cds' => [ 
122           { 
123             'cdid' => 3,
124             'artist' => 1,
125             'title' => 'Caterwaulin\' Blues',
126             'year' => '1997'
127           },
128           { 
129             'cdid' => 1,
130             'artist' => 1,
131             'title' => 'Spoonful of bees',
132             'year' => '1999'
133           },
134           { 
135             'cdid' => 2,
136             'artist' => 1,
137             'title' => 'Forkful of bees',
138             'year' => '2001'
139           }
140         ],
141         'name' => 'Caterwauler McCrae',
142         'substr' => 'Cat'
143       }
144         ], 'substring before prefetch okay');
145 }
146
147 done_testing();