Commit | Line | Data |
83eef562 |
1 | use DBIx::Class::Optional::Dependencies -skip_all_without => 'cdbicompat'; |
2 | |
95a70f01 |
3 | use strict; |
4a233f30 |
4 | use warnings; |
83eef562 |
5 | |
95a70f01 |
6 | use Test::More; |
7 | |
50891152 |
8 | use lib 't/cdbi/testlib'; |
95a70f01 |
9 | use Film; |
10 | |
223b8fe3 |
11 | my $it_class = "DBIx::Class::ResultSet"; |
95a70f01 |
12 | |
13 | my @film = ( |
6a3bf251 |
14 | Film->create({ Title => 'Film 1' }), |
15 | Film->create({ Title => 'Film 2' }), |
16 | Film->create({ Title => 'Film 3' }), |
17 | Film->create({ Title => 'Film 4' }), |
18 | Film->create({ Title => 'Film 5' }), |
19 | Film->create({ Title => 'Film 6' }), |
95a70f01 |
20 | ); |
21 | |
22 | { |
6a3bf251 |
23 | my $it1 = Film->retrieve_all; |
24 | isa_ok $it1, $it_class; |
95a70f01 |
25 | |
6a3bf251 |
26 | my $it2 = Film->retrieve_all; |
27 | isa_ok $it2, $it_class; |
95a70f01 |
28 | |
6a3bf251 |
29 | while (my $from1 = $it1->next) { |
30 | my $from2 = $it2->next; |
31 | is $from1->id, $from2->id, "Both iterators get $from1"; |
32 | } |
95a70f01 |
33 | } |
34 | |
35 | { |
6a3bf251 |
36 | my $it = Film->retrieve_all; |
37 | is $it->first->title, "Film 1", "Film 1 first"; |
38 | is $it->next->title, "Film 2", "Film 2 next"; |
39 | is $it->first->title, "Film 1", "First goes back to 1"; |
40 | is $it->next->title, "Film 2", "With 2 still next"; |
41 | $it->reset; |
42 | is $it->next->title, "Film 1", "Reset brings us to film 1 again"; |
43 | is $it->next->title, "Film 2", "And 2 is still next"; |
95a70f01 |
44 | } |
45 | |
95a70f01 |
46 | |
47 | { |
6a3bf251 |
48 | my $it = Film->retrieve_all; |
49 | my @slice = $it->slice(2,4); |
50 | is @slice, 3, "correct slice size (array)"; |
51 | is $slice[0]->title, "Film 3", "Film 3 first"; |
52 | is $slice[2]->title, "Film 5", "Film 5 last"; |
95a70f01 |
53 | } |
54 | |
55 | { |
6a3bf251 |
56 | my $it = Film->retrieve_all; |
57 | my $slice = $it->slice(2,4); |
58 | isa_ok $slice, $it_class, "slice as iterator"; |
59 | is $slice->count, 3,"correct slice size (array)"; |
60 | is $slice->first->title, "Film 3", "Film 3 first"; |
61 | is $slice->next->title, "Film 4", "Film 4 next"; |
62 | is $slice->first->title, "Film 3", "First goes back to 3"; |
63 | is $slice->next->title, "Film 4", "With 4 still next"; |
64 | $slice->reset; |
65 | is $slice->next->title, "Film 3", "Reset brings us to film 3 again"; |
66 | is $slice->next->title, "Film 4", "And 4 is still next"; |
95a70f01 |
67 | |
6a3bf251 |
68 | # check if the original iterator still works |
69 | is $it->count, 6, "back to the original iterator, is of right size"; |
70 | is $it->first->title, "Film 1", "Film 1 first"; |
71 | is $it->next->title, "Film 2", "Film 2 next"; |
72 | is $it->first->title, "Film 1", "First goes back to 1"; |
73 | is $it->next->title, "Film 2", "With 2 still next"; |
74 | is $it->next->title, "Film 3", "Film 3 is still in original Iterator"; |
75 | $it->reset; |
76 | is $it->next->title, "Film 1", "Reset brings us to film 1 again"; |
77 | is $it->next->title, "Film 2", "And 2 is still next"; |
95a70f01 |
78 | } |
79 | |
e60dc79f |
80 | { |
81 | my $it = Film->retrieve_all; |
82 | is $it, $it->count, "iterator returns count as a scalar"; |
83 | ok $it, "iterator returns true when there are results"; |
84 | } |
85 | |
86 | { |
87 | my $it = Film->search( Title => "something which does not exist" ); |
88 | is $it, 0; |
89 | ok !$it, "iterator returns false when no results"; |
90 | } |
d9bd5195 |
91 | |
92 | done_testing; |