From 8d205d62c26c73eb5ea430dc9492cb2b8cd11015 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Thu, 31 Aug 2017 18:04:51 +0100 Subject: [PATCH] Refactor stats test to use fixtures and fix stats endpoints for entity update --- lib/Pear/LocalLoop/Controller/Api/Stats.pm | 12 ++--- t/api/stats.t | 59 +++++++--------------- 2 files changed, 25 insertions(+), 46 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index c6b1c7d..bf15384 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -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, diff --git a/t/api/stats.t b/t/api/stats.t index 2a6d889..8e8fbc1 100644 --- a/t/api/stats.t +++ b/t/api/stats.t @@ -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)