Skip to content

Commit

Permalink
Added more output styles for to_output() loader (#1238)
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech authored Sep 29, 2024
1 parent 34d5526 commit 4706273
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/core/etl/src/Flow/ETL/Loader/StreamLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public function load(Rows $rows, FlowContext $context) : void
\fwrite(
$stream,
match ($this->output) {
Output::rows_count => 'Rows: ' . $rows->count() . "\n",
Output::column_count => 'Columns: ' . $rows->schema()->count() . "\n",
Output::rows_and_column_count => 'Rows: ' . $rows->count() . ', Columns: ' . $rows->schema()->count() . "\n",
Output::rows => $this->formatter->format($rows, $this->truncate),
Output::schema => $this->schemaFormatter->format($rows->schema()),
Output::rows_and_schema => $this->formatter->format($rows, $this->truncate) . "\n" . $this->schemaFormatter->format($rows->schema()),
Expand Down
3 changes: 3 additions & 0 deletions src/core/etl/src/Flow/ETL/Loader/StreamLoader/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

enum Output
{
case column_count;
case rows;
case rows_and_column_count;
case rows_and_schema;
case rows_count;
case schema;
}
86 changes: 82 additions & 4 deletions src/core/etl/tests/Flow/ETL/Tests/Unit/Loader/StreamLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@

final class StreamLoaderTest extends TestCase
{
public function test_columns_count_to_php_output_stream() : void
{
$loader = to_output(false, StreamLoader\Output::column_count);

\ob_start();

$loader->load(
rows(
row(int_entry('id', 1), str_entry('name', 'id_1')),
row(int_entry('id', 2), str_entry('name', 'id_2')),
row(int_entry('id', 3), str_entry('name', 'id_3'))
),
new FlowContext(Config::default())
);
$output = \ob_get_contents();
\ob_end_clean();

self::assertSame(
<<<'ASCII'
Columns: 2

ASCII,
$output
);
}

public function test_loading_data_into_invalid_stream() : void
{
$this->expectExceptionMessage("Can't open stream for url: php://qweqweqw in mode: w");
Expand All @@ -30,7 +56,7 @@ public function test_loading_data_into_invalid_stream() : void
);
}

public function test_loading_partitioned_rows_into_php_memory_stream() : void
public function test_loading_partitioned_rows_into_php_output_stream() : void
{
$loader = new StreamLoader('php://output', Mode::WRITE, 0);

Expand Down Expand Up @@ -64,7 +90,7 @@ public function test_loading_partitioned_rows_into_php_memory_stream() : void
);
}

public function test_loading_rows_and_schema_into_php_memory_stream() : void
public function test_loading_rows_and_schema_into_output_stream() : void
{
$loader = to_output(false, StreamLoader\Output::rows_and_schema);

Expand Down Expand Up @@ -101,7 +127,7 @@ public function test_loading_rows_and_schema_into_php_memory_stream() : void
);
}

public function test_loading_rows_into_php_memory_stream() : void
public function test_loading_rows_into_php_output_stream() : void
{
$loader = new StreamLoader('php://output', Mode::WRITE, 0);

Expand Down Expand Up @@ -133,7 +159,7 @@ public function test_loading_rows_into_php_memory_stream() : void
);
}

public function test_loading_schema_into_php_memory_stream() : void
public function test_loading_schema_into_php_output_stream() : void
{
$loader = new StreamLoader('php://output', Mode::WRITE, 0, StreamLoader\Output::schema);

Expand All @@ -156,6 +182,58 @@ public function test_loading_schema_into_php_memory_stream() : void
|-- id: integer
|-- name: string

ASCII,
$output
);
}

public function test_rows_and_columns_count_to_php_output_stream() : void
{
$loader = to_output(false, StreamLoader\Output::rows_and_column_count);

\ob_start();

$loader->load(
rows(
row(int_entry('id', 1), str_entry('name', 'id_1')),
row(int_entry('id', 2), str_entry('name', 'id_2')),
row(int_entry('id', 3), str_entry('name', 'id_3'))
),
new FlowContext(Config::default())
);
$output = \ob_get_contents();
\ob_end_clean();

self::assertSame(
<<<'ASCII'
Rows: 3, Columns: 2

ASCII,
$output
);
}

public function test_rows_count_to_php_output_stream() : void
{
$loader = to_output(false, StreamLoader\Output::rows_count);

\ob_start();

$loader->load(
rows(
row(int_entry('id', 1), str_entry('name', 'id_1')),
row(int_entry('id', 2), str_entry('name', 'id_2')),
row(int_entry('id', 3), str_entry('name', 'id_3'))
),
new FlowContext(Config::default())
);
$output = \ob_get_contents();
\ob_end_clean();

self::assertSame(
<<<'ASCII'
Rows: 3

ASCII,
$output
);
Expand Down

0 comments on commit 4706273

Please sign in to comment.