Changeset 2153

Show
Ignore:
Timestamp:
04/29/08 22:37:19 (7 months ago)
Author:
mpaschal
Message:

Don't cache test objects Foo and Bar
Generalize is_first_foo() into is_object() tester
Use is_object() tester where we were testing only ref and id before
Use an are_objects() tester for multiple-return load() tests
Better test descriptions

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/release-36/t/driver-tests.pl

    r2144 r2153  
    3030} 
    3131 
    32 use constant TESTS => 223
     32use constant TESTS => 245
    3333 
    3434use Test::More; 
     
    8888is($foo[0]->column('id'), $foo[0]->id, 'First Foo was given an id of 1, says column()'); 
    8989 
    90 sub is_first_foo
    91     my ($obj, $name) = @_; 
    92  
    93     isa_ok($obj, 'Foo', $name); 
    94     isnt($obj, $foo[0], "$name is not the same instance as the first Foo"); 
     90sub is_object
     91    my ($got, $expected, $name) = @_; 
     92 
     93    isa_ok($got, ref $expected, $name); 
     94    isnt($got, $expected, "$name is not the same instance as the expected object"); 
    9595 
    9696    # Ignore object columns that have undefined values. 
    97     # TODO: test that the objects are unequal unless we ignore undef members 
    98     my %obj_values; 
    99     while (my ($field, $value) = each %{ $obj->{column_values} }) { 
    100         $obj_values{$field} = $value if defined $value; 
     97    my (%got_values, %expected_values); 
     98    while (my ($field, $value) = each %{ $got->{column_values} }) { 
     99        $got_values{$field} = $value if defined $value; 
    101100    } 
    102  
    103     is_deeply(\%obj_values, $foo[0]->{column_values}, "$name is equivalent to the first Foo"); 
    104 
    105  
    106 is_first_foo(scalar Foo->load(1), 'Foo #1 by id'); 
    107 is_first_foo(scalar Foo->load({ id => 1 }), 'Foo #1 by id hash'); 
    108 is_first_foo(scalar Foo->load({ id => 1, name => 'foo' }), 'Foo #1 by id-name hash'); 
    109 is_first_foo(scalar Foo->load({ name => 'foo' }), 'Foo #1 by name hash'); 
    110 is_first_foo(scalar Foo->load({ created_on => $foo[0]->created_on }), 'Foo #1 by created_on hash'); 
    111 is_first_foo(scalar Foo->load({ status => 2 }), 'Foo #1 by status hash'); 
     101    while (my ($field, $value) = each %{ $expected->{column_values} }) { 
     102        $expected_values{$field} = $value if defined $value; 
     103    } 
     104 
     105    is_deeply(\%got_values, \%expected_values, "$name is equivalent to the expected object"); 
     106
     107 
     108is_object(scalar Foo->load(1), $foo[0], 'Foo #1 by id'); 
     109is_object(scalar Foo->load({ id => 1 }), $foo[0], 'Foo #1 by id hash'); 
     110is_object(scalar Foo->load({ id => 1, name => 'foo' }), $foo[0], 'Foo #1 by id-name hash'); 
     111is_object(scalar Foo->load({ name => 'foo' }), $foo[0], 'Foo #1 by name hash'); 
     112is_object(scalar Foo->load({ created_on => $foo[0]->created_on }), $foo[0], 'Foo #1 by created_on hash'); 
     113is_object(scalar Foo->load({ status => 2 }), $foo[0], 'Foo #1 by status hash'); 
    112114 
    113115##     Change column value, save, try to load using old value (fail?), 
     
    118120ok(!$tmp, 'Foo #1 no longer loads with old status (2)'); 
    119121$tmp = Foo->load({ status => 0 }); 
    120 isa_ok($tmp, 'Foo', 'Foo #1 by new status (0)'); 
    121 is($tmp->id, 1, 'Foo #1 still has id of 1'); 
    122 is($tmp->status, 0, 'Foo #1 has its new status (0)'); 
     122is_object($tmp, $foo[0], 'Foo #1 by new status (0)'); 
    123123 
    124124## Create a new object so we can do range and last/first lookups. 
     
    137137## Load all objects via iterator 
    138138my $iter = Foo->load_iter(undef, { sort => 'created_on', direction => 'ascend' }); 
    139 isa_ok($iter, 'CODE', "load_iter() for all Foos returned an iterator"); 
     139isa_ok($iter, 'CODE', "Iterator for all Foos"); 
    140140ok($tmp = $iter->(), 'Iterator for our two Foos had one object'); 
    141 is($tmp->id, $foo[0]->id, "All Foo iterator's first Foo was Foo #1"); 
     141is_object($tmp, $foo[0], "All Foo iterator's first Foo"); 
    142142ok($tmp = $iter->(), 'Iterator for our two Foos had two objects'); 
    143 is($tmp->id, $foo[1]->id, "All Foo iterator's second Foo was Foo #2"); 
     143is_object($tmp, $foo[1], "All Foo iterator's second Foo"); 
    144144ok(!$iter->(), 'Iterator for our two Foos did not have a third object'); 
    145145 
    146146## Load all objects with status == 1 via iterator 
    147147$iter = Foo->load_iter({ status => 1 }); 
    148 isa_ok($iter, 'CODE', "load_iter() for status=1 Foos returned an iterator"); 
     148isa_ok($iter, 'CODE', "Iterator for status=1 Foos"); 
    149149ok($tmp = $iter->(), 'Iterator for our status=1 Foos had one object'); 
    150 is($tmp->id, $foo[1]->id, "Status=1 Foo iterator's first Foo was Foo #2"); 
     150is_object($tmp, $foo[1], "Status=1 Foo iterator's first Foo"); 
    151151ok(!$iter->(), "Iterator for our status=1 Foos did not have a second object"); 
    152  
    153 # TODO: didn't we already do these tests? 
    154 ## Load using ID 
    155 $tmp = Foo->load($foo[1]->id); 
    156 isa_ok($iter, 'CODE');  # TODO: useless test 
    157 is($foo[1]->id, $tmp->id, 'id'); 
    158 is($foo[1]->name, $tmp->name, 'name'); 
    159 is($foo[1]->text, $tmp->text, 'text'); 
    160  
    161 ## Load using single-column index 
    162 $tmp = Foo->load({ name => $foo[1]->name, }); 
    163 isa_ok($iter, 'CODE');  # TODO: useless test 
    164 is($foo[1]->id, $tmp->id, 'id'); 
    165152 
    166153## Load using non-existent ID (should fail) 
     
    173160    direction => 'descend', 
    174161    limit => 1 }); 
    175 is($tmp->id, 2, 'id'); 
     162is_object($tmp, $foo[1], 'Newest Foo'); 
    176163 
    177164## Load using ascending sort (oldest) 
     
    180167    direction => 'ascend', 
    181168    limit => 1 }); 
    182 is($tmp->id, 1, 'id'); 
     169is_object($tmp, $foo[0], 'Oldest Foo'); 
     170 
     171sub are_objects { 
     172    my ($got, $expected, $name) = @_; 
     173 
     174    my $count = scalar @$expected; 
     175    is(scalar @$got, $count, "$name got $count objects"); 
     176    is_object($$got[$_], $$expected[$_], "$name #$_") for (0..$count-1); 
     177
    183178 
    184179## Load using descending sort with limit = 2 
     
    187182    direction => 'descend', 
    188183    limit => 2 }); 
    189 is(@tmp, 2, 'array length 2'); 
    190 is($tmp[0]->id, 2, 'id'); 
    191 is($tmp[1]->id, 1, 'id'); 
     184are_objects(\@tmp, [ reverse @foo ], 'Two Foos newest-first load()'); 
    192185 
    193186## Load using descending sort by created_on, no limit 
     
    195188    sort => 'created_on', 
    196189    direction => 'descend' }); 
    197 is(@tmp, 2, 'array length 2'); 
    198 is($tmp[0]->id, 2, 'id'); 
    199 is($tmp[1]->id, 1, 'id'); 
     190are_objects(\@tmp, [ reverse @foo ], 'All Foos newest-first load()'); 
    200191 
    201192## Load using ascending sort by status, no limit 
    202193@tmp = Foo->load(undef, { sort => 'status', }); 
    203 is(@tmp, 2, 'array length 2'); 
    204 is($tmp[0]->id, 1, 'id'); 
    205 is($tmp[1]->id, 2, 'id'); 
     194are_objects(\@tmp, \@foo, 'All Foos lowest-status-first load()'); 
    206195 
    207196## Load using 'last' where status == 0 
     
    210199    direction => 'descend', 
    211200    limit => 1 }); 
    212 is($tmp->id, 1, 'id'); 
     201is_object($tmp, $foo[0], 'Newest status=0 Foo'); 
    213202 
    214203## Load using range search, one less than foo[1]->created_on and newer 
     
    216205    { created_on => [ $foo[1]->column('created_on')-1 ] }, 
    217206    { range => { created_on => 1 } }); 
    218 isa_ok($tmp, 'Foo'); 
    219 is($tmp->id, 2, 'id'); 
     207is_object($tmp, $foo[1], 'Foo from open-ended date range before Foo #2'); 
    220208 
    221209## Load using EXCLUSIVE range search, up through the momment $foo[1] created 
     
    224212                      $foo[1]->column('created_on') ] }, 
    225213    { range => { created_on => 1 } }); 
    226 ok(!$tmp, 'no Foo'); 
     214ok(!$tmp, "Exclusive date range load() ending at Foo #1's date found no Foos"); 
    227215 
    228216$tmp = Foo->load( 
     
    230218                      $foo[1]->column('created_on')+1 ] }, 
    231219    { range => { created_on => 1 } }); 
    232 ok(!$tmp, 'no Foo'); 
     220ok(!$tmp, "Exclusive date range load() starting at Foo #1's date found no Foos"); 
    233221 
    234222## Load using INCLUSIVE range search, up through the momment $foo[1] created 
     
    238226    { range_incl => { created_on => 1 } }); 
    239227ok($tmp, 'Loaded an object based on range_incl (ts-1 to ts)'); 
    240 is($tmp->id, 2, 'id'); 
     228is_object($tmp, $foo[1], "Foo from inclusive date-range load() ending at Foo #1's date"); 
    241229 
    242230$tmp = Foo->load( 
     
    245233    { range_incl => { created_on => 1 } }); 
    246234ok($tmp, 'Loaded an object based on range_incl (ts to ts+1)'); 
    247 is($tmp->id, 2, 'id'); 
     235is_object($tmp, $foo[1], "Foo from inclusive date-range load() starting at Foo #1's date"); 
    248236 
    249237## Check that range searches return nothing when nothing is in the range. 
    250238$tmp = Foo->load( { created_on => [ undef, '19690101000000' ] }, 
    251239                  { range => { created_on => 1 } }); 
    252 ok(!$tmp, 'no Foo'); 
     240ok(!$tmp, 'Prehistoric date range load() found no Foos'); 
    253241 
    254242## Range search, all items with created_on less than foo[1]->created_on 
     
    256244    { created_on => [ undef, $foo[1]->column('created_on')-1 ] }, 
    257245    { range => { created_on => 1 } }); 
    258 isa_ok($tmp, 'Foo'); 
    259 is($tmp->id, 1, 'id'); 
    260  
    261 ## Range search, all items with created_on >= to foo[1]->created_on 
    262 $tmp = Foo->load( 
     246is_object($tmp, $foo[0], "Foo from exclusive open-started date-range load() ending before Foo #1"); 
     247 
     248## Get count of objects 
     249is(Foo->count(), 2, 'Count of all Foos finds both'); 
     250is(Foo->count({ status => 0 }), 1, 'Count of all status=0 Foos finds all one'); 
     251my $ranged_count = Foo->count( 
    263252    { created_on => [ $foo[1]->column('created_on')-1 ] }, 
    264     { range_incl => { created_on => 1 } }); 
    265 isa_ok($tmp, 'Foo'); 
    266 is($tmp->id, 2, 'id'); 
    267  
    268 ## Get count of objects 
    269 is(Foo->count, 2, 'count1'); 
    270 is(Foo->count({ status => 0 }), 1, 'count2'); 
    271 is(Foo->count( 
    272     { created_on => [ $foo[1]->column('created_on')-1 ] }, 
    273     { range => { created_on => 1 } }), 1, 'count3'); 
     253    { range => { created_on => 1 } } 
     254); 
     255is($ranged_count, 1, 'Count of all Foos in open-ended date range starting before Foo #1 finds all one'); 
    274256 
    275257## Update status for later tests. 
  • branches/release-36/t/lib/Bar.pm

    r1098 r2153  
    2222    datasource => 'bar', 
    2323    primary_key => 'id', 
     24    cacheable => 0, 
    2425}); 
    2526 
  • branches/release-36/t/lib/Foo.pm

    r1098 r2153  
    2222    datasource => 'foo', 
    2323    primary_key => 'id', 
     24    cacheable => 0, 
    2425}); 
    2526