Skip to content

Commit

Permalink
Refactor test suite structure and update namespaces (#34)
Browse files Browse the repository at this point in the history
The commit renames and restructures test files, shifting them from the
`Blueprint` namespace to more specific and relevant namespaces. This
includes updating of `use` statements and class extensions accordingly.
It also relocates certain utility functions to a central `Tools` class
for shared use.
  • Loading branch information
SmetDenis committed Mar 17, 2024
1 parent 06d62ce commit 8640b7e
Show file tree
Hide file tree
Showing 26 changed files with 336 additions and 336 deletions.
38 changes: 20 additions & 18 deletions README.md
Expand Up @@ -48,12 +48,8 @@ Integrating CSV validation into CI processes promotes higher data integrity, rel
* [demo.csv](tests/fixtures/demo.csv)


## Usage

Also see demo in the [GitHub Actions](https://github.com/JBZoo/Csv-Blueprint/actions/workflows/demo.yml) file.

### Schema Definition
Define your CSV validation schema in a YAML file.
Define your CSV validation schema in a [YAML](schema-examples/full.yml). Other formats are also available: , [JSON](schema-examples/full.json), [PHP](schema-examples/full.php).

This example defines a simple schema for a CSV file with a header row, specifying that the `id` column must not be empty and must contain integer values.
Also, it checks that the `name` column has a minimum length of 3 characters.
Expand All @@ -74,6 +70,9 @@ columns:

```


### Full description of the scheme

In the [example Yml file](schema-examples/full.yml) you can find a detailed description of all features.
It's also covered by tests, so it's always up-to-date.

Expand All @@ -85,10 +84,6 @@ It's also covered by tests, so it's always up-to-date.
* You are always free to add your option anywhere (except the `rules` list) and it will be ignored. I find it convenient for additional integrations and customization.


### Schema file examples

Available formats: [YAML](schema-examples/full.yml), [JSON](schema-examples/full.json), [PHP](schema-examples/full.php).

```yml
# It's a full example of the CSV schema file in YAML format.

Expand Down Expand Up @@ -124,12 +119,12 @@ columns:
# 5. The order of rules execution is the same as in the scheme. But it doesn't matter.
# The result will be the same in any order.
# 6. Most of the rules are case-sensitive. Unless otherwise specified.
# 7. As backup plan, you alsways can use the "regex" rule.
# 7. As backup plan, you always can use the "regex" rule.

# General rules
not_empty: true # Value is not an empty string. Actually checks if the string length is not 0.
exact_value: Some string # Case-sensitive. Exact value for string in the column.
allow_values: [ y, n, "" ] # Strict set of values that are allowed. Case-sensitive.
exact_value: Some string # Exact value for string in the column.
allow_values: [ y, n, "" ] # Strict set of values that are allowed.

# Any valid regex pattern. See https://www.php.net/manual/en/reference.pcre.pattern.syntax.php.
# Of course it's an ultimatum to verify any sort of string data.
Expand Down Expand Up @@ -158,11 +153,11 @@ columns:
word_count_max: 10

# Contains rules
contains: Hello # Case-sensitive. Example: "Hello World".
contains_one: [ a, b ] # At least one of the string must be in the CSV value. Case-sensitive.
contains_all: [ a, b, c ] # All the strings must be part of a CSV value. Case-sensitive.
starts_with: "prefix " # Case-sensitive. Example: "prefix Hello World".
ends_with: " suffix" # Case-sensitive. Example: "Hello World suffix".
contains: Hello # Example: "Hello World".
contains_one: [ a, b ] # At least one of the string must be in the CSV value.
contains_all: [ a, b, c ] # All the strings must be part of a CSV value.
starts_with: "prefix " # Example: "prefix Hello World".
ends_with: " suffix" # Example: "Hello World suffix".

# Under the hood it convertes and compares as float values.
# Comparison accuracy is 12 digits after a dot.
Expand Down Expand Up @@ -223,6 +218,11 @@ columns:
```


## Usage

You can find launch examples in the [workflow demo](https://github.com/JBZoo/Csv-Blueprint/actions/workflows/demo.yml).


### As GitHub Action

```yml
Expand Down Expand Up @@ -432,10 +432,12 @@ It's random ideas and plans. No orderings and deadlines. <u>But batch processing
**Batch processing**
* If option `--csv` is not specified, then the STDIN is used. To build a pipeline in Unix-like systems.
* Discovering CSV files by `filename_pattern` in the schema file. In case you have a lot of schemas and a lot of CSV files and want to automate the process as one command.
* Flag to ignore file name pattern. It's useful when you have a lot of files and you don't want to validate the file name.
* Flag to ignore file name pattern. It's useful when you have a lot of files, and you don't want to validate the file name.

**Validation**
* More aggregate rules.
* More cell rules.
* `required` flag for the column.
* Custom cell rule as a callback. It's useful when you have a complex rule that can't be described in the schema file.
* Custom agregate rule as a callback. It's useful when you have a complex rule that can't be described in the schema file.
* Configurable keyword for null/empty values. By default, it's an empty string. But you will use `null`, `nil`, `none`, `empty`, etc. Overridable on the column level.
Expand Down
16 changes: 8 additions & 8 deletions schema-examples/full.yml
Expand Up @@ -44,12 +44,12 @@ columns:
# 5. The order of rules execution is the same as in the scheme. But it doesn't matter.
# The result will be the same in any order.
# 6. Most of the rules are case-sensitive. Unless otherwise specified.
# 7. As backup plan, you alsways can use the "regex" rule.
# 7. As backup plan, you always can use the "regex" rule.

# General rules
not_empty: true # Value is not an empty string. Actually checks if the string length is not 0.
exact_value: Some string # Case-sensitive. Exact value for string in the column.
allow_values: [ y, n, "" ] # Strict set of values that are allowed. Case-sensitive.
exact_value: Some string # Exact value for string in the column.
allow_values: [ y, n, "" ] # Strict set of values that are allowed.

# Any valid regex pattern. See https://www.php.net/manual/en/reference.pcre.pattern.syntax.php.
# Of course it's an ultimatum to verify any sort of string data.
Expand Down Expand Up @@ -78,11 +78,11 @@ columns:
word_count_max: 10

# Contains rules
contains: Hello # Case-sensitive. Example: "Hello World".
contains_one: [ a, b ] # At least one of the string must be in the CSV value. Case-sensitive.
contains_all: [ a, b, c ] # All the strings must be part of a CSV value. Case-sensitive.
starts_with: "prefix " # Case-sensitive. Example: "prefix Hello World".
ends_with: " suffix" # Case-sensitive. Example: "Hello World suffix".
contains: Hello # Example: "Hello World".
contains_one: [ a, b ] # At least one of the string must be in the CSV value.
contains_all: [ a, b, c ] # All the strings must be part of a CSV value.
starts_with: "prefix " # Example: "prefix Hello World".
ends_with: " suffix" # Example: "Hello World suffix".

# Under the hood it convertes and compares as float values.
# Comparison accuracy is 12 digits after a dot.
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Cell/AllowValues.php
Expand Up @@ -19,7 +19,7 @@
class AllowValues extends AbstarctCellRule
{
protected const HELP_OPTIONS = [
self::DEFAULT => ['[ y, n, "" ]', 'Strict set of values that are allowed. Case-sensitive.'],
self::DEFAULT => ['[ y, n, "" ]', 'Strict set of values that are allowed.'],
];

public function validateRule(string $cellValue): ?string
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Cell/Contains.php
Expand Up @@ -19,7 +19,7 @@
final class Contains extends AbstarctCellRule
{
protected const HELP_OPTIONS = [
self::DEFAULT => ['Hello', 'Case-sensitive. Example: "Hello World".'],
self::DEFAULT => ['Hello', 'Example: "Hello World".'],
];

public function validateRule(string $cellValue): ?string
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Cell/ContainsAll.php
Expand Up @@ -19,7 +19,7 @@
final class ContainsAll extends AbstarctCellRule
{
protected const HELP_OPTIONS = [
self::DEFAULT => ['[ a, b, c ]', 'All the strings must be part of a CSV value. Case-sensitive.'],
self::DEFAULT => ['[ a, b, c ]', 'All the strings must be part of a CSV value.'],
];

public function validateRule(string $cellValue): ?string
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Cell/ContainsOne.php
Expand Up @@ -19,7 +19,7 @@
final class ContainsOne extends AbstarctCellRule
{
protected const HELP_OPTIONS = [
self::DEFAULT => ['[ a, b ]', 'At least one of the string must be in the CSV value. Case-sensitive.'],
self::DEFAULT => ['[ a, b ]', 'At least one of the string must be in the CSV value.'],
];

public function validateRule(string $cellValue): ?string
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Cell/EndsWith.php
Expand Up @@ -19,7 +19,7 @@
final class EndsWith extends AbstarctCellRule
{
protected const HELP_OPTIONS = [
self::DEFAULT => ['" suffix"', 'Case-sensitive. Example: "Hello World suffix".'],
self::DEFAULT => ['" suffix"', 'Example: "Hello World suffix".'],
];

public function validateRule(string $cellValue): ?string
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Cell/ExactValue.php
Expand Up @@ -19,7 +19,7 @@
final class ExactValue extends AbstarctCellRule
{
protected const HELP_OPTIONS = [
self::DEFAULT => ['Some string', 'Case-sensitive. Exact value for string in the column.'],
self::DEFAULT => ['Some string', 'Exact value for string in the column.'],
];

public function validateRule(string $cellValue): ?string
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Cell/StartsWith.php
Expand Up @@ -19,7 +19,7 @@
final class StartsWith extends AbstarctCellRule
{
protected const HELP_OPTIONS = [
self::DEFAULT => ['"prefix "', 'Case-sensitive. Example: "prefix Hello World".'],
self::DEFAULT => ['"prefix "', 'Example: "prefix Hello World".'],
];

public function validateRule(string $cellValue): ?string
Expand Down
Expand Up @@ -14,17 +14,17 @@

declare(strict_types=1);

namespace JBZoo\PHPUnit\Blueprint;
namespace JBZoo\PHPUnit\Commands;

use JBZoo\PHPUnit\PHPUnit;
use JBZoo\PHPUnit\TestCase;
use JBZoo\PHPUnit\Tools;
use JBZoo\Utils\Cli;
use Symfony\Component\Console\Input\StringInput;

use function JBZoo\PHPUnit\isNotEmpty;
use function JBZoo\PHPUnit\isSame;

final class ValidateCsvTest extends PHPUnit
final class ValidateCsvTest extends TestCase
{
public function testValidateOneFilePositive(): void
{
Expand Down Expand Up @@ -57,8 +57,6 @@ public function testValidateOneFileNegativeTable(): void
'schema' => './tests/schemas/demo_invalid.yml', // Relative path
]);

Tools::dumpText($actual);

$expected = <<<'TXT'
Schema: ./tests/schemas/demo_invalid.yml
Found CSV files: 1
Expand Down Expand Up @@ -99,8 +97,6 @@ public function testValidateManyFileNegativeTable(): void
$optionsAsString = new StringInput(Cli::build('', $options));
[$actual, $exitCode] = Tools::virtualExecution('validate:csv', $options);

Tools::dumpText($actual);

$expected = <<<'TXT'
Schema: ./tests/schemas/demo_invalid.yml
Found CSV files: 3
Expand Down Expand Up @@ -152,8 +148,6 @@ public function testValidateOneFileNegativeText(): void
'report' => 'text',
]);

Tools::dumpText($actual);

$expected = <<<'TXT'
Schema: ./tests/schemas/demo_invalid.yml
Found CSV files: 1
Expand Down Expand Up @@ -281,8 +275,6 @@ public function testCreateValidateNegativeTeamcity(): void
'report' => 'teamcity',
]);

Tools::dumpText($actual);

$expected = <<<'TXT'
Schema: ./tests/schemas/demo_invalid.yml
Found CSV files: 3
Expand Down
21 changes: 8 additions & 13 deletions tests/Blueprint/CsvReaderTest.php → tests/Csv/CsvFileTest.php
Expand Up @@ -14,25 +14,20 @@

declare(strict_types=1);

namespace JBZoo\PHPUnit\Blueprint;
namespace JBZoo\PHPUnit\Csv;

use JBZoo\CsvBlueprint\Csv\CsvFile;
use JBZoo\PHPUnit\PHPUnit;
use JBZoo\PHPUnit\TestCase;
use JBZoo\PHPUnit\Tools;

use function JBZoo\PHPUnit\isSame;

final class CsvReaderTest extends PHPUnit
final class CsvFileTest extends TestCase
{
private const CSV_SIMPLE_HEADER = PROJECT_TESTS . '/fixtures/simple_header.csv';
private const CSV_SIMPLE_NO_HEADER = PROJECT_TESTS . '/fixtures/simple_no_header.csv';

private const SCHEMA_SIMPLE_HEADER = PROJECT_TESTS . '/schemas/simple_header.yml';
private const SCHEMA_SIMPLE_NO_HEADER = PROJECT_TESTS . '/schemas/simple_no_header.yml';

public function testReadCsvFileWithoutHeader(): void
{
$csv = new CsvFile(self::CSV_SIMPLE_NO_HEADER, self::SCHEMA_SIMPLE_NO_HEADER);
isSame(self::CSV_SIMPLE_NO_HEADER, $csv->getCsvFilename());
$csv = new CsvFile(Tools::CSV_SIMPLE_NO_HEADER, Tools::SCHEMA_SIMPLE_NO_HEADER);
isSame(Tools::CSV_SIMPLE_NO_HEADER, $csv->getCsvFilename());

isSame([], $csv->getHeader());

Expand All @@ -49,8 +44,8 @@ public function testReadCsvFileWithoutHeader(): void

public function testReadCsvFileWithHeader(): void
{
$csv = new CsvFile(self::CSV_SIMPLE_HEADER, self::SCHEMA_SIMPLE_HEADER);
isSame(self::CSV_SIMPLE_HEADER, $csv->getCsvFilename());
$csv = new CsvFile(Tools::CSV_SIMPLE_HEADER, Tools::SCHEMA_SIMPLE_HEADER);
isSame(Tools::CSV_SIMPLE_HEADER, $csv->getCsvFilename());

isSame(['seq', 'bool', 'exact'], $csv->getHeader());

Expand Down

0 comments on commit 8640b7e

Please sign in to comment.