Skip to content

Commit

Permalink
MDTT-31: Ability to handle dis-similar data sets (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
subhojit-axl authored Apr 20, 2022
1 parent 701b036 commit 20bb9fb
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 47 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ class Trim implements \Mdtt\Transform\Transform
./vendor/bin/mdtt run -vvv
```

You can view all available options by doing this:

```shell
./vendor/bin/mdtt run --help
```

### Specify email where notification will be sent when test completes

```shell
Expand Down
6 changes: 3 additions & 3 deletions src/DataSource.php → src/DataSource/DataSource.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Mdtt;
namespace Mdtt\DataSource;

use Iterator;

Expand All @@ -14,9 +14,9 @@ public function __construct(string $data)
}

/**
* Returns an item from the source.
* Returns the datasource iterator.
*
* @return Iterator
*/
abstract public function getItem(): Iterator;
abstract public function getIterator(): Iterator;
}
56 changes: 44 additions & 12 deletions src/Definition/DefaultDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,39 @@ public function setGroup(string $group): void
$this->group = $group;
}

/**
* @inheritDoc
*/
public function runSmokeTests(): void
{
$source = $this->getSource();
$destination = $this->getDestination();
$this->logger->info(sprintf("Running smoke tests of definition id: %s", $this->getId()));

$sourceIterator = $source->getIterator();
$destinationIterator = $destination->getIterator();

$sourceRowCounts = iterator_count($sourceIterator);
$destinationRowCounts = iterator_count($destinationIterator);

try {
Assert::assertSame(
$sourceRowCounts,
$destinationRowCounts
);

$this->logger->notice("Source row count matches with destination row count.", [
'Source row count' => $sourceRowCounts,
'Destination row count' => $destinationRowCounts,
]);
} catch (ExpectationFailedException) {
$this->logger->emergency("Source row count does not matches with destination row count.", [
'Source row count' => $sourceRowCounts,
'Destination row count' => $destinationRowCounts,
]);
}
}

/**
* @inheritDoc
*/
Expand All @@ -64,27 +97,26 @@ public function runTests(): void
$destination = $this->getDestination();
$this->logger->info(sprintf("Running the tests of definition id: %s", $this->getId()));

$sourceData = $source->getItem();
$destinationData = $destination->getItem();
$sourceIterator = $source->getIterator();
$destinationIterator = $destination->getIterator();

// Combining the iterators is required so that the tests can be run for every returned item.
$combinedDataSources = new \MultipleIterator();
$combinedDataSources->attachIterator($sourceData);
$combinedDataSources->attachIterator($destinationData);
$combinedIterators = new \MultipleIterator();
$combinedIterators->attachIterator($sourceIterator);
$combinedIterators->attachIterator($destinationIterator);

foreach ($combinedDataSources as [$sourceValue, $destinationValue]) {
foreach ($combinedIterators as [$sourceValue, $destinationValue]) {
foreach ($this->getTests() as $test) {
$test->execute($sourceValue, $destinationValue);
}
}

try {
Assert::assertTrue(
!$sourceData->valid() && !$destinationData->valid(),
"Number of source items does not match number of destination items."
);
} catch (ExpectationFailedException $exception) {
$this->logger->emergency($exception->getMessage());
Assert::assertTrue(!$sourceIterator->valid() && !$destinationIterator->valid());

$this->logger->notice("Source row count matches with destination row count.");
} catch (ExpectationFailedException) {
$this->logger->emergency("Source row count does not matches with destination row count.");
}
}
}
16 changes: 11 additions & 5 deletions src/Definition/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mdtt\Definition;

use Mdtt\DataSource;
use Mdtt\DataSource\DataSource;
use Mdtt\Test\Test;

abstract class Definition
Expand Down Expand Up @@ -31,7 +31,7 @@ public function getTests(): array
}

/**
* @param \Mdtt\DataSource $source
* @param \Mdtt\DataSource\DataSource $source
*/
public function setSource(DataSource $source): void
{
Expand All @@ -47,7 +47,7 @@ public function getId(): string
}

/**
* @return \Mdtt\DataSource
* @return \Mdtt\DataSource\DataSource
*/
public function getSource(): DataSource
{
Expand All @@ -71,18 +71,24 @@ public function setTests(array $tests): void
}

/**
* @param \Mdtt\DataSource $destination
* @param \Mdtt\DataSource\DataSource $destination
*/
public function setDestination(DataSource $destination): void
{
$this->destination = $destination;
}

/**
* @return \Mdtt\DataSource
* @return \Mdtt\DataSource\DataSource
*/
public function getDestination(): DataSource
{
return $this->destination;
}

/**
* Runs smoke tests.
* @return void
*/
abstract public function runSmokeTests(): void;
}
4 changes: 2 additions & 2 deletions src/Definition/Validate/DataSource/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Mdtt\Definition\Validate\DataSource;

use Mdtt\DataSource;
use Mdtt\DataSource\DataSource;
use Mdtt\Exception\SetupException;
use Mdtt\Utility\DataSource\Json as JsonDataSourceUtility;

Expand All @@ -23,7 +23,7 @@ public function __construct(JsonDataSourceUtility $jsonDataSourceUtility)
* @param string $type
* @param array<string> $rawDataSourceDefinition
*
* @return \Mdtt\DataSource
* @return \Mdtt\DataSource\DataSource
* @throws \Mdtt\Exception\SetupException
*/
public function validate(string $type, array $rawDataSourceDefinition): DataSource
Expand Down
4 changes: 2 additions & 2 deletions src/Destination/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Mdtt\Destination;

use Iterator;
use Mdtt\DataSource;
use Mdtt\DataSource\DataSource;
use Mdtt\Exception\SetupException;
use Mdtt\Utility\DataSource\Database as DbDatabase;
use mysqli_result;
Expand All @@ -24,7 +24,7 @@ public function __construct(string $data, string $databaseKey)
/**
* @inheritDoc
*/
public function getItem(): Iterator
public function getIterator(): Iterator
{
$specification = require "tests/mdtt/spec.php";

Expand Down
4 changes: 2 additions & 2 deletions src/Destination/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Iterator;
use JsonMachine\Items;
use Mdtt\DataSource;
use Mdtt\DataSource\DataSource;
use Mdtt\Utility\DataSource\Json as JsonDataSourceUtility;

class Json extends DataSource
Expand Down Expand Up @@ -36,7 +36,7 @@ public function getSelector(): string
/**
* @inheritDoc
*/
public function getItem(): Iterator
public function getIterator(): Iterator
{
if (!isset($this->items)) {
$this->items = $this->jsonDataSourceUtility->getItems($this->data, $this->selector);
Expand Down
11 changes: 10 additions & 1 deletion src/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ protected function configure(): void
null,
InputOption::VALUE_REQUIRED,
'The email address where the notification will be sent when test completes.'
)
->addOption(
'smoke-test',
null,
InputOption::VALUE_NONE,
'Specifies whether it should perform a smoke test, instead of a detailed row by row comparison.'
);
}

Expand All @@ -54,8 +60,11 @@ protected function execute(
]);
/** @var \Mdtt\Definition\Definition[] $definitions */
$definitions = $this->definitionLoader->validate($rawTestDefinitions);

/** @var bool $isSmokeTest */
$isSmokeTest = $input->getOption('smoke-test');
foreach ($definitions as $definition) {
$definition->runTests();
$isSmokeTest ? $definition->runSmokeTests() : $definition->runTests();
}
} catch (IOException $exception) {
$this->logger->error($exception->getMessage());
Expand Down
4 changes: 2 additions & 2 deletions src/Source/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Mdtt\Source;

use Iterator;
use Mdtt\DataSource;
use Mdtt\DataSource\DataSource;
use Mdtt\Exception\SetupException;
use Mdtt\Utility\DataSource\Database as DbDataSource;
use mysqli_result;
Expand All @@ -24,7 +24,7 @@ public function __construct(string $data, string $databaseKey)
/**
* @inheritDoc
*/
public function getItem(): Iterator
public function getIterator(): Iterator
{
$specification = require "tests/mdtt/spec.php";

Expand Down
4 changes: 2 additions & 2 deletions src/Source/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Iterator;
use JsonMachine\Items;
use Mdtt\DataSource;
use Mdtt\DataSource\DataSource;
use Mdtt\Utility\DataSource\Json as JsonDataSourceUtility;

class Json extends DataSource
Expand Down Expand Up @@ -36,7 +36,7 @@ public function getSelector(): string
/**
* @inheritDoc
*/
public function getItem(): Iterator
public function getIterator(): Iterator
{
if (!isset($this->items)) {
$this->items = $this->jsonDataSourceUtility->getItems($this->data, $this->selector);
Expand Down
44 changes: 28 additions & 16 deletions src/Test/DefaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,45 @@ public function execute(array $sourceData, array $destinationData): void
throw new ExecutionException("Destination field could not be found in the destination data.");
}

$this->getLogger()->info(sprintf(
"Comparing source <info>%s</info> with destination <info>%s</info>",
$sourceData[$this->getSourceField()],
$destinationData[$this->getDestinationField()]
));

/** @var string|int $sourceValue */
$sourceValue = $sourceData[$this->getSourceField()];
/** @var string|int $destinationValue */
$destinationValue = $destinationData[$this->getDestinationField()];

$this->getLogger()->info("Comparing source with destination.", [
'Source' => $sourceValue,
'Destination' => $destinationValue,
]);


if ($this->getTransform() !== null) {
$sourceValue = $this->getTransform()->process($sourceValue);

$this->getLogger()->notice(sprintf(
"Applied transform: %s on source. Comparing source %s with destination %s",
$this->getTransform()->name(),
$sourceValue,
$destinationData[$this->getDestinationField()]
));
$this->getLogger()->notice(
"Applied transform on source. Comparing source with destination.",
[
'Source' => $sourceValue,
'Destination' => $destinationValue,
'Transform' => $this->getTransform()->name()
]
);
}

try {
Assert::assertSame(
$sourceValue,
$destinationData[$this->getDestinationField()],
"Source and destination does not match."
$destinationValue,
);
} catch (ExpectationFailedException $exception) {
$this->getLogger()->emergency($exception->getMessage());

$this->getLogger()->notice("Source and destination matches.", [
"Source" => $sourceValue,
"Destination" => $destinationValue,
]);
} catch (ExpectationFailedException) {
$this->getLogger()->emergency("Source and destination does not match.", [
"Source" => $sourceValue,
"Destination" => $destinationValue,
]);
}
}
}

0 comments on commit 20bb9fb

Please sign in to comment.