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