Refactor stats test to use fixtures and fix stats endpoints for entity

update
This commit is contained in:
Tom Bloor 2017-08-31 18:04:51 +01:00
parent 2980fd129f
commit 8d205d62c2
2 changed files with 25 additions and 46 deletions

View file

@ -15,21 +15,21 @@ has error_messages => sub {
sub post_index {
my $c = shift;
my $user = $c->stash->{api_user};
my $user = $c->stash->{api_user}->entity;
my $today_rs = $user->transactions->today_rs;
my $today_rs = $user->purchases->today_rs;
my $today_sum = $today_rs->get_column('value')->sum;
my $today_count = $today_rs->count;
my $week_rs = $user->transactions->week_rs;
my $week_rs = $user->purchases->week_rs;
my $week_sum = $week_rs->get_column('value')->sum;
my $week_count = $week_rs->count;
my $month_rs = $user->transactions->month_rs;
my $month_rs = $user->purchases->month_rs;
my $month_sum = $month_rs->get_column('value')->sum;
my $month_count = $month_rs->count;
my $user_rs = $user->transactions;
my $user_rs = $user->purchases;
my $user_sum = $user_rs->get_column('value')->sum;
my $user_count = $user_rs->count;
@ -40,7 +40,7 @@ sub post_index {
my $leaderboard_rs = $c->schema->resultset('Leaderboard');
my $monthly_board = $leaderboard_rs->get_latest( 'monthly_total' );
my $monthly_values = $monthly_board->values;
my $current_user_position = $monthly_values ? $monthly_values->find({ user_id => $c->stash->{api_user}->id }) : undef;
my $current_user_position = $monthly_values ? $monthly_values->find({ entity_id => $user->id }) : undef;
return $c->render( json => {
success => Mojo::JSON->true,

View file

@ -1,54 +1,33 @@
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;
my $framework = Test::Pear::LocalLoop->new(
etc_dir => "$Bin/../etc",
);
$framework->install_fixtures('users');
my $t = $framework->framework;
my $schema = $t->app->schema;
my $dtf = $schema->storage->datetime_parser;
my $user = {
token => 'a',
full_name => 'Test User',
display_name => 'Test User',
email => 'test@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
year_of_birth => 2006,
};
my $org = {
token => 'b',
email => 'test2@example.com',
name => 'Test Org',
street_name => 'Test Street',
town => 'Lancaster',
postcode => 'LA1 1AA',
password => 'abc123',
sector => 'A',
};
$schema->resultset('AccountToken')->create({ name => $user->{token} });
$schema->resultset('AccountToken')->create({ name => $org->{token} });
$framework->register_customer($user);
$framework->register_organisation($org);
my $org_result = $schema->resultset('Organisation')->find({ name => $org->{name} });
my $user_result = $schema->resultset('User')->find({ email => $user->{email} });
my $org_result = $schema->resultset('Organisation')->find({ name => 'Test Org' })->entity;
my $user_result = $schema->resultset('User')->find({ email => 'test1@example.com' })->entity;
my $session_key = $framework->login({
email => $user->{email},
password => $user->{password},
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)
->status_is(200)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->true)
->json_is('/today_sum', 0)
->json_is('/today_count', 0)
@ -62,7 +41,7 @@ $t->post_ok('/api/stats' => json => { session_key => $session_key } )
->json_is('/global_count', 0);
for ( 1 .. 10 ) {
$user_result->create_related( 'transactions', {
$user_result->create_related( 'purchases', {
seller_id => $org_result->id,
value => $_,
proof_image => 'a',
@ -70,7 +49,7 @@ for ( 1 .. 10 ) {
}
for ( 11 .. 20 ) {
$user_result->create_related( 'transactions', {
$user_result->create_related( 'purchases', {
seller_id => $org_result->id,
value => $_,
proof_image => 'a',
@ -79,7 +58,7 @@ for ( 11 .. 20 ) {
}
for ( 21 .. 30 ) {
$user_result->create_related( 'transactions', {
$user_result->create_related( 'purchases', {
seller_id => $org_result->id,
value => $_,
proof_image => 'a',
@ -88,7 +67,7 @@ for ( 21 .. 30 ) {
}
for ( 31 .. 40 ) {
$user_result->create_related( 'transactions', {
$user_result->create_related( 'purchases', {
seller_id => $org_result->id,
value => $_,
proof_image => 'a',
@ -97,7 +76,7 @@ for ( 31 .. 40 ) {
}
for ( 41 .. 50 ) {
$org_result->user->create_related( 'transactions', {
$org_result->create_related( 'purchases', {
seller_id => $org_result->id,
value => $_,
proof_image => 'a',
@ -105,7 +84,7 @@ for ( 41 .. 50 ) {
});
}
is $user_result->transactions->search({
is $user_result->purchases->search({
purchase_time => {
-between => [
$dtf->format_datetime(DateTime->today()),
@ -113,7 +92,7 @@ is $user_result->transactions->search({
],
},
})->get_column('value')->sum, 55, 'Got correct sum';
is $user_result->transactions->today_rs->get_column('value')->sum, 55, 'Got correct sum through rs';
is $user_result->purchases->today_rs->get_column('value')->sum, 55, 'Got correct sum through rs';
$t->post_ok('/api/stats' => json => { session_key => $session_key } )
->status_is(200)