added .gitignore
[dbsrgits/DBIx-Class-ResultSet-HashRef.git] / t / 10-hashref.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5
6 BEGIN {
7     eval "use DBD::SQLite ();";
8     plan skip_all => 'DBD::SQLite required to run this test' if $@;
9
10     eval "use SQL::Translator ();";
11     plan skip_all => 'SQL::Translator required to run this test' if $@;
12
13     plan( tests => 10 );
14 }
15
16 use lib 't/lib';
17 use TestSchema;
18 use File::Temp;
19
20 # setup
21 my ( undef, $db ) = File::Temp::tempfile();
22 my $schema = TestSchema->connect( "dbi:SQLite:dbname=${db}", undef, undef );
23 $schema->deploy;
24
25 my @users = qw/root toor daemon operator bin tty/;
26 my @roles = qw/admin superuser user/;
27
28 @users = $schema->populate( 'User' => [ ['login'] => ( map { [$_] } @users ) ] );
29 @roles = $schema->populate( 'Role' => [ ['name']  => ( map { [$_] } @roles ) ] );
30
31 my $u          = 1;
32 my @user_roles = ();
33 foreach my $user (@users) {
34     my $r = 0;
35     foreach my $role (@roles) {
36         next if $r >= $u;
37         push @user_roles, [ $user->id, $role->id ];
38         $r++;
39     }
40     $u++;
41     $u = 1 if $u > scalar @roles;
42 }
43
44 @user_roles = $schema->populate( 'UserRole' => [ [qw/user_id role_id/] => @user_roles ] );
45
46 {
47     my $rs = $schema->resultset('User')->search(
48         {},
49         {
50             prefetch => { user_role => [qw/role/] },
51             order_by => 'me.id ASC'
52         }
53     )->hashref_array;
54
55     is_deeply(
56         $rs,
57         [
58             {
59                 'id'        => '1',
60                 'login'     => 'root',
61                 'user_role' => [
62                     {
63                         'role' => {
64                             'id'   => '1',
65                             'name' => 'admin'
66                         },
67                         'role_id' => '1',
68                         'user_id' => '1'
69                     }
70                 ]
71             },
72             {
73                 'id'        => '2',
74                 'login'     => 'toor',
75                 'user_role' => [
76                     {
77                         'role' => {
78                             'id'   => '1',
79                             'name' => 'admin'
80                         },
81                         'role_id' => '1',
82                         'user_id' => '2'
83                     },
84                     {
85                         'role' => {
86                             'id'   => '2',
87                             'name' => 'superuser'
88                         },
89                         'role_id' => '2',
90                         'user_id' => '2'
91                     }
92                 ]
93             },
94             {
95                 'id'        => '3',
96                 'login'     => 'daemon',
97                 'user_role' => [
98                     {
99                         'role' => {
100                             'id'   => '1',
101                             'name' => 'admin'
102                         },
103                         'role_id' => '1',
104                         'user_id' => '3'
105                     },
106                     {
107                         'role' => {
108                             'id'   => '2',
109                             'name' => 'superuser'
110                         },
111                         'role_id' => '2',
112                         'user_id' => '3'
113                     },
114                     {
115                         'role' => {
116                             'id'   => '3',
117                             'name' => 'user'
118                         },
119                         'role_id' => '3',
120                         'user_id' => '3'
121                     }
122                 ]
123             },
124             {
125                 'id'        => '4',
126                 'login'     => 'operator',
127                 'user_role' => [
128                     {
129                         'role' => {
130                             'id'   => '1',
131                             'name' => 'admin'
132                         },
133                         'role_id' => '1',
134                         'user_id' => '4'
135                     }
136                 ]
137             },
138             {
139                 'id'        => '5',
140                 'login'     => 'bin',
141                 'user_role' => [
142                     {
143                         'role' => {
144                             'id'   => '1',
145                             'name' => 'admin'
146                         },
147                         'role_id' => '1',
148                         'user_id' => '5'
149                     },
150                     {
151                         'role' => {
152                             'id'   => '2',
153                             'name' => 'superuser'
154                         },
155                         'role_id' => '2',
156                         'user_id' => '5'
157                     }
158                 ]
159             },
160             {
161                 'id'        => '6',
162                 'login'     => 'tty',
163                 'user_role' => [
164                     {
165                         'role' => {
166                             'id'   => '1',
167                             'name' => 'admin'
168                         },
169                         'role_id' => '1',
170                         'user_id' => '6'
171                     },
172                     {
173                         'role' => {
174                             'id'   => '2',
175                             'name' => 'superuser'
176                         },
177                         'role_id' => '2',
178                         'user_id' => '6'
179                     },
180                     {
181                         'role' => {
182                             'id'   => '3',
183                             'name' => 'user'
184                         },
185                         'role_id' => '3',
186                         'user_id' => '6'
187                     }
188                 ]
189             }
190         ],
191         'hashref_array'
192     );
193 }
194
195 {
196     my @rs = $schema->resultset('User')->search( {}, { order_by => 'me.id ASC' } )->hashref_array;
197     is_deeply(
198         \@rs,
199         [
200             {
201                 'id'    => '1',
202                 'login' => 'root'
203             },
204             {
205                 'id'    => '2',
206                 'login' => 'toor'
207             },
208             {
209                 'id'    => '3',
210                 'login' => 'daemon'
211             },
212             {
213                 'id'    => '4',
214                 'login' => 'operator'
215             },
216             {
217                 'id'    => '5',
218                 'login' => 'bin'
219             },
220             {
221                 'id'    => '6',
222                 'login' => 'tty'
223             }
224         ]
225     );
226 }
227
228 {
229     my $rs = $schema->resultset('User')->search(
230         {},
231         {
232             prefetch => { user_role => [qw/role/] },
233             order_by => 'me.id DESC'
234         }
235     )->hashref_rs->next;
236     is_deeply(
237         $rs,
238         {
239             'id'        => '6',
240             'login'     => 'tty',
241             'user_role' => [
242                 {
243                     'role' => {
244                         'id'   => '1',
245                         'name' => 'admin'
246                     },
247                     'role_id' => '1',
248                     'user_id' => '6'
249                 },
250                 {
251                     'role' => {
252                         'id'   => '2',
253                         'name' => 'superuser'
254                     },
255                     'role_id' => '2',
256                     'user_id' => '6'
257                 },
258                 {
259                     'role' => {
260                         'id'   => '3',
261                         'name' => 'user'
262                     },
263                     'role_id' => '3',
264                     'user_id' => '6'
265                 }
266             ]
267         },
268         'hashref_rs->next'
269     );
270 }
271
272 {
273     my $expected_users = [
274         {
275             'id'    => '1',
276             'login' => 'root'
277         },
278         {
279             'id'    => '2',
280             'login' => 'toor'
281         },
282         {
283             'id'    => '3',
284             'login' => 'daemon'
285         },
286         {
287             'id'    => '4',
288             'login' => 'operator'
289         },
290         {
291             'id'    => '5',
292             'login' => 'bin'
293         },
294         {
295             'id'    => '6',
296             'login' => 'tty'
297         }
298     ];
299     my $rs = $schema->resultset('User')->search( {}, { order_by => 'me.id ASC' } )->hashref_rs;
300     while ( my $row = $rs->next ) {
301         my $user = shift(@$expected_users);
302         is_deeply( $row, $user, "hashref_rs in while loop, user: " . $user->{login} );
303     }
304 }
305
306 {
307     my $first_row = $schema->resultset('User')->search( { login => 'root' } )->hashref_first;
308     is_deeply(
309         $first_row,
310         {
311             'id'    => '1',
312             'login' => 'root'
313         },
314         "hashref_first"
315     );
316 }