PIREP tests working, using minutes to track flight hours instead of seconds

This commit is contained in:
Nabeel Shahzad
2017-12-13 10:56:26 -06:00
parent 86c727ba55
commit 55250b8789
13 changed files with 124 additions and 156 deletions

View File

@@ -12,41 +12,6 @@ class PIREPTest extends TestCase
#use DatabaseMigrations;
protected $pirepSvc;
protected $SAMPLE_PIREP
= [
'user_id' => 1,
'airline_id' => 1,
'flight_id' => 1,
'aircraft_id' => 1,
'dpt_airport_id' => 1,
'arr_airport_id' => 2,
'flight_time' => 21600, # 6 hours
'source' => 0, # manual
'fuel_used' => 100,
'notes' => 'just a pilot report',
];
/**
* Add $count number of PIREPs and return a User object
* @param int $count
* @param bool $accept
*
* @return User
*/
protected function addPIREP($count=1, $accept=true): User
{
for($i = 0; $i < $count; $i++) {
$pirep = new Pirep($this->SAMPLE_PIREP);
$pirep = $this->pirepSvc->create($pirep, []);
if($accept) {
$this->pirepSvc->changeStatus($pirep,
config('enums.pirep_status.ACCEPTED'));
}
}
$pilot = User::where('id', $this->SAMPLE_PIREP['user_id'])->first();
return $pilot;
}
public function setUp()
{
@@ -59,32 +24,36 @@ class PIREPTest extends TestCase
*/
public function testAddPirep()
{
$pirep = new Pirep($this->SAMPLE_PIREP);
$pirep->save();
$pirep = factory(App\Models\Pirep::class)->create();
$pirep = $this->pirepSvc->create($pirep, []);
/**
* Check the initial status info
*/
$this->assertEquals($pirep->pilot->flights, 0);
$this->assertEquals($pirep->status, config('enums.pirep_status.PENDING'));
/**
* Now set the PIREP state to ACCEPTED
*/
$this->pirepSvc->changeStatus($pirep, "1");
$this->assertEquals(1, $pirep->pilot->flights);
$this->assertEquals(21600, $pirep->pilot->flight_time);
$this->assertEquals(1, $pirep->pilot->rank_id);
$new_pirep_count = $pirep->pilot->flights + 1;
$original_flight_time = $pirep->pilot->flight_time ;
$new_flight_time = $pirep->pilot->flight_time + $pirep->flight_time;
$this->pirepSvc->changeStatus($pirep, '1');
$this->assertEquals($new_pirep_count, $pirep->pilot->flights);
$this->assertEquals($new_flight_time, $pirep->pilot->flight_time);
$this->assertEquals($pirep->arr_airport_id, $pirep->pilot->curr_airport_id);
#$this->assertEquals(1, $pirep->pilot->rank_id);
/**
* Now go from ACCEPTED to REJECTED
*/
$new_pirep_count = $pirep->pilot->flights - 1;
$new_flight_time = $pirep->pilot->flight_time - $pirep->flight_time;
$this->pirepSvc->changeStatus($pirep, config('enums.pirep_status.REJECTED'));
$this->assertEquals(0, $pirep->pilot->flights);
$this->assertEquals(0, $pirep->pilot->flight_time);
$this->assertEquals(1, $pirep->pilot->rank_id);
$this->assertEquals($new_pirep_count, $pirep->pilot->flights);
$this->assertEquals($new_flight_time, $pirep->pilot->flight_time);
//$this->assertEquals(1, $pirep->pilot->rank_id);
$this->assertEquals($pirep->arr_airport_id, $pirep->pilot->curr_airport_id);
}
@@ -93,30 +62,49 @@ class PIREPTest extends TestCase
*/
public function testPilotStatsIncr()
{
$original_pilot = User::find(1);
# Submit two PIREPs
$pilot = $this->addPIREP(2);
$pireps = factory(Pirep::class, 2)->create([
'user_id' => 1,
# 360min == 6 hours, rank should bump up
'flight_time' => 360,
]);
foreach ($pireps as $pirep) {
$this->pirepSvc->create($pirep);
$this->pirepSvc->accept($pirep);
}
$pilot = User::find(1);
$last_pirep = Pirep::where('id', $pilot->last_pirep_id)->first();
$this->assertEquals(2, $pilot->flights);
$this->assertEquals(43200, $pilot->flight_time);
$this->assertEquals(2, $pilot->rank_id);
# Make sure rank went up
$this->assertGreaterThan($original_pilot->rank_id, $pilot->rank_id);
$this->assertEquals($last_pirep->arr_airport_id, $pilot->curr_airport_id);
#
# Submit another PIREP, adding another 6 hours
# it should automatically be accepted
#
$pilot = $this->addPIREP(1, false);
$pirep = factory(Pirep::class)->create([
'user_id' => 1,
# 120min == 2 hours, currently at 9 hours
# Rank bumps up at 10 hours
'flight_time' => 120,
]);
# Pilot should be at rank 2, where accept should be automatic
$this->pirepSvc->create($pirep);
$pilot->refresh();
$latest_pirep = Pirep::where('id', $pilot->last_pirep_id)->first();
# Make sure PIREP was auto updated
$this->assertEquals(config('enums.pirep_status.ACCEPTED'), $latest_pirep->status);
# Make sure latest PIREP was updated
$this->assertNotEquals($last_pirep->id, $latest_pirep->id);
# The PIREP should have been automatically accepted
$this->assertEquals(
config('enums.pirep_status.ACCEPTED'),
$latest_pirep->status
);
}
public function testPirepFinances()

View File

@@ -26,16 +26,16 @@ class UtilsTest extends TestCase
}
public function testSecondsToTime() {
$t = Utils::secondsToTime(3600);
$t = Utils::secondsToTimeString(3600);
$this->assertEquals('1h 0m', $t);
$t = Utils::secondsToTime(3720);
$t = Utils::secondsToTimeString(3720);
$this->assertEquals('1h 2m', $t);
$t = Utils::secondsToTime(3722);
$t = Utils::secondsToTimeString(3722);
$this->assertEquals('1h 2m', $t);
$t = Utils::secondsToTime(3722, true);
$t = Utils::secondsToTimeString(3722, true);
$this->assertEquals('1h 2m 2s', $t);
}
}

View File

@@ -20,25 +20,12 @@ users:
rank_id: 1
created_at: now
updated_at: now
- id: 2
name: Test User
email: test@phpvms.net
password: test
api_key: testuserapikey
airline_id: 1
home_airport_id: KJFK
curr_airport_id: KJFK
rank_id: 1
created_at: now
updated_at: now
role_user:
- user_id: 1
role_id: 1
- user_id: 1
role_id: 2
- user_id: 2
role_id: 2
ranks:
- id: 1
@@ -58,39 +45,6 @@ ranks:
auto_approve_manual: 1
auto_promote: 0
airports:
- id: KAUS
icao: KAUS
name: Austin-Bergstrom
location: Austin, Texas, USA
lat: 30.1945278
lon: -97.6698889
fuel_jeta_cost: 100
- id: KJFK
icao: KJFK
name: John F Kennedy
location: New York, New York, USA
lat: 40.6399257
lon: -73.7786950
fuel_jeta_cost: 50
aircraft:
- id: 1
subfleet_id: 1
name: Boeing 747-400
registration: NC17
tail_number: 17
- id: 2
subfleet_id: 2
name: Boeing 777-200
registration: NC20
tail_number: 20
- id: 3
subfleet_id: 1
name: Boeing 747-400-PW
registration: PW744
tail_number: 207X
fares:
- id: 1
code: Y
@@ -108,20 +62,6 @@ fares:
price: 800
capacity: 5
subfleets:
- id: 1
airline_id: 1
name: 747-400 Winglets
type: 744W
fuel_type: 1
fuel_capacity: 2000
- id: 2
airline_id: 1
name: 777-200 LR
type: 772-LR
fuel_type: 1
fuel_capacity: 1000
# add a few mods to aircraft and fares
subfleet_fare:
@@ -148,11 +88,3 @@ subfleet_fare:
subfleet_flight:
- subfleet_id: 1
flight_id: 1
flights:
- id: 1
airline_id: 1
flight_number: 100
dpt_airport_id: KAUS
arr_airport_id: KJFK
route: KAUS KJFK