Importer fixes for dirty fields (#1623)
* Importer fixes for dirty fields * Code cleanup
This commit is contained in:
parent
91c0733243
commit
fce71e96d9
@ -32,6 +32,7 @@ use Kyslik\ColumnSortable\Sortable;
|
||||
* @property float lat
|
||||
* @property float lon
|
||||
* @property int elevation
|
||||
* @property bool hub
|
||||
*/
|
||||
class Airport extends Model
|
||||
{
|
||||
|
||||
@ -89,7 +89,7 @@ class AircraftImporter extends ImportExport
|
||||
'registration' => $row['registration'],
|
||||
], $row);
|
||||
} catch (\Exception $e) {
|
||||
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
|
||||
$this->errorLog('Error in row '.($index + 1).': '.$e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@ -48,13 +48,13 @@ class AirportImporter extends ImportExport
|
||||
$row['id'] = $row['icao'];
|
||||
$row['hub'] = get_truth_state($row['hub']);
|
||||
|
||||
if ($row['ground_handling_cost'] === null && $row['ground_handling_cost'] !== 0.0) {
|
||||
if (!is_numeric($row['ground_handling_cost'])) {
|
||||
$row['ground_handling_cost'] = (float) setting('airports.default_ground_handling_cost');
|
||||
} else {
|
||||
$row['ground_handling_cost'] = (float) $row['ground_handling_cost'];
|
||||
}
|
||||
|
||||
if ($row['fuel_jeta_cost'] === null && $row['fuel_jeta_cost'] !== 0.0) {
|
||||
if (!is_numeric($row['fuel_jeta_cost'])) {
|
||||
$row['fuel_jeta_cost'] = (float) setting('airports.default_jet_a_fuel_cost');
|
||||
} else {
|
||||
$row['fuel_jeta_cost'] = (float) $row['fuel_jeta_cost'];
|
||||
@ -65,7 +65,7 @@ class AirportImporter extends ImportExport
|
||||
'id' => $row['icao'],
|
||||
], $row);
|
||||
} catch (\Exception $e) {
|
||||
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
|
||||
$this->errorLog('Error in row '.($index + 1).': '.$e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@ -59,7 +59,7 @@ class ExpenseImporter extends ImportExport
|
||||
'name' => $row['name'],
|
||||
], $row);
|
||||
} catch (\Exception $e) {
|
||||
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
|
||||
$this->errorLog('Error in row '.($index + 1).': '.$e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@ -42,7 +42,7 @@ class FareImporter extends ImportExport
|
||||
'code' => $row['code'],
|
||||
], $row);
|
||||
} catch (\Exception $e) {
|
||||
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
|
||||
$this->errorLog('Error in row '.($index + 1).': '.$e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@ -121,7 +121,7 @@ class FlightImporter extends ImportExport
|
||||
try {
|
||||
$flight->save();
|
||||
} catch (\Exception $e) {
|
||||
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
|
||||
$this->errorLog('Error in row '.($index + 1).': '.$e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -144,7 +144,7 @@ class FlightImporter extends ImportExport
|
||||
$this->processFares($flight, $row['fares']);
|
||||
$this->processFields($flight, $row['fields']);
|
||||
|
||||
$this->log('Imported row '.$index);
|
||||
$this->log('Imported row '.($index + 1));
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -162,31 +162,31 @@ class FlightImporter extends ImportExport
|
||||
}
|
||||
|
||||
$days = [];
|
||||
if (strpos($day_str, '1') !== false) {
|
||||
if (str_contains($day_str, '1')) {
|
||||
$days[] = Days::MONDAY;
|
||||
}
|
||||
|
||||
if (strpos($day_str, '2') !== false) {
|
||||
if (str_contains($day_str, '2')) {
|
||||
$days[] = Days::TUESDAY;
|
||||
}
|
||||
|
||||
if (strpos($day_str, '3') !== false) {
|
||||
if (str_contains($day_str, '3')) {
|
||||
$days[] = Days::WEDNESDAY;
|
||||
}
|
||||
|
||||
if (strpos($day_str, '4') !== false) {
|
||||
if (str_contains($day_str, '4')) {
|
||||
$days[] = Days::THURSDAY;
|
||||
}
|
||||
|
||||
if (strpos($day_str, '5') !== false) {
|
||||
if (str_contains($day_str, '5')) {
|
||||
$days[] = Days::FRIDAY;
|
||||
}
|
||||
|
||||
if (strpos($day_str, '6') !== false) {
|
||||
if (str_contains($day_str, '6')) {
|
||||
$days[] = Days::SATURDAY;
|
||||
}
|
||||
|
||||
if (strpos($day_str, '7') !== false) {
|
||||
if (str_contains($day_str, '7')) {
|
||||
$days[] = Days::SUNDAY;
|
||||
}
|
||||
|
||||
|
||||
@ -64,7 +64,7 @@ class SubfleetImporter extends ImportExport
|
||||
'type' => $row['type'],
|
||||
], $row);
|
||||
} catch (\Exception $e) {
|
||||
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
|
||||
$this->errorLog('Error in row '.($index + 1).': '.$e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@ -66,9 +66,11 @@ class ImportService extends Service
|
||||
public function openCsv($csv_file)
|
||||
{
|
||||
try {
|
||||
$reader = Reader::createFromPath($csv_file);
|
||||
$reader = Reader::createFromPath($csv_file, 'r');
|
||||
$reader->setDelimiter(',');
|
||||
$reader->setHeaderOffset(0);
|
||||
$reader->setEnclosure('"');
|
||||
$reader->setEscape('\\');
|
||||
return $reader;
|
||||
} catch (Exception $e) {
|
||||
$this->throwError('Error opening CSV: '.$e->getMessage(), $e);
|
||||
@ -94,33 +96,13 @@ class ImportService extends Service
|
||||
$first_header = $cols[0];
|
||||
|
||||
$first = true;
|
||||
$records = $reader->getRecords($cols);
|
||||
$header_rows = $reader->getHeader();
|
||||
$records = $reader->getRecords($header_rows);
|
||||
foreach ($records as $offset => $row) {
|
||||
// check if the first row being read is the header
|
||||
if ($first) {
|
||||
$first = false;
|
||||
|
||||
if ($row[$first_header] !== $first_header) {
|
||||
$this->throwError('CSV file doesn\'t seem to match import type');
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Do a sanity check on the number of columns first
|
||||
if (!$importer->checkColumns($row)) {
|
||||
$importer->errorLog('Number of columns in row doesn\'t match');
|
||||
continue;
|
||||
}
|
||||
|
||||
// turn it into a collection and run some filtering
|
||||
$row = collect($row)->map(function ($val, $index) {
|
||||
$val = trim($val);
|
||||
if ($val === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
return $val;
|
||||
return str_ireplace(['\\n', '\\r'], '', $val);
|
||||
})->toArray();
|
||||
|
||||
// Try to validate
|
||||
|
||||
@ -23,7 +23,6 @@ use App\Services\ImportExport\AirportExporter;
|
||||
use App\Services\ImportExport\FlightExporter;
|
||||
use App\Services\ImportService;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class ImporterTest extends TestCase
|
||||
{
|
||||
@ -375,9 +374,10 @@ class ImporterTest extends TestCase
|
||||
*/
|
||||
public function testInvalidFileImport(): void
|
||||
{
|
||||
$this->expectException(ValidationException::class);
|
||||
// $this->expectException(ValidationException::class);
|
||||
$file_path = base_path('tests/data/aircraft.csv');
|
||||
$this->importSvc->importAirports($file_path);
|
||||
$status = $this->importSvc->importAirports($file_path);
|
||||
$this->assertCount(2, $status['errors']);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -686,6 +686,29 @@ class ImporterTest extends TestCase
|
||||
$this->assertEquals(setting('airports.default_ground_handling_cost'), $airport->ground_handling_cost);
|
||||
}
|
||||
|
||||
public function testAirportImporterInvalidInputs(): void
|
||||
{
|
||||
$file_path = base_path('tests/data/airports_errors.csv');
|
||||
$status = $this->importSvc->importAirports($file_path);
|
||||
|
||||
$this->assertCount(5, $status['success']);
|
||||
$this->assertCount(1, $status['errors']);
|
||||
|
||||
// See if it imported
|
||||
/** @var Airport $airport */
|
||||
$airport = Airport::where([
|
||||
'id' => 'CYAV',
|
||||
])->first();
|
||||
|
||||
$this->assertNotNull($airport);
|
||||
$this->assertEquals('CYAV', $airport->id);
|
||||
$this->assertEquals('', $airport->iata);
|
||||
$this->assertEquals('America/Winnipeg', $airport->timezone);
|
||||
$this->assertFalse($airport->hub);
|
||||
$this->assertEquals('50.0564003', $airport->lat);
|
||||
$this->assertEquals('-97.03250122', $airport->lon);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test importing the subfleets
|
||||
*
|
||||
|
||||
7
tests/data/airports_errors.csv
Normal file
7
tests/data/airports_errors.csv
Normal file
@ -0,0 +1,7 @@
|
||||
icao,iata,name,location,region,country,timezone,hub,lat,lon,elevation,ground_handling_cost,fuel_100ll_cost,fuel_jeta_cost,fuel_mogas_cost,notes
|
||||
\N,\N,Sun Island Resort and SPA,South Aari Atoll,,Maldives,America/Sao_Paulo,,3.488334,72.862989,,,,,,
|
||||
CYAV,\N,Winnipeg / St. Andrews Airport,Winnipeg,,Canada,America/Winnipeg,,50.0564003,-97.03250122,,,,,,
|
||||
CYAW,\N,Halifax / CFB Shearwater Heliport,Halifax,,Canada,America/Halifax,,44.639702,-63.499401,,,,,,
|
||||
CYDC,\N,Princeton Airport,Princeton,,Canada,America/Vancouver,,49.4681015,-120.5110016,,,,,,
|
||||
CYDF,YDF,Deer Lake Airport,Deer Lake,,Canada,America/St_Johns,,49.21080017,-57.39139938,,,,,,
|
||||
CYDL,YDL,Dease Lake Airport,Dease Lake,,Canada,America/Vancouver,,58.42219925,-130.0319977,,,,,,
|
||||
|
Loading…
Reference in New Issue
Block a user