PObject/Test/Basic.pm

filter: Statementstmt 100% 252 / 252 Branchbran 70.0% 7 / 10 Conditioncond 33.3% 6 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 1 totaltotal 91.7% 279 / 304 CC 14 SCAR 20.5
CC
14
Cov
95%
run
14.0
BEGIN
1.0
BEGIN
1.0
CRAP 7.8
« PObject/Driver/DBI.pm ↑ Summary PObject/Template.pm »
1 package Class::PObject::Test::Basic;
2  
3 # Basic.pm,v 1.14 2005/02/20 18:05:00 sherzodr Exp
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use diagnostics;
7 1 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Class::PObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Class::PObject::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use vars ('$VERSION', '@ISA');
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 @ISA = ('Class::PObject::Test');
14 1 $VERSION = '1.03';
15  
16  
17 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 plan(tests => 204);
19 1 ▶ use_ok("Class::PObject")
Subroutine BEGIN: called CC=1 SCAR=0.0
20 }
21  
22  
23 1 ▶sub run {
Subroutine run: called CC=14 SCAR=26.4
Pod undocumented
24 1 my $self = shift;
25  
26 pobject 'PO::Author' => {
27 columns => ['id', 'name', 'url', 'email'],
28 driver => $self->{driver},
29 1 serializer => 'storable',
30 tmap => {
31 name => 'VARCHAR(40)'
32 }
33 };
34 1 ok(1);
35  
36  
37 1 pobject 'PO::Article' => {
38 columns => ['id', 'title', 'author', 'content', 'rating'],
39 #driver => $self->{driver},
40 serializer => 'storable'
41 };
42 1 ok(1);
43  
44 ####################
45 #
46 # 2. Testing pobject_init() method
47 #
48 {
49 1 package PO::Author;
50 1 ▶ use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 71 ▶ *pobject_init = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 71 $_[0]->set_datasource($self->{datasource});
53 71 ok(ref($_[0]) eq 'PO::Author')
54 1 };
55  
56 package PO::Article;
57 1 ▶ use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 5 ▶ *pobject_init = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
59 5 $_[0]->set_datasource($self->{datasource});
60 5 $_[0]->set_driver( $self->{driver} );
61 5 ok(ref($_[0]) eq 'PO::Article');
62 1 };
63 }
64  
65 #
66 # Setting datasrource on PO::Article
67 #
68 #PO::Article->set_datasource( $self->{datasource} );
69  
70 ################
71 #
72 # Testing for the values of @ISA
73 #
74 1 ok($PO::Author::ISA[0] eq "Class::PObject::Template");
75 1 ok($PO::Article::ISA[0] eq "Class::PObject::Template");
76  
77  
78 ####################
79 #
80 # 3. Creating new objects
81 #
82 1 my $author1 = new PO::Author();
83 1 ok(ref $author1);
84  
85 1 my $author2 = new PO::Author();
86 1 ok(ref $author2);
87  
88 1 my $author3 = new PO::Author(name=>"Geek", email=>'sherzodr@cpan.org');
89 1 ok(ref $author3);
90  
91  
92 1 my $article1 = new PO::Article();
93 1 ok(ref $article1);
94  
95 1 my $article2 = new PO::Article();
96 1 ok(ref $article2);
97  
98 1 my $article3 = new PO::Article();
99 1 ok(ref $article3);
100  
101  
102 ################
103 #
104 # Testing if our object is a 'Class::PObject::Template'
105 #
106 1 ▶ ok($author1->isa('Class::PObject::Template') && $author2->isa('Class::PObject::Template') &&
Condition33%2/6
$author1->isa('Class::PObject::Template') && $author2->isa('Class::PObject::Template') && $article1->isa('Class::PObject::Template')
!ll&&!rl&&r
001
$author1->isa("Class::PObject::Template") and $author2->isa("Class::PObject::Template")
!ll&&!rl&&r
001
MC/DC0%0/3
$author1->isa('Class::PObject::Template') && $author2->isa('Class::PObject::Template') && $article1->isa('Class::PObject::Template')
$author1->isa("Class::PObject::Template") $author2->isa("Class::PObject::Template") $article1->isa('Class::PObject::Template')
Truth table25%1/4
A and B && C
A= $author1->isa("Class::PObject::Template")B= $author2->isa("Class::PObject::Template")C= $article1->isa('Class::PObject::Template')
ABCresult
0XX0
10X0
1100
1111
107 $article1->isa('Class::PObject::Template'));
108  
109 ################
110 #
111 # Testing if driver of the object is of "Class::PObject::Driver::$driver":
112 #
113 1 ok($author1->__driver()->isa("Class::PObject::Driver::" . $self->{driver}));
114  
115 ################
116 #
117 # Testing if overloading is applicable
118 # to these particular objects
119 #
120 1 ok(overload::Overloaded($author1));
121 1 ok(overload::Overloaded($author2));
122 1 ok(overload::Overloaded($author3));
123 1 ok(overload::Overloaded($article1));
124 1 ok(overload::Overloaded($article2));
125 1 ok(overload::Overloaded($article3));
126  
127 ####################
128 #
129 # 4. Test columns(), __props() and __driver() methods
130 #
131 1 ok(ref($author1) eq 'PO::Author');
132 1 ok(ref($author1->columns) eq 'HASH');
133 1 ok(ref($author1->__props) eq 'HASH');
134 1 ok(ref($author1->__driver) eq 'Class::PObject::Driver::' . $self->{driver});
135  
136  
137 ####################
138 #
139 # 5. Test if accessor methods have been created successfully
140 #
141 1 ▶ ok($author1->can('id') && $author2->can('name') && $author3->can('email'));
Condition33%2/6
$author1->can('id') && $author2->can('name') && $author3->can('email')
!ll&&!rl&&r
001
$author1->can("id") and $author2->can("name")
!ll&&!rl&&r
001
MC/DC0%0/3
$author1->can('id') && $author2->can('name') && $author3->can('email')
$author1->can("id") $author2->can("name") $author3->can('email')
Truth table25%1/4
A and B && C
A= $author1->can("id")B= $author2->can("name")C= $author3->can('email')
ABCresult
0XX0
10X0
1100
1111
142 1 ▶ ok($author1->name ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$author1->name ? : 0 1 1
143 #ok(1);
144  
145  
146 1 ▶ ok($article1->can('id') && $article2->can('title') && $article3->can('author'));
Condition33%2/6
$article1->can('id') && $article2->can('title') && $article3->can('author')
!ll&&!rl&&r
001
$article1->can("id") and $article2->can("title")
!ll&&!rl&&r
001
MC/DC0%0/3
$article1->can('id') && $article2->can('title') && $article3->can('author')
$article1->can("id") $article2->can("title") $article3->can('author')
Truth table25%1/4
A and B && C
A= $article1->can("id")B= $article2->can("title")C= $article3->can('author')
ABCresult
0XX0
10X0
1100
1111
147 #exit;
148  
149 ####################
150 #
151 # 6. Checking if accessor methods function as expected
152 #
153 1 $author1->name("Sherzod Ruzmetov");
154 1 $author1->url('http://author.handalak.com/');
155 1 $author1->email('sherzodr@cpan.org');
156  
157 1 my $name = $author1->name();
158 1 ok($name->isa("Class::PObject::Type"));
159 1 ok($name eq "Sherzod Ruzmetov");
160  
161 1 ok($author1->name eq "Sherzod Ruzmetov");
162 1 ok($author1->email eq 'sherzodr@cpan.org');
163  
164 1 $author1->name(undef);
165  
166 1 ▶ ok($author1->name ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$author1->name ? : 0 1 1
167 #ok($author1->{_is_new} == 1);
168 1 ok(1);
169 #exit;
170  
171 1 $author2->name("Hilola Nasyrova");
172 1 $author2->url('http://hilola.handalak.com/');
173 1 $author2->email('some@email.com');
174  
175 1 ok($author2->name eq "Hilola Nasyrova");
176 1 ok($author2->email eq 'some@email.com');
177 1 ok($author2->{_is_new} == 1);
178  
179 1 $article1->title("Class::PObject rules!");
180 1 $article1->content("This is the article about Class::PObject and how great this library is");
181 1 $article1->rating(0);
182  
183 1 ok($article1->title eq "Class::PObject rules!");
184 1 ok($article1->rating == 0);
185  
186  
187 ####################
188 #
189 # 7. Testing save()
190 #
191 1 my $author1_id = undef;
192 1 ok($author1_id = $author1->save);
193  
194 1 my $author2_id = undef;
195 1 ok($author2_id = $author2->save);
196  
197 1 my $author3_id = undef;
198 1 ok($author3_id = $author3->save);
199  
200 1 $article1->author($author1_id);
201  
202 1 my $article1_id = undef;
203 1 print $article1->dump;
204 1 ok($article1_id = $article1->save);
205  
206 1 ok($article1_id == $article1->id);
207 1 ok($article1 == $article1_id);
208  
209 1 undef($author1);
210 1 undef($author2);
211 1 undef($article1);
212  
213 #
214 # 'undef'ining 'datasource' class attribute.
215 # Since this attribuate was defined inside pobject_init() earlier,
216 # we need to check whether will it be redefined when load() is called
217 #
218 {
219 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
220 1 ${"PO::Article::props"}->{datasource} = undef;
221 1 ${"PO::Author::props"}->{datasource} = undef;
222 }
223  
224  
225 ####################
226 #
227 # 8. Testing load() and integrity of the object
228 #
229  
230 1 $article1 = PO::Article->load($article1_id);
231 1 ok($article1);
232  
233 1 ok($article1->title eq "Class::PObject rules!");
234 1 ok(defined $article1->rating);
235 1 ok($article1->rating == 0);
236  
237 1 $author1 = PO::Author->load($article1->author);
238 1 ok($author1);
239  
240 1 ok($author1->{_is_new} == 0);
241 1 ok($author1->email eq 'sherzodr@cpan.org');
242 1 ▶ ok($author1->name ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$author1->name ? : 0 1 1
243 1 ok($author1->url eq 'http://author.handalak.com/');
244  
245 ####################
246 #
247 # 9. Checking if object properties can be updated
248 #
249 1 $author1->url('http://sherzodr.handalak.com/');
250 1 $author1->name("Sherzod Ruzmetov");
251 1 ok($author1->save);
252  
253 1 $author1 = undef;
254 1 $author1 = PO::Author->load($author1_id);
255 1 ok($author1);
256  
257 1 ok($author1->name eq "Sherzod Ruzmetov");
258 1 ok($author1->email eq "sherzodr\@cpan.org");
259 1 ok($author1->url eq 'http://sherzodr.handalak.com/');
260  
261 ####################
262 #
263 # 10. load()ing pobject in array context
264 #
265 1 my @authors = PO::Author->load();
266 1 ok(@authors == 3);
267 1 for ( @authors ) {
268 3 printf("[%d] - %s <%s>\n", $_->id, $_->name, $_->email)
269 }
270 1 @authors = undef;
271  
272  
273  
274 ################
275 #
276 # FIX: if load(0) was treated the same way as load()
277 #
278 1 my $author4 = PO::Author->load(0);
279 1 ok(!$author4);
280  
281 ####################
282 #
283 # 11. Loading object(s) in array context with terms
284 #
285 1 @authors = PO::Author->load({id=>$author1_id});
286 1 for ( @authors ) {
287 1 ok($_->name eq "Sherzod Ruzmetov");
288 1 ok($_->email eq "sherzodr\@cpan.org")
289 }
290  
291  
292 ####################
293 #
294 # 12. Checking count()
295 #
296 1 ok(PO::Author->count == 3);
297  
298 1 ok(PO::Author->count({name=>"Doesn't exist!"}) == 0);
299  
300 1 ok(PO::Author->count({email=>'sherzodr@cpan.org', name=>"Sherzod Ruzmetov"}) == 1);
301  
302 1 ok(PO::Author->count({email=>'sherzodr@cpan.org'}) == 2);
303  
304  
305 ####################
306 #
307 # 13. Checking more complex terms
308 #
309 1 @authors = PO::Author->load({email=>'sherzodr@cpan.org', name=>"Sherzod Ruzmetov"});
310 1 ok(@authors == 1);
311 1 ok($authors[0]->id == $author1_id);
312 1 ok($authors[0]->url eq 'http://sherzodr.handalak.com/', $authors[0]->url);
313  
314 1 @authors = undef;
315 1 @authors = PO::Author->load({url=>'http://hilola.handalak.com/', name=>"Bogus"});
316 1 ok(@authors == 0);
317  
318  
319 1 @authors = PO::Author->load({url=>'http://hilola.handalak.com/'});
320 1 ok(@authors == 1);
321  
322  
323 ####################
324 #
325 # 14. Checking load(undef, \%args) syntax
326 #
327 1 @authors = PO::Author->load(undef, {'sort' => 'name'});
328 1 ok(@authors == 3);
329  
330 1 ok($authors[0]->name eq "Geek");
331 1 ok($authors[1]->name eq "Hilola Nasyrova");
332 1 ok($authors[2]->name eq "Sherzod Ruzmetov");
333  
334  
335  
336  
337  
338 1 @authors = ();
339 1 @authors = PO::Author->load(undef, {'sort' => 'email'});
340 1 ok(@authors == 3);
341  
342 1 ok($authors[0]->email eq 'sherzodr@cpan.org');
343 1 ok($authors[1]->email eq 'sherzodr@cpan.org');
344  
345  
346  
347  
348  
349 # same as above, but with explicit 'direction'
350 1 @authors = ();
351 1 @authors = PO::Author->load(undef, {'sort' => 'email', direction=>'asc'});
352 1 ok(@authors == 3);
353  
354 1 ok($authors[0]->email eq 'sherzodr@cpan.org');
355 1 ok($authors[1]->email eq 'sherzodr@cpan.org');
356 1 ok($authors[2]->email eq 'some@email.com');
357  
358  
359  
360  
361  
362  
363  
364 1 @authors = ();
365 1 @authors = PO::Author->load(undef, {'sort'=>'name', direction=>'desc'});
366 1 ok(@authors == 3);
367  
368 1 ok($authors[0]->name eq "Sherzod Ruzmetov");
369 1 ok($authors[1]->name eq "Hilola Nasyrova");
370 1 ok($authors[2]->name eq "Geek");
371  
372  
373  
374  
375 1 @authors = ();
376 1 @authors = PO::Author->load(undef, {'sort'=>'id', direction=>'desc', limit=>1});
377 1 ok(@authors == 1);
378  
379 1 ok($authors[0]->name eq "Geek");
380  
381  
382  
383 # same as above, but with explicit 'offset'
384 1 @authors = ();
385 1 @authors = PO::Author->load(undef, {'sort'=>'id', direction=>'desc', limit=>1, offset=>0});
386 1 ok(@authors == 1);
387  
388 1 ok($authors[0]->name eq "Geek");
389  
390  
391  
392 1 @authors = ();
393 1 @authors = PO::Author->load(undef, {'sort'=>'id', direction=>'desc', offset=>1, limit=>1});
394 1 ok(@authors == 1);
395  
396 1 ok($authors[0]->name eq "Hilola Nasyrova");
397  
398  
399  
400 1 $author1 = PO::Author->load(undef, {'sort'=>'id', direction=>'desc', limit=>1});
401 1 ok($author1);
402 1 ok($author1->name eq "Geek");
403  
404  
405  
406 ####################
407 #
408 # 15. Checking load(\%terms, \%args) syntax
409 #
410 1 @authors = ();
411 1 @authors = PO::Author->load({name=>"Sherzod Ruzmetov"}, {'sort'=>'name'});
412 1 ok(@authors == 1);
413 1 ok($authors[0]->email eq 'sherzodr@cpan.org');
414  
415  
416  
417  
418  
419  
420  
421 1 @authors = ();
422 1 @authors = PO::Author->load({email=>'sherzodr@cpan.org'}, {'sort'=>'name'});
423 1 ok(@authors == 2);
424  
425 1 ok($authors[0]->name eq "Geek");
426 1 ok($authors[1]->name eq "Sherzod Ruzmetov");
427  
428  
429  
430  
431 1 @authors = ();
432 1 @authors = PO::Author->load({email=>'sherzodr@cpan.org'}, {'sort'=>'name', direction=>'desc'});
433 1 ok(@authors == 2);
434  
435 1 ok($authors[0]->name eq "Sherzod Ruzmetov");
436 1 ok($authors[1]->name eq "Geek");
437  
438  
439  
440  
441 1 @authors = PO::Author->load({email=>'sherzodr@cpan.org'}, {'sort'=>'name', direction=>'asc', limit=>1});
442 1 ok(@authors == 1);
443  
444 1 ok($authors[0]->name eq "Geek");
445  
446  
447  
448 1 $author3 = undef;
449 1 $author3 = PO::Author->load({email=>'sherzodr@cpan.org'}, {'sort'=>'name', direction=>'asc', limit=>1});
450 1 ok($author3);
451 1 ok($author3->name eq "Geek");
452  
453  
454 1 @authors = PO::Author->load({email=>'sherzodr@cpan.org'}, {'sort'=>'name', limit=>1, offset=>1});
455 1 ok(@authors == 1);
456  
457 1 ok($authors[0]->name eq "Sherzod Ruzmetov");
458  
459  
460 ###################
461 #
462 # Checking the iterator
463 #
464 1 my $iterator = PO::Author->fetch(undef, {'sort'=>'name'});
465 #die $iterator->dump;
466 1 ok( $iterator->size == 3);
467 #die $iterator->size;
468 1 ok(ref $iterator eq "Class::PObject::Iterator");
469 1 $author1 = $iterator->next();
470 1 ok(ref $author1 eq "PO::Author");
471 1 ok( $author1->name eq "Geek");
472  
473 1 $author2 = $iterator->next();
474 1 ok( ref $author2 eq "PO::Author");
475 1 ok( $author2->name eq "Hilola Nasyrova");
476  
477 1 ok( $iterator->size == 1);
478  
479 # trying to iterate through the list. We should
480 # have only one object left, since we already called next() two times:
481 1 ▶ while ( my $author = $iterator->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $author = $iterator->next) 1 1 2
482 1 ok( $author->name eq "Sherzod Ruzmetov" );
483 }
484  
485 # now we need to reset the pointers:
486 1 $iterator->reset();
487  
488 1 ▶ while ( my $author = $iterator->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $author = $iterator->next) 3 1 4
489 3 ok( ref $author eq "PO::Author");
490 }
491  
492  
493 ####################
494 #
495 # Cleaning up all the objects so that for the next 'make test'
496 # can start with brand new scratch board
497 #
498  
499 1 $iterator->reset();
500 1 my $author = $iterator->next;
501  
502 # removing author named 'Geek':
503 1 ok($author->remove, $author->errstr);
504  
505 1 ok(PO::Author->count == 2, "count: " . PO::Author->count);
506  
507 # again trying to remove the author 'Geek'. This should have no
508 # effect, since it's been removed in previous query
509 1 ok(PO::Author->remove_all({name=>"Geek"}));
510 # we still should have two objects in total in our database
511 1 ok(PO::Author->count == 2, "count: " . PO::Author->count);
512 1 ok(PO::Author->remove_all);
513 1 ok(PO::Author->count == 0);
514  
515 1 ok(PO::Article->remove_all);
516 1 ok(PO::Article->count == 0);
517  
518 1 ok(PO::Article->drop_datasource);
519 1 ok(PO::Author->drop_datasource);
520 }
521  
522  
523  
524  
525  
526  
527  
528  
529  
530 1 1;
531 __END__
« PObject/Driver/DBI.pm ↑ Summary PObject/Template.pm »