[Rt-commit] r5864 - in rt/branches/3.7-EXPERIMENTAL: .
lib/t/regression/shredder
ruz at bestpractical.com
ruz at bestpractical.com
Tue Sep 5 12:59:13 EDT 2006
Author: ruz
Date: Tue Sep 5 12:59:11 2006
New Revision: 5864
Added:
rt/branches/3.7-EXPERIMENTAL/lib/t/regression/shredder/03plugin_tickets.t
Modified:
rt/branches/3.7-EXPERIMENTAL/ (props changed)
rt/branches/3.7-EXPERIMENTAL/lib/RT/Shredder/Plugin/Tickets.pm
Log:
r3701 at cubic-pc: cubic | 2006-09-05 21:01:01 +0400
::Shredder::PLugin::Tickets
* add 'with_linked' option
* add 'apply_query_to_linked' option
* tests
Modified: rt/branches/3.7-EXPERIMENTAL/lib/RT/Shredder/Plugin/Tickets.pm
==============================================================================
--- rt/branches/3.7-EXPERIMENTAL/lib/RT/Shredder/Plugin/Tickets.pm (original)
+++ rt/branches/3.7-EXPERIMENTAL/lib/RT/Shredder/Plugin/Tickets.pm Tue Sep 5 12:59:11 2006
@@ -24,9 +24,19 @@
as you can easy make the same search with the C<query> option.
See examples above.
+=head2 with_linked - boolen
+
+Deletes all tickets that are linked to tickets that match L<query>.
+
+=head2 apply_query_to_linked - boolean
+
+Delete linked tickets only if those too match L<query>.
+
+See also L<with_linked>.
+
=cut
-sub SupportArgs { return $_[0]->SUPER::SupportArgs, qw(query) }
+sub SupportArgs { return $_[0]->SUPER::SupportArgs, qw(query with_linked apply_query_to_linked) }
sub TestArgs
{
@@ -38,21 +48,58 @@
$objs->{'allow_deleted_search'} = 1;
my ($status, $msg) = $objs->FromSQL( $args{'query'} );
return( 0, "Bad query argument, error: $msg" ) unless $status;
- $args{'query'} = $objs;
+ $self->{'opt'}{'objects'} = $objs;
}
+ $args{'with_linked'} = 1 if $args{'apply_query_to_linked'};
+
return $self->SUPER::TestArgs( %args );
}
sub Run
{
my $self = shift;
- my $objs = $self->{'opt'}{'query'}
+ my $objs = $self->{'opt'}{'objects'}
or return (1, undef);
- if( $self->{'opt'}{'limit'} ) {
- $objs->RowsPerPage( $self->{'opt'}{'limit'} );
- }
+
$objs->OrderByCols( { FIELD => 'id', ORDER => 'ASC' } );
- return (1, $objs);
+ unless ( $self->{'opt'}{'with_linked'} ) {
+ if( $self->{'opt'}{'limit'} ) {
+ $objs->RowsPerPage( $self->{'opt'}{'limit'} );
+ }
+ return (1, $objs);
+ }
+
+ my (@top, @linked, %seen);
+ $self->FetchNext($objs, 1);
+ while ( my $obj = $self->FetchNext( $objs ) ) {
+ next if $seen{ $obj->id }++;
+ push @linked, $self->GetLinked( Object => $obj, Seen => \%seen );
+ push @top, $obj;
+ last if $self->{'opt'}{'limit'}
+ && @top >= $self->{'opt'}{'limit'};
+ }
+ return (1, @top, @linked);
+}
+
+sub GetLinked
+{
+ my $self = shift;
+ my %arg = @_;
+ my @res = ();
+ my $query = 'Linked = '. $arg{'Object'}->id;
+ if ( $self->{'opt'}{'apply_query_to_linked'} ) {
+ $query .= " AND ( ". $self->{'opt'}{'query'} ." )";
+ }
+ my $objs = RT::Tickets->new( $RT::SystemUser );
+ $objs->{'allow_deleted_search'} = 1;
+ $objs->FromSQL( $query );
+ $self->FetchNext( $objs, 1 );
+ while ( my $linked_obj = $self->FetchNext( $objs ) ) {
+ next if $arg{'Seen'}->{ $linked_obj->id }++;
+ push @res, $self->GetLinked( %arg, Object => $linked_obj );
+ push @res, $linked_obj;
+ }
+ return @res;
}
1;
Added: rt/branches/3.7-EXPERIMENTAL/lib/t/regression/shredder/03plugin_tickets.t
==============================================================================
--- (empty file)
+++ rt/branches/3.7-EXPERIMENTAL/lib/t/regression/shredder/03plugin_tickets.t Tue Sep 5 12:59:11 2006
@@ -0,0 +1,146 @@
+#!/usr/bin/perl -w
+
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Deep;
+BEGIN { require "lib/t/regression/shredder/utils.pl"; }
+
+plan tests => 44;
+
+use_ok('RT::Shredder::Plugin::Tickets');
+{
+ my $plugin = new RT::Shredder::Plugin::Tickets;
+ isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
+
+ is(lc $plugin->Type, 'search', 'correct type');
+}
+
+init_db();
+create_savepoint('clean');
+use_ok('RT::Ticket');
+use_ok('RT::Tickets');
+
+{ # create parent and child and check functionality of 'with_linked' arg
+ my $parent = RT::Ticket->new( $RT::SystemUser );
+ my ($pid) = $parent->Create( Subject => 'parent', Queue => 1 );
+ ok( $pid, "created new ticket" );
+
+ my $child = RT::Ticket->new( $RT::SystemUser );
+ my ($cid) = $child->Create( Subject => 'child', Queue => 1, MemberOf => $pid );
+ ok( $cid, "created new ticket" );
+
+ my $plugin = new RT::Shredder::Plugin::Tickets;
+ isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
+
+ my ($status, $msg, @objs);
+ ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"' );
+ ok($status, "plugin arguments are ok") or diag "error: $msg";
+
+ ($status, @objs) = $plugin->Run;
+ ok($status, "executed plugin successfully") or diag "error: @objs";
+ @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
+ is(scalar @objs, 1, "only one object in result set");
+ is($objs[0]->id, $pid, "parent is in result set");
+
+ ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"', with_linked => 1 );
+ ok($status, "plugin arguments are ok") or diag "error: $msg";
+
+ ($status, @objs) = $plugin->Run;
+ ok($status, "executed plugin successfully") or diag "error: @objs";
+ @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
+ my %has = map { $_->id => 1 } @objs;
+ is(scalar @objs, 2, "two objects in the result set");
+ ok($has{$pid}, "parent is in the result set");
+ ok($has{$cid}, "child is in the result set");
+
+ my $shredder = shredder_new();
+ $shredder->PutObjects( Objects => \@objs );
+ $shredder->WipeoutAll;
+}
+cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");
+
+{ # create parent and child and link them reqursively to check that we don't hang
+ my $parent = RT::Ticket->new( $RT::SystemUser );
+ my ($pid) = $parent->Create( Subject => 'parent', Queue => 1 );
+ ok( $pid, "created new ticket" );
+
+ my $child = RT::Ticket->new( $RT::SystemUser );
+ my ($cid) = $child->Create( Subject => 'child', Queue => 1, MemberOf => $pid );
+ ok( $cid, "created new ticket" );
+
+ my ($status, $msg) = $child->AddLink( Target => $pid, Type => 'DependsOn' );
+ ok($status, "added reqursive link") or diag "error: $msg";
+
+ my $plugin = new RT::Shredder::Plugin::Tickets;
+ isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
+
+ my (@objs);
+ ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"' );
+ ok($status, "plugin arguments are ok") or diag "error: $msg";
+
+ ($status, @objs) = $plugin->Run;
+ ok($status, "executed plugin successfully") or diag "error: @objs";
+ @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
+ is(scalar @objs, 1, "only one object in result set");
+ is($objs[0]->id, $pid, "parent is in result set");
+
+ ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"', with_linked => 1 );
+ ok($status, "plugin arguments are ok") or diag "error: $msg";
+
+ ($status, @objs) = $plugin->Run;
+ ok($status, "executed plugin successfully") or diag "error: @objs";
+ @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
+ is(scalar @objs, 2, "two objects in the result set");
+ my %has = map { $_->id => 1 } @objs;
+ ok($has{$pid}, "parent is in the result set");
+ ok($has{$cid}, "child is in the result set");
+
+ my $shredder = shredder_new();
+ $shredder->PutObjects( Objects => \@objs );
+ $shredder->WipeoutAll;
+}
+cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");
+
+{ # create parent and child and check functionality of 'apply_query_to_linked' arg
+ my $parent = RT::Ticket->new( $RT::SystemUser );
+ my ($pid) = $parent->Create( Subject => 'parent', Queue => 1, Status => 'resolved' );
+ ok( $pid, "created new ticket" );
+
+ my $child1 = RT::Ticket->new( $RT::SystemUser );
+ my ($cid1) = $child1->Create( Subject => 'child', Queue => 1, MemberOf => $pid );
+ ok( $cid1, "created new ticket" );
+ my $child2 = RT::Ticket->new( $RT::SystemUser );
+ my ($cid2) = $child2->Create( Subject => 'child', Queue => 1, MemberOf => $pid, Status => 'resolved' );
+ ok( $cid2, "created new ticket" );
+
+ my $plugin = new RT::Shredder::Plugin::Tickets;
+ isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
+
+ my ($status, $msg) = $plugin->TestArgs( query => 'Status = "resolved"', apply_query_to_linked => 1 );
+ ok($status, "plugin arguments are ok") or diag "error: $msg";
+
+ my @objs;
+ ($status, @objs) = $plugin->Run;
+ ok($status, "executed plugin successfully") or diag "error: @objs";
+ @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
+ is(scalar @objs, 2, "two objects in the result set");
+ my %has = map { $_->id => 1 } @objs;
+ ok($has{$pid}, "parent is in the result set");
+ ok(!$has{$cid1}, "first child is in the result set");
+ ok($has{$cid2}, "second child is in the result set");
+
+ my $shredder = shredder_new();
+ $shredder->PutObjects( Objects => \@objs );
+ $shredder->WipeoutAll;
+
+ my $ticket = RT::Ticket->new( $RT::SystemUser );
+ $ticket->Load( $cid1 );
+ is($ticket->id, $cid1, 'loaded ticket');
+
+ $shredder->PutObjects( Objects => $ticket );
+ $shredder->WipeoutAll;
+}
+cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");
+
More information about the Rt-commit
mailing list