Skip to content

Commit

Permalink
Added tests for all of spaceport API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
icydee authored and Iain C Docherty committed Feb 15, 2016
1 parent d58658c commit ef33ca1
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 44 deletions.
11 changes: 11 additions & 0 deletions Fleet-Action-Status.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Current status of fleet-action code in the API

Shipyard:
- I am fairly sure that all APIs for the shipyard are complete

Spaceport:
- All API calls to the Spaceport are working and at least return something sensible
- look at 460_fleet_spaceport.t for example calls to the API
- there are a few minor issues, but these will be easier to test once the UI is working


30 changes: 17 additions & 13 deletions docs/SpacePort.pod
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ See above for these options.
=head3 RESPONSE

{
"number_of_incoming" : 26,
"number_of_incoming_fleets" : 3,
"number_of_incoming_ships" : 25,
"incoming" : [
{
"id" : "id-goes-here",
Expand Down Expand Up @@ -813,7 +814,7 @@ that report the B<can_scuttle> status of '1'.

Accepts either fixed arguments or a hash of named parameters

( session_id, building_id )
( session_id, building_id, fleet_id, quantity )
( parameter_hash )

=head3 session_id (required)
Expand Down Expand Up @@ -936,7 +937,7 @@ The unique id of the planet that the spies are on and ship will be dispatched fr
=head3 to_body_id

The unique id of the planet that the spies should be sent to. This is a required parameter
if using fixed arguments. With named arguments you can use either C<to_body_id> or C<target>
if using fixed arguments. With named arguments you can use either C<to_body_id> or C<to_body>

=head3 to_body

Expand Down Expand Up @@ -1005,16 +1006,14 @@ if using fixed arguments. With named arguments you can use either C<to_body_id>

=head3 fleet_id (required)

The unique ID of the fleet.
The unique ID of the fleet. The number of ships from the fleet actually sent will depend
upon the number of spies and the number of passengers each ship can carry. The minimum number
of ships required will be sent. The rest of the fleet will stay at home.

=head3 spy_ids (required)

An array reference of spy ids to send

=head3 ship_quantity

The number of ships to send from the fleet (defaults to 1)

=head3 to_body

The planet where you want to send spies to. This can be used instead of C<to_body_id> if
Expand All @@ -1031,11 +1030,16 @@ May only be specified with the named arguments calling convention.
"fleet" : {
"id" : "id-goes-here",
"quantity" : 1,
"name" : "CS4",
"hold_size" : 1100,
"speed" : 400,
"type" : "cargo_ship",
"date_arrives" : "01 31 2010 13:09:05 +0600",
"task" : "Travelling",
"date_arrives" : "02 12 2012 12:08:33 +0000",
"details" : {
"name" : "CS4",
"can_recall" : 0,
"hold_size" : 1100,
"speed" : 400,
"type" : "cargo_ship",
"date_available" : "01 31 2010 13:09:05 +0600",
...
...
},
"spies_sent" : ["id-goes-here","id-goes-here","id-goes-here"], # should be identical to "spy_ids"
Expand Down
2 changes: 1 addition & 1 deletion lib/Lacuna/DB/Result/Fleet.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ has 'hostile_action' => (
sub has_that_quantity {
my ($self, $qty) = @_;

if ($qty < 0 or int($qty) != $qty) {
if (not defined $qty or $qty < 0 or int($qty) != $qty) {
confess [1009, 'Quantity must be a positive integer'];
}
if ($qty > $self->quantity) {
Expand Down
36 changes: 20 additions & 16 deletions lib/Lacuna/RPC/Building/SpacePort.pm
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ sub _view_available_fleets {

my %out = (
status => $self->format_status($empire),
available => $option eq 'available' ? \@available : \@unavailable,
$option => $option eq 'available' ? \@available : \@unavailable,
);

return \%out;
Expand Down Expand Up @@ -188,7 +188,7 @@ sub send_fleet {
$new_fleet->send(target => $target, arrival => $arrival_date);
}
return {
fleet => $fleet->get_status,
fleet => $new_fleet->get_status,
status => $self->format_status($empire),
};
}
Expand Down Expand Up @@ -360,7 +360,6 @@ sub send_spies {
spy_ids => shift,
};
}
$args->{ship_quantity} = $args->{ship_quantity} || 1;
$args->{arrival_date} = {soonest => 1} if not defined $args->{arrival_date};

my $empire = $self->get_empire_by_session($args->{session_id});
Expand All @@ -379,7 +378,7 @@ sub send_spies {
confess [ 1013, sprintf('%s is an isolationist empire, and must be left alone.',$to_body->empire->name)];
}

unless ($on_body->empire_id == $to_body->empire_id) {
if ($on_body->empire_id != $to_body->empire_id) {
$empire->current_session->check_captcha;
}

Expand All @@ -395,9 +394,6 @@ sub send_spies {
if ($fleet->berth_level > $max_berth) {
confess [1010, "Your spaceport level is not high enough to support a fleet with a Berth Level of ".$fleet->berth_level."."];
}
if ($fleet->quantity < $args->{ship_quantity}) {
confess [1009, "That fleet does not have ".$args->{ship_quantity}." ships in it."];
}

# check size
my $spies_to_send = scalar(@{$args->{spy_ids}});
Expand All @@ -406,10 +402,6 @@ sub send_spies {
confess [1013, "You can't send a fleet with no spies."];
}

if ($fleet->max_occupants * $args->{ship_quantity} < $spies_to_send) {
confess [1010, "That number of ships cannot hold the spies selected."];
}

# get the spies
my @ids_sent;
my @ids_not_sent;
Expand Down Expand Up @@ -438,9 +430,13 @@ sub send_spies {
push @ids_not_sent, $spy->id;
}
}
my $qty = ceil(scalar(@ids_sent) / $fleet->max_occupants);
if ($fleet->quantity < $qty) {
confess [1010, "That fleet is not big enough to hold the spies selected."];
}
if (scalar @ids_sent) {
# send it
my $fleet = $fleet->split($args->{ship_quantity});
$fleet = $fleet->split($qty);
$fleet->send(
target => $to_body,
payload => {spies => \@ids_sent }, # add the spies to the payload when we send, otherwise they'll get added again
Expand Down Expand Up @@ -803,6 +799,7 @@ sub view_incoming_fleets {
}

$args->{task} = 'travelling';
$args->{task_title} = 'incoming';
return $self->_view_fleets($args);
}

Expand All @@ -821,6 +818,7 @@ sub view_orbiting_fleets {
}

$args->{task} = 'orbiting';
$args->{task_title} = 'orbiting';
return $self->_view_fleets($args);
}

Expand Down Expand Up @@ -864,7 +862,7 @@ sub view_mining_platforms {
sub _view_fleets {
my ($self, $args) = @_;

my $empire = $self->get_empire_by_session($args->{session_id});
my $empire = $self->get_empire_by_session($args->{session_id});
# see all incoming ships from own empire, or from any alliance member
# if the target is an allied colony, see all incoming ships dependent upon the highest
# level of space-port on the target
Expand All @@ -877,14 +875,14 @@ sub _view_fleets {
task => 'Travelling',
direction => 'out',
},{
join => 'body',
prefetch => 'body',
});
}
if ($args->{task} eq 'orbiting') {
$fleet_rs = $fleet_rs->search({
task => ['Orbit','Defend'],
},{
join => 'body',
prefetch => 'body',
});
}

Expand Down Expand Up @@ -912,13 +910,19 @@ sub _view_fleets {
$fleet_rs = $fleet_rs->search({ 'body.empire_id' => \@ally_ids });
}
my @fleets;
my $no_ships = 0;
while (my $fleet = $fleet_rs->next) {
push @fleets, $fleet->get_status($target, $status_args);
$no_ships += $fleet->quantity;
}
my %out = (
status => $self->format_status($empire),
$args->{task} => \@fleets,
$args->{task_title} => \@fleets,
);
if ($args->{task_title} eq 'incoming') {
$out{number_of_incoming_fleets} = scalar(@fleets);
$out{number_of_incoming_ships} = $no_ships;
}
return \%out;
}

Expand Down
72 changes: 59 additions & 13 deletions t/460_fleet_spaceport.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use DateTime;

use TestHelper;

TestHelper->clear_all_test_empires;
diag("Cleared all test empires");
#TestHelper->clear_all_test_empires;
#diag("Cleared all test empires");

my $tester = TestHelper->new->use_existing_test_empire;
my $enemy = TestHelper->new({empire_name => 'TLE Test Enemy'})->use_existing_test_empire;
Expand Down Expand Up @@ -47,18 +47,30 @@ foreach my $ship (sort keys %{$result->{result}{docked_ships}} ) {

## spaceport - view_all_fleets
##
$result = $tester->post('spaceport','view', [{
session_id => $test_session_id,
building_id => $test_spaceport->id,
}]);
ok($result->{result}{docked_ships}, "Has docked ships");
ok($result->{result}{docked_ships}{sweeper} > 1000, "Has sweepers");

$result = $tester->post('spaceport','view_all_fleets', [{
session_id => $test_session_id,
building_id => $test_spaceport->id,
paging => {no_paging => 1},
no_status => 1,
}]);

my ($sweepers) = grep {$_->{details}{type} eq 'sweeper'} @{$result->{result}{fleets}};
ok($sweepers->{quantity} > 1000, "view_all_fleets sweepers");

$result = $tester->post('spaceport','view_incoming_fleets', [{
session_id => $test_session_id,
paging => {no_paging => 1},
target => { body_id => $test_home->id},
no_status => 1,
}]);
ok($result->{result}{incoming}, "Has incoming");

$result = $tester->post('spaceport','view_available_fleets', [{
session_id => $test_session_id,
Expand All @@ -82,22 +94,20 @@ $result = $tester->post('spaceport','send_fleet', [{
quantity => 1,
target => { body_id => $test_home->id},
arrival_date => {
month => 12,
date => 25,
hour => 8,
month => 1,
date => 1,
hour => 0,
minute => 0,
second => 0,
},
no_status => 1,
}]);

$result = $tester->post('spaceport','view_fleets_travelling', [{
$result = $tester->post('spaceport','view_travelling_fleets', [{
session_id => $test_session_id,
building_id => $test_spaceport->id,
no_status => 1,
}]);
exit;


$fleets = $test_home->fleets->search({
task => 'Defend',
Expand All @@ -110,26 +120,62 @@ while (my $fleet = $fleets->next) {
quantity => 1,
no_status => 1,
}]);
exit;

last;
}

exit;

$result = $tester->post('spaceport','view_unavailable_fleets', [{
session_id => $test_session_id,
body_id => $test_home->id,
target => { body_id => $test_home->id},
no_status => 1,
}]);

diag Dumper $result;
my @unavailable = @{$result->{result}{unavailable}};
for my $fleet (@unavailable) {
diag "Unavailable [".$fleet->{id}."][".$fleet->{details}{type}."][".$fleet->{quantity}."][".$fleet->{reason}."]\n";
#diag Dumper $fleet;
}

$result = $tester->post('spaceport','view_orbiting_fleets', [{
session_id => $test_session_id,
target => { body_id => $test_home->id},
}]);

$result = $tester->post('spaceport','view_all_fleets', [{
session_id => $test_session_id,
building_id => $test_spaceport->id,
no_status => 1,
}]);
my @fleets = @{$result->{result}{fleets}};
($fleet) = grep {$_->{details}{can_scuttle}} @fleets;

diag "Can scuttle [".$fleet->{details}{type}."]";

$result = $tester->post('spaceport','scuttle_fleet', [{
session_id => $test_session_id,
building_id => $test_spaceport->id,
fleet_id => $fleet->{id},
}]);

$result = $tester->post('spaceport', 'prepare_send_spies', [{
session_id => $test_session_id,
on_body_id => $test_home->id,
to_body => { body_id => $test_home->id},
}]);

my $spy = $result->{result}{spies}[0];
$fleet = $result->{result}{fleets}[0];

diag(Dumper($spy));
diag(Dumper($fleet));

$result = $tester->post('spaceport', 'send_spies', [{
session_id => $test_session_id,
on_body_id => $test_home->id,
to_body_id => $test_home->id,
fleet_id => $fleet->{id},
spy_ids => [$spy->{id}],
}]);


exit;
Expand Down
2 changes: 1 addition & 1 deletion t/TestHelper.pm
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ sub post {
Content => to_json($content),
Accept => 'application/json',
);
my $response = from_json($response->content);
$response = from_json($response->content);
say "RESPONSE: ".Dumper($response);
# sleep 2;
return $response;
Expand Down
Loading

0 comments on commit ef33ca1

Please sign in to comment.