Changeset 2590

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

Move tests for alias and conjunctor support into Test::Search
BugzID: 79953

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

Legend:

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

    r2585 r2590  
    206206use base qw( Test::Class MT::Test ); 
    207207 
    208 sub make_foos : Test(setup) { 
    209     MT::Test->reset_table_for('Foo'); 
    210  
    211     sleep(1); 
    212     my $foo = Foo->new; 
    213     $foo->set_values({ 
    214         name   => 'foo', 
    215         status => 2, 
    216         text   => 'bar', 
    217     }); 
    218     $foo->save or die "Could not save test Foo: ", $foo->errstr, "\n"; 
    219      
    220     sleep(3); 
    221     $foo = Foo->new; 
    222     $foo->set_values({ 
    223         name   => 'baz', 
    224         status => 1, 
    225         text   => 'quux', 
    226     }); 
    227     $foo->save or die "Could not save test Foo #2: ", $foo->errstr, "\n"; 
     208sub reset_db : Test(setup) { 
     209    MT::Test->reset_table_for(qw( Foo Bar )); 
     210} 
     211 
     212sub make_basic_data { 
     213    my $self = shift; 
     214    $self->make_objects( 
     215        { __class  => 'Foo', 
     216          __wait   => 1, 
     217          name     => 'foo', 
     218          text     => 'bar', 
     219          status   => 2,     }, 
     220        { __class  => 'Foo', 
     221          __wait   => 3, 
     222          name     => 'baz', 
     223          text     => 'quux', 
     224          status   => 1,      }, 
     225    ); 
     226} 
     227 
     228sub make_pc_data { 
     229    my $self = shift; 
     230    $self->make_objects( 
     231        { __class => 'Foo', 
     232          name    => 'Apple', 
     233          text    => 'MacBook', 
     234          status  => 11,        }, 
     235        { __class => 'Foo', 
     236          name    => 'Linux', 
     237          text    => 'Ubuntu', 
     238          status  => 12,       }, 
     239        { __class => 'Foo', 
     240          name    => 'Microsoft', 
     241          text    => 'Vista', 
     242          status  => 13,          }, 
     243        { __class => 'Foo', 
     244          name    => 'Microsoft', 
     245          text    => 'XP', 
     246          status  => 10,          }, 
     247        { __class => 'Foo', 
     248          name    => 'Apple', 
     249          text    => 'iBook', 
     250          status  => 10,      }, 
     251 
     252        { __class => 'Bar', 
     253          name    => 'Silverlight', 
     254          status  => 2, 
     255          foo_id  => 3,             }, 
     256        { __class => 'Bar', 
     257          name    => 'IronPython', 
     258          status  => 3, 
     259          foo_id  => 3,            }, 
     260        { __class => 'Bar', 
     261          name    => 'IronRuby', 
     262          status  => 0, 
     263          foo_id  => 1,          }, 
     264    ); 
    228265} 
    229266 
    230267sub basic : Tests(5) { 
     268    my $self = shift; 
     269    $self->make_basic_data(); 
     270 
    231271    my $foo = Foo->load(1);  # not a search 
    232      
     272 
    233273    is_object(scalar Foo->load({ id => 1 }), $foo, 'Foo #1 by id hash is Foo #1'); 
    234274    is_object(scalar Foo->load({ id => 1, name => 'foo' }), $foo, 'Foo #1 by id-name hash is Foo #1'); 
     
    239279 
    240280sub sorting : Tests(6) { 
     281    my $self = shift; 
     282    $self->make_basic_data(); 
     283     
    241284    my ($tmp, @tmp); 
    242285    my @foo = map { Foo->load($_) } (1..2); 
     
    282325 
    283326sub ranges : Tests(9) { 
     327    my $self = shift; 
     328    $self->make_basic_data(); 
     329 
    284330    my $tmp; 
    285331    my @foo = map { Foo->load($_) } (1..2); 
     
    331377} 
    332378 
    333 sub unmake_foos : Test(teardown) { 
    334     MT::Test->reset_table_for('Foo'); 
     379sub alias : Tests(2) { 
     380    my $self = shift; 
     381    $self->make_pc_data(); 
     382 
     383    my $vista = Foo->load(3);  # not a search 
     384 
     385    # select * from foo, bar bar1, bar bar2 
     386    # where bar1.bar_foo_id = foo_id 
     387    # and bar2.bar_foo_id = bar1.bar_foo_id 
     388    # and bar1.status = 2 
     389    # and bar2.status = 3 
     390    my @a_foos = Foo->load( 
     391        undef, 
     392        { join => [ 'Bar', undef, { foo_id => \'= foo_id', status => 2 }, 
     393            { join => [ 'Bar', undef, { foo_id => \'= bar1.bar_foo_id', status => 3 }, 
     394                { alias => 'bar2' } ], 
     395              alias => 'bar1' 
     396            } 
     397          ], 
     398          sort => 'created_on', direction => 'descend', 
     399        } 
     400    ); 
     401    are_objects(\@a_foos, [ $vista ], 'Has Bars with status=2 and status=3 (alias)'); 
     402 
     403    @a_foos = Foo->load( 
     404        undef, 
     405        { join => [ 'Bar', undef, { foo_id => \'= foo_id', status => 2 }, 
     406            { join => [ 'Bar', undef, { foo_id => \'= bar1.bar_foo_id', status => 0 }, 
     407                { alias => 'bar2' } ], 
     408              alias => 'bar1' 
     409            } 
     410          ], 
     411          sort => 'created_on', direction => 'descend', 
     412        } 
     413    ); 
     414    is_deeply(\@a_foos, [], 'No Foo has Bars with status=2 and status=0 (alias)'); 
     415}  
     416 
     417sub conjunctions : Tests(3) { 
     418    my $self = shift; 
     419    $self->make_pc_data(); 
     420 
     421    my $count = Foo->count( [{status => 10}, -or => {name => 'Apple'}] ); 
     422    # ==> select count(*) from mt_foo where foo_status = 10 or foo_name = 'Apple' 
     423    is($count, 3, '-or count'); 
     424 
     425    $count = Foo->count( [ { status => { '<=' => 20 }, name => 'Apple' }, -and_not => { status => 11 } ] ); 
     426    # ==> select count(*) from mt_foo where (foo_status <= 20 and foo_name = 'Apple') and not (foo_status = 11) 
     427    is($count, 1, '-and_not count'); 
     428 
     429    $count = Foo->count( [ 
     430        { status => 10 }, 
     431        -or => { name => 'Apple' }, 
     432        -or => { name => { like => '%nux' } }, 
     433    ] ); 
     434    # ==> select count(*) from mt_foo where (foo_status = 10) or (foo_name = 'Apple') or (foo_name like '%nux') 
     435    # (selects Apple+MacBook, Apple+iBook, Microsoft+XP, Linux+Ubuntu) 
     436    is($count, 4, '-or count, 3 clauses'); 
     437} 
     438 
     439sub clean_db : Test(teardown) { 
     440    MT::Test->reset_table_for(qw( Foo Bar )); 
    335441} 
    336442 
     
    339445use MT::Test; 
    340446 
    341 Test::Class->runtests('Test::GroupBy', 'Test::Search', +132); 
     447Test::Class->runtests('Test::GroupBy', 'Test::Search', +126); 
    342448 
    343449my($foo, @foo, @bar); 
     
    804910} 
    805911 
    806 # -or 
    807 my $newdata = Foo->new; 
    808 $newdata->status(11); 
    809 $newdata->name('Apple'); 
    810 $newdata->text('MacBook'); 
    811 $newdata->save; 
    812 $newdata = Foo->new; 
    813 $newdata->status(12); 
    814 $newdata->name('Linux'); 
    815 $newdata->text('Ubuntu'); 
    816 $newdata->save; 
    817 $newdata = Foo->new; 
    818 $newdata->status(13); 
    819 $newdata->name('Microsoft'); 
    820 $newdata->text('Vista'); 
    821 $newdata->save; 
    822 $newdata = Foo->new; 
    823 $newdata->status(10); 
    824 $newdata->name('Microsoft'); 
    825 $newdata->text('XP'); 
    826 $newdata->save; 
    827 $newdata = Foo->new; 
    828 $newdata->status(10); 
    829 $newdata->name('Apple'); 
    830 $newdata->text('iBook'); 
    831 $newdata->save; 
    832  
    833 my $count = Foo->count( [{status => 10}, -or => {name => 'Apple'}] ); 
    834 # ==> select count(*) from mt_foo where foo_status = 10 or foo_name = 'Apple' 
    835 is($count, 3, '-or count'); 
    836  
    837 $count = Foo->count( [ { status => { '<=' => 20 }, name => 'Apple' }, -and_not => { status => 11 } ] ); 
    838 # ==> select count(*) from mt_foo where (foo_status <= 20 and foo_name = 'Apple') and not (foo_status = 11) 
    839 is($count, 1, '-and_not count'); 
    840  
    841 $count = Foo->count( [ 
    842     { status => 10 }, 
    843     -or => { name => 'Apple' }, 
    844     -or => { name => { like => '%nux' } }, 
    845 ] ); 
    846 # ==> select count(*) from mt_foo where (foo_status = 10) or (foo_name = 'Apple') or (foo_name like '%nux') 
    847 # (selects Apple+MacBook, Apple+iBook, Microsoft+XP, Linux+Ubuntu) 
    848 is($count, 4, '-or count, 3 clauses'); 
    849  
    850 # alias support 
    851 my $vista = Foo->load({name=>'Microsoft', status=>13}); 
    852 my $newbar = Bar->new; 
    853 $newbar->foo_id($vista->id); 
    854 $newbar->name('Silverlight'); 
    855 $newbar->status(2); 
    856 $newbar->save; 
    857 sleep(3); 
    858 $newbar = Bar->new; 
    859 $newbar->foo_id($vista->id); 
    860 $newbar->name('IronPython'); 
    861 $newbar->status(3); 
    862 $newbar->save; 
    863 sleep(3); 
    864 my $mb = Foo->load({name=>'Apple', status=>11}); 
    865 $newbar = Bar->new; 
    866 $newbar->foo_id($mb->id); 
    867 $newbar->name('IronRuby'); 
    868 $newbar->status(0); 
    869 $newbar->save; 
    870  
    871 # select * from foo, bar bar1, bar bar2 
    872 # where bar1.bar_foo_id = foo_id 
    873 # and bar2.bar_foo_id = bar1.bar_foo_id 
    874 # and bar1.status = 2 
    875 # and bar2.status = 3 
    876 my @a_foos = Foo->load( 
    877     undef, 
    878     { join => [ 'Bar', undef, { foo_id => \'= foo_id', status => 2 }, 
    879         { join => [ 'Bar', undef, { foo_id => \'= bar1.bar_foo_id', status => 3 }, 
    880             { alias => 'bar2' } ], 
    881           alias => 'bar1' 
    882         } 
    883       ], 
    884       sort => 'created_on', direction => 'descend', 
    885     } 
    886 ); 
    887 is(scalar(@a_foos), 1, 'join the same table using alias 1'); 
    888 is($a_foos[0]->id, $vista->id, 'join the same table using alias 2'); 
    889  
    890 @a_foos = Foo->load( 
    891     undef, 
    892     { join => [ 'Bar', undef, { foo_id => \'= foo_id', status => 2 }, 
    893         { join => [ 'Bar', undef, { foo_id => \'= bar1.bar_foo_id', status => 0 }, 
    894             { alias => 'bar2' } ], 
    895           alias => 'bar1' 
    896         } 
    897       ], 
    898       sort => 'created_on', direction => 'descend', 
    899     } 
    900 ); 
    901 is(scalar(@a_foos), 0, 'join the same table using alias 3'); 
    902   
    903  
    9049121; 
  • branches/release-40/t/lib/MT/Test.pm

    r2584 r2590  
    10111011} 
    10121012 
     1013sub make_objects { 
     1014    my $self = shift; 
     1015    my @obj_data = @_; 
     1016 
     1017    for my $data (@obj_data) { 
     1018        if (my $wait = delete $data->{__wait}) { 
     1019            sleep($wait); 
     1020        } 
     1021        my $class = delete $data->{__class}; 
     1022        my $obj = $class->new; 
     1023        $obj->set_values($data); 
     1024        $obj->save() or die "Could not save test Foo: ", $obj->errstr, "\n"; 
     1025    } 
     1026} 
     1027 
    101310281;