reverse r4290 since we -do- -not- currently want these namespaces indexed
[dbsrgits/DBIx-Class.git] / t / 99schema_roles.t
1 use strict;
2 use warnings;
3 use lib qw(t/lib);
4 use Test::More;
5
6 BEGIN {
7     eval "use Moose";
8     plan $@
9         ? ( skip_all => 'needs Moose for testing' )
10         : ( tests => 35 );
11 }
12
13 =head1 NAME
14
15 DBICNGTest::Schema::ResultSet:Person; Example Resultset
16
17 =head1 DESCRIPTION
18
19 Tests for the various Schema roles you can either use or apply
20
21 =head1 TESTS
22
23 =head2 initialize database
24
25 create a schema and setup
26
27 =cut
28
29 use_ok 'DBICNGTest::Schema';
30
31 ok my $db_file = Path::Class::File->new(qw/t var DBIxClassNG.db/)
32     => 'created a path for the test database';
33
34 unlink $db_file;
35
36 ok my $schema = DBICNGTest::Schema->connect_and_setup($db_file)
37     => 'Created a good Schema';
38
39 is ref $schema->source('Person'), 'DBIx::Class::ResultSource::Table'
40     => 'Found Expected Person Source';
41     
42 is $schema->resultset('Person')->count, 5
43     => 'Got the correct number of people';
44
45 is $schema->resultset('Gender')->count, 3
46     => 'Got the correct number of genders';
47
48
49 =head2 check query counter
50
51 Test the query counter role
52
53 =cut
54
55 use_ok 'DBIx::Class::Storage::DBI::Role::QueryCounter';
56 DBIx::Class::Storage::DBI::Role::QueryCounter->meta->apply($schema->storage);
57
58 is $schema->storage->query_count, 0
59     => 'Query Count is zero';
60     
61 is $schema->resultset('Person')->find(1)->name, 'john'
62     => 'Found John!';
63
64 is $schema->resultset('Person')->find(2)->name, 'dan'
65     => 'Found Dan!';
66
67 is $schema->storage->query_count, 2
68     => 'Query Count is two'; 
69
70
71 =head2 check at query interval 
72     
73 Test the role for associating events with a given query interval
74
75 =cut
76
77 use_ok 'DBIx::Class::Schema::Role::AtQueryInterval';
78 DBIx::Class::Schema::Role::AtQueryInterval->meta->apply($schema);
79
80 ok my $job1 = $schema->create_job(runs=>sub { 'hello'})
81     => 'Created a job';
82
83 is $job1->execute, 'hello',
84     => 'Got expected information from the job';
85
86 ok my $job2 = $schema->create_job(runs=>'job_handler_echo')
87     => 'Created a job';
88
89 is $job2->execute($schema, 'hello1'), 'hello1',
90     => 'Got expected information from the job';
91
92 ok my $interval1 = $schema->create_query_interval(every=>10)
93     => 'Created an interval';
94
95 ok $interval1->matches(10)
96     => 'correctly matched 10';
97
98 ok $interval1->matches(20)
99     => 'correctly matched 20';
100
101 ok !$interval1->matches(22)
102     => 'correctly didnt matched 22';
103
104 ok my $interval2 = $schema->create_query_interval(every=>10, offset=>2)
105     => 'Created an interval';
106
107 ok $interval2->matches(12)
108     => 'correctly matched 12';
109
110 ok $interval2->matches(22)
111     => 'correctly matched 22';
112
113 ok !$interval2->matches(25)
114     => 'correctly didnt matched 25';
115     
116 ok my $at = $schema->create_at_query_interval(interval=>$interval2, job=>$job2)
117     => 'created the at query interval object';
118     
119 is $at->execute_if_matches(32, $schema, 'hello2'), 'hello2'
120     => 'Got correct return';
121     
122 ok $schema->at_query_intervals([$at])
123     => 'added job to run at a given interval';
124
125 is_deeply [$schema->execute_jobs_at_query_interval(42, 'hello4')], ['hello4']
126     => 'got expected job return value';
127     
128 =head2 create jobs via express method
129
130 Using the express method, build a bunch of jobs
131
132 =cut
133
134 ok my @ats = $schema->create_and_add_at_query_intervals(
135
136     {every => 10} => {
137         runs => sub {10},
138     },
139     {every => 20} => {
140         runs => sub {20},
141     },
142     {every => 30} => {
143         runs => sub {30},
144     },
145     {every => 101} => [
146         {runs => sub {101.1}},
147         {runs => sub {101.2}},       
148     ],
149            
150 ) => 'created express method at query intervals';
151
152
153 is_deeply [$schema->execute_jobs_at_query_interval(10)], [10]
154     => 'Got Expected return for 10';
155
156 is_deeply [$schema->execute_jobs_at_query_interval(12, 'hello5')], ['hello5']
157     => 'Got Expected return for 12';
158        
159 is_deeply [$schema->execute_jobs_at_query_interval(20)], [10,20]
160     => 'Got Expected return for 20';
161
162 is_deeply [$schema->execute_jobs_at_query_interval(30)], [10,30]
163     => 'Got Expected return for 30';
164     
165 is_deeply [$schema->execute_jobs_at_query_interval(60)], [10,20,30]
166     => 'Got Expected return for 60';    
167      
168 is_deeply [$schema->execute_jobs_at_query_interval(101)], [101.1,101.2]
169     => 'Got Expected return for 101';
170    
171    
172
173
174
175 =head2 cleanup
176
177 Cleanup after ourselves
178
179 =cut
180
181 unlink $db_file;
182
183
184 =head1 AUTHORS
185
186 See L<DBIx::Class> for more information regarding authors.
187
188 =head1 LICENSE
189
190 You may distribute this code under the same terms as Perl itself.
191
192 =cut