Merge branch 'master' into topic/constructor_rewrite
[dbsrgits/DBIx-Class.git] / t / resultset / is_ordered.t
1 use strict;
2 use warnings;
3
4 use lib qw(t/lib);
5 use Test::More;
6 use DBICTest;
7
8 my $schema = DBICTest->init_schema();
9 my $rs = $schema->resultset('Artist');
10
11 ok !$rs->is_ordered, 'vanilla resultset is not ordered';
12
13 # Simple ordering with a single column
14 {
15   my $ordered = $rs->search(undef, { order_by => 'artistid' });
16   ok $ordered->is_ordered, 'Simple column ordering detected by is_ordered';
17 }
18
19 # Hashref order direction
20 {
21   my $ordered = $rs->search(undef, { order_by => { -desc => 'artistid' } });
22   ok $ordered->is_ordered, 'resultset with order direction is_ordered';
23 }
24
25 # Column ordering with literal SQL
26 {
27   my $ordered = $rs->search(undef, { order_by => \'artistid DESC' });
28   ok $ordered->is_ordered, 'resultset with literal SQL is_ordered';
29 }
30
31 # Multiple column ordering
32 {
33   my $ordered = $rs->search(undef, { order_by => ['artistid', 'name'] });
34   ok $ordered->is_ordered, 'ordering with multiple columns as arrayref is ordered';
35 }
36
37 # More complicated ordering
38 {
39   my $ordered = $rs->search(undef, {
40     order_by => [
41       { -asc => 'artistid' },
42       { -desc => 'name' },
43     ]
44   });
45   ok $ordered->is_ordered, 'more complicated resultset ordering is_ordered';
46 }
47
48 # Empty multi-column ordering arrayref
49 {
50   my $ordered = $rs->search(undef, { order_by => [] });
51   ok !$ordered->is_ordered, 'ordering with empty arrayref is not ordered';
52 }
53
54 # Multi-column ordering syntax with empty hashref
55 {
56   my $ordered = $rs->search(undef, { order_by => [{}] });
57   ok !$ordered->is_ordered, 'ordering with [{}] is not ordered';
58 }
59
60 # Remove ordering after being set
61 {
62   my $ordered = $rs->search(undef, { order_by => 'artistid' });
63   ok $ordered->is_ordered, 'resultset with ordering applied works..';
64   my $unordered = $ordered->search(undef, { order_by => undef });
65   ok !$unordered->is_ordered, '..and is not ordered with ordering removed';
66 }
67
68 # Search without ordering
69 {
70   my $ordered = $rs->search({ name => 'We Are Goth' }, { join => 'cds' });
71   ok !$ordered->is_ordered, 'WHERE clause but no order_by is not ordered';
72 }
73
74 # Other functions without ordering
75 {
76   # Join
77   my $joined = $rs->search(undef, { join => 'cds' });
78   ok !$joined->is_ordered, 'join but no order_by is not ordered';
79
80   # Group By
81   my $grouped = $rs->search(undef, { group_by => 'rank' });
82   ok !$grouped->is_ordered, 'group_by but no order_by is not ordered';
83
84   # Paging
85   my $paged = $rs->search(undef, { page=> 5 });
86   ok !$paged->is_ordered, 'paging but no order_by is not ordered';
87 }
88
89 done_testing;