From 19c5ce2c820a997c6de95a8ce2e68bd904fa860e Mon Sep 17 00:00:00 2001 From: mgrauer Date: Thu, 1 Oct 2015 19:56:49 +0000 Subject: [PATCH] Test tracker scalar API endpoints for cloven params --- ...ontrollerTest.php => ApiComponentTest.php} | 70 +++++++++++- .../controllers/ApiScalarComponentTest.php | 104 ++++++++++++++++++ .../tracker/tests/controllers/CMakeLists.txt | 3 +- 3 files changed, 171 insertions(+), 6 deletions(-) rename modules/tracker/tests/controllers/{ApiControllerTest.php => ApiComponentTest.php} (53%) create mode 100644 modules/tracker/tests/controllers/ApiScalarComponentTest.php diff --git a/modules/tracker/tests/controllers/ApiControllerTest.php b/modules/tracker/tests/controllers/ApiComponentTest.php similarity index 53% rename from modules/tracker/tests/controllers/ApiControllerTest.php rename to modules/tracker/tests/controllers/ApiComponentTest.php index 6788a6325..9fddce300 100644 --- a/modules/tracker/tests/controllers/ApiControllerTest.php +++ b/modules/tracker/tests/controllers/ApiComponentTest.php @@ -21,7 +21,7 @@ require_once BASE_PATH.'/modules/api/tests/controllers/CallMethodsTestCase.php'; /** API test for tracker module. */ -class Tracker_ApiControllerTest extends Api_CallMethodsTestCase +class Tracker_ApiComponentTest extends Api_CallMethodsTestCase { public $moduleName = 'tracker'; @@ -50,8 +50,10 @@ public function testUploadScalarWithSubmission() $outputs = array(); $outputs['metric_0'] = $this->_submitScalar($token, $uuid, 'metric_0', '18'); - $outputs['metric_1'] = $this->_submitScalar($token, $uuid, 'metric_1', '19', 'meters'); - $outputs['metric_2'] = $this->_submitScalar($token, $uuid, 'metric_2', '20', 'mm'); + $metric1Params = array('num_param' => 19.0, 'text_param' => 'metric1 text', 'null_param' => null); + $outputs['metric_1'] = $this->_submitScalar($token, $uuid, 'metric_1', '19', 'meters', $metric1Params); + $metric2Params = array('num_param' => 20.0, 'text_param' => 'metric2 text', 'null_param' => null); + $outputs['metric_2'] = $this->_submitScalar($token, $uuid, 'metric_2', '20', 'mm', $metric2Params); /** @var Tracker_SubmissionModel $submissionModel */ $submissionModel = MidasLoader::loadModel('Submission', 'tracker'); @@ -60,12 +62,67 @@ public function testUploadScalarWithSubmission() $submissionDao = $submissionModel->getSubmission($uuid); $scalarDaos = $submissionModel->getScalars($submissionDao); + // Maps the scalars for each metric. + $metricToScalar = array(); + /** @var Tracker_ScalarDao $scalarDao */ foreach ($scalarDaos as $scalarDao) { $curOutput = $outputs[$scalarDao->getTrend()->getMetricName()]; + $metricToScalar[$scalarDao->getTrend()->getMetricName()] = $scalarDao; $this->assertEquals($curOutput->value, $scalarDao->getValue()); $this->assertEquals($submissionDao->getKey(), $scalarDao->getSubmissionId()); } + + // Params should be a zero element array here. + $this->assertTrue(!($metricToScalar['metric_0']->getParams())); + + $metric_1_params = $metricToScalar['metric_1']->getParams(); + $metric_1_paramChecks = array( + 'num_param' => array('found' => false, 'type' => 'numeric', 'val' => 19.0), + 'text_param' => array('found' => false, 'type' => 'text', 'val' => 'metric1 text'), + 'null_param' => array('found' => false, 'type' => 'text', 'val' => ''), + ); + + // Test that the params are saved as the correct type and value. + foreach ($metric_1_params as $param) { + $checks = $metric_1_paramChecks[$param->getParamName()]; + $this->assertEquals($checks['type'], $param->getParamType()); + if ($checks['type'] === 'numeric') { + $this->assertEquals($checks['val'], $param->getNumericValue()); + } else { + $this->assertEquals($checks['val'], $param->getTextValue()); + } + $metric_1_paramChecks[$param->getParamName()]['found'] = true; + } + + // Test that all params are tested. + foreach ($metric_1_paramChecks as $checks) { + $this->assertTrue($checks['found']); + } + + $metric_2_params = $metricToScalar['metric_2']->getParams(); + $metric_2_paramChecks = array( + 'num_param' => array('found' => false, 'type' => 'numeric', 'val' => 20.0), + 'text_param' => array('found' => false, 'type' => 'text', 'val' => 'metric2 text'), + 'null_param' => array('found' => false, 'type' => 'text', 'val' => ''), + ); + + // Test that the params are saved as the correct type and value. + foreach ($metric_2_params as $param) { + $checks = $metric_2_paramChecks[$param->getParamName()]; + $this->assertEquals($checks['type'], $param->getParamType()); + if ($checks['type'] === 'numeric') { + $this->assertEquals($checks['val'], $param->getNumericValue()); + } else { + $this->assertEquals($checks['val'], $param->getTextValue()); + } + $metric_2_paramChecks[$param->getParamName()]['found'] = true; + } + + // Test that all params are tested. + foreach ($metric_2_paramChecks as $checks) { + $this->assertTrue($checks['found']); + } } /** @@ -76,9 +133,10 @@ public function testUploadScalarWithSubmission() * @param string $metric the metric name of the trend * @param float $value the scalar value * @param false|string $unit (Optional) the unit of the trend, defaults to false + * @param false|array $scalarParams (Optional) the params of the scalar, defaults to false * @return mixed response object from the API */ - private function _submitScalar($token, $uuid, $metric, $value, $unit = false) + private function _submitScalar($token, $uuid, $metric, $value, $unit = false, $scalarParams = false) { $this->resetAll(); $this->params['method'] = 'midas.tracker.scalar.add'; @@ -93,7 +151,9 @@ private function _submitScalar($token, $uuid, $metric, $value, $unit = false) if ($unit !== false) { $this->params['unit'] = $unit; } - + if ($scalarParams !== false) { + $this->params['params'] = json_encode($scalarParams); + } $res = $this->_callJsonApi(); return $res->data; diff --git a/modules/tracker/tests/controllers/ApiScalarComponentTest.php b/modules/tracker/tests/controllers/ApiScalarComponentTest.php new file mode 100644 index 000000000..52769f547 --- /dev/null +++ b/modules/tracker/tests/controllers/ApiScalarComponentTest.php @@ -0,0 +1,104 @@ +enabledModules = array('api', 'scheduler', $this->moduleName); + $this->_models = array('Assetstore', 'Community', 'Setting', 'User'); + + $this->setupDatabase(array('default')); + $this->setupDatabase(array('default'), 'tracker'); + + ControllerTestCase::setUp(); + } + + /** + * Test updating an existing scalar with a set of params, via PUT. + * + * @throws Zend_Exception + */ + public function testPUT() + { + $usersFile = $this->loadData('User', 'default'); + $userDao = $this->User->load($usersFile[0]->getKey()); + + // Create a scalar attached to a trend. + + $trendModel = MidasLoader::loadModel('Trend', 'tracker'); + $trend = $trendModel->load(1); + + $scalarModel = MidasLoader::loadModel('Scalar', 'tracker'); + $scalarArgs = array( + 'trend_id' => $trend->getTrendId(), + 'value' => 42, + 'build_results_url' => 'http://localhost', + ); + $scalar = $scalarModel->initDao('Scalar', $scalarArgs, $this->moduleName); + $scalarModel->save($scalar); + + $token = $this->_loginAsAdministrator(); + + $params = array( + 'num_param' => 90, + 'text_param' => 'master', + 'null_param' => '', + ); + $restParams = array( + 'trend_id' => $trend->getTrendId(), + 'params' => json_encode($params), + 'token' => $token, + ); + + $this->resetAll(); + $this->params = $restParams; + $resp = $this->_callRestApi('PUT', '/tracker/scalar/'.$scalar->getScalarId()); + + // Ensure params are properly set on scalar. + + $paramChecks = array( + 'num_param' => array('found' => false, 'type' => 'numeric', 'val' => 90), + 'text_param' => array('found' => false, 'type' => 'text', 'val' => 'master'), + 'null_param' => array('found' => false, 'type' => 'text', 'val' => ''), + ); + + foreach ($scalar->getParams() as $param) { + $checks = $paramChecks[$param->getParamName()]; + $this->assertEquals($checks['type'], $param->getParamType()); + if ($checks['type'] === 'numeric') { + $this->assertEquals($checks['val'], $param->getNumericValue()); + } else { + $this->assertEquals($checks['val'], $param->getTextValue()); + } + $paramChecks[$param->getParamName()]['found'] = true; + } + + foreach ($paramChecks as $checks) { + $this->assertTrue($checks['found']); + } + } +} diff --git a/modules/tracker/tests/controllers/CMakeLists.txt b/modules/tracker/tests/controllers/CMakeLists.txt index a6bfb616b..6e4b16561 100644 --- a/modules/tracker/tests/controllers/CMakeLists.txt +++ b/modules/tracker/tests/controllers/CMakeLists.txt @@ -20,4 +20,5 @@ set(module_name tracker) to_titlecase(${module_name} module_name_titlecase) -add_midas_test(${module_name_titlecase}ApiController ApiControllerTest.php) +add_midas_test(${module_name_titlecase}ApiComponent ApiComponentTest.php) +add_midas_test(${module_name_titlecase}ApiScalarComponent ApiScalarComponentTest.php)