X-Git-Url: https://ruin.nu/git/?p=ndwebbie.git;a=blobdiff_plain;f=t%2Fscans.t;fp=t%2Fscans.t;h=df404dd62030f6facb7924ff1642489f3b64d8c8;hp=0000000000000000000000000000000000000000;hb=0000c72e6700ef1af5dd4ef608d6a9f00b30442d;hpb=75707e9b9cb6a816379ac2a8ac0c215a4bbe2545 diff --git a/t/scans.t b/t/scans.t new file mode 100644 index 0000000..df404dd --- /dev/null +++ b/t/scans.t @@ -0,0 +1,111 @@ +use strict; +use warnings; +use Test2::V0; + +use DBD::Mock; + +use Data::Dumper; +use FindBin; + +use NDWeb::Scans; + +my $milscan = do { + open my $in, '<', "$FindBin::Bin/milscan.html" or die "Can't read file: $!"; + local $/; + <$in> +}; + +my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) + or die "Cannot create handle: $DBI::errstr\n"; + +{ + my @fleets = parseMilScan($milscan); + + #print Dumper(\@fleets), "\n"; + + is ($fleets[0]->{name}, 'Base'); + is (scalar @{$fleets[0]->{ships}}, 4); + is ($fleets[0]->{ships}->[0]->{ship}, 'Centaur'); + is ($fleets[0]->{ships}->[0]->{amount}, 199900); + is ($fleets[0]->{amount}, 253901); + is ($fleets[1]->{name}, 'Fleet 1'); + is (scalar @{$fleets[1]->{ships}}, 1); + is ($fleets[1]->{ships}->[0]->{ship}, 'Chimera'); + is ($fleets[1]->{ships}->[0]->{amount}, 54500); + is ($fleets[1]->{amount}, 54500); + is ($fleets[2]->{name}, 'Fleet 2'); + is ($fleets[2]->{amount}, 0); + is ($fleets[3]->{name}, 'Fleet 3'); + is ($fleets[3]->{amount}, 0); +} + +{ + my $scan = {id => 31337, type => 'Military', pid => 1337, tick => 123}; + my $fid = 666; + $dbh->{mock_add_resultset} = { + sql => qr/^INSERT INTO fleets \(name, mission, pid, tick, amount\)/i, + results => [['fid'], [$fid]], + }; + doMilScan($dbh, $scan, $milscan); + my $history = $dbh->{mock_all_history}; + is(scalar(@{$history}), 3, 'Correct number of statements executed'); + + #print Dumper($history), "\n"; + { + my $sth = $history->[0]; + my $exec_history = $sth->{execution_history}; + like($sth->statement, + qr{INSERT INTO fleets \(name, mission, pid, tick, amount\).*}sm, + ); + is(scalar(@{$exec_history}), 2); + is($exec_history->[0]->{params}->[0], "Base"); + is($exec_history->[0]->{params}->[1], "Military"); + is($exec_history->[0]->{params}->[2], $scan->{pid}); + is($exec_history->[0]->{params}->[3], $scan->{tick}); + is($exec_history->[0]->{params}->[4], 253901); + + is($exec_history->[1]->{params}->[0], "Fleet 1"); + is($exec_history->[1]->{params}->[1], "Military"); + is($exec_history->[1]->{params}->[2], $scan->{pid}); + is($exec_history->[1]->{params}->[3], $scan->{tick}); + is($exec_history->[1]->{params}->[4], 54500); + + } + { + my $sth = $history->[1]; + my $exec_history = $sth->{execution_history}; + like($sth->statement, + qr{INSERT INTO fleet_scans \(fid, id\).*}sm, + ); + is(scalar(@{$exec_history}), 2); + is($exec_history->[0]->{params}->[0], $fid); + is($exec_history->[0]->{params}->[1], $scan->{id}); + is($exec_history->[1]->{params}->[0], $fid); + is($exec_history->[1]->{params}->[1], $scan->{id}); + } + { + my $sth = $history->[2]; + my $exec_history = $sth->{execution_history}; + like($sth->statement, + qr{INSERT INTO fleet_ships \(fid, ship, amount\).*}sm, + ); + is(scalar(@{$exec_history}), 5); + is($exec_history->[0]->{params}->[0], $fid); + is($exec_history->[0]->{params}->[1], 'Centaur'); + is($exec_history->[0]->{params}->[2], 199900); + is($exec_history->[1]->{params}->[0], $fid); + is($exec_history->[1]->{params}->[1], 'Titan'); + is($exec_history->[1]->{params}->[2], 50000); + is($exec_history->[2]->{params}->[0], $fid); + is($exec_history->[2]->{params}->[1], 'Wyvern'); + is($exec_history->[2]->{params}->[2], 3000); + is($exec_history->[3]->{params}->[0], $fid); + is($exec_history->[3]->{params}->[1], 'Medusa'); + is($exec_history->[3]->{params}->[2], 1001); + is($exec_history->[4]->{params}->[0], $fid); + is($exec_history->[4]->{params}->[1], 'Chimera'); + is($exec_history->[4]->{params}->[2], 54500); + } +} + +done_testing();