Skip to content

Commit

Permalink
Add and improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smgallo committed Jan 30, 2019
1 parent 4118111 commit e04c16b
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 4 deletions.
40 changes: 40 additions & 0 deletions open_xdmod/modules/xdmod/tests/lib/BaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Abstract base class to encapsulate funcationality common to unit tests.
*/

namespace UnitTesting;

abstract class BaseTest extends \PHPUnit_Framework_TestCase
{
/**
* Recursively filter out any keys matching one in $keyList. This is a helper function to
* address the issue specified in Asana https://app.asana.com/0/807629084565719/1101232922862525
* where the key generated for JSON file DataEndpoints is unstable. Note that 2-dimensional
* arrays are not handled.
*
* @param array $keyList The list of keys to remove
* @param array $input The input object being filtered.
*
* @return array The filtered object with specified keys removed
*/

protected function filterKeysRecursive(array $keyList, \stdClass $input)
{
foreach ($input as $key => &$value)
{
if ( in_array($key, $keyList) ) {
unset($input->$key);
} elseif ( is_object($value) ) {
$this->filterKeysRecursive($keyList, $value);
} elseif ( is_array($value) ) {
foreach ( $value as $element ) {
if ( is_object($element) ) {
$this->filterKeysRecursive($keyList, $element);
}
}
}
}
return $input;
}
} // BaseTest
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,28 @@

namespace UnitTesting\ETL\Configuration;

use CCR\Log;
use Configuration\Configuration;

class ConfigurationTest extends \PHPUnit_Framework_TestCase
{
const TEST_ARTIFACT_INPUT_PATH = "./artifacts/xdmod-test-artifacts/xdmod/etlv2/configuration/input";
const TEST_ARTIFACT_OUTPUT_PATH = "./artifacts/xdmod-test-artifacts/xdmod/etlv2/configuration/output";

protected static $logger = null;

public static function setupBeforeClass()
{
// Set up a logger so we can get warnings and error messages from the ETL infrastructure
$conf = array(
'file' => false,
'db' => false,
'mail' => false,
'consoleLogLevel' => Log::DEBUG
);
self::$logger = Log::factory('PHPUnit', $conf);
}

/**
* Test JSON parse errors
*
Expand Down Expand Up @@ -84,6 +99,18 @@ public function testRelativePathReference()
$this->assertEquals($generated, $expected);
}

/**
* Test inclusion of a reference with fully qualified path names.
*
* @expectedException Exception
*/

public function testBadFragment()
{
$configObj = new Configuration(self::TEST_ARTIFACT_INPUT_PATH . '/rfc6901_bad_fragment.json');
$configObj->initialize();
}

/**
* Test variables in the configuration file.
*/
Expand All @@ -101,4 +128,39 @@ public function testConfigurationVariables()
$expected = json_decode(file_get_contents(self::TEST_ARTIFACT_OUTPUT_PATH . '/sample_config.expected'));
$this->assertEquals($generated, $expected);
}

/**
* Test inclusion of a the following with:
* - A JSON reference with variables in the referenced JSON
* - A JSON-encoded include file with variables in the include path. Note that a comment is
* included in the reference object to ensure comments are removed before transformers are
* processed.
* - A nested JSON reference
*/

public function testJsonReferenceAndIncludeWithVariables()
{
@copy(self::TEST_ARTIFACT_INPUT_PATH . '/sample_config_with_variables.json', '/tmp/sample_config_with_variables.json');
@copy(self::TEST_ARTIFACT_INPUT_PATH . '/sample_config_with_reference.json', '/tmp/sample_config_with_reference.json');
$configObj = new Configuration(
self::TEST_ARTIFACT_INPUT_PATH . '/sample_config_with_transformer_keys.json',
null,
self::$logger,
array(
'config_variables' => array(
'TABLE_NAME' => 'resource_allocations',
'WIDTH' => 40,
'TMPDIR' => '/tmp',
'SQLDIR' => 'etl_sql.d',
'SOURCE_SCHEMA' => 'modw'
)
)
);
$configObj->initialize();
$generated = json_decode($configObj->toJson());
$expected = json_decode(file_get_contents(self::TEST_ARTIFACT_OUTPUT_PATH . '/sample_config_with_transformer_keys.expected'));
@unlink('/tmp/sample_config_with_variables.json');
@unlink('/tmp/sample_config_with_reference.json');
$this->assertEquals($generated, $expected, "Test multiple transformer directives");
}
} // class ConfigurationTest
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use TestHarness\TestFiles;
use Xdmod\Config;

class EtlConfigurationTest extends \PHPUnit_Framework_TestCase
class EtlConfigurationTest extends \UnitTesting\BaseTest
{
const TEST_ARTIFACT_INPUT_PATH = "./artifacts/xdmod-test-artifacts/xdmod/etlv2/configuration/input";
const TEST_ARTIFACT_OUTPUT_PATH = "./artifacts/xdmod-test-artifacts/xdmod/etlv2/configuration/output";
Expand Down Expand Up @@ -121,6 +121,7 @@ public function testConfigurationVariables()
);
$configObj->initialize();
$generated = json_decode($configObj->toJson());
$this->filterKeysRecursive(array('key'), $generated);
$file = self::TEST_ARTIFACT_OUTPUT_PATH . '/xdmod_etl_config_with_variables.json';
$expected = json_decode(file_get_contents($file));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public function testRfc6901LastArrayElement()
}

/**
* Include whole document
*/
* Include whole document
*/

public function testRfc6901SpecialCharacter()
{
Expand All @@ -121,4 +121,18 @@ public function testRfc6901SpecialCharacter()
$this->assertNull($key);
$this->assertEquals($value, 'specialchar');
}

/**
* Include whole document
*
* @expectedException Exception
*/

public function testRfc6901BadFragment()
{
$key = '$ref';
$value = 'rfc6901.json#/does-not-exist';
$obj = (object) array($key => $value);
$this->transformer->transform($key, $value, $obj, $this->config);
}
} // class Rfc6901Test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SELECT
name,
description
FROM ${SOURCE_SCHEMA}.jobs j
WHERE 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"reference": {
"$ref": "reference_target.json#/does-not-exist"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"subreference": {
"$ref": "file://${TMPDIR}/sample_config_with_variables.json#/key_two"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"subreference": {
"$ref": "file://${TMPDIR}/sample_config_with_variables.json#/key_two"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"reference": {
"$ref": "file://${TMPDIR}/sample_config_with_variables.json#/key_one"
},
"include": {
"#": "Implicit file reference",
"$include": "${SQLDIR}/query_with_variables.sql"
},
"reference_with_subreference": {
"$ref": "file://${TMPDIR}/sample_config_with_reference.json"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"reference":{"name":"resource_allocations","engine":"MyISAM","columns":[{"name":"resource","type":"varchar(40)","nullable":true}]},"include":"\"SELECT\\n name,\\n description\\nFROM modw.jobs j\\nWHERE 1;\\n\"","reference_with_subreference":{"subreference":[{"object1":"value"},{"object2":"value"}]}}

Large diffs are not rendered by default.

0 comments on commit e04c16b

Please sign in to comment.