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

Commit 19c5ce2

Browse files
author
mgrauer
committed
Test tracker scalar API endpoints for cloven params
1 parent 36d04fa commit 19c5ce2

File tree

3 files changed

+171
-6
lines changed

3 files changed

+171
-6
lines changed

modules/tracker/tests/controllers/ApiControllerTest.php renamed to modules/tracker/tests/controllers/ApiComponentTest.php

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
require_once BASE_PATH.'/modules/api/tests/controllers/CallMethodsTestCase.php';
2222

2323
/** API test for tracker module. */
24-
class Tracker_ApiControllerTest extends Api_CallMethodsTestCase
24+
class Tracker_ApiComponentTest extends Api_CallMethodsTestCase
2525
{
2626
public $moduleName = 'tracker';
2727

@@ -50,8 +50,10 @@ public function testUploadScalarWithSubmission()
5050

5151
$outputs = array();
5252
$outputs['metric_0'] = $this->_submitScalar($token, $uuid, 'metric_0', '18');
53-
$outputs['metric_1'] = $this->_submitScalar($token, $uuid, 'metric_1', '19', 'meters');
54-
$outputs['metric_2'] = $this->_submitScalar($token, $uuid, 'metric_2', '20', 'mm');
53+
$metric1Params = array('num_param' => 19.0, 'text_param' => 'metric1 text', 'null_param' => null);
54+
$outputs['metric_1'] = $this->_submitScalar($token, $uuid, 'metric_1', '19', 'meters', $metric1Params);
55+
$metric2Params = array('num_param' => 20.0, 'text_param' => 'metric2 text', 'null_param' => null);
56+
$outputs['metric_2'] = $this->_submitScalar($token, $uuid, 'metric_2', '20', 'mm', $metric2Params);
5557

5658
/** @var Tracker_SubmissionModel $submissionModel */
5759
$submissionModel = MidasLoader::loadModel('Submission', 'tracker');
@@ -60,12 +62,67 @@ public function testUploadScalarWithSubmission()
6062
$submissionDao = $submissionModel->getSubmission($uuid);
6163
$scalarDaos = $submissionModel->getScalars($submissionDao);
6264

65+
// Maps the scalars for each metric.
66+
$metricToScalar = array();
67+
6368
/** @var Tracker_ScalarDao $scalarDao */
6469
foreach ($scalarDaos as $scalarDao) {
6570
$curOutput = $outputs[$scalarDao->getTrend()->getMetricName()];
71+
$metricToScalar[$scalarDao->getTrend()->getMetricName()] = $scalarDao;
6672
$this->assertEquals($curOutput->value, $scalarDao->getValue());
6773
$this->assertEquals($submissionDao->getKey(), $scalarDao->getSubmissionId());
6874
}
75+
76+
// Params should be a zero element array here.
77+
$this->assertTrue(!($metricToScalar['metric_0']->getParams()));
78+
79+
$metric_1_params = $metricToScalar['metric_1']->getParams();
80+
$metric_1_paramChecks = array(
81+
'num_param' => array('found' => false, 'type' => 'numeric', 'val' => 19.0),
82+
'text_param' => array('found' => false, 'type' => 'text', 'val' => 'metric1 text'),
83+
'null_param' => array('found' => false, 'type' => 'text', 'val' => ''),
84+
);
85+
86+
// Test that the params are saved as the correct type and value.
87+
foreach ($metric_1_params as $param) {
88+
$checks = $metric_1_paramChecks[$param->getParamName()];
89+
$this->assertEquals($checks['type'], $param->getParamType());
90+
if ($checks['type'] === 'numeric') {
91+
$this->assertEquals($checks['val'], $param->getNumericValue());
92+
} else {
93+
$this->assertEquals($checks['val'], $param->getTextValue());
94+
}
95+
$metric_1_paramChecks[$param->getParamName()]['found'] = true;
96+
}
97+
98+
// Test that all params are tested.
99+
foreach ($metric_1_paramChecks as $checks) {
100+
$this->assertTrue($checks['found']);
101+
}
102+
103+
$metric_2_params = $metricToScalar['metric_2']->getParams();
104+
$metric_2_paramChecks = array(
105+
'num_param' => array('found' => false, 'type' => 'numeric', 'val' => 20.0),
106+
'text_param' => array('found' => false, 'type' => 'text', 'val' => 'metric2 text'),
107+
'null_param' => array('found' => false, 'type' => 'text', 'val' => ''),
108+
);
109+
110+
// Test that the params are saved as the correct type and value.
111+
foreach ($metric_2_params as $param) {
112+
$checks = $metric_2_paramChecks[$param->getParamName()];
113+
$this->assertEquals($checks['type'], $param->getParamType());
114+
if ($checks['type'] === 'numeric') {
115+
$this->assertEquals($checks['val'], $param->getNumericValue());
116+
} else {
117+
$this->assertEquals($checks['val'], $param->getTextValue());
118+
}
119+
$metric_2_paramChecks[$param->getParamName()]['found'] = true;
120+
}
121+
122+
// Test that all params are tested.
123+
foreach ($metric_2_paramChecks as $checks) {
124+
$this->assertTrue($checks['found']);
125+
}
69126
}
70127

71128
/**
@@ -76,9 +133,10 @@ public function testUploadScalarWithSubmission()
76133
* @param string $metric the metric name of the trend
77134
* @param float $value the scalar value
78135
* @param false|string $unit (Optional) the unit of the trend, defaults to false
136+
* @param false|array $scalarParams (Optional) the params of the scalar, defaults to false
79137
* @return mixed response object from the API
80138
*/
81-
private function _submitScalar($token, $uuid, $metric, $value, $unit = false)
139+
private function _submitScalar($token, $uuid, $metric, $value, $unit = false, $scalarParams = false)
82140
{
83141
$this->resetAll();
84142
$this->params['method'] = 'midas.tracker.scalar.add';
@@ -93,7 +151,9 @@ private function _submitScalar($token, $uuid, $metric, $value, $unit = false)
93151
if ($unit !== false) {
94152
$this->params['unit'] = $unit;
95153
}
96-
154+
if ($scalarParams !== false) {
155+
$this->params['params'] = json_encode($scalarParams);
156+
}
97157
$res = $this->_callJsonApi();
98158

99159
return $res->data;
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/*=========================================================================
3+
Midas Server
4+
Copyright Kitware SAS, 26 rue Louis Guérin, 69100 Villeurbanne, France.
5+
All rights reserved.
6+
For more information visit http://www.kitware.com/.
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
require_once BASE_PATH.'/core/tests/controllers/api/RestCallMethodsTestCase.php';
22+
23+
/** API test for tracker module ApiscalarComponent. */
24+
class Tracker_ApiScalarComponentTest extends RestCallMethodsTestCase
25+
{
26+
public $moduleName = 'tracker';
27+
28+
/** Setup. */
29+
public function setUp()
30+
{
31+
$this->enabledModules = array('api', 'scheduler', $this->moduleName);
32+
$this->_models = array('Assetstore', 'Community', 'Setting', 'User');
33+
34+
$this->setupDatabase(array('default'));
35+
$this->setupDatabase(array('default'), 'tracker');
36+
37+
ControllerTestCase::setUp();
38+
}
39+
40+
/**
41+
* Test updating an existing scalar with a set of params, via PUT.
42+
*
43+
* @throws Zend_Exception
44+
*/
45+
public function testPUT()
46+
{
47+
$usersFile = $this->loadData('User', 'default');
48+
$userDao = $this->User->load($usersFile[0]->getKey());
49+
50+
// Create a scalar attached to a trend.
51+
52+
$trendModel = MidasLoader::loadModel('Trend', 'tracker');
53+
$trend = $trendModel->load(1);
54+
55+
$scalarModel = MidasLoader::loadModel('Scalar', 'tracker');
56+
$scalarArgs = array(
57+
'trend_id' => $trend->getTrendId(),
58+
'value' => 42,
59+
'build_results_url' => 'http://localhost',
60+
);
61+
$scalar = $scalarModel->initDao('Scalar', $scalarArgs, $this->moduleName);
62+
$scalarModel->save($scalar);
63+
64+
$token = $this->_loginAsAdministrator();
65+
66+
$params = array(
67+
'num_param' => 90,
68+
'text_param' => 'master',
69+
'null_param' => '',
70+
);
71+
$restParams = array(
72+
'trend_id' => $trend->getTrendId(),
73+
'params' => json_encode($params),
74+
'token' => $token,
75+
);
76+
77+
$this->resetAll();
78+
$this->params = $restParams;
79+
$resp = $this->_callRestApi('PUT', '/tracker/scalar/'.$scalar->getScalarId());
80+
81+
// Ensure params are properly set on scalar.
82+
83+
$paramChecks = array(
84+
'num_param' => array('found' => false, 'type' => 'numeric', 'val' => 90),
85+
'text_param' => array('found' => false, 'type' => 'text', 'val' => 'master'),
86+
'null_param' => array('found' => false, 'type' => 'text', 'val' => ''),
87+
);
88+
89+
foreach ($scalar->getParams() as $param) {
90+
$checks = $paramChecks[$param->getParamName()];
91+
$this->assertEquals($checks['type'], $param->getParamType());
92+
if ($checks['type'] === 'numeric') {
93+
$this->assertEquals($checks['val'], $param->getNumericValue());
94+
} else {
95+
$this->assertEquals($checks['val'], $param->getTextValue());
96+
}
97+
$paramChecks[$param->getParamName()]['found'] = true;
98+
}
99+
100+
foreach ($paramChecks as $checks) {
101+
$this->assertTrue($checks['found']);
102+
}
103+
}
104+
}

modules/tracker/tests/controllers/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
set(module_name tracker)
2121
to_titlecase(${module_name} module_name_titlecase)
2222

23-
add_midas_test(${module_name_titlecase}ApiController ApiControllerTest.php)
23+
add_midas_test(${module_name_titlecase}ApiComponent ApiComponentTest.php)
24+
add_midas_test(${module_name_titlecase}ApiScalarComponent ApiScalarComponentTest.php)

0 commit comments

Comments
 (0)