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