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/v1/customer/snippets.t

83 lines
2 KiB
Perl
Raw Permalink Normal View History

2017-12-12 17:21:32 +00:00
use Mojo::Base -strict;
2018-03-21 16:37:40 +00:00
BEGIN {
use Test::MockTime qw/ set_absolute_time /;
}
2017-12-12 17:21:32 +00:00
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;
2018-03-21 16:37:40 +00:00
set_absolute_time('2017-01-02T00:00:00Z');
2017-12-12 17:21:32 +00:00
my $start = DateTime->today->subtract( hours => 12 );
# create 30 days worth of data
for my $count ( 0 .. 60 ) {
my $trans_day = $start->clone->subtract( days => $count );
create_random_transaction( 'test1@example.com', $trans_day );
if ( $count % 2 ) {
2018-03-21 16:37:40 +00:00
create_random_transaction( 'test1@example.com', $trans_day );
2017-12-12 17:21:32 +00:00
}
if ( $count % 3 ) {
2018-03-21 16:37:40 +00:00
create_random_transaction( 'test1@example.com', $trans_day );
2017-12-12 17:21:32 +00:00
}
if ( $count % 4 ) {
2018-03-21 16:37:40 +00:00
create_random_transaction( 'test1@example.com', $trans_day );
2017-12-12 17:21:32 +00:00
}
}
2018-03-21 16:37:40 +00:00
my $lb_start = $start->clone->truncate( to => 'month' )->subtract( months => 1);
#use Devel::Dwarn; Dwarn({ $_->get_columns }) for $schema->resultset('Transaction')->all;
$schema->resultset('Leaderboard')->create_new( 'monthly_total', $lb_start );
2017-12-12 17:21:32 +00:00
my $session_key = $framework->login({
email => 'test1@example.com',
password => 'abc123',
});
$t->post_ok('/api/v1/customer/snippets' => json => {
session_key => $session_key,
})
->status_is(200)->or($framework->dump_error)
->json_is('/snippets', {
2018-03-21 16:37:40 +00:00
user_sum => 1760,
2017-12-12 17:21:32 +00:00
user_position => 1,
});
$framework->logout( $session_key );
$session_key = $framework->login({
email => 'test1@example.com',
password => 'abc123',
});
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;