made customer graphs work with passing test

This commit is contained in:
Finn 2017-12-13 13:33:29 +00:00
parent 8e1e9b2ec2
commit 8067d3cb96
2 changed files with 163 additions and 82 deletions

View file

@ -1,5 +1,82 @@
use Mojo::Base -strict;
use Test::More skip_all => 'Test needs making!';
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( 'test1@example.com', $trans_day );
}
if ( $count % 3 ) {
create_random_transaction( 'test1@example.com', $trans_day );
}
if ( $count % 4 ) {
create_random_transaction( 'test1@example.com', $trans_day );
}
}
my $session_key = $framework->login({
email => 'test1@example.com',
password => 'abc123',
});
$t->post_ok('/api/v1/customer/graphs' => json => {
session_key => $session_key,
graph => 'avg_spend_last_week',
})
->status_is(200)->or($framework->dump_error)
->json_is('/graph', {
labels => [ map { $start->clone->subtract( days => $_ )->day_name } reverse ( 0 .. 6 ) ],
data => [ 10, 10, 10, 10, 10, 10, 10 ],
});
$framework->logout( $session_key );
$session_key = $framework->login({
email => 'org@example.com',
password => 'abc123',
});
$t->post_ok('/api/v1/customer/graphs' => json => {
session_key => $session_key,
graph => 'avg_spend_last_week',
})
->status_is(403)
->json_is('/success', Mojo::JSON->false)
->json_is('/error', 'user_not_cust');
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 * 100000,
proof_image => 'a',
purchase_time => $time,
});
}
done_testing;