-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'count_not_empty' rules to aggregate functions (#41)
Expanded the aggregate functions by including 'count_not_empty' in the application schema to count only not empty values. This new functionality is accurately portrayed in the updated files full.json, full.php, and full.yml. To support this change, a new class 'ComboCountNotEmpty' is added in Csv-Blueprint along with its corresponding unit test.
- Loading branch information
Showing
7 changed files
with
181 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Csv-Blueprint. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Csv-Blueprint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\CsvBlueprint\Rules\Aggregate; | ||
|
||
final class ComboCountNotEmpty extends AbstarctAggregateRuleCombo | ||
{ | ||
protected const NAME = 'number of not empty rows'; | ||
|
||
protected const HELP_TOP = ['Counts only not empty values (string length is not 0).']; | ||
|
||
protected const HELP_OPTIONS = [ | ||
self::EQ => ['5', ''], | ||
self::NOT => ['4', ''], | ||
self::MIN => ['1', ''], | ||
self::MAX => ['10', ''], | ||
]; | ||
|
||
protected function getActualAggregate(array $colValues): float | ||
{ | ||
return \count(\array_filter($colValues, static fn ($colValue) => $colValue !== '')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Csv-Blueprint. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Csv-Blueprint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\PHPUnit\Rules\Aggregate; | ||
|
||
use JBZoo\CsvBlueprint\Rules\AbstarctRule as Combo; | ||
use JBZoo\CsvBlueprint\Rules\Aggregate\ComboCountNotEmpty; | ||
use JBZoo\PHPUnit\Rules\AbstractAggregateRuleCombo; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
class ComboCountNotEmptyTest extends AbstractAggregateRuleCombo | ||
{ | ||
protected string $ruleClass = ComboCountNotEmpty::class; | ||
|
||
public function testEqual(): void | ||
{ | ||
$rule = $this->create(1, Combo::EQ); | ||
|
||
isSame('', $rule->test(['', '', ' '])); | ||
isSame('', $rule->test(['', '', '', '1'])); | ||
|
||
isSame( | ||
'The number of not empty rows in the column is "2", which is not equal than the expected "1"', | ||
$rule->test(['', '', ' ', '1']), | ||
); | ||
} | ||
|
||
public function testNotEqual(): void | ||
{ | ||
$rule = $this->create(1, Combo::NOT); | ||
|
||
isSame('', $rule->test(['', ''])); | ||
|
||
isSame( | ||
'The number of not empty rows in the column is "1", which is equal than the not expected "1"', | ||
$rule->test(['', '1']), | ||
); | ||
} | ||
|
||
public function testMin(): void | ||
{ | ||
$rule = $this->create(1, Combo::MIN); | ||
|
||
isSame('', $rule->test(['', '', '', '1'])); | ||
|
||
isSame( | ||
'The number of not empty rows in the column is "0", which is less than the expected "1"', | ||
$rule->test(['']), | ||
); | ||
} | ||
|
||
public function testMax(): void | ||
{ | ||
$rule = $this->create(3, Combo::MAX); | ||
|
||
isSame('', $rule->test(['', '', ''])); | ||
isSame('', $rule->test(['', ''])); | ||
isSame('', $rule->test([])); | ||
|
||
isSame( | ||
'The number of not empty rows in the column is "5", which is greater than the expected "3"', | ||
$rule->test([1, 2, 3, 4, 5]), | ||
); | ||
} | ||
|
||
public function testInvalidOption(): void | ||
{ | ||
$this->expectExceptionMessage( | ||
'Invalid option "[1, 2]" for the "ag:count_not_empty_max" rule. It should be integer/float.', | ||
); | ||
$rule = $this->create([1, 2], Combo::MAX); | ||
$rule->validate(['1', '2', '3']); | ||
} | ||
|
||
public function testInvalidParsing(): void | ||
{ | ||
$rule = $this->create(0, Combo::EQ); | ||
isSame('', $rule->test([])); | ||
} | ||
} |