]> ruin.nu Git - ndwebbie.git/blob - t/scans.t
Add support for Military Scans
[ndwebbie.git] / t / scans.t
1 use strict;
2 use warnings;
3 use Test2::V0;
4
5 use DBD::Mock;
6
7 use Data::Dumper;
8 use FindBin;
9
10 use NDWeb::Scans;
11
12 my $milscan = do {
13         open my $in, '<', "$FindBin::Bin/milscan.html" or die "Can't read file: $!";
14         local $/;
15         <$in>
16 };
17
18 my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
19         or die "Cannot create handle: $DBI::errstr\n";
20
21 {
22         my @fleets = parseMilScan($milscan);
23
24         #print Dumper(\@fleets), "\n";
25
26         is ($fleets[0]->{name}, 'Base');
27         is (scalar @{$fleets[0]->{ships}}, 4);
28         is ($fleets[0]->{ships}->[0]->{ship}, 'Centaur');
29         is ($fleets[0]->{ships}->[0]->{amount}, 199900);
30         is ($fleets[0]->{amount}, 253901);
31         is ($fleets[1]->{name}, 'Fleet 1');
32         is (scalar @{$fleets[1]->{ships}}, 1);
33         is ($fleets[1]->{ships}->[0]->{ship}, 'Chimera');
34         is ($fleets[1]->{ships}->[0]->{amount}, 54500);
35         is ($fleets[1]->{amount}, 54500);
36         is ($fleets[2]->{name}, 'Fleet 2');
37         is ($fleets[2]->{amount}, 0);
38         is ($fleets[3]->{name}, 'Fleet 3');
39         is ($fleets[3]->{amount}, 0);
40 }
41
42 {
43         my $scan = {id => 31337, type => 'Military', pid => 1337, tick => 123};
44         my $fid = 666;
45         $dbh->{mock_add_resultset} = {
46                 sql     => qr/^INSERT INTO fleets \(name, mission, pid, tick, amount\)/i,
47                 results => [['fid'], [$fid]],
48         };
49         doMilScan($dbh, $scan, $milscan);
50         my $history = $dbh->{mock_all_history};
51         is(scalar(@{$history}), 3, 'Correct number of statements executed');
52
53         #print Dumper($history), "\n";
54         {
55                 my $sth = $history->[0];
56                 my $exec_history = $sth->{execution_history};
57                 like($sth->statement,
58                         qr{INSERT INTO fleets \(name, mission, pid, tick, amount\).*}sm,
59                 );
60                 is(scalar(@{$exec_history}), 2);
61                 is($exec_history->[0]->{params}->[0], "Base");
62                 is($exec_history->[0]->{params}->[1], "Military");
63                 is($exec_history->[0]->{params}->[2], $scan->{pid});
64                 is($exec_history->[0]->{params}->[3], $scan->{tick});
65                 is($exec_history->[0]->{params}->[4], 253901);
66
67                 is($exec_history->[1]->{params}->[0], "Fleet 1");
68                 is($exec_history->[1]->{params}->[1], "Military");
69                 is($exec_history->[1]->{params}->[2], $scan->{pid});
70                 is($exec_history->[1]->{params}->[3], $scan->{tick});
71                 is($exec_history->[1]->{params}->[4], 54500);
72
73         }
74         {
75                 my $sth = $history->[1];
76                 my $exec_history = $sth->{execution_history};
77                 like($sth->statement,
78                         qr{INSERT INTO fleet_scans \(fid, id\).*}sm,
79                 );
80                 is(scalar(@{$exec_history}), 2);
81                 is($exec_history->[0]->{params}->[0], $fid);
82                 is($exec_history->[0]->{params}->[1], $scan->{id});
83                 is($exec_history->[1]->{params}->[0], $fid);
84                 is($exec_history->[1]->{params}->[1], $scan->{id});
85         }
86         {
87                 my $sth = $history->[2];
88                 my $exec_history = $sth->{execution_history};
89                 like($sth->statement,
90                         qr{INSERT INTO fleet_ships \(fid, ship, amount\).*}sm,
91                 );
92                 is(scalar(@{$exec_history}), 5);
93                 is($exec_history->[0]->{params}->[0], $fid);
94                 is($exec_history->[0]->{params}->[1], 'Centaur');
95                 is($exec_history->[0]->{params}->[2], 199900);
96                 is($exec_history->[1]->{params}->[0], $fid);
97                 is($exec_history->[1]->{params}->[1], 'Titan');
98                 is($exec_history->[1]->{params}->[2], 50000);
99                 is($exec_history->[2]->{params}->[0], $fid);
100                 is($exec_history->[2]->{params}->[1], 'Wyvern');
101                 is($exec_history->[2]->{params}->[2], 3000);
102                 is($exec_history->[3]->{params}->[0], $fid);
103                 is($exec_history->[3]->{params}->[1], 'Medusa');
104                 is($exec_history->[3]->{params}->[2], 1001);
105                 is($exec_history->[4]->{params}->[0], $fid);
106                 is($exec_history->[4]->{params}->[1], 'Chimera');
107                 is($exec_history->[4]->{params}->[2], 54500);
108         }
109 }
110
111 done_testing();