Merge pull request #52 from Pear-Trading/finn/DeleteTransaction

Added transaction delete and admin transaction test
This commit is contained in:
Finn 2017-09-07 13:39:32 +01:00 committed by GitHub
commit ebbbbd865a
4 changed files with 130 additions and 24 deletions

View file

@ -189,6 +189,7 @@ sub startup {
$admin_routes->get('/transactions')->to('admin-transactions#index'); $admin_routes->get('/transactions')->to('admin-transactions#index');
$admin_routes->get('/transactions/:id')->to('admin-transactions#read'); $admin_routes->get('/transactions/:id')->to('admin-transactions#read');
$admin_routes->get('/transactions/:id/image')->to('admin-transactions#image'); $admin_routes->get('/transactions/:id/image')->to('admin-transactions#image');
$admin_routes->post('/transactions/:id/delete')->to('admin-transactions#delete');
# my $user_routes = $r->under('/')->to('root#under'); # my $user_routes = $r->under('/')->to('root#under');

View file

@ -48,4 +48,19 @@ sub image {
} }
} }
sub delete {
my $c = shift;
my $id = $c->param('id');
if ( my $transaction = $c->result_set->find($id) ) {
$transaction->delete;
$c->flash( success => 'Successfully deleted transaction' );
$c->redirect_to( '/admin/transactions' );
} else {
$c->flash( error => 'No transaction found' );
$c->redirect_to( '/admin/transactions' );
}
}
1; 1;

78
t/admin/transaction.t Normal file
View file

@ -0,0 +1,78 @@
use Mojo::Base -strict;
use FindBin qw/ $Bin /;
use Test::More;
use Mojo::JSON;
use Test::Pear::LocalLoop;
use DateTime;
my $framework = Test::Pear::LocalLoop->new(
etc_dir => "$Bin/../etc",
);
$framework->install_fixtures('users');
my $t = $framework->framework;
my $schema = $t->app->schema;
my $start = DateTime->today->subtract( hours => 12 );
# create 30 days worth of data
for my $count ( 0 .. 29 ) {
my $trans_day = $start->clone->subtract( days => $count );
create_random_transaction( 'test1@example.com', $trans_day );
if ( $count % 2 ) {
create_random_transaction( 'test2@example.com', $trans_day );
}
if ( $count % 3 ) {
create_random_transaction( 'test3@example.com', $trans_day );
}
if ( $count % 4 ) {
create_random_transaction( 'test4@example.com', $trans_day );
}
}
is $schema->resultset('Transaction')->count, 87;
#login to admin
$t->ua->max_redirects(10);
$t->post_ok('/admin', form => {
email => 'admin@example.com',
password => 'abc123',
})->status_is(200);
#Read valid transaction
$t->get_ok("/admin/transactions/1")
->status_is(200)->or($framework->dump_error);
#get stock image for valid transaction
$t->get_ok("/admin/transactions/1/image")
->status_is(200)->or($framework->dump_error);
#Delete valid transaction
$t->post_ok("/admin/transactions/1/delete")
->status_is(200)->or($framework->dump_error)
->content_like(qr/Successfully deleted transaction/);
is $schema->resultset('Transaction')->count, 86;
#Read deleted transaction
$t->get_ok("/admin/transactions/1")
->content_like(qr/No transaction found/);
sub create_random_transaction {
my $buyer = shift;
my $time = shift;
my $buyer_result = $schema->resultset('User')->find({ email => $buyer })->entity;
my $seller_result = $schema->resultset('Organisation')->find({ name => 'Test Org' })->entity;
$schema->resultset('Transaction')->create({
buyer => $buyer_result,
seller => $seller_result,
value => 10,
purchase_time => $time,
});
}
done_testing;

View file

@ -11,7 +11,17 @@
<strong>Success!</strong> <%= $success %> <strong>Success!</strong> <%= $success %>
</div> </div>
% } % }
<form action="<%= url_for %>" method="post"> <div class="card mb-3">
<h3 class="card-header">
Transaction Details
<form action="<%= url_for . '/delete' %>" method="post">
<div class="form-group">
<button class="btn btn-danger" type="submit" style="float: left">Delete Transaction</button>
</div>
</form>
</h3>
<div class="card-block">
<form>
<div class="form-group"> <div class="form-group">
<label for="email">Buyer</label> <label for="email">Buyer</label>
<input id="buyer" type="text" class="form-control" placeholder="Buyer ID" name="buyer" value="<%= $transaction->buyer->name %>" disabled> <input id="buyer" type="text" class="form-control" placeholder="Buyer ID" name="buyer" value="<%= $transaction->buyer->name %>" disabled>
@ -35,4 +45,6 @@
<div class="form-group"> <div class="form-group">
<img src="<%= url_for . '/image' %>"/> <img src="<%= url_for . '/image' %>"/>
</div> </div>
</form> </form>
</div>
</div>