2017-08-29 12:42:27 +01:00
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 (
etc_dir = > "$Bin/../../../etc" ,
) ;
$ framework - > install_fixtures ( 'users' ) ;
my $ t = $ framework - > framework ;
my $ schema = $ t - > app - > schema ;
my $ start = DateTime - > today - > subtract ( hours = > 12 ) ;
# create 30 days worth of data
for my $ count ( 0 .. 29 ) {
my $ trans_day = $ start - > clone - > subtract ( days = > $ count ) ;
create_random_transaction ( 'test1@example.com' , $ trans_day ) ;
if ( $ count % 2 ) {
create_random_transaction ( 'test2@example.com' , $ trans_day ) ;
}
if ( $ count % 3 ) {
create_random_transaction ( 'test3@example.com' , $ trans_day ) ;
}
if ( $ count % 4 ) {
create_random_transaction ( 'test4@example.com' , $ trans_day ) ;
}
}
my $ session_key = $ framework - > login ( {
email = > 'org@example.com' ,
password = > 'abc123' ,
} ) ;
$ t - > post_ok ( '/api/v1/organisation/graphs' = > json = > {
session_key = > $ session_key ,
graph = > 'customers_last_7_days' ,
} )
- > status_is ( 200 ) - > or ( $ framework - > dump_error )
- > json_is ( '/graph' , {
2017-12-14 17:20:06 +00:00
labels = > [ map { $ t - > app - > format_iso_datetime (
$ start - > clone - > subtract ( days = > $ _ ) - > subtract ( hours = > 12 )
) } reverse ( 0 .. 6 ) ] ,
bounds = > {
min = > $ t - > app - > format_iso_datetime ( $ start - > clone - > subtract ( days = > 6 ) - > subtract ( hours = > 12 ) ) ,
max = > $ t - > app - > format_iso_datetime ( $ start - > clone - > add ( hours = > 12 ) ) ,
} ,
2017-09-04 17:29:28 +01:00
data = > [ 2 , 4 , 2 , 3 , 3 , 4 , 1 ] ,
2017-08-29 12:42:27 +01:00
} ) ;
$ t - > post_ok ( '/api/v1/organisation/graphs' = > json = > {
session_key = > $ session_key ,
graph = > 'customers_last_30_days' ,
} )
- > status_is ( 200 ) - > or ( $ framework - > dump_error )
- > json_is ( '/graph' , {
2017-12-14 17:20:06 +00:00
labels = > [ map { $ t - > app - > format_iso_datetime (
$ start - > clone - > subtract ( days = > $ _ ) - > subtract ( hours = > 12 )
) } reverse ( 0 .. 29 ) ] ,
bounds = > {
2017-12-14 17:25:00 +00:00
min = > $ t - > app - > format_iso_datetime ( $ start - > clone - > subtract ( days = > 29 ) - > subtract ( hours = > 12 ) ) ,
2017-12-14 17:20:06 +00:00
max = > $ t - > app - > format_iso_datetime ( $ start - > clone - > add ( hours = > 12 ) ) ,
} ,
2017-09-04 17:29:28 +01:00
data = > [ 4 , 2 , 3 , 3 , 4 , 1 , 4 , 3 , 3 , 2 , 4 , 2 , 4 , 2 , 3 , 3 , 4 , 1 , 4 , 3 , 3 , 2 , 4 , 2 , 4 , 2 , 3 , 3 , 4 , 1 ] ,
} ) ;
$ t - > post_ok ( '/api/v1/organisation/graphs' = > json = > {
session_key = > $ session_key ,
graph = > 'sales_last_7_days' ,
} )
- > status_is ( 200 ) - > or ( $ framework - > dump_error )
- > json_is ( '/graph' , {
2017-12-14 17:20:06 +00:00
labels = > [ map { $ t - > app - > format_iso_datetime (
$ start - > clone - > subtract ( days = > $ _ ) - > subtract ( hours = > 12 )
) } reverse ( 0 .. 6 ) ] ,
bounds = > {
min = > $ t - > app - > format_iso_datetime ( $ start - > clone - > subtract ( days = > 6 ) - > subtract ( hours = > 12 ) ) ,
max = > $ t - > app - > format_iso_datetime ( $ start - > clone - > add ( hours = > 12 ) ) ,
} ,
2017-09-04 17:29:28 +01:00
data = > [ 20 , 40 , 20 , 30 , 30 , 40 , 10 ] ,
} ) ;
$ t - > post_ok ( '/api/v1/organisation/graphs' = > json = > {
session_key = > $ session_key ,
graph = > 'sales_last_30_days' ,
} )
- > status_is ( 200 ) - > or ( $ framework - > dump_error )
- > json_is ( '/graph' , {
2017-12-14 17:20:06 +00:00
labels = > [ map { $ t - > app - > format_iso_datetime (
$ start - > clone - > subtract ( days = > $ _ ) - > subtract ( hours = > 12 )
) } reverse ( 0 .. 29 ) ] ,
bounds = > {
2017-12-14 17:25:00 +00:00
min = > $ t - > app - > format_iso_datetime ( $ start - > clone - > subtract ( days = > 29 ) - > subtract ( hours = > 12 ) ) ,
2017-12-14 17:20:06 +00:00
max = > $ t - > app - > format_iso_datetime ( $ start - > clone - > add ( hours = > 12 ) ) ,
} ,
2017-09-04 17:29:28 +01:00
data = > [ 40 , 20 , 30 , 30 , 40 , 10 , 40 , 30 , 30 , 20 , 40 , 20 , 40 , 20 , 30 , 30 , 40 , 10 , 40 , 30 , 30 , 20 , 40 , 20 , 40 , 20 , 30 , 30 , 40 , 10 ] ,
2017-08-29 12:42:27 +01:00
} ) ;
2017-09-08 12:47:20 +01:00
$ t - > post_ok ( '/api/v1/organisation/graphs' = > json = > {
session_key = > $ session_key ,
graph = > 'customers_range' ,
start = > $ start - > clone - > subtract ( days = > 8 ) - > ymd ,
end = > $ start - > clone - > ymd ,
} )
- > status_is ( 200 ) - > or ( $ framework - > dump_error )
- > json_is ( '/graph' , {
labels = > [ map { $ start - > clone - > subtract ( days = > $ _ ) - > ymd } reverse ( 0 .. 8 ) ] ,
data = > [ 2 , 4 , 2 , 4 , 2 , 3 , 3 , 4 , 1 ] ,
} ) ;
2017-08-29 12:42:27 +01:00
$ framework - > logout ( $ session_key ) ;
$ session_key = $ framework - > login ( {
email = > 'test1@example.com' ,
password = > 'abc123' ,
} ) ;
$ t - > post_ok ( '/api/v1/organisation/graphs' = > json = > {
session_key = > $ session_key ,
graph = > 'customers_last_7_days' ,
} )
- > status_is ( 403 )
- > json_is ( '/success' , Mojo::JSON - > false )
- > json_is ( '/error' , 'user_not_org' ) ;
2017-09-04 17:29:28 +01:00
2017-08-29 12:42:27 +01:00
sub create_random_transaction {
my $ buyer = shift ;
my $ time = shift ;
2017-08-31 19:12:13 +01:00
my $ buyer_result = $ schema - > resultset ( 'User' ) - > find ( { email = > $ buyer } ) - > entity ;
my $ seller_result = $ schema - > resultset ( 'Organisation' ) - > find ( { name = > 'Test Org' } ) - > entity ;
2017-08-29 12:42:27 +01:00
$ schema - > resultset ( 'Transaction' ) - > create ( {
2017-08-31 19:12:13 +01:00
buyer = > $ buyer_result ,
seller = > $ seller_result ,
2017-09-13 16:07:23 +01:00
value = > 10 * 100000 ,
2017-08-29 12:42:27 +01:00
proof_image = > 'a' ,
purchase_time = > $ time ,
} ) ;
}
done_testing ;