2017-05-16 20:30:38 +00:00
|
|
|
use Mojo::Base -strict;
|
|
|
|
|
2017-08-31 17:04:51 +00:00
|
|
|
use FindBin qw/ $Bin /;
|
|
|
|
|
2017-05-16 20:30:38 +00:00
|
|
|
use Test::More;
|
|
|
|
use Mojo::JSON;
|
|
|
|
use Test::Pear::LocalLoop;
|
|
|
|
use DateTime;
|
|
|
|
|
2017-08-31 17:04:51 +00:00
|
|
|
my $framework = Test::Pear::LocalLoop->new(
|
|
|
|
etc_dir => "$Bin/../etc",
|
|
|
|
);
|
|
|
|
$framework->install_fixtures('users');
|
|
|
|
|
2017-05-16 20:30:38 +00:00
|
|
|
my $t = $framework->framework;
|
|
|
|
my $schema = $t->app->schema;
|
|
|
|
|
2017-12-14 20:30:44 +00:00
|
|
|
my $start = DateTime->today->subtract( hours => 12 );
|
|
|
|
|
|
|
|
# create 40 days worth of data
|
|
|
|
for my $count ( 0 .. 40 ) {
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
2017-05-16 20:30:38 +00:00
|
|
|
|
2017-05-22 21:21:05 +00:00
|
|
|
my $session_key = $framework->login({
|
2017-12-14 20:30:44 +00:00
|
|
|
email => 'test1@example.com',
|
2017-08-31 17:04:51 +00:00
|
|
|
password => 'abc123',
|
2017-05-22 21:21:05 +00:00
|
|
|
});
|
|
|
|
|
2017-12-15 17:52:02 +00:00
|
|
|
#TODO be able to define start and end below in request
|
|
|
|
|
2017-12-18 12:56:45 +00:00
|
|
|
$t->post_ok('/api/stats/customer' => json => {
|
2017-12-14 20:30:44 +00:00
|
|
|
session_key => $session_key,
|
|
|
|
})
|
2017-08-31 17:04:51 +00:00
|
|
|
->status_is(200)->or($framework->dump_error)
|
2017-12-15 15:05:04 +00:00
|
|
|
->json_is('/weeks', {
|
|
|
|
purchases => [ 8, 21, 19, 22, 20, 20, 8 ],
|
2017-12-15 17:52:02 +00:00
|
|
|
})
|
|
|
|
->json_is('/sectors', {
|
|
|
|
sectors => ['A'],
|
|
|
|
purchases => [118],
|
2017-05-16 21:45:49 +00:00
|
|
|
});
|
|
|
|
|
2017-12-14 20:30:44 +00:00
|
|
|
sub create_random_transaction {
|
|
|
|
my $buyer = shift;
|
|
|
|
my $time = shift;
|
2017-05-16 21:45:49 +00:00
|
|
|
|
2017-12-14 20:30:44 +00:00
|
|
|
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,
|
2017-05-16 21:45:49 +00:00
|
|
|
proof_image => 'a',
|
2017-12-14 20:30:44 +00:00
|
|
|
purchase_time => $time,
|
2017-05-16 21:45:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-16 20:30:38 +00:00
|
|
|
done_testing;
|