Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/PST-1507 - Mark run as error if all fetched tables fails #29

Merged
merged 8 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ App is developed on localhost using TDD.

1. Clone from repository: `git clone [email protected]:keboola/google-drive-writer.git`
2. Change directory: `cd google-drive-writer`
3. Install dependencies: `composer install --no-interaction`
4. Create `tests.sh` file from template `tests.sh.template`.
5. You will need working OAuth credentials.
3. Install dependencies: `docker compose run --rm dev composer install`
4. You will need working OAuth credentials.
- Go to Googles [OAuth 2.0 Playground](https://developers.google.com/oauthplayground).
- In the configuration (the cog wheel on the top right side) check `Use your own OAuth credentials` and paste your OAuth Client ID and Secret.
- Go through the authorization flow and generate `Access` and `Refresh` tokens. Copy and paste them into the `tests.sh` file.
6. Run the tests: `./tests.sh`
- Go through the authorization flow and generate `Access` and `Refresh` tokens. Copy and paste them into the `.env` file.
5. Run the tests: `docker compose run --rm dev composer tests`
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"pimple/pimple": "^3.0",
"monolog/monolog": "^1.17",
"symfony/config": "^4.0",
"symfony/process": "^4.0",
"symfony/process": "^5.4",
"guzzlehttp/guzzle": "^6.2",
"keboola/php-temp": "^0.1.6",
"keboola/google-sheets-client": "^1.5",
Expand All @@ -39,6 +39,7 @@
"phpcs": "phpcs -n --ignore=vendor --extensions=php .",
"phpcbf": "phpcbf -n --ignore=vendor --extensions=php .",
"phpstan": "phpstan analyse --no-progress --level=max src tests -c phpstan.neon",
"phpstan-baseline": "phpstan analyse --no-progress --level=max src tests -c phpstan.neon --generate-baseline",
"build": [
"@phpstan",
"@phpcs",
Expand Down
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ parameters:
count: 1
path: src/Keboola/GoogleDriveWriter/Application.php

-
message: "#^Parameter \\#1 \\$tables of method Keboola\\\\GoogleDriveWriter\\\\Writer\\:\\:processTables\\(\\) expects array, mixed given\\.$#"
count: 1
path: src/Keboola/GoogleDriveWriter/Application.php

-
message: "#^Parameter \\#2 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
count: 1
Expand Down Expand Up @@ -140,11 +135,6 @@ parameters:
count: 1
path: tests/FunctionalTest.php

-
message: "#^Parameter \\#1 \\$command of class Symfony\\\\Component\\\\Process\\\\Process constructor expects array, string given\\.$#"
count: 1
path: tests/FunctionalTest.php

-
message: "#^Parameter \\#1 \\$fileId of method Keboola\\\\GoogleSheetsClient\\\\Client\\:\\:getFile\\(\\) expects string, mixed given\\.$#"
count: 3
Expand Down
33 changes: 29 additions & 4 deletions src/Keboola/GoogleDriveWriter/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,14 @@ protected function runAction(): array
/** @var Writer $writer */
$writer = $this->container['writer'];

if (!empty($this->container['parameters']['tables'])) {
$writer->processTables($this->container['parameters']['tables']);
}
$status = $this->processTables() ? 'ok' : 'error';

if (!empty($this->container['parameters']['files'])) {
$writer->processFiles($this->container['parameters']['files']);
}

return [
'status' => 'ok',
'status' => $status,
];
}

Expand Down Expand Up @@ -133,4 +131,31 @@ private function validateParameters(array $parameters): array
throw new UserException($e->getMessage(), 400, $e);
}
}

private function processTables(): bool
{
/** @var array<int, mixed> $tables */
$tables = $this->container['parameters']['tables'] ?? [];

if ($tables !== []) {
$tableCount = count($tables);

/** @var Writer $writer */
$writer = $this->container['writer'];

$response = $writer->processTables($tables);
$warningsCount = count(
array_filter(
$response,
fn ($item) => isset($item['status']) && $item['status'] === 'warning',
),
);

if ($warningsCount >= $tableCount) {
return false;
}
}

return true;
}
}
6 changes: 3 additions & 3 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,15 +448,15 @@ public function testCreateDuplicateFile(): void
];
$process2 = $this->runProcess($config2);
$this->assertEquals(1, $process2->getExitCode(), $process2->getOutput());
$this->assertContains('409 Conflict', $process2->getOutput());
$this->assertContains('A file already exists with the provided ID', $process2->getOutput());
$this->assertContains('409 Conflict', $process2->getErrorOutput());
$this->assertContains('A file already exists with the provided ID', $process2->getErrorOutput());
}

private function runProcess(array $config): Process
{
file_put_contents($this->tmpDataPath . '/config.json', json_encode($config));

$process = new Process(sprintf('php run.php --data=%s 2>&1', $this->tmpDataPath));
$process = new Process(['php', 'run.php', '--data=' . $this->tmpDataPath, '2>&1']);
$process->setTimeout(180);
$process->run();

Expand Down
Loading