Skip to content

Commit

Permalink
#4273: Datastore MySQL Import handles empty file incorrect (with a te…
Browse files Browse the repository at this point in the history
…st) (#4293)

* #4273: Datastore MySQL Import handles empty file incorrect
* Added a test for empty CSV.

Co-authored-by: Stefan Korn <drupal@stefan-korn.de>
  • Loading branch information
dmundra and stefan-korn authored Sep 18, 2024
1 parent c104b61 commit b58b750
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ protected function runIt() {
return $this->setResultError(sprintf('Unable to resolve file name "%s" for resource with identifier "%s".', $this->resource->getFilePath(), $this->resource->getId()));
}

$size = @filesize($file_path);
if (!$size) {
return $this->setResultError("Can't get size from file {$file_path}");
}

// Read the columns and EOL character sequence from the CSV file.
$delimiter = $this->resource->getMimeType() == 'text/tab-separated-values' ? "\t" : ',';
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,33 @@ public function testHasBeenImported() {
$this->assertEquals(Result::DONE, $result->getStatus(), $result->getError());
}

/**
* Test MysqlImport importer with an empty CSV file.
*/
public function testMysqlImporterWithEmptyCSVFile() {
$identifier = 'my_id';
$file_path = dirname(__FILE__, 7) . '/tests/data/empty.csv';
$data_resource = new DataResource($file_path, 'text/csv');

$import_factory = $this->container->get('dkan.datastore.service.factory.import');
$this->assertInstanceOf(MysqlImportFactory::class, $import_factory);

$import_service = $import_factory->getInstance(
$identifier,
['resource' => $data_resource]
);
$this->assertInstanceOf(ImportService::class, $import_service);
$import_service->setImporterClass(MockQueryVisibilityImport::class);
$mysql_import = $import_service->getImporter();
$this->assertInstanceOf(MockQueryVisibilityImport::class, $mysql_import);
$this->assertInstanceOf(MySqlDatabaseTable::class, $mysql_import->getStorage());

// Run the import and confirm result is valid albeit an error.
$result = $mysql_import->run();
$this->assertEquals(Result::ERROR, $result->getStatus());
$this->assertStringStartsWith("Can't get size from file", $result->getError());
}

}

class MockQueryVisibilityImport extends MysqlImport {
Expand Down
Empty file.

0 comments on commit b58b750

Please sign in to comment.