Skip to content

Commit

Permalink
Merge pull request #33 from keboola/COM-595-ondra-implements-datadir-…
Browse files Browse the repository at this point in the history
…tests

Implements datadir tests
  • Loading branch information
ondrajodas authored Jan 4, 2021
2 parents 696930d + 54f3426 commit eb5b989
Show file tree
Hide file tree
Showing 55 changed files with 355 additions and 278 deletions.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"require-dev": {
"keboola/coding-standard": ">=7.0.2",
"keboola/datadir-tests": "^5.2",
"php-parallel-lint/php-parallel-lint": "^1.2",
"phpstan/phpstan": "^0.12.64",
"phpunit/phpunit": "^9.5"
Expand All @@ -30,13 +31,16 @@
},
"autoload-dev": {
"psr-4": {
"Keboola\\GoogleDriveExtractor\\Tests\\": "tests/phpunit/"
"Keboola\\GoogleDriveExtractor\\Tests\\": "tests/phpunit/",
"Keboola\\GoogleDriveExtractor\\FunctionalTests\\": "tests/functional/"
}
},
"scripts": {
"tests-phpunit": "phpunit",
"tests-datadir": "phpunit tests/functional",
"tests": [
"@tests-phpunit"
"@tests-phpunit",
"@tests-datadir"
],

"phpstan": "phpstan analyse ./src ./tests --level=max --no-progress -c phpstan.neon",
Expand Down
49 changes: 47 additions & 2 deletions composer.lock

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

4 changes: 0 additions & 4 deletions src/Configuration/ConfigDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ protected function getParametersDefinition(): ArrayNodeDefinition
$parametersNode = parent::getParametersDefinition();
$parametersNode
->children()
->integerNode('id')
->isRequired()
->min(0)
->end()
->scalarNode('fileId')
->isRequired()
->cannotBeEmpty()
Expand Down
2 changes: 1 addition & 1 deletion src/Extractor/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function createCsv(): string
mkdir($outTablesDir, 0777, true);
}

$filename = $outTablesDir . '/' . $this->config->getFileId() . '_' . $this->config->getSheetId() . '.csv';
$filename = $outTablesDir . '/' . $this->config->getOutputTable() . '.csv';
touch($filename);

$this->csv = new CsvWriter($filename);
Expand Down
105 changes: 105 additions & 0 deletions tests/functional/DatadirTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace Keboola\GoogleDriveExtractor\FunctionalTests;

use Keboola\DatadirTests\DatadirTestCase;
use Keboola\DatadirTests\DatadirTestSpecificationInterface;
use Keboola\Google\ClientBundle\Google\RestApi;
use Keboola\GoogleDriveExtractor\GoogleDrive\Client;
use Keboola\Temp\Temp;
use Symfony\Component\Finder\Finder;
use \Throwable;

class DatadirTest extends DatadirTestCase
{
/**
* @dataProvider provideDatadirSpecifications
*/
public function testDatadir(DatadirTestSpecificationInterface $specification): void
{
$tempDatadir = $this->getTempDatadir($specification);

$googleDriveApi = new Client(
new RestApi(
(string) getenv('CLIENT_ID'),
(string) getenv('CLIENT_SECRET'),
(string) getenv('ACCESS_TOKEN'),
(string) getenv('REFRESH_TOKEN')
)
);

$testFileInfo = $this->prepareTestFile(
$googleDriveApi,
(string) $specification->getSourceDatadirDirectory()
);

$this->prepareConfigFile($tempDatadir, $testFileInfo);

$process = $this->runScript($tempDatadir->getTmpFolder());

$this->removeTestFile($googleDriveApi, $testFileInfo['spreadsheetId']);

$this->assertMatchesSpecification($specification, $process, $tempDatadir->getTmpFolder());
}

private function prepareTestFile(Client $googleDriveApi, string $sourceDatadir): array
{
$finder = new Finder();
$finder->in(sprintf('%s/setup/files/', $sourceDatadir));
$finder->files();

$items = iterator_to_array($finder->getIterator());

/** @var \SplFileInfo $item */
$item = current($items);
$file = $googleDriveApi->createFile(
$item->getPathname(),
$item->getBasename('.' . $item->getExtension())
);
return $googleDriveApi->getSpreadsheet($file['id']);
}

private function prepareConfigFile(Temp $tempDatadir, array $testFileInfo): void
{
$configPath = $tempDatadir->getTmpFolder() . '/config.json';

$content = (string) file_get_contents($configPath);

$jsonContent = json_decode($content, true);

$jsonContent['authorization']['oauth_api']['credentials'] = [
'appKey' => (string) getenv('CLIENT_ID'),
'#appSecret' => (string) getenv('CLIENT_SECRET'),
'#data' => json_encode(
[
'access_token' => (string) getenv('ACCESS_TOKEN'),
'refresh_token' => (string) getenv('REFRESH_TOKEN'),
]
),
];

$parameters = $jsonContent['parameters'];

$jsonContent['parameters'] = array_merge(
$parameters,
[
'fileId' => $parameters['fileId'] ?? $testFileInfo['spreadsheetId'],
'fileTitle' => $parameters['fileTitle'] ?? $testFileInfo['properties']['title'],
'sheetId' => $parameters['sheetId'] ?? $testFileInfo['sheets'][0]['properties']['sheetId'],
'sheetTitle' => $parameters['sheetTitle'] ?? $testFileInfo['sheets'][0]['properties']['title'],
]
);

file_put_contents($configPath, json_encode($jsonContent));
}

private function removeTestFile(Client $googleDriveApi, string $spreadsheetId): void
{
try {
$googleDriveApi->deleteFile($spreadsheetId);
} catch (Throwable $e) {
}
}
}
1 change: 1 addition & 0 deletions tests/functional/correct-run/expected-code
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Empty file.
3 changes: 3 additions & 0 deletions tests/functional/correct-run/expected-stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Obtained spreadsheet metadata
Extracting sheet titanic
Extracting rows 1 to 1001
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"incremental":false}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{
"parameters": {
"id": 0,
"fileId": null,
"fileTitle": null,
"sheetId": null,
"sheetTitle": null,
"outputTable": null,
"header": {
"rows": 1
}
},
"outputTable": "titanic-output"
}
}
33 changes: 33 additions & 0 deletions tests/functional/correct-run/source/data/setup/files/titanic.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"Id","Class","Sex","Age","Survived","Freq"
"1","1st","Male","Child","No","0"
"2","2nd","Male","Child","No","0"
"3","3rd","Male","Child","No","35"
"4","Crew","Male","Child","No","0"
"5","1st","Female","Child","No","0"
"6","2nd","Female","Child","No","0"
"7","3rd","Female","Child","No","17"
"8","Crew","Female","Child","No","0"
"9","1st","Male","Adult","No","118"
"10","2nd","Male","Adult","No","154"
"11","3rd","Male","Adult","No","387"
"12","Crew","Male","Adult","No","670"
"13","1st","Female","Adult","No","4"
"14","2nd","Female","Adult","No","13"
"15","3rd","Female","Adult","No","89"
"16","Crew","Female","Adult","No","3"
"17","1st","Male","Child","Yes","5"
"18","2nd","Male","Child","Yes","11"
"19","3rd","Male","Child","Yes","13"
"20","Crew","Male","Child","Yes","0"
"21","1st","Female","Child","Yes","1"
"22","2nd","Female","Child","Yes","13"
"23","3rd","Female","Child","Yes","14"
"24","Crew","Female","Child","Yes","0"
"25","1st","Male","Adult","Yes","57"
"26","2nd","Male","Adult","Yes","14"
"27","3rd","Male","Adult","Yes","75"
"28","Crew","Male","Adult","Yes","192"
"29","1st","Female","Adult","Yes","140"
"30","2nd","Female","Adult","Yes","80"
"31","3rd","Female","Adult","Yes","76"
"32","Crew","Female","Adult","Yes","20"
1 change: 1 addition & 0 deletions tests/functional/empty-file/expected-code
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Empty file.
3 changes: 3 additions & 0 deletions tests/functional/empty-file/expected-stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Obtained spreadsheet metadata
Extracting sheet empty
Extracting rows 1 to 1001
Empty file.
Empty file.
12 changes: 12 additions & 0 deletions tests/functional/empty-file/source/data/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"parameters": {
"fileId": null,
"fileTitle": null,
"sheetId": null,
"sheetTitle": null,
"header": {
"rows": 1
},
"outputTable": "empty"
}
}
Empty file.
1 change: 1 addition & 0 deletions tests/functional/invalid-spreadsheet-id/expected-code
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions tests/functional/invalid-spreadsheet-id/expected-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sheet id "18293729" not found
2 changes: 2 additions & 0 deletions tests/functional/invalid-spreadsheet-id/expected-stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Obtained spreadsheet metadata
Extracting sheet titanic
Empty file.
Empty file.
12 changes: 12 additions & 0 deletions tests/functional/invalid-spreadsheet-id/source/data/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"parameters": {
"fileId": null,
"fileTitle": null,
"sheetId": 18293729,
"sheetTitle": null,
"header": {
"rows": 1
},
"outputTable": "titanic-output"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"Id","Class","Sex","Age","Survived","Freq"
"1","1st","Male","Child","No","0"
"2","2nd","Male","Child","No","0"
"3","3rd","Male","Child","No","35"
"4","Crew","Male","Child","No","0"
"5","1st","Female","Child","No","0"
"6","2nd","Female","Child","No","0"
"7","3rd","Female","Child","No","17"
"8","Crew","Female","Child","No","0"
"9","1st","Male","Adult","No","118"
"10","2nd","Male","Adult","No","154"
"11","3rd","Male","Adult","No","387"
"12","Crew","Male","Adult","No","670"
"13","1st","Female","Adult","No","4"
"14","2nd","Female","Adult","No","13"
"15","3rd","Female","Adult","No","89"
"16","Crew","Female","Adult","No","3"
"17","1st","Male","Child","Yes","5"
"18","2nd","Male","Child","Yes","11"
"19","3rd","Male","Child","Yes","13"
"20","Crew","Male","Child","Yes","0"
"21","1st","Female","Child","Yes","1"
"22","2nd","Female","Child","Yes","13"
"23","3rd","Female","Child","Yes","14"
"24","Crew","Female","Child","Yes","0"
"25","1st","Male","Adult","Yes","57"
"26","2nd","Male","Adult","Yes","14"
"27","3rd","Male","Adult","Yes","75"
"28","Crew","Male","Adult","Yes","192"
"29","1st","Female","Adult","Yes","140"
"30","2nd","Female","Adult","Yes","80"
"31","3rd","Female","Adult","Yes","76"
"32","Crew","Female","Adult","Yes","20"
1 change: 1 addition & 0 deletions tests/functional/multiple-header/expected-code
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Empty file.
3 changes: 3 additions & 0 deletions tests/functional/multiple-header/expected-stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Obtained spreadsheet metadata
Extracting sheet multiple_header
Extracting rows 1 to 1001
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"incremental":false}
12 changes: 12 additions & 0 deletions tests/functional/multiple-header/source/data/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"parameters": {
"fileId": null,
"fileTitle": null,
"sheetId": null,
"sheetTitle": null,
"header": {
"rows": 2
},
"outputTable": "multiple_header"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"Col_1_0","Col_1_1","Col_1_2","Col_1_3","Col_1_4","Col_1_5","Col_1_6"
"Col_2_0","Col_2_1","Col_2_2","Col_2_3","Col_2_4"
"Col_3_0","Col_3_1","Col_3_2","Col_3_3","Col_3_4","Col_3_5","Col_3_6","Col_3_7","Col_3_8","Col_3_9","Col_3_10","Col_3_11","Col_3_12","Col_3_13","Col_3_14","Col_3_15","Col_3_16","Col_3_17","Col_3_18","Col_3_19"
"Col_4_0","Col_4_1"
"Col_5_0","Col_5_1","Col_5_2","Col_5_3","Col_5_4","Col_5_5","Col_5_6"
1 change: 1 addition & 0 deletions tests/functional/not-sanitize-header/expected-code
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Empty file.
3 changes: 3 additions & 0 deletions tests/functional/not-sanitize-header/expected-stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Obtained spreadsheet metadata
Extracting sheet not-sanitize
Extracting rows 1 to 1001
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"Weird-[]./;-_*Chars", "Second column", "# poops per day"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"incremental":false}
Loading

0 comments on commit eb5b989

Please sign in to comment.