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

Commit 0eb5166

Browse files
author
Michael Grauer
committed
ENH: refs #258. Added Itemmetric models and tests, and tests for Task model.
1 parent 60831ec commit 0eb5166

File tree

9 files changed

+324
-0
lines changed

9 files changed

+324
-0
lines changed

modules/batchmake/database/mysql/0.1.0.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@ CREATE TABLE batchmake_task (
33
user_id bigint(20) NOT NULL,
44
PRIMARY KEY (batchmake_task_id)
55
);
6+
7+
8+
CREATE TABLE batchmake_itemmetric (
9+
itemmetric_id bigint(20) NOT NULL AUTO_INCREMENT,
10+
metric_name character varying(64) NOT NULL,
11+
bms_name character varying(256) NOT NULL,
12+
PRIMARY KEY (itemmetric_id)
13+
);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
5+
69328 Lyon, FRANCE.
6+
7+
See Copyright.txt for details.
8+
This software is distributed WITHOUT ANY WARRANTY; without even
9+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10+
PURPOSE. See the above copyright notices for more information.
11+
=========================================================================*/
12+
abstract class Batchmake_ItemmetricModelBase extends Batchmake_AppModel {
13+
14+
/**
15+
* constructor
16+
*/
17+
public function __construct()
18+
{
19+
parent::__construct();
20+
$this->_name = 'batchmake_itemmetric';
21+
$this->_key = 'itemmetric_id';
22+
23+
$this->_mainData = array(
24+
'itemmetric_id' => array('type' => MIDAS_DATA),
25+
'metric_name' => array('type' => MIDAS_DATA),
26+
'bms_name' => array('type' => MIDAS_DATA, )
27+
);
28+
$this->initialize(); // required
29+
}
30+
31+
/** Create an Itemmetric
32+
* @return ItemmetricDao, will throw a Zend_Exception if an
33+
* Itemmetric already exists with this metricName
34+
*/
35+
public function createItemmetric($metricName, $bmsName)
36+
{
37+
$this->loadDaoClass('ItemmetricDao', 'batchmake');
38+
$itemmetric = new Batchmake_ItemmetricDao();
39+
40+
// make sure one isn't already there by this name
41+
$found = $this->findBy('metric_name', $metricName);
42+
if(isset($found) && sizeof($found) > 0)
43+
{
44+
// don't allow the creation, as we have a metric of this name already
45+
throw new Zend_Exception('An Itemmetric already exists with that name');
46+
}
47+
48+
$itemmetric->setMetricName($metricName);
49+
$itemmetric->setBmsName($bmsName);
50+
$this->save($itemmetric);
51+
return $itemmetric;
52+
} // end createItemmetric()
53+
54+
55+
/**
56+
* getAll returns all rows
57+
*/
58+
public abstract function getAll();
59+
60+
61+
62+
63+
64+
} // end class Batchmake_ItemmetricModelBase
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
5+
69328 Lyon, FRANCE.
6+
7+
See Copyright.txt for details.
8+
This software is distributed WITHOUT ANY WARRANTY; without even
9+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10+
PURPOSE. See the above copyright notices for more information.
11+
=========================================================================*/
12+
/** Batchmake_ItemmetricDao */
13+
class Batchmake_ItemmetricDao extends AppDao {
14+
15+
public $_model = 'Itemmetric';
16+
public $_module = 'batchmake';
17+
18+
}
19+
20+
?>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
5+
69328 Lyon, FRANCE.
6+
7+
See Copyright.txt for details.
8+
This software is distributed WITHOUT ANY WARRANTY; without even
9+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10+
PURPOSE. See the above copyright notices for more information.
11+
=========================================================================*/
12+
require_once BASE_PATH . '/modules/batchmake/models/base/ItemmetricModelBase.php';
13+
14+
/** Batchmake_ItemmetricModel */
15+
class Batchmake_ItemmetricModel extends Batchmake_ItemmetricModelBase {
16+
17+
/**
18+
* @return all rows stored.
19+
*/
20+
public function getAll()
21+
{
22+
$rowsetDAOs = $this->database->getAll('Itemmetric', 'batchmake');
23+
return $rowsetDAOs;
24+
}
25+
26+
27+
}
28+
29+
?>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<dataset>
3+
4+
<user user_id="1" firstname="FirstName1" lastname="LastName1"
5+
email="user1@user1.com" password="35fd8ba86ba403ffcc00feac5355ad20" creation="2011-01-27 12:09:02"
6+
folder_id="1000" publicfolder_id="1001" privatefolder_id="1002" />
7+
8+
<user user_id="2" firstname="FirstName2" lastname="LastName2"
9+
email="user2@user2.com" password="35fd8ba86ba403ffcc00feac5355ad20" creation="2011-01-27 12:09:02"
10+
folder_id="1003" publicfolder_id="1004" privatefolder_id="1005" />
11+
12+
<batchmake_itemmetric itemmetric_id="1" metric_name="metric1" bms_name="metric1.bms" />
13+
<batchmake_itemmetric itemmetric_id="2" metric_name="metric2" bms_name="metric2.bms" />
14+
<batchmake_itemmetric itemmetric_id="3" metric_name="metric3" bms_name="metric3.bms" />
15+
16+
17+
</dataset>
18+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory( base )
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_midas_test( BatchmakeTaskModel TaskModelTest.php )
2+
add_midas_test( BatchmakeItemmetricModel ItemmetricModelTest.php )
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
5+
69328 Lyon, FRANCE.
6+
7+
See Copyright.txt for details.
8+
This software is distributed WITHOUT ANY WARRANTY; without even
9+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10+
PURPOSE. See the above copyright notices for more information.
11+
=========================================================================*/
12+
/** ItemmetricModelTest*/
13+
class ItemmetricModelTest extends DatabaseTestCase
14+
{
15+
16+
/** init test*/
17+
public function setUp()
18+
{
19+
$this->enabledModules = array('batchmake');
20+
parent::setUp();
21+
$this->setupDatabase(array('default'), 'batchmake'); // module dataset
22+
}
23+
24+
/** Test that ItemmetricModel::createTask($userDao) works */
25+
public function testCreateItemmetric()
26+
{
27+
$usersFile = $this->loadData('User', 'default', '', 'batchmake');
28+
29+
$modelLoad = new MIDAS_ModelLoader();
30+
$itemmetricModel = $modelLoad->loadModel('Itemmetric', 'batchmake');
31+
$user1Dao = $usersFile[0];
32+
33+
34+
// create an itemmetric
35+
$metricName = 'metrictest1';
36+
$bmsName = 'metrictest1.bms';
37+
$itemmetric1Dao = $itemmetricModel->createItemmetric($metricName, $bmsName);
38+
// check the dao is correct
39+
$this->assertNotEmpty($itemmetric1Dao);
40+
$this->assertTrue($itemmetric1Dao instanceof Batchmake_ItemmetricDao);
41+
// check that what we passed in is what we got out
42+
$this->assertEquals($metricName, $itemmetric1Dao->getMetricName());
43+
$this->assertEquals($bmsName, $itemmetric1Dao->getBmsName());
44+
// now try to retrieve it by key
45+
$key = $itemmetric1Dao->getKey();
46+
$dupDao = $itemmetricModel->load($key);
47+
$this->assertTrue($itemmetricModel->compareDao($itemmetric1Dao, $dupDao));
48+
49+
// now try creating another one with the same name, see that it fails
50+
try
51+
{
52+
$itemmetricDaoDup = $itemmetricModel->createItemmetric($metricName, $bmsName);
53+
$this->fail('Expected an exception for '.$metricName.', but did not get one.');
54+
}
55+
catch(Zend_Exception $ze)
56+
{
57+
// if we got here, this is the correct behavior
58+
$this->assertTrue(true);
59+
}
60+
61+
// be sure we can create one with a different name
62+
$metricName2 = 'metrictest2';
63+
$bmsName2 = 'metrictest2.bms';
64+
$itemmetricDao2 = $itemmetricModel->createItemmetric($metricName2, $bmsName2);
65+
// check the dao is correct
66+
$this->assertNotEmpty($itemmetricDao2);
67+
$this->assertTrue($itemmetricDao2 instanceof Batchmake_ItemmetricDao);
68+
// check that what we passed in is what we got out
69+
$this->assertEquals($metricName2, $itemmetricDao2->getMetricName());
70+
$this->assertEquals($bmsName2, $itemmetricDao2->getBmsName());
71+
// now try to retrieve it by key
72+
$key = $itemmetricDao2->getKey();
73+
$dupDao = $itemmetricModel->load($key);
74+
$this->assertTrue($itemmetricModel->compareDao($itemmetricDao2, $dupDao));
75+
}
76+
77+
/**
78+
* tests getAll abstract function
79+
*/
80+
public function testGetAll()
81+
{
82+
$itemmetricFileDaos = $this->loadData('Itemmetric', 'default', 'batchmake', 'batchmake');
83+
84+
$modelLoad = new MIDAS_ModelLoader();
85+
$itemmetricModel = $modelLoad->loadModel('Itemmetric', 'batchmake');
86+
$modelDaos = $itemmetricModel->getAll();
87+
88+
// now check that each of the itemmetrics in the file are loaded as daos
89+
foreach($itemmetricFileDaos as $fileDao)
90+
{
91+
$found = false;
92+
foreach($modelDaos as $modelDao)
93+
{
94+
if($itemmetricModel->compareDao($modelDao, $fileDao))
95+
{
96+
$found = true;
97+
break;
98+
}
99+
}
100+
if(!$found)
101+
{
102+
$this->fail("Did not find a testdata itemmetric , with getAll");
103+
}
104+
}
105+
}
106+
107+
108+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
5+
69328 Lyon, FRANCE.
6+
7+
See Copyright.txt for details.
8+
This software is distributed WITHOUT ANY WARRANTY; without even
9+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10+
PURPOSE. See the above copyright notices for more information.
11+
=========================================================================*/
12+
/** TaskModelTest*/
13+
class TaskModelTest extends DatabaseTestCase
14+
{
15+
16+
/** init test*/
17+
public function setUp()
18+
{
19+
$this->enabledModules = array('batchmake');
20+
parent::setUp();
21+
$this->setupDatabase(array('default'), 'batchmake'); // module dataset
22+
}
23+
24+
25+
/** Test that TaskModel::createTask($userDao) works */
26+
public function testCreateTask()
27+
{
28+
$usersFile = $this->loadData('User', 'default', '', 'batchmake');
29+
30+
$modelLoad = new MIDAS_ModelLoader();
31+
$taskModel = $modelLoad->loadModel('Task', 'batchmake');
32+
33+
$user1Dao = $usersFile[0];
34+
35+
$task1Dao = $taskModel->createTask($user1Dao);
36+
37+
$this->assertNotEmpty($task1Dao);
38+
$this->assertTrue($task1Dao instanceof Batchmake_TaskDao);
39+
$userId1 = $task1Dao->getUserId();
40+
$this->assertTrue(is_numeric($userId1));
41+
$this->assertFalse(is_object($userId1));
42+
$this->assertEquals($userId1, $user1Dao->getUserId());
43+
$taskId1 = $task1Dao->getKey();
44+
$this->assertTrue(is_numeric($taskId1));
45+
$this->assertFalse(is_object($taskId1));
46+
47+
// now try a different user
48+
$user2Dao = $usersFile[1];
49+
50+
$task2Dao = $taskModel->createTask($user2Dao);
51+
$this->assertNotEmpty($task2Dao);
52+
$this->assertTrue($task2Dao instanceof Batchmake_TaskDao);
53+
$userId2 = $task2Dao->getUserId();
54+
$this->assertTrue(is_numeric($userId2));
55+
$this->assertFalse(is_object($userId2));
56+
$this->assertEquals($userId2, $user2Dao->getUserId());
57+
$taskId2 = $task2Dao->getKey();
58+
$this->assertTrue(is_numeric($taskId2));
59+
$this->assertFalse(is_object($taskId2));
60+
61+
// make sure each of the tasks got a different id
62+
$this->assertNotEquals($taskId1, $taskId2);
63+
64+
65+
// now try to retrieve it by key
66+
$task3Dao = $taskModel->load($taskId1);
67+
$this->assertTrue($taskModel->compareDao($task1Dao, $task3Dao));
68+
69+
$task4Dao = $taskModel->load($taskId2);
70+
$this->assertTrue($taskModel->compareDao($task2Dao, $task4Dao));
71+
}
72+
73+
74+
}

0 commit comments

Comments
 (0)