Skip to content

Commit

Permalink
Adjust SheetRange & GoogleSheetExtractor with to low values (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd authored Oct 22, 2023
1 parent 3c732d0 commit 785ff55
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(
private readonly int $rowsInBatch,
private readonly array $options = [],
) {
if ($this->rowsInBatch <= 0) {
if ($this->rowsInBatch < 1) {

Check warning on line 28 in src/adapter/etl-adapter-google-sheet/src/Flow/ETL/Adapter/GoogleSheet/GoogleSheetExtractor.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (locked, 8.1, ubuntu-latest)

Escaped Mutant for Mutator "LessThan": --- Original +++ New @@ @@ */ public function __construct(private readonly Sheets $service, private readonly string $spreadsheetId, private readonly Columns $columnRange, private readonly bool $withHeader, private readonly int $rowsInBatch, private readonly array $options = []) { - if ($this->rowsInBatch < 1) { + if ($this->rowsInBatch <= 1) { throw new InvalidArgumentException('Rows in batch must be greater than 0'); } }
throw new InvalidArgumentException('Rows in batch must be greater than 0');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,23 @@ public function __construct(
public readonly int $startRow,
public readonly int $endRow,
) {
if ($this->startRow<=0) {
throw new InvalidArgumentException(\sprintf(
'Start row `%d` must be greater than 0',
$this->startRow
));
if ($this->startRow < 1) {
throw new InvalidArgumentException(\sprintf('Start row "%d" must be greater than 0', $this->startRow));
}

if ($this->endRow<=0) {
throw new InvalidArgumentException(\sprintf(
'End row `%d` must be greater than 0',
$this->endRow
));
if ($this->endRow < 1) {
throw new InvalidArgumentException(\sprintf('End row "%d" must be greater than 0', $this->endRow));
}

if ($this->endRow<$this->startRow) {
throw new InvalidArgumentException(\sprintf(
'End row `%d` must be greater or equal to start row `%d`',
$this->endRow,
$this->startRow
));
if ($this->endRow < $this->startRow) {
throw new InvalidArgumentException(\sprintf('End row "%d" must be greater or equal to start row "%d"', $this->endRow, $this->startRow));
}
}

public function nextRows(int $count) : self
{
if ($count <= 0) {
throw new InvalidArgumentException(\sprintf(
'Count `%d` must be greater than 0',
$count
));
if ($count < 1) {
throw new InvalidArgumentException(\sprintf('Count "%d" must be greater than 0', $count));
}

return new self(
Expand All @@ -54,7 +41,7 @@ public function nextRows(int $count) : self

public function toString() : string
{
return \sprintf(
return \sprintf(
'%s!%s%d:%s%d',
$this->columnRange->sheetName,
$this->columnRange->startColumn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Flow\ETL\ConfigBuilder;
use Flow\ETL\DSL\Entry;
use Flow\ETL\DSL\GoogleSheet;
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\FlowContext;
use Flow\ETL\Row;
use Flow\ETL\Row\Entry\StringEntry;
Expand Down Expand Up @@ -54,6 +55,22 @@ public function test_its_stop_fetching_data_if_processed_row_count_is_less_then_
$this->assertEquals(Row::create($sheetNameEntry, $spreadSheetIdEntry, Entry::string('header', 'row2')), $rowsArray[1]->first());
}

public function test_rows_in_batch_must_be_positive_integer() : void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Rows in batch must be greater than 0');

GoogleSheet::from_columns(
$this->createMock(Sheets::class),
'spread-id',
'sheet',
'A',
'B',
true,
0
);
}

public function test_works_for_no_data() : void
{
$extractor = GoogleSheet::from_columns(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Flow\ETL\Adapter\GoogleSheet\Columns;
use Flow\ETL\Adapter\GoogleSheet\SheetRange;
use Flow\ETL\Exception\InvalidArgumentException;
use PHPUnit\Framework\TestCase;

final class SheetRangeTest extends TestCase
Expand Down Expand Up @@ -32,15 +33,15 @@ public static function invalid_cases() : \Generator
{
yield 'start row under 0' => [
0, 1,
'Start row `0` must be greater than 0',
'Start row "0" must be greater than 0',
];
yield 'end row under 0' => [
1, 0,
'End row `0` must be greater than 0',
'End row "0" must be greater than 0',
];
yield 'end row greater or equal to start row 0' => [
19, 10,
'End row `10` must be greater or equal to start row `19`',
'End row "10" must be greater or equal to start row "19"',
];
}

Expand All @@ -49,9 +50,10 @@ public static function invalid_cases() : \Generator
*/
public function test_assertions(int $startRow, int $endRow, string $expectedExceptionMessage) : void
{
$columnRange = new Columns('Sheet2', 'A', 'B');
$this->expectExceptionMessage($expectedExceptionMessage);
new SheetRange($columnRange, $startRow, $endRow);
$this->expectException(InvalidArgumentException::class);

new SheetRange(new Columns('Sheet2', 'A', 'B'), $startRow, $endRow);
}

public function test_next_rows_range() : void
Expand Down

0 comments on commit 785ff55

Please sign in to comment.