From be6332936f79e4bd557e9b1a55e945065792b42a Mon Sep 17 00:00:00 2001 From: exciler Date: Tue, 23 Mar 2021 13:50:19 +0100 Subject: [PATCH] When importing flights, set subfleet name only if subfleet has been created (#1095) * when importing flights, set subfleet name only if subfleet has been created, do not update existing subfleets * add tests for flights import regarding subfleets Co-authored-by: Andreas Palm --- app/Services/ImportExport/FlightImporter.php | 2 +- tests/ImporterTest.php | 12 +++++++++++- tests/data/flights.csv | 9 +++++---- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/app/Services/ImportExport/FlightImporter.php b/app/Services/ImportExport/FlightImporter.php index d2fd6a91..5e03be69 100644 --- a/app/Services/ImportExport/FlightImporter.php +++ b/app/Services/ImportExport/FlightImporter.php @@ -209,7 +209,7 @@ class FlightImporter extends ImportExport $count = 0; $subfleets = $this->parseMultiColumnValues($col); foreach ($subfleets as $subfleet_type) { - $subfleet = Subfleet::updateOrCreate( + $subfleet = Subfleet::firstOrCreate( ['type' => $subfleet_type], ['name' => $subfleet_type] ); diff --git a/tests/ImporterTest.php b/tests/ImporterTest.php index eaa2a10f..7f63129d 100644 --- a/tests/ImporterTest.php +++ b/tests/ImporterTest.php @@ -472,7 +472,7 @@ class ImporterTest extends TestCase $file_path = base_path('tests/data/flights.csv'); $status = $this->importSvc->importFlights($file_path); - $this->assertCount(2, $status['success']); + $this->assertCount(3, $status['success']); $this->assertCount(1, $status['errors']); // See if it imported @@ -531,6 +531,16 @@ class ImporterTest extends TestCase // Check the subfleets $subfleets = $flight->subfleets; $this->assertCount(1, $subfleets); + $this->assertNotEquals('A32X', $subfleets[0]->name); + + $flight = Flight::where([ + 'airline_id' => $airline->id, + 'flight_number' => '999', + ])->first(); + $subfleets = $flight->subfleets; + $this->assertCount(2, $subfleets); + $this->assertEquals('B737', $subfleets[1]->type); + $this->assertEquals('B737', $subfleets[1]->name); } /** diff --git a/tests/data/flights.csv b/tests/data/flights.csv index 0474a0f9..db2f99a7 100644 --- a/tests/data/flights.csv +++ b/tests/data/flights.csv @@ -1,4 +1,5 @@ -airline,flight_number,route_code,route_leg,dpt_airport,arr_airport,alt_airport,days,dpt_time,arr_time,level,distance,flight_time,flight_type,load_factor, load_factor_variance,pilot_pay,route,notes,active,subfleets,fares,fields -VMS,1972,,,KAUS,KJFK,KLGA,15,0810 CST,1235 EST,350,1477,207,J,85,0,100, ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=4;Arrival Gate=C41 -" ",1972,,,KAUS,KJFK,KLGA,15,0810 CST,1235 EST,350,1477,207,J,100,10, ,ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=4;Arrival Gate=C41 -VMS,113,,,KJFK,KAUS,KDFW,15,0810 EST,1035 CST,350,,207,J,70,2,,,"Empty distance",1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=C41;Arrival Gate=2 +airline,flight_number,route_code,route_leg,dpt_airport,arr_airport,alt_airport,days,dpt_time,arr_time,level,distance,flight_time,flight_type,load_factor, load_factor_variance,pilot_pay,route,notes,active,subfleets,fares,fields +VMS,1972,,,KAUS,KJFK,KLGA,15,0810 CST,1235 EST,350,1477,207,J,85,0,100, ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=4;Arrival Gate=C41 +" ",1972,,,KAUS,KJFK,KLGA,15,0810 CST,1235 EST,350,1477,207,J,100,10, ,ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=4;Arrival Gate=C41 +VMS,113,,,KJFK,KAUS,KDFW,15,0810 EST,1035 CST,350,,207,J,70,2,,,"Empty distance",1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=C41;Arrival Gate=2 +VMS,999,,,KAUS,KJFK,KLGA,15,0810 CST,1235 EST,350,1477,207,J,85,0,100, ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",1,A32X;B737,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=4;Arrival Gate=C41