This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
Foodloop-Server/t/api/stats.t

112 lines
3.1 KiB
Perl
Raw Permalink Normal View History

2017-05-16 20:30:38 +00:00
use Mojo::Base -strict;
use FindBin qw/ $Bin /;
2017-05-16 20:30:38 +00:00
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');
2017-05-16 20:30:38 +00:00
my $t = $framework->framework;
my $schema = $t->app->schema;
2017-05-16 21:45:49 +00:00
my $dtf = $schema->storage->datetime_parser;
2017-05-16 20:30:38 +00:00
my $org_result = $schema->resultset('Organisation')->find({ name => 'Test Org' })->entity;
my $user_result = $schema->resultset('User')->find({ email => 'test1@example.com' })->entity;
2017-05-16 20:30:38 +00:00
my $session_key = $framework->login({
email => 'test1@example.com',
password => 'abc123',
});
$t->app->schema->resultset('Leaderboard')->create_new( 'monthly_total', DateTime->now->truncate(to => 'month' )->subtract( months => 1) );
$t->post_ok('/api/stats' => json => { session_key => $session_key } )
->status_is(200)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->true)
->json_is('/today_sum', 0)
->json_is('/today_count', 0)
->json_is('/week_sum', 0)
->json_is('/week_count', 0)
->json_is('/month_sum', 0)
->json_is('/month_count', 0)
->json_is('/user_sum', 0)
->json_is('/user_count', 0)
->json_is('/global_sum', 0)
->json_is('/global_count', 0);
2017-05-16 20:30:38 +00:00
for ( 1 .. 10 ) {
$user_result->create_related( 'purchases', {
2017-05-16 20:30:38 +00:00
seller_id => $org_result->id,
value => $_ * 100000,
2017-05-16 20:30:38 +00:00
proof_image => 'a',
});
}
2017-05-16 21:45:49 +00:00
for ( 11 .. 20 ) {
$user_result->create_related( 'purchases', {
2017-05-16 21:45:49 +00:00
seller_id => $org_result->id,
value => $_ * 100000,
2017-05-16 21:45:49 +00:00
proof_image => 'a',
2017-08-14 15:10:14 +00:00
purchase_time => $dtf->format_datetime(DateTime->today()->subtract( days => 5 )),
2017-05-16 21:45:49 +00:00
});
}
for ( 21 .. 30 ) {
$user_result->create_related( 'purchases', {
2017-05-16 21:45:49 +00:00
seller_id => $org_result->id,
value => $_ * 100000,
2017-05-16 21:45:49 +00:00
proof_image => 'a',
2017-08-14 15:10:14 +00:00
purchase_time => $dtf->format_datetime(DateTime->today()->subtract( days => 25 )),
2017-05-16 21:45:49 +00:00
});
}
for ( 31 .. 40 ) {
$user_result->create_related( 'purchases', {
2017-05-16 21:45:49 +00:00
seller_id => $org_result->id,
value => $_ * 100000,
2017-05-16 21:45:49 +00:00
proof_image => 'a',
2017-08-14 15:10:14 +00:00
purchase_time => $dtf->format_datetime(DateTime->today()->subtract( days => 50 )),
2017-05-16 21:45:49 +00:00
});
}
for ( 41 .. 50 ) {
$org_result->create_related( 'purchases', {
2017-05-16 21:45:49 +00:00
seller_id => $org_result->id,
value => $_ * 100000,
2017-05-16 21:45:49 +00:00
proof_image => 'a',
2017-08-14 15:10:14 +00:00
purchase_time => $dtf->format_datetime(DateTime->today()->subtract( days => 50 )),
2017-05-16 21:45:49 +00:00
});
}
is $user_result->purchases->search({
2017-08-14 15:10:14 +00:00
purchase_time => {
2017-05-16 20:30:38 +00:00
-between => [
$dtf->format_datetime(DateTime->today()),
$dtf->format_datetime(DateTime->today()->add( days => 1 )),
],
},
})->get_column('value')->sum, 5500000, 'Got correct sum';
is $user_result->purchases->today_rs->get_column('value')->sum, 5500000, 'Got correct sum through rs';
2017-05-16 20:30:38 +00:00
$t->post_ok('/api/stats' => json => { session_key => $session_key } )
->status_is(200)
->json_is('/success', Mojo::JSON->true)
->json_is('/today_sum', 55)
2017-05-16 21:45:49 +00:00
->json_is('/today_count', 10)
->json_is('/week_sum', 155)
->json_is('/week_count', 10)
->json_is('/month_sum', 410)
->json_is('/month_count', 20)
->json_is('/user_sum', 820)
->json_is('/user_count', 40)
->json_is('/global_sum', 1275)
->json_is('/global_count', 50);
2017-05-16 20:30:38 +00:00
done_testing;