38 private $uploadPermissionDao;
40 private $job_pks = array(2,1);
42 protected function setUp()
45 $this->
dbManager = &$this->testDb->getDbManager();
47 $this->testDb->createPlainTables(
56 $this->testDb->createInheritedTables(array(
'uploadtree_a'));
58 $uploadArray = array(array(
'upload_pk'=>1,
'uploadtree_tablename'=>
'uploadtree'),
59 array(
'upload_pk'=>2,
'uploadtree_tablename'=>
'uploadtree_a'));
60 foreach ($uploadArray as $uploadEntry) {
61 $this->
dbManager->insertTableRow(
'upload', $uploadEntry);
64 $this->
dbManager->prepare($stmt =
'insert.job',
65 "INSERT INTO job (job_pk, job_queued, job_name, job_upload_fk, job_user_fk) VALUES ($1, $2, $3, $4, $5)");
66 $jobArray = array(array(1,date(
'c',time()-5),
"FCKeditor_2.6.4.zip", 1,1 ),
67 array(2,date(
'c'),
"zlib_1.2.8.zip", 2,2));
68 foreach ($jobArray as $uploadEntry) {
72 $logger = M::mock(
'Monolog\Logger');
73 $logger->shouldReceive(
'debug');
74 $this->uploadPermissionDao = M::mock(
'Fossology\Lib\Dao\UploadPermissionDao');
75 $this->uploadDao =
new UploadDao($this->
dbManager, $logger, $this->uploadPermissionDao);
78 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
81 protected function tearDown()
83 $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
90 public function testUploads2Jobs()
92 $jobs = array(3=>2, 4=>3, 5=>5, 6=>8%6, 7=>13%6, 8=>21%6);
93 foreach ($jobs as $jobId => $jobUpload) {
94 $this->
dbManager->insertTableRow(
'job', array(
'job_pk' => $jobId,
'job_upload_fk' => $jobUpload));
96 $uploadDao = M::mock(
'Fossology\Lib\Dao\UploadDao');
98 $jobsWithoutUpload = $showJobDao->uploads2Jobs(array());
99 assertThat($jobsWithoutUpload, is(emptyArray()));
100 $jobsWithUploadIdOne = $showJobDao->uploads2Jobs(array(1));
101 assertThat($jobsWithUploadIdOne, equalTo(array(array(1,7),0)));
102 $jobsAtAll = $showJobDao->uploads2Jobs(array(1,2,3,4,5));
103 assertThat($jobsAtAll, equalTo(array(array(1,7, 2,3,6, 4,8, 5),0)));
104 $jobsWithUploadFour = $showJobDao->uploads2Jobs(array(4));
105 assertThat($jobsWithUploadFour[0], is(emptyArray()));
108 public function testUploads2JobsPaged()
110 $jobs = array_combine(range(3,13),range(3,13));
111 foreach ($jobs as $jobId => $jobUpload) {
112 $this->
dbManager->insertTableRow(
'job', array(
'job_pk' => $jobId,
'job_upload_fk' => $jobUpload));
114 $uploadDao = M::mock(
'Fossology\Lib\Dao\UploadDao');
117 $jobsPage1 = $showJobDao->uploads2Jobs(range(1,17),0);
118 assertThat($jobsPage1[0], arrayWithSize(10));
119 assertThat($jobsPage1[1], is(1));
120 $jobsPage2 = $showJobDao->uploads2Jobs(array_combine(range(10,16),range(11,17)),1);
121 assertThat($jobsPage2[0], arrayWithSize(3));
122 assertThat($jobsPage2[1], is(0));
123 $jobsPage3 = $showJobDao->uploads2Jobs(array(),2);
124 assertThat($jobsPage3, arrayWithSize(0));
128 public function testgetJobName()
130 $testJobName = $this->showJobsDao->getJobName(1);
131 assertThat($testJobName, equalTo(
"FCKeditor_2.6.4.zip"));
133 $testJobNameIfNothingQueued = $this->showJobsDao->getJobName($uploadId = 3);
134 assertThat($testJobNameIfNothingQueued, equalTo($uploadId));
137 public function testMyJobs()
143 $this->uploadPermissionDao->shouldReceive(
'isAccessible')->withArgs(array(anything(),$groupId))
144 ->andReturnUsing(
function($upload,$group)
146 return ($upload==1 || $upload==2 || $upload==4);
148 $testOurJobs = $this->showJobsDao->myJobs(
true);
149 assertThat($testOurJobs[0], is(arrayContainingInAnyOrder($this->job_pks)));
150 $testMyJobs = $this->showJobsDao->myJobs(
false);
151 assertThat($testMyJobs, equalTo(array(array(1), 0)));
153 $this->
dbManager->queryOnce(
"UPDATE job SET job_queued=job_queued-INTERVAL '30 days' WHERE job_pk=1");
154 $this->
dbManager->prepare(__METHOD__.
'insert.perm_upload',
155 "INSERT INTO perm_upload (perm_upload_pk, perm, upload_fk, group_fk) VALUES ($1, $2, $3, $4)");
156 $testOutdatedJobs = $this->showJobsDao->myJobs(
true);
157 assertThat($testOutdatedJobs, equalTo(array(array(2), 0)));
160 public function testgetNumItemsPerSec()
163 $testFilesPerSec = $this->showJobsDao->getNumItemsPerSec(5*$numSecs, $numSecs);
164 assertThat($testFilesPerSec,is(greaterThan(1)));
166 $testFilesPerSec = $this->showJobsDao->getNumItemsPerSec(0.9*$numSecs, $numSecs);
167 assertThat($testFilesPerSec,is(lessThanOrEqualTo(1)));
170 public function testGetJobInfo()
172 $this->
dbManager->prepare($stmt =
'insert.jobqueue',
173 "INSERT INTO jobqueue (jq_pk, jq_job_fk, jq_type, jq_args, jq_starttime, jq_endtime, jq_endtext, jq_end_bits, jq_schedinfo, jq_itemsprocessed)" 174 .
"VALUES ($1, $2, $3, $4,$5, $6,$7,$8,$9,$10)");
178 $nomosTime = date(
'Y-m-d H:i:sO',$nowTime-$diffTime);
179 $uploadArrayQue = array(array(8, $jobId=1,
"nomos", 1,$nomosTime,null ,
"Started", 0,
"localhost.5963", $itemNomos=147),
180 array(1, $jobId,
"ununpack", 1,
"2015-04-21 18:29:19.23825+05:30",
"2015-04-21 18:29:26.396562+05:30",
"Completed",1,null,$itemCount=646 ));
181 foreach ($uploadArrayQue as $uploadEntry) {
185 $this->
dbManager->prepare($stmt =
'insert.uploadtree_a',
186 "INSERT INTO uploadtree_a (uploadtree_pk, parent, upload_fk, pfile_fk, ufile_mode, lft, rgt, ufile_name)" 187 .
"VALUES ($1, $2, $3, $4,$5, $6, $7, $8)");
188 $uploadTreeArray = array(array(123, 121, 1, 103, 32768, 542, 543,
"fckeditorcode_ie.js"),
189 array(121,120, 1, 0, 536888320, 537, 544,
"js"),
190 array(715,651, 2,607 ,33188 ,534 ,535 ,
"zconf.h.cmakein"),
191 array(915, 651, 2, 606 ,33188 ,532 ,533 ,
"zconf.h"),
193 foreach ($uploadTreeArray as $uploadEntry) {
197 $this->
dbManager->prepare($stmt =
'insert.jobdepends',
198 "INSERT INTO jobdepends (jdep_jq_fk, jdep_jq_depends_fk) VALUES ($1, $2 )");
199 $jqWithTwoDependencies = 8;
200 $jobDependsArray = array(array(2,1),
205 array($jqWithTwoDependencies,4),
206 array($jqWithTwoDependencies,4),
208 foreach ($jobDependsArray as $uploadEntry) {
212 $testMyJobInfo = $this->showJobsDao->getJobInfo($this->job_pks);
213 assertThat($testMyJobInfo,hasKey($jobId));
214 assertThat($testMyJobInfo[$jobId][
'jobqueue'][$jqWithTwoDependencies][
'depends'], is(arrayWithSize(2)));
216 $testFilesPerSec = 0.23;
217 $formattedEstimatedTime = $this->showJobsDao->getEstimatedTime($job_pk=1, $jq_Type=
"nomos", $testFilesPerSec);
218 assertThat($formattedEstimatedTime, matchesPattern (
'/\\d+:\\d{2}:\\d{2}/'));
219 $hourMinSec = explode(
':', $formattedEstimatedTime);
220 assertThat($hourMinSec[0]*3600+$hourMinSec[1]*60+$hourMinSec[2],
221 is(closeTo(($itemCount-$itemNomos)/$testFilesPerSec,0.5+$testFilesPerSec)));
223 $testGetEstimatedTime = $this->showJobsDao->getEstimatedTime($job_pk=1, $jq_Type, 0);
224 assertThat($testGetEstimatedTime, matchesPattern (
'/\\d+:\\d{2}:\\d{2}/'));
225 $hourMinSec = explode(
':', $testGetEstimatedTime);
226 $tolerance = 0.5+($itemCount-$itemNomos)/$itemNomos+(time()-$nowTime);
227 assertThat($hourMinSec[0]*3600+$hourMinSec[1]*60+$hourMinSec[2],
228 is(closeTo(($itemCount-$itemNomos)/$itemNomos*$diffTime,$tolerance)));
230 $fewFilesPerSec = 0.003;
231 $formattedLongTime = $this->showJobsDao->getEstimatedTime($job_pk=1, $jq_Type=
"nomos", $fewFilesPerSec);
232 assertThat($formattedLongTime, matchesPattern (
'/\\d+:\\d{2}:\\d{2}/'));
233 $hourMinSec = explode(
':', $formattedLongTime);
234 assertThat($hourMinSec[0]*3600+$hourMinSec[1]*60+$hourMinSec[2],
235 is(closeTo(($itemCount-$itemNomos)/$fewFilesPerSec,0.5+$fewFilesPerSec)));
238 public function testGetEstimatedTimeShouldNotDivideByZero()
240 $this->
dbManager->prepare($stmt =
'insert.jobqueue',
241 "INSERT INTO jobqueue (jq_pk, jq_job_fk, jq_type, jq_args, jq_starttime, jq_endtime, jq_endtext, jq_end_bits, jq_schedinfo, jq_itemsprocessed)" 242 .
"VALUES ($1, $2, $3, $4,$5, $6,$7,$8,$9,$10)");
246 $nomosTime = date(
'Y-m-d H:i:sO',$nowTime-$diffTime);
247 $uploadArrayQue = array(array(8, $jobId=1, $jqType=
"nomos", 1,$nomosTime,null ,
"Started", 0,
"localhost.5963", $itemNomos=147),
248 array(1, $jobId,
"ununpack", 1,
"2015-04-21 18:29:19.23825+05:30",
"2015-04-21 18:29:26.396562+05:30",
"Completed",1,null,$itemCount=646 ));
249 foreach ($uploadArrayQue as $uploadEntry) {
253 $showJobsDaoMock = M::mock(
'Fossology\\Lib\\Dao\\ShowJobsDao[getNumItemsPerSec]',array($this->
dbManager, $this->uploadDao));
254 $showJobsDaoMock->shouldReceive(
'getNumItemsPerSec')->andReturn(0);
256 $estimated = $showJobsDaoMock->getEstimatedTime($jobId, $jqType);
257 assertThat($estimated, equalTo(
'0:00:00'));
fo_dbManager * dbManager
fo_dbManager object