Changeset 2584

Show
Ignore:
Timestamp:
06/16/08 21:25:31 (20 months ago)
Author:
mpaschal
Message:

Move basic searching tests into a test class
Move is_object(), are_object(), and table resetting code into MT::Test routines
Export is_object and are_object from MT::Test for Test::More-like behavior
BugzID: 79953

Location:
branches/release-40/t
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/release-40/t/driver-tests.pl

    r2574 r2584  
    2424 
    2525use Test::More; 
    26 use Test::Deep; 
    2726use lib 't/lib'; 
    2827 
     
    198197 
    199198sub clean_db : Test(teardown) { 
    200     for my $class (qw( Foo Bar )) { 
    201         my $driver    = $class->dbi_driver; 
    202         my $dbh       = $driver->rw_handle; 
    203         my $ddl_class = $driver->dbd->ddl_class; 
    204          
    205         $dbh->do($ddl_class->drop_table_sql($class)) or die $dbh->errstr; 
    206         $dbh->do($ddl_class->create_table_sql($class)) or die $dbh->errstr; 
    207         $dbh->do($_) or die $dbh->errstr for $ddl_class->index_table_sql($class); 
    208         $ddl_class->create_sequence($class);  # may do nothing 
    209     } 
    210 } 
     199    MT::Test->reset_table_for(qw( Foo Bar )); 
     200} 
     201 
     202 
     203package Test::Search; 
     204use MT::Test; 
     205use base qw( Test::Class MT::Test ); 
     206 
     207sub make_foos : Test(setup) { 
     208    MT::Test->reset_table_for('Foo'); 
     209 
     210    my $foo = Foo->new; 
     211    $foo->set_values({ 
     212        name => 'foo', 
     213        status => 2, 
     214        text => 'bar', 
     215    }); 
     216    $foo->save or die "Could not save test Foo: ", $foo->errstr, "\n"; 
     217} 
     218 
     219sub basic : Tests(5) { 
     220    my $foo = Foo->load(1);  # not a search 
     221     
     222    is_object(scalar Foo->load({ id => 1 }), $foo, 'Foo #1 by id hash is Foo #1'); 
     223    is_object(scalar Foo->load({ id => 1, name => 'foo' }), $foo, 'Foo #1 by id-name hash is Foo #1'); 
     224    is_object(scalar Foo->load({ name => 'foo' }), $foo, 'Foo #1 by name hash is Foo #1'); 
     225    is_object(scalar Foo->load({ created_on => $foo->created_on }), $foo, 'Foo #1 by created_on hash is Foo #1'); 
     226    is_object(scalar Foo->load({ status => 2 }), $foo, 'Foo #1 by status hash is Foo #1'); 
     227} 
     228 
     229sub unmake_foos : Test(teardown) { 
     230    MT::Test->reset_table_for('Foo'); 
     231} 
     232 
    211233 
    212234package main; 
    213  
    214 Test::GroupBy->runtests( +152 ); 
     235use MT::Test; 
     236 
     237Test::Class->runtests(qw( Test::GroupBy Test::Search +147 )); 
    215238 
    216239my($foo, @foo, @bar); 
     
    238261is($foo->column('id'), 1, 'First Foo was given an id of 1, says column()'); 
    239262 
    240 sub _is_object { 
    241     my ($got, $expected, $name) = @_; 
    242  
    243     if (!defined $got) { 
    244         fail($name); 
    245         diag('    got undef, not an object'); 
    246         return; 
    247     } 
    248  
    249     if (!$got->isa(ref $expected)) { 
    250         fail($name); 
    251         diag('    got a ', ref($got), ' but expected a ', ref $expected); 
    252         return; 
    253     } 
    254  
    255     if ($got == $expected) { 
    256         fail($name); 
    257         diag('    got the exact same instance as expected, when really expected a different but equivalent object'); 
    258         return; 
    259     } 
    260  
    261     # Ignore object columns that have undefined values. 
    262     my (%got_values, %expected_values); 
    263     while (my ($field, $value) = each %{ $got->{column_values} }) { 
    264         $got_values{$field} = $value if defined $value; 
    265     } 
    266     while (my ($field, $value) = each %{ $expected->{column_values} }) { 
    267         $expected_values{$field} = $value if defined $value; 
    268     } 
    269  
    270     if (!eq_deeply(\%got_values, \%expected_values)) { 
    271         # 'Test' again so the helpful failure diagnostics are output. 
    272         is_deeply(\%got_values, \%expected_values, $name); 
    273         return; 
    274     } 
    275  
    276     return 1; 
    277 } 
    278  
    279 sub is_object { 
    280     my ($got, $expected, $name) = @_; 
    281     pass($name) if _is_object(@_); 
    282 } 
    283  
    284 sub are_objects { 
    285     my ($got, $expected, $name) = @_; 
    286  
    287     my $count = scalar @$expected; 
    288     if ($count != scalar @$got) { 
    289         fail($name); 
    290         diag('    got ', scalar(@$got), ' objects but expected ', $count); 
    291         return; 
    292     } 
    293  
    294     for my $i (0..$count-1) { 
    295         return if !_is_object($$got[$i], $$expected[$i], "$name (#$i)"); 
    296     } 
    297     pass($name); 
    298 } 
    299  
    300263is_object(scalar Foo->load(1), $foo, 'Foo #1 by id is Foo #1'); 
    301 is_object(scalar Foo->load({ id => 1 }), $foo, 'Foo #1 by id hash is Foo #1'); 
    302 is_object(scalar Foo->load({ id => 1, name => 'foo' }), $foo, 'Foo #1 by id-name hash is Foo #1'); 
    303 is_object(scalar Foo->load({ name => 'foo' }), $foo, 'Foo #1 by name hash is Foo #1'); 
    304 is_object(scalar Foo->load({ created_on => $foo->created_on }), $foo, 'Foo #1 by created_on hash is Foo #1'); 
    305 is_object(scalar Foo->load({ status => 2 }), $foo, 'Foo #1 by status hash is Foo #1'); 
    306264 
    307265##     Change column value, save, try to load using old value (fail?), 
  • branches/release-40/t/lib/MT/Test.pm

    r2578 r2584  
    11package MT::Test; 
     2use base qw( Exporter ); 
     3 
     4our @EXPORT = qw( is_object are_objects ); 
    25 
    36use strict; 
     
    811 
    912use Test::More; 
     13use Test::Deep qw( eq_deeply ); 
    1014 
    1115BEGIN { 
     
    4347sub import { 
    4448    my $pkg = shift; 
     49    my @to_export; 
     50    # TODO: only use these init_* calls as calls, not as import args, now that we have real functions to export. 
    4551    foreach my $opt (@_) { 
    4652        if ($opt =~ m{ \A : (.+) \z }xms) { 
     
    4854            $pkg->$command() if $pkg->can($command); 
    4955        } 
    50     } 
     56        else { 
     57            push @to_export, $opt; 
     58        } 
     59    } 
     60    # Export requested or all exportable functions. 
     61    $pkg->export_to_level(1, @to_export || qw( :DEFAULT )); 
    5162} 
    5263 
     
    926937} 
    927938 
     939sub _is_object { 
     940    my ($got, $expected, $name) = @_; 
     941 
     942    if (!defined $got) { 
     943        fail($name); 
     944        diag('    got undef, not an object'); 
     945        return; 
     946    } 
     947 
     948    if (!$got->isa(ref $expected)) { 
     949        fail($name); 
     950        diag('    got a ', ref($got), ' but expected a ', ref $expected); 
     951        return; 
     952    } 
     953 
     954    if ($got == $expected) { 
     955        fail($name); 
     956        diag('    got the exact same instance as expected, when really expected a different but equivalent object'); 
     957        return; 
     958    } 
     959 
     960    # Ignore object columns that have undefined values. 
     961    my (%got_values, %expected_values); 
     962    while (my ($field, $value) = each %{ $got->{column_values} }) { 
     963        $got_values{$field} = $value if defined $value; 
     964    } 
     965    while (my ($field, $value) = each %{ $expected->{column_values} }) { 
     966        $expected_values{$field} = $value if defined $value; 
     967    } 
     968 
     969    if (!eq_deeply(\%got_values, \%expected_values)) { 
     970        # 'Test' again so the helpful failure diagnostics are output. 
     971        is_deeply(\%got_values, \%expected_values, $name); 
     972        return; 
     973    } 
     974 
     975    return 1; 
     976} 
     977 
     978sub is_object { 
     979    my ($got, $expected, $name) = @_; 
     980    pass($name) if _is_object(@_); 
     981} 
     982 
     983sub are_objects { 
     984    my ($got, $expected, $name) = @_; 
     985 
     986    my $count = scalar @$expected; 
     987    if ($count != scalar @$got) { 
     988        fail($name); 
     989        diag('    got ', scalar(@$got), ' objects but expected ', $count); 
     990        return; 
     991    } 
     992 
     993    for my $i (0..$count-1) { 
     994        return if !_is_object($$got[$i], $$expected[$i], "$name (#$i)"); 
     995    } 
     996    pass($name); 
     997} 
     998 
     999sub reset_table_for { 
     1000    my $self = shift; 
     1001    for my $class (@_) { 
     1002        my $driver    = $class->dbi_driver; 
     1003        my $dbh       = $driver->rw_handle; 
     1004        my $ddl_class = $driver->dbd->ddl_class; 
     1005 
     1006        $dbh->do($ddl_class->drop_table_sql($class)) or die $dbh->errstr; 
     1007        $dbh->do($ddl_class->create_table_sql($class)) or die $dbh->errstr; 
     1008        $dbh->do($_) or die $dbh->errstr for $ddl_class->index_table_sql($class); 
     1009        $ddl_class->create_sequence($class);  # may do nothing         
     1010    } 
     1011} 
     1012 
    92810131;