Skip to content

Commit

Permalink
Fixed handling empty headers (#942)
Browse files Browse the repository at this point in the history
* Fixed handling empty headers

* Fixed randomly failing tests
  • Loading branch information
norberttech authored Jan 31, 2024
1 parent b78bdf6 commit 0662851
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public function extract(FlowContext $context) : \Generator
$headers = \array_map(fn (int $e) : string => 'e' . \str_pad((string) $e, 2, '0', STR_PAD_LEFT), \range(0, \count($rowData) - 1));
}

$headers = \array_map(fn (string $header) : string => \trim($header), $headers);
$headers = \array_map(fn (string $header, int $index) : string => $header !== '' ? $header : 'e' . \str_pad((string) $index, 2, '0', STR_PAD_LEFT), $headers, \array_keys($headers));

while (\is_array($rowData)) {
if (\count($headers) > \count($rowData)) {
\array_push(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
,name,active
,,false
1,Norbert
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,28 @@ public function test_extracting_csv_empty_columns_as_null() : void
);
}

public function test_extracting_csv_empty_headers() : void
{
$extractor = from_csv(
__DIR__ . '/../Fixtures/file_with_empty_headers.csv'
);

$this->assertSame(
[
[
['e00' => null, 'name' => null, 'active' => 'false'],
],
[
['e00' => '1', 'name' => 'Norbert', 'active' => null],
],
],
\array_map(
fn (Rows $r) => $r->toArray(),
\iterator_to_array($extractor->extract(new FlowContext(Config::default())))
)
);
}

public function test_extracting_csv_files_from_directory_recursively() : void
{
$extractor = from_csv(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ public function test_pruning_multiple_partitions() : void
->collect()
->fetch();

$days = $rows->reduceToArray('day');
\sort($days);
$this->assertCount(2, $rows);
$this->assertSame([1, 2], $rows->reduceToArray('day'));
$this->assertSame([1, 2], $days);
}

public function test_pruning_single_partition() : void
Expand Down

0 comments on commit 0662851

Please sign in to comment.