Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
Test tracker scalar API endpoints for cloven params
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrauer committed Oct 1, 2015
1 parent 36d04fa commit 19c5ce2
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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');
Expand All @@ -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']);
}
}

/**
Expand All @@ -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';
Expand All @@ -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;
Expand Down
104 changes: 104 additions & 0 deletions modules/tracker/tests/controllers/ApiScalarComponentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/*=========================================================================
Midas Server
Copyright Kitware SAS, 26 rue Louis Guérin, 69100 Villeurbanne, France.
All rights reserved.
For more information visit http://www.kitware.com/.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

require_once BASE_PATH.'/core/tests/controllers/api/RestCallMethodsTestCase.php';

/** API test for tracker module ApiscalarComponent. */
class Tracker_ApiScalarComponentTest extends RestCallMethodsTestCase
{
public $moduleName = 'tracker';

/** Setup. */
public function setUp()
{
$this->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']);
}
}
}
3 changes: 2 additions & 1 deletion modules/tracker/tests/controllers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 19c5ce2

Please sign in to comment.